blob: 19bdc9079b3b4d05ab51e91324f12250630bd0e1 [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ärvelin91d79382019-10-02 09:09:31 +0300794 mbedtls_platform_memcpy( tmp + md_len, label, nb );
795 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
805 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
806 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100807
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100808 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_hmac_reset ( &md_ctx );
811 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
812 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_md_hmac_reset ( &md_ctx );
815 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
816 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000817
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000819
820 for( j = 0; j < k; j++ )
821 dstbuf[i + j] = h_i[j];
822 }
823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100825
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500826 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
827 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 return( 0 );
830}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100833MBEDTLS_NO_INLINE static int tls_prf_sha256(
834 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100835 const char *label,
836 const unsigned char *random, size_t rlen,
837 unsigned char *dstbuf, size_t dlen )
838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100840 label, random, rlen, dstbuf, dlen ) );
841}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100845MBEDTLS_NO_INLINE static int tls_prf_sha384(
846 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200847 const char *label,
848 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849 unsigned char *dstbuf, size_t dlen )
850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100852 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_SHA512_C */
855#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000856
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100857/*
858 * Call the appropriate PRF function
859 */
Hanno Becker2793f742019-08-16 14:28:43 +0100860MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100861 mbedtls_md_type_t hash,
862 const unsigned char *secret, size_t slen,
863 const char *label,
864 const unsigned char *random, size_t rlen,
865 unsigned char *dstbuf, size_t dlen )
866{
867#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
868 (void) hash;
869#endif
870
871#if defined(MBEDTLS_SSL_PROTO_SSL3)
872 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
873 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
874 else
875#endif
876#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100877 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100878 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
879 else
880#endif
881#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
882#if defined(MBEDTLS_SHA512_C)
883 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
884 hash == MBEDTLS_MD_SHA384 )
885 {
886 return( tls_prf_sha384( secret, slen, label, random, rlen,
887 dstbuf, dlen ) );
888 }
889 else
890#endif
891#if defined(MBEDTLS_SHA256_C)
892 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
893 {
894 return( tls_prf_sha256( secret, slen, label, random, rlen,
895 dstbuf, dlen ) );
896 }
897#endif
898#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
899
900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
901}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200902
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100903#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100904MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100905 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
906{
907 const char *sender;
908 mbedtls_md5_context md5;
909 mbedtls_sha1_context sha1;
910
911 unsigned char padbuf[48];
912 unsigned char md5sum[16];
913 unsigned char sha1sum[20];
914
915 mbedtls_ssl_session *session = ssl->session_negotiate;
916 if( !session )
917 session = ssl->session;
918
919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
920
921 mbedtls_md5_init( &md5 );
922 mbedtls_sha1_init( &sha1 );
923
924 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
925 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
926
927 /*
928 * SSLv3:
929 * hash =
930 * MD5( master + pad2 +
931 * MD5( handshake + sender + master + pad1 ) )
932 * + SHA1( master + pad2 +
933 * SHA1( handshake + sender + master + pad1 ) )
934 */
935
936#if !defined(MBEDTLS_MD5_ALT)
937 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
938 md5.state, sizeof( md5.state ) );
939#endif
940
941#if !defined(MBEDTLS_SHA1_ALT)
942 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
943 sha1.state, sizeof( sha1.state ) );
944#endif
945
946 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
947 : "SRVR";
948
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200949 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100950
951 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
952 mbedtls_md5_update_ret( &md5, session->master, 48 );
953 mbedtls_md5_update_ret( &md5, padbuf, 48 );
954 mbedtls_md5_finish_ret( &md5, md5sum );
955
956 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
959 mbedtls_sha1_finish_ret( &sha1, sha1sum );
960
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200961 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100962
963 mbedtls_md5_starts_ret( &md5 );
964 mbedtls_md5_update_ret( &md5, session->master, 48 );
965 mbedtls_md5_update_ret( &md5, padbuf, 48 );
966 mbedtls_md5_update_ret( &md5, md5sum, 16 );
967 mbedtls_md5_finish_ret( &md5, buf );
968
969 mbedtls_sha1_starts_ret( &sha1 );
970 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
971 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
972 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
973 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
974
975 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
976
977 mbedtls_md5_free( &md5 );
978 mbedtls_sha1_free( &sha1 );
979
980 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
981 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
982 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
983
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
985}
986#endif /* MBEDTLS_SSL_PROTO_SSL3 */
987
988#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100989MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100990 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
991{
992 int len = 12;
993 const char *sender;
994 mbedtls_md5_context md5;
995 mbedtls_sha1_context sha1;
996 unsigned char padbuf[36];
997
998 mbedtls_ssl_session *session = ssl->session_negotiate;
999 if( !session )
1000 session = ssl->session;
1001
1002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1003
1004 mbedtls_md5_init( &md5 );
1005 mbedtls_sha1_init( &sha1 );
1006
1007 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1008 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1009
1010 /*
1011 * TLSv1:
1012 * hash = PRF( master, finished_label,
1013 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1014 */
1015
1016#if !defined(MBEDTLS_MD5_ALT)
1017 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1018 md5.state, sizeof( md5.state ) );
1019#endif
1020
1021#if !defined(MBEDTLS_SHA1_ALT)
1022 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1023 sha1.state, sizeof( sha1.state ) );
1024#endif
1025
1026 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1027 ? "client finished"
1028 : "server finished";
1029
1030 mbedtls_md5_finish_ret( &md5, padbuf );
1031 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1032
1033 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1034 mbedtls_ssl_suite_get_mac(
1035 mbedtls_ssl_ciphersuite_from_id(
1036 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1037 session->master, 48, sender,
1038 padbuf, 36, buf, len );
1039
1040 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1041
1042 mbedtls_md5_free( &md5 );
1043 mbedtls_sha1_free( &sha1 );
1044
1045 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1046
1047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1048}
1049#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1050
1051#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1052#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001053MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001054 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1055{
1056 int len = 12;
1057 const char *sender;
1058 mbedtls_sha256_context sha256;
1059 unsigned char padbuf[32];
1060
1061 mbedtls_ssl_session *session = ssl->session_negotiate;
1062 if( !session )
1063 session = ssl->session;
1064
1065 mbedtls_sha256_init( &sha256 );
1066
1067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1068
1069 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1070
1071 /*
1072 * TLSv1.2:
1073 * hash = PRF( master, finished_label,
1074 * Hash( handshake ) )[0.11]
1075 */
1076
1077#if !defined(MBEDTLS_SHA256_ALT)
1078 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1079 sha256.state, sizeof( sha256.state ) );
1080#endif
1081
1082 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1083 ? "client finished"
1084 : "server finished";
1085
1086 mbedtls_sha256_finish_ret( &sha256, padbuf );
1087
1088 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1089 mbedtls_ssl_suite_get_mac(
1090 mbedtls_ssl_ciphersuite_from_id(
1091 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1092 session->master, 48, sender,
1093 padbuf, 32, buf, len );
1094
1095 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1096
1097 mbedtls_sha256_free( &sha256 );
1098
1099 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1100
1101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1102}
1103#endif /* MBEDTLS_SHA256_C */
1104
1105#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001106MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001107 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1108{
1109 int len = 12;
1110 const char *sender;
1111 mbedtls_sha512_context sha512;
1112 unsigned char padbuf[48];
1113
1114 mbedtls_ssl_session *session = ssl->session_negotiate;
1115 if( !session )
1116 session = ssl->session;
1117
1118 mbedtls_sha512_init( &sha512 );
1119
1120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1121
1122 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1123
1124 /*
1125 * TLSv1.2:
1126 * hash = PRF( master, finished_label,
1127 * Hash( handshake ) )[0.11]
1128 */
1129
1130#if !defined(MBEDTLS_SHA512_ALT)
1131 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1132 sha512.state, sizeof( sha512.state ) );
1133#endif
1134
1135 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1136 ? "client finished"
1137 : "server finished";
1138
1139 mbedtls_sha512_finish_ret( &sha512, padbuf );
1140
1141 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1142 mbedtls_ssl_suite_get_mac(
1143 mbedtls_ssl_ciphersuite_from_id(
1144 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1145 session->master, 48, sender,
1146 padbuf, 48, buf, len );
1147
1148 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1149
1150 mbedtls_sha512_free( &sha512 );
1151
1152 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1153
1154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1155}
1156#endif /* MBEDTLS_SHA512_C */
1157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1158
Hanno Becker2793f742019-08-16 14:28:43 +01001159MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1160 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001161 mbedtls_md_type_t hash,
1162 mbedtls_ssl_context *ssl,
1163 unsigned char *buf,
1164 int from )
1165{
1166#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1167 (void) hash;
1168#endif
1169
1170#if defined(MBEDTLS_SSL_PROTO_SSL3)
1171 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1172 ssl_calc_finished_ssl( ssl, buf, from );
1173 else
1174#endif
1175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001176 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001177 ssl_calc_finished_tls( ssl, buf, from );
1178 else
1179#endif
1180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1181#if defined(MBEDTLS_SHA512_C)
1182 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1183 hash == MBEDTLS_MD_SHA384 )
1184 {
1185 ssl_calc_finished_tls_sha384( ssl, buf, from );
1186 }
1187 else
1188#endif
1189#if defined(MBEDTLS_SHA256_C)
1190 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1191 ssl_calc_finished_tls_sha256( ssl, buf, from );
1192 else
1193#endif
1194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1196
1197 return( 0 );
1198}
1199
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001200/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001201 * Populate a transform structure with session keys and all the other
1202 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001203 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001204 * Parameters:
1205 * - [in/out]: transform: structure to populate
1206 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001207 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001208 * - [in] ciphersuite
1209 * - [in] master
1210 * - [in] encrypt_then_mac
1211 * - [in] trunc_hmac
1212 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001213 * - [in] tls_prf: pointer to PRF to use for key derivation
1214 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001215 * - [in] minor_ver: SSL/TLS minor version
1216 * - [in] endpoint: client or server
1217 * - [in] ssl: optionally used for:
1218 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1219 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1220 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001221 */
Hanno Becker298a4702019-08-16 10:21:32 +01001222/* Force compilers to inline this function if it's used only
1223 * from one place, because at least ARMC5 doesn't do that
1224 * automatically. */
1225#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1226MBEDTLS_ALWAYS_INLINE static inline
1227#else
1228static
1229#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1230int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001231 int ciphersuite,
1232 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001233#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001234#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1235 int encrypt_then_mac,
1236#endif
1237#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1238 int trunc_hmac,
1239#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001240#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001241#if defined(MBEDTLS_ZLIB_SUPPORT)
1242 int compression,
1243#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001244 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001245 int minor_ver,
1246 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001247 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001248{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001249 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 unsigned char keyblk[256];
1251 unsigned char *key1;
1252 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 unsigned char *mac_enc;
1254 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001255 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001256 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001257 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001258 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001260 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001261
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001262#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1263 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1264 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001265 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001266 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001267#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001268
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001269 /*
1270 * Some data just needs copying into the structure
1271 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001272#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1273 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001274 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001275#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001276
1277#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001278 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001279#else
1280 ((void) minor_ver);
1281#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001283#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001284 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001285#endif
1286
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001287 /*
1288 * Get various info structures
1289 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001290 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001291 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001292 {
1293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001294 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1296 }
1297
Hanno Becker473f98f2019-06-26 10:27:32 +01001298 cipher_info = mbedtls_cipher_info_from_type(
1299 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001300 if( cipher_info == NULL )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001303 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001305 }
1306
Hanno Becker473f98f2019-06-26 10:27:32 +01001307 md_info = mbedtls_md_info_from_type(
1308 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001309 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001312 mbedtls_ssl_suite_get_mac( 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 Beckera5a2b082019-05-15 14:03:01 +01001316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001317 /* Copy own and peer's CID if the use of the CID
1318 * extension has been negotiated. */
1319 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1320 {
1321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001322
Hanno Becker4932f9f2019-05-03 15:23:51 +01001323 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001324 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1325 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001326 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001327 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001328
1329 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001330 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1331 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001332 ssl->handshake->peer_cid_len );
1333 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1334 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001335 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001340 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001341 ret = ssl_prf( minor_ver,
1342 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1343 master, 48, "key expansion", randbytes, 64,
1344 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001345 if( ret != 0 )
1346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001348 return( ret );
1349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001352 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001353 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001354 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 /*
1358 * Determine the appropriate key, IV and MAC length.
1359 */
Paul Bakker68884e32013-01-07 18:20:04 +01001360
Hanno Beckere7f2df02017-12-27 08:17:40 +00001361 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001362
Hanno Beckerf1229442018-01-03 15:32:31 +00001363#if defined(MBEDTLS_GCM_C) || \
1364 defined(MBEDTLS_CCM_C) || \
1365 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001367 cipher_info->mode == MBEDTLS_MODE_CCM ||
1368 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001370 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001371 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001372 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1373 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001374
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001375 /* All modes haves 96-bit IVs;
1376 * GCM and CCM has 4 implicit and 8 explicit bytes
1377 * ChachaPoly has all 12 bytes implicit
1378 */
Paul Bakker68884e32013-01-07 18:20:04 +01001379 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001380 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1381 transform->fixed_ivlen = 12;
1382 else
1383 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001384 }
1385 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001386#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1387#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1388 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1389 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001390 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001391 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1393 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001396 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001399 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001400 mac_key_len = mbedtls_md_get_size( md_info );
1401 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001404 /*
1405 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1406 * (rfc 6066 page 13 or rfc 2104 section 4),
1407 * so we only need to adjust the length here.
1408 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001409 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001412
1413#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1414 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001415 * HMAC implementation which also truncates the key
1416 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001417 mac_key_len = transform->maclen;
1418#endif
1419 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001421
1422 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001423 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001425 else
1426#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1427 {
1428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Hanno Beckera9d5c452019-07-25 16:47:12 +01001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001433 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001434 (unsigned) transform->ivlen,
1435 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
1437 /*
1438 * Finally setup the cipher contexts, IVs and MAC secrets.
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001441 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001443 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001444 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Paul Bakker68884e32013-01-07 18:20:04 +01001446 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001447 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001449 /*
1450 * This is not used in TLS v1.1.
1451 */
Paul Bakker48916f92012-09-16 19:57:18 +00001452 iv_copy_len = ( transform->fixed_ivlen ) ?
1453 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001454 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1455 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001456 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
1458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_SSL_CLI_C */
1460#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001461 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001463 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001464 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Hanno Becker81c7b182017-11-09 18:39:33 +00001466 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001467 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
1470 * This is not used in TLS v1.1.
1471 */
Paul Bakker48916f92012-09-16 19:57:18 +00001472 iv_copy_len = ( transform->fixed_ivlen ) ?
1473 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001474 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1475 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001476 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
Hanno Becker92231322018-01-03 15:32:51 +00001485#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Hanno Becker92231322018-01-03 15:32:51 +00001489 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001493 }
1494
Teppo Järvelin91d79382019-10-02 09:09:31 +03001495 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1496 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001497 }
1498 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1500#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1501 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001502 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001503 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001504 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1505 For AEAD-based ciphersuites, there is nothing to do here. */
1506 if( mac_key_len != 0 )
1507 {
1508 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1509 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1510 }
Paul Bakker68884e32013-01-07 18:20:04 +01001511 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001512 else
1513#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001517 }
Hanno Becker92231322018-01-03 15:32:51 +00001518#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1521 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001522 {
1523 int ret = 0;
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Hanno Beckere7f2df02017-12-27 08:17:40 +00001527 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001528 transform->iv_enc, transform->iv_dec,
1529 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001530 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001531 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1534 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001535 }
1536 }
Hanno Becker92231322018-01-03 15:32:51 +00001537#else
1538 ((void) mac_dec);
1539 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001541
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001542#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1543 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001544 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001545 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001546 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001547 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001548 iv_copy_len );
1549 }
1550#endif
1551
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001552 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001553 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001559 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 cipher_info ) ) != 0 )
1561 {
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001567 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 return( ret );
1572 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001575 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 return( ret );
1580 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_CIPHER_MODE_CBC)
1583 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1586 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001589 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001590 }
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_dec,
1593 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +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 );
1597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001601 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001603 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001605 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001609 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1610 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001611
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001612 if( deflateInit( &transform->ctx_deflate,
1613 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001614 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1617 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618 }
1619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001621
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 return( 0 );
1623}
1624
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001625#if defined(MBEDTLS_SSL_PROTO_SSL3)
1626static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1627 unsigned char hash[36],
1628 size_t *hlen )
1629{
1630 mbedtls_md5_context md5;
1631 mbedtls_sha1_context sha1;
1632 unsigned char pad_1[48];
1633 unsigned char pad_2[48];
1634
1635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1636
1637 mbedtls_md5_init( &md5 );
1638 mbedtls_sha1_init( &sha1 );
1639
1640 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1641 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1642
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001643 mbedtls_platform_memset( pad_1, 0x36, 48 );
1644 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001645
1646 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1647 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1648 mbedtls_md5_finish_ret( &md5, hash );
1649
1650 mbedtls_md5_starts_ret( &md5 );
1651 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1652 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1653 mbedtls_md5_update_ret( &md5, hash, 16 );
1654 mbedtls_md5_finish_ret( &md5, hash );
1655
1656 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1657 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1658 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1659
1660 mbedtls_sha1_starts_ret( &sha1 );
1661 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1662 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1663 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1664 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1665
1666 *hlen = 36;
1667
1668 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1670
1671 mbedtls_md5_free( &md5 );
1672 mbedtls_sha1_free( &sha1 );
1673
1674 return;
1675}
1676#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1677
1678#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1679static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1680 unsigned char hash[36],
1681 size_t *hlen )
1682{
1683 mbedtls_md5_context md5;
1684 mbedtls_sha1_context sha1;
1685
1686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1687
1688 mbedtls_md5_init( &md5 );
1689 mbedtls_sha1_init( &sha1 );
1690
1691 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1692 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1693
1694 mbedtls_md5_finish_ret( &md5, hash );
1695 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1696
1697 *hlen = 36;
1698
1699 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1701
1702 mbedtls_md5_free( &md5 );
1703 mbedtls_sha1_free( &sha1 );
1704
1705 return;
1706}
1707#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1708
1709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1710#if defined(MBEDTLS_SHA256_C)
1711static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1712 unsigned char hash[32],
1713 size_t *hlen )
1714{
1715 mbedtls_sha256_context sha256;
1716
1717 mbedtls_sha256_init( &sha256 );
1718
1719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1720
1721 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1722 mbedtls_sha256_finish_ret( &sha256, hash );
1723
1724 *hlen = 32;
1725
1726 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1728
1729 mbedtls_sha256_free( &sha256 );
1730
1731 return;
1732}
1733#endif /* MBEDTLS_SHA256_C */
1734
1735#if defined(MBEDTLS_SHA512_C)
1736static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1737 unsigned char hash[48],
1738 size_t *hlen )
1739{
1740 mbedtls_sha512_context sha512;
1741
1742 mbedtls_sha512_init( &sha512 );
1743
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1745
1746 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1747 mbedtls_sha512_finish_ret( &sha512, hash );
1748
1749 *hlen = 48;
1750
1751 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1753
1754 mbedtls_sha512_free( &sha512 );
1755
1756 return;
1757}
1758#endif /* MBEDTLS_SHA512_C */
1759#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1760
Hanno Becker2f41b242019-08-15 17:29:43 +01001761int mbedtls_ssl_calc_verify( int minor_ver,
1762 mbedtls_md_type_t hash,
1763 mbedtls_ssl_context const *ssl,
1764 unsigned char *dst,
1765 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001766{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001767#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1768 (void) hash;
1769#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001771#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001772 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001773 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001774 else
1775#endif
1776#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001777 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 else
1780#endif
1781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1782#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001783 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1784 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001785 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001786 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001787 }
1788 else
1789#endif
1790#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001791 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001792 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001793 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 }
1795 else
1796#endif
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1798 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1800 }
1801
1802 return( 0 );
1803}
1804
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001805/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001806 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001807 *
1808 * Parameters:
1809 * [in/out] handshake
1810 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1811 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001812 * [out] master
1813 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001814 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001815static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001816 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001817 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001818{
1819 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001820
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001821/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1822/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1823/* (void) ssl; */
1824/* #endif */
1825
1826 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1827 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001828
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001829#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001830 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1833 return( 0 );
1834 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001835#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
1837 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1838 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001839
1840#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001841 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1842 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843 {
1844 unsigned char session_hash[48];
1845 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846
Hanno Becker2f41b242019-08-15 17:29:43 +01001847 mbedtls_ssl_calc_verify(
1848 mbedtls_ssl_get_minor_ver( ssl ),
1849 mbedtls_ssl_suite_get_mac( ciphersuite ),
1850 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001851
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001852 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1853 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001854
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001855 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1856 mbedtls_ssl_suite_get_mac( ciphersuite ),
1857 handshake->premaster, handshake->pmslen,
1858 "extended master secret",
1859 session_hash, hash_len,
1860 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001861 }
1862 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001863#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001864 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001865 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1866 mbedtls_ssl_suite_get_mac( ciphersuite ),
1867 handshake->premaster, handshake->pmslen,
1868 "master secret",
1869 handshake->randbytes, 64,
1870 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001871 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001872 if( ret != 0 )
1873 {
1874 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1875 return( ret );
1876 }
1877
1878 mbedtls_platform_zeroize( handshake->premaster,
1879 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001880
1881 return( 0 );
1882}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001883
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001884int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1885{
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02001886 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001887
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001889 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001890 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001891 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001892 ssl->session_negotiate->master,
1893 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001894 if( ret != 0 )
1895 {
1896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1897 return( ret );
1898 }
1899
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001900 /* Swap the client and server random values:
1901 * - MS derivation wanted client+server (RFC 5246 8.1)
1902 * - key derivation wants server+client (RFC 5246 6.3) */
1903 {
1904 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001905 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1906 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1907 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001908 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1909 }
1910
1911 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001912 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1914 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001915#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001916#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001917 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001918#endif
1919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001921#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001922#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001923#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001926 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001927 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001928 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1929 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001930 if( ret == 0 )
1931 {
1932 mbedtls_platform_enforce_volatile_reads();
1933 if( ret == 0 )
1934 {
1935 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1936 }
1937 else
1938 {
1939 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1940 }
1941 }
1942 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001943 {
1944 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1945 return( ret );
1946 }
1947
1948 /* We no longer need Server/ClientHello.random values */
1949 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1950 sizeof( ssl->handshake->randbytes ) );
1951
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001952 /* Allocate compression buffer */
1953#if defined(MBEDTLS_ZLIB_SUPPORT)
1954 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1955 ssl->compress_buf == NULL )
1956 {
1957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1958 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1959 if( ssl->compress_buf == NULL )
1960 {
1961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001962 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001963 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1964 }
1965 }
1966#endif
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1969
1970 return( 0 );
1971}
1972
Hanno Becker09d23642019-07-22 17:18:18 +01001973int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1974{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001975 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001976
1977 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1978 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001979#if defined(MBEDTLS_USE_TINYCRYPT)
1980 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001981 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001982 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001983 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1984 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1985 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1986 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1987 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001988 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001989 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1990 ssl->handshake->ecdh_privkey,
1991 ssl->handshake->premaster );
1992 if( ret == UECC_FAULT_DETECTED )
1993 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1994 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001996 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01001997
1998 ssl->handshake->pmslen = NUM_ECC_BYTES;
1999 }
2000 else
2001#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002002#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2003 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2004 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2005 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002006 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002007 ssl->handshake->premaster,
2008 MBEDTLS_PREMASTER_SIZE,
2009 &ssl->handshake->pmslen,
2010 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002011 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2012 if( ret == 0 )
2013 {
2014 mbedtls_platform_enforce_volatile_reads();
2015 if( ret == 0 )
2016 {
2017 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2018 }
2019 else
2020 {
2021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2022 return( ret );
2023 }
2024 }
2025 else
Hanno Becker09d23642019-07-22 17:18:18 +01002026 {
2027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2028 return( ret );
2029 }
2030
2031 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2032 }
2033 else
2034#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002035#if defined(MBEDTLS_ECDH_C) && \
2036 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2037 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2038 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2039 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002040 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2041 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2042 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2043 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2044 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2045 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2046 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2047 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2048 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002049 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002050 &ssl->handshake->pmslen,
2051 ssl->handshake->premaster,
2052 MBEDTLS_MPI_MAX_SIZE,
2053 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002054 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2055 if( ret == 0 )
2056 {
2057 mbedtls_platform_enforce_volatile_reads();
2058 if( ret == 0 )
2059 {
2060 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2061 }
2062 else
2063 {
2064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002065 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002066 }
2067 }
2068 else
Hanno Becker09d23642019-07-22 17:18:18 +01002069 {
2070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2071 return( ret );
2072 }
2073
2074 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2075 }
2076 else
2077#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2078 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2079 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2080 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2081#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2082 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2083 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002084 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2085 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2086 if( ret == 0 )
2087 {
2088 mbedtls_platform_enforce_volatile_reads();
2089 if( ret == 0 )
2090 {
2091 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2092 }
2093 else
2094 {
2095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002096 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002097 }
2098 }
2099 else
Hanno Becker09d23642019-07-22 17:18:18 +01002100 {
2101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2102 return( ret );
2103 }
2104 }
2105 else
2106#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2107#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2108 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2109 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2110 {
2111 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2112 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2113 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002114 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002115 if( ret == 0 )
2116 {
2117 mbedtls_platform_enforce_volatile_reads();
2118 if( ret == 0 )
2119 {
2120 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2121 }
2122 else
2123 {
2124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002125 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002126 }
2127 }
2128 else
Hanno Becker09d23642019-07-22 17:18:18 +01002129 {
2130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2131 return( ret );
2132 }
2133 }
2134 else
2135#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2136#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2137 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2138 == MBEDTLS_KEY_EXCHANGE_RSA )
2139 {
2140 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002141 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002142 * ssl_rsa_generate_partial_pms(). Only the
2143 * PMS length needs to be set. */
2144 ssl->handshake->pmslen = 48;
2145 }
2146 else
2147#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2148 {
2149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2150 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2151 }
2152
2153 return( 0 );
2154}
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2157int 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 +02002158{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 unsigned char *p = ssl->handshake->premaster;
2160 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002161 const unsigned char *psk = ssl->conf->psk;
2162 size_t psk_len = ssl->conf->psk_len;
2163
2164 /* If the psk callback was called, use its result */
2165 if( ssl->handshake->psk != NULL )
2166 {
2167 psk = ssl->handshake->psk;
2168 psk_len = ssl->handshake->psk_len;
2169 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002170
2171 /*
2172 * PMS = struct {
2173 * opaque other_secret<0..2^16-1>;
2174 * opaque psk<0..2^16-1>;
2175 * };
2176 * with "other_secret" depending on the particular key exchange
2177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2179 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002180 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002183
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002184 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002185
2186 if( end < p || (size_t)( end - p ) < psk_len )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002189 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002190 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 }
2192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2194#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2195 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002196 {
2197 /*
2198 * other_secret already set by the ClientKeyExchange message,
2199 * and is 48 bytes long
2200 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002201 if( end - p < 2 )
2202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2203
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002204 *p++ = 0;
2205 *p++ = 48;
2206 p += 48;
2207 }
2208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2210#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2211 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002212 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002213 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002214 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002215
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002216 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002218 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002219 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002220 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002223 return( ret );
2224 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002225 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002226 p += len;
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002229 }
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2232#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2233 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002235 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 size_t zlen;
2237
Hanno Becker982da7e2019-09-02 09:47:39 +01002238#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002239 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2240 ssl->handshake->ecdh_privkey,
2241 p + 2 );
2242 if( ret == UECC_FAULT_DETECTED )
2243 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2244 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002246
2247 zlen = NUM_ECC_BYTES;
2248#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002250 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002251 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002252 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002255 return( ret );
2256 }
2257
Hanno Becker982da7e2019-09-02 09:47:39 +01002258 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2259 MBEDTLS_DEBUG_ECDH_Z );
2260#endif /* MBEDTLS_USE_TINYCRYPT */
2261
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002262 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 }
2265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002270 }
2271
2272 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002273 if( end - p < 2 )
2274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002275
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002276 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002277
2278 if( end < p || (size_t)( end - p ) < psk_len )
2279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2280
Teppo Järvelin91d79382019-10-02 09:09:31 +03002281 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002282 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002283
2284 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2285
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002286 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2287
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002288 return( 0 );
2289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002293/*
2294 * SSLv3.0 MAC functions
2295 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002296#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002297static void ssl_mac( mbedtls_md_context_t *md_ctx,
2298 const unsigned char *secret,
2299 const unsigned char *buf, size_t len,
2300 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002301 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302{
2303 unsigned char header[11];
2304 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002305 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2307 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002308
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002309 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002312 else
Paul Bakker68884e32013-01-07 18:20:04 +01002313 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
Teppo Järvelin91d79382019-10-02 09:09:31 +03002315 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002316 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002317 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002319 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 mbedtls_md_starts( md_ctx );
2321 mbedtls_md_update( md_ctx, secret, md_size );
2322 mbedtls_md_update( md_ctx, padding, padlen );
2323 mbedtls_md_update( md_ctx, header, 11 );
2324 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002325 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002327 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_md_starts( md_ctx );
2329 mbedtls_md_update( md_ctx, secret, md_size );
2330 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002331 mbedtls_md_update( md_ctx, out, md_size );
2332 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002335
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002336/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002337 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002338#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002339 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2340 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2341 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2342/* This function makes sure every byte in the memory region is accessed
2343 * (in ascending addresses order) */
2344static void ssl_read_memory( unsigned char *p, size_t len )
2345{
2346 unsigned char acc = 0;
2347 volatile unsigned char force;
2348
2349 for( ; len != 0; p++, len-- )
2350 acc ^= *p;
2351
2352 force = acc;
2353 (void) force;
2354}
2355#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2356
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002359 */
Hanno Becker3307b532017-12-27 21:37:21 +00002360
Hanno Beckera5a2b082019-05-15 14:03:01 +01002361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002362/* This functions transforms a DTLS plaintext fragment and a record content
2363 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002364 *
2365 * struct {
2366 * opaque content[DTLSPlaintext.length];
2367 * ContentType real_type;
2368 * uint8 zeros[length_of_padding];
2369 * } DTLSInnerPlaintext;
2370 *
2371 * Input:
2372 * - `content`: The beginning of the buffer holding the
2373 * plaintext to be wrapped.
2374 * - `*content_size`: The length of the plaintext in Bytes.
2375 * - `max_len`: The number of Bytes available starting from
2376 * `content`. This must be `>= *content_size`.
2377 * - `rec_type`: The desired record content type.
2378 *
2379 * Output:
2380 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2381 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2382 *
2383 * Returns:
2384 * - `0` on success.
2385 * - A negative error code if `max_len` didn't offer enough space
2386 * for the expansion.
2387 */
2388static int ssl_cid_build_inner_plaintext( unsigned char *content,
2389 size_t *content_size,
2390 size_t remaining,
2391 uint8_t rec_type )
2392{
2393 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002394 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2395 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2396 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002397
2398 /* Write real content type */
2399 if( remaining == 0 )
2400 return( -1 );
2401 content[ len ] = rec_type;
2402 len++;
2403 remaining--;
2404
2405 if( remaining < pad )
2406 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002407 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002408 len += pad;
2409 remaining -= pad;
2410
2411 *content_size = len;
2412 return( 0 );
2413}
2414
Hanno Becker7dc25772019-05-20 15:08:01 +01002415/* This function parses a DTLSInnerPlaintext structure.
2416 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002417static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2418 size_t *content_size,
2419 uint8_t *rec_type )
2420{
2421 size_t remaining = *content_size;
2422
2423 /* Determine length of padding by skipping zeroes from the back. */
2424 do
2425 {
2426 if( remaining == 0 )
2427 return( -1 );
2428 remaining--;
2429 } while( content[ remaining ] == 0 );
2430
2431 *content_size = remaining;
2432 *rec_type = content[ remaining ];
2433
2434 return( 0 );
2435}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002437
Hanno Becker99abf512019-05-20 14:50:53 +01002438/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002439 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002440static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002441 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002442 mbedtls_record *rec )
2443{
Hanno Becker99abf512019-05-20 14:50:53 +01002444 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002445 *
2446 * additional_data = seq_num + TLSCompressed.type +
2447 * TLSCompressed.version + TLSCompressed.length;
2448 *
Hanno Becker99abf512019-05-20 14:50:53 +01002449 * For the CID extension, this is extended as follows
2450 * (quoting draft-ietf-tls-dtls-connection-id-05,
2451 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 *
2453 * additional_data = seq_num + DTLSPlaintext.type +
2454 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002455 * cid +
2456 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002457 * length_of_DTLSInnerPlaintext;
2458 */
2459
Teppo Järvelin91d79382019-10-02 09:09:31 +03002460 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002461 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002462 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002463
Hanno Beckera5a2b082019-05-15 14:03:01 +01002464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002465 if( rec->cid_len != 0 )
2466 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002467 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002468 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002469 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2470 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002471 *add_data_len = 13 + 1 + rec->cid_len;
2472 }
2473 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002476 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 *add_data_len = 13;
2478 }
Hanno Becker3307b532017-12-27 21:37:21 +00002479}
2480
Hanno Becker611a83b2018-01-03 14:27:32 +00002481int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2482 mbedtls_ssl_transform *transform,
2483 mbedtls_record *rec,
2484 int (*f_rng)(void *, unsigned char *, size_t),
2485 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002489 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002490 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002491 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002492 size_t post_avail;
2493
2494 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002495#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002496 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002497 ((void) ssl);
2498#endif
2499
2500 /* The PRNG is used for dynamic IV generation that's used
2501 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2502#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2503 ( defined(MBEDTLS_AES_C) || \
2504 defined(MBEDTLS_ARIA_C) || \
2505 defined(MBEDTLS_CAMELLIA_C) ) && \
2506 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2507 ((void) f_rng);
2508 ((void) p_rng);
2509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker3307b532017-12-27 21:37:21 +00002513 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002514 {
Hanno Becker3307b532017-12-27 21:37:21 +00002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2517 }
Hanno Becker505089d2019-05-01 09:45:57 +01002518 if( rec == NULL
2519 || rec->buf == NULL
2520 || rec->buf_len < rec->data_offset
2521 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002522#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002523 || rec->cid_len != 0
2524#endif
2525 )
Hanno Becker3307b532017-12-27 21:37:21 +00002526 {
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002529 }
2530
Hanno Becker3307b532017-12-27 21:37:21 +00002531 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002532 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002534 data, rec->data_len );
2535
2536 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2537
2538 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2539 {
2540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2541 (unsigned) rec->data_len,
2542 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2544 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002545
Hanno Beckera5a2b082019-05-15 14:03:01 +01002546#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002547 /*
2548 * Add CID information
2549 */
2550 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002551 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2552 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002553 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002554
2555 if( rec->cid_len != 0 )
2556 {
2557 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002558 * Wrap plaintext into DTLSInnerPlaintext structure.
2559 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002560 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002561 * Note that this changes `rec->data_len`, and hence
2562 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002563 */
2564 if( ssl_cid_build_inner_plaintext( data,
2565 &rec->data_len,
2566 post_avail,
2567 rec->type ) != 0 )
2568 {
2569 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2570 }
2571
2572 rec->type = MBEDTLS_SSL_MSG_CID;
2573 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002574#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002575
Hanno Becker92c930f2019-04-29 17:31:37 +01002576 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2577
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002579 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002581#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 if( mode == MBEDTLS_MODE_STREAM ||
2583 ( mode == MBEDTLS_MODE_CBC
2584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002585 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002586#endif
2587 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( post_avail < transform->maclen )
2590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2592 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002596 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2597 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002599 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002600 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2601 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002602 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002603 }
2604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2607 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002608 if( mbedtls_ssl_ver_geq(
2609 mbedtls_ssl_transform_get_minor_ver( transform ),
2610 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002611 {
Hanno Becker992b6872017-11-09 18:57:39 +00002612 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2613
Hanno Beckere83efe62019-04-29 13:52:53 +01002614 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002615
Hanno Becker3307b532017-12-27 21:37:21 +00002616 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002617 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002618 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2619 data, rec->data_len );
2620 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2621 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2622
Teppo Järvelin91d79382019-10-02 09:09:31 +03002623 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002624 }
2625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002630 }
2631
Hanno Becker3307b532017-12-27 21:37:21 +00002632 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2633 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634
Hanno Becker3307b532017-12-27 21:37:21 +00002635 rec->data_len += transform->maclen;
2636 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002637 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002638 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002639#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641 /*
2642 * Encrypt
2643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2645 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002647 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002648 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002650 "including %d bytes of padding",
2651 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker3307b532017-12-27 21:37:21 +00002653 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2654 transform->iv_enc, transform->ivlen,
2655 data, rec->data_len,
2656 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002659 return( ret );
2660 }
2661
Hanno Becker3307b532017-12-27 21:37:21 +00002662 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
Paul Bakker68884e32013-01-07 18:20:04 +01002668 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002670
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671#if defined(MBEDTLS_GCM_C) || \
2672 defined(MBEDTLS_CCM_C) || \
2673 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002675 mode == MBEDTLS_MODE_CCM ||
2676 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002677 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002678 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002679 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002680 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002681
Hanno Becker3307b532017-12-27 21:37:21 +00002682 /* Check that there's space for both the authentication tag
2683 * and the explicit IV before and after the record content. */
2684 if( post_avail < transform->taglen ||
2685 rec->data_offset < explicit_iv_len )
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2688 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2689 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690
Paul Bakker68884e32013-01-07 18:20:04 +01002691 /*
2692 * Generate IV
2693 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002694 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2695 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002696 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002697 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2698 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002699 explicit_iv_len );
2700 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002701 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002702 }
2703 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2704 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002705 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 unsigned char i;
2707
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709
2710 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002711 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 }
2713 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002714 {
2715 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002718 }
2719
Hanno Beckere83efe62019-04-29 13:52:53 +01002720 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002721
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002722 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2723 iv, transform->ivlen );
2724 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002725 data - explicit_iv_len, explicit_iv_len );
2726 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002727 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002730 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002731
Paul Bakker68884e32013-01-07 18:20:04 +01002732 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002733 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002734 */
Hanno Becker3307b532017-12-27 21:37:21 +00002735
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002737 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002738 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002739 data, rec->data_len, /* source */
2740 data, &rec->data_len, /* destination */
2741 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002744 return( ret );
2745 }
2746
Hanno Becker3307b532017-12-27 21:37:21 +00002747 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2748 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002749
Hanno Becker3307b532017-12-27 21:37:21 +00002750 rec->data_len += transform->taglen + explicit_iv_len;
2751 rec->data_offset -= explicit_iv_len;
2752 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002753 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002754 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2757#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002758 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002761 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002762 size_t padlen, i;
2763 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002764
Hanno Becker3307b532017-12-27 21:37:21 +00002765 /* Currently we're always using minimal padding
2766 * (up to 255 bytes would be allowed). */
2767 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2768 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 padlen = 0;
2770
Hanno Becker3307b532017-12-27 21:37:21 +00002771 /* Check there's enough space in the buffer for the padding. */
2772 if( post_avail < padlen + 1 )
2773 {
2774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2775 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2776 }
2777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002779 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Hanno Becker3307b532017-12-27 21:37:21 +00002781 rec->data_len += padlen + 1;
2782 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002785 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002786 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2787 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002788 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002789 if( mbedtls_ssl_ver_geq(
2790 mbedtls_ssl_transform_get_minor_ver( transform ),
2791 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 {
Hanno Becker3307b532017-12-27 21:37:21 +00002793 if( f_rng == NULL )
2794 {
2795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2797 }
2798
2799 if( rec->data_offset < transform->ivlen )
2800 {
2801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2802 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2803 }
2804
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 /*
2806 * Generate IV
2807 */
Hanno Becker3307b532017-12-27 21:37:21 +00002808 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002809 if( ret != 0 )
2810 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Teppo Järvelin91d79382019-10-02 09:09:31 +03002812 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002813 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002820 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002821 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Hanno Becker3307b532017-12-27 21:37:21 +00002823 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2824 transform->iv_enc,
2825 transform->ivlen,
2826 data, rec->data_len,
2827 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002830 return( ret );
2831 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002832
Hanno Becker3307b532017-12-27 21:37:21 +00002833 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002837 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002840 if( mbedtls_ssl_ver_lt(
2841 mbedtls_ssl_transform_get_minor_ver( transform ),
2842 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 {
2844 /*
2845 * Save IV in SSL3 and TLS1
2846 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002847 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002848 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 }
Hanno Becker3307b532017-12-27 21:37:21 +00002850 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002851#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002852 {
2853 data -= transform->ivlen;
2854 rec->data_offset -= transform->ivlen;
2855 rec->data_len += transform->ivlen;
2856 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002860 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002861 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2862
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002863 /*
2864 * MAC(MAC_write_key, seq_num +
2865 * TLSCipherText.type +
2866 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002867 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 * IV + // except for TLS 1.0
2869 * ENC(content + padding + padding_length));
2870 */
Hanno Becker3307b532017-12-27 21:37:21 +00002871
2872 if( post_avail < transform->maclen)
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2875 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2876 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877
Hanno Beckere83efe62019-04-29 13:52:53 +01002878 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002881 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002882 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002883
Hanno Becker3307b532017-12-27 21:37:21 +00002884 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002885 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002886 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2887 data, rec->data_len );
2888 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2889 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002890
Teppo Järvelin91d79382019-10-02 09:09:31 +03002891 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002892
Hanno Becker3307b532017-12-27 21:37:21 +00002893 rec->data_len += transform->maclen;
2894 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002895 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002896 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002901 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002907 /* Make extra sure authentication was performed, exactly once */
2908 if( auth_done != 1 )
2909 {
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é-Gonnard352143f2015-01-13 10:59:51 +01002912 }
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
2916 return( 0 );
2917}
2918
Hanno Becker40478be2019-07-12 08:23:59 +01002919int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002920 mbedtls_ssl_transform *transform,
2921 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002923 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002925 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002926#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002927 size_t padlen = 0, correct = 1;
2928#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002930 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002931 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932
Hanno Becker611a83b2018-01-03 14:27:32 +00002933#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002934 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 ((void) ssl);
2936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939 if( rec == NULL ||
2940 rec->buf == NULL ||
2941 rec->buf_len < rec->data_offset ||
2942 rec->buf_len - rec->data_offset < rec->data_len )
2943 {
2944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002946 }
2947
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 data = rec->buf + rec->data_offset;
2949 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Hanno Beckera5a2b082019-05-15 14:03:01 +01002951#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002952 /*
2953 * Match record's CID with incoming CID.
2954 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002955 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002956 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002957 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002958 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002959 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002960#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2963 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002964 {
2965 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2967 transform->iv_dec,
2968 transform->ivlen,
2969 data, rec->data_len,
2970 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002973 return( ret );
2974 }
2975
Hanno Becker4c6876b2017-12-27 21:28:58 +00002976 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 }
Paul Bakker68884e32013-01-07 18:20:04 +01002982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002984#if defined(MBEDTLS_GCM_C) || \
2985 defined(MBEDTLS_CCM_C) || \
2986 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002988 mode == MBEDTLS_MODE_CCM ||
2989 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002990 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002991 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002992 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002994 /*
2995 * Prepare IV from explicit and implicit data.
2996 */
2997
2998 /* Check that there's enough space for the explicit IV
2999 * (at the beginning of the record) and the MAC (at the
3000 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003001 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003004 "+ taglen (%d)", rec->data_len,
3005 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003007 }
Paul Bakker68884e32013-01-07 18:20:04 +01003008
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003009#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003010 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3011 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003012 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003013
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003014 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003015 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003016 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003017 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003018 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019 else
3020#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3021#if defined(MBEDTLS_CHACHAPOLY_C)
3022 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003023 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003024 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003025 unsigned char i;
3026
Teppo Järvelin91d79382019-10-02 09:09:31 +03003027 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003028
3029 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003030 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003031 }
3032 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003033#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 {
3035 /* Reminder if we ever add an AEAD mode with a different size */
3036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3038 }
3039
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003040 /* Group changes to data, data_len, and add_data, because
3041 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003042 data += explicit_iv_len;
3043 rec->data_offset += explicit_iv_len;
3044 rec->data_len -= explicit_iv_len + transform->taglen;
3045
Hanno Beckere83efe62019-04-29 13:52:53 +01003046 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003048 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003050 /* Because of the check above, we know that there are
3051 * explicit_iv_len Bytes preceeding data, and taglen
3052 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003053 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003054 * mbedtls_cipher_auth_decrypt() below. */
3055
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003056 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003058 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003059
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003060 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003061 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003062 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3064 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003065 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 data, rec->data_len,
3067 data, &olen,
3068 data + rec->data_len,
3069 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3074 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003075
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003076 return( ret );
3077 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003078 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003080 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003081 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003085 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3089#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003090 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003093 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003094
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 /*
Paul Bakker45829992013-01-03 14:52:21 +01003096 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003099 if( mbedtls_ssl_ver_geq(
3100 mbedtls_ssl_transform_get_minor_ver( transform ),
3101 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 {
3103 /* The ciphertext is prefixed with the CBC IV. */
3104 minlen += transform->ivlen;
3105 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003106#endif
Paul Bakker45829992013-01-03 14:52:21 +01003107
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 /* Size considerations:
3109 *
3110 * - The CBC cipher text must not be empty and hence
3111 * at least of size transform->ivlen.
3112 *
3113 * Together with the potential IV-prefix, this explains
3114 * the first of the two checks below.
3115 *
3116 * - The record must contain a MAC, either in plain or
3117 * encrypted, depending on whether Encrypt-then-MAC
3118 * is used or not.
3119 * - If it is, the message contains the IV-prefix,
3120 * the CBC ciphertext, and the MAC.
3121 * - If it is not, the padded plaintext, and hence
3122 * the CBC ciphertext, has at least length maclen + 1
3123 * because there is at least the padding length byte.
3124 *
3125 * As the CBC ciphertext is not empty, both cases give the
3126 * lower bound minlen + maclen + 1 on the record size, which
3127 * we test for in the second check below.
3128 */
3129 if( rec->data_len < minlen + transform->ivlen ||
3130 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133 "+ 1 ) ( + expl IV )", rec->data_len,
3134 transform->ivlen,
3135 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003137 }
3138
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003139 /*
3140 * Authenticate before decrypt if enabled
3141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003144 {
Hanno Becker992b6872017-11-09 18:57:39 +00003145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003148
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003149 /* Update data_len in tandem with add_data.
3150 *
3151 * The subtraction is safe because of the previous check
3152 * data_len >= minlen + maclen + 1.
3153 *
3154 * Afterwards, we know that data + data_len is followed by at
3155 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003156 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 *
3158 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003159 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003160 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003161
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003162 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003163 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3164 add_data_len );
3165 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3166 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3168 data, rec->data_len );
3169 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3170 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003171
Hanno Becker4c6876b2017-12-27 21:28:58 +00003172 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3173 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003174 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003175 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003176
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003177 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003178 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003184 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187
3188 /*
3189 * Check length sanity
3190 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003191
3192 /* We know from above that data_len > minlen >= 0,
3193 * so the following check in particular implies that
3194 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003195 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003198 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003200 }
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003203 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003205 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003206 if( mbedtls_ssl_ver_geq(
3207 mbedtls_ssl_transform_get_minor_ver( transform ),
3208 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003209 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003210 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003211 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212
Hanno Becker4c6876b2017-12-27 21:28:58 +00003213 data += transform->ivlen;
3214 rec->data_offset += transform->ivlen;
3215 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003218
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003219 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3220
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3222 transform->iv_dec, transform->ivlen,
3223 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003226 return( ret );
3227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003228
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003229 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003234 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003237 if( mbedtls_ssl_ver_lt(
3238 mbedtls_ssl_transform_get_minor_ver( transform ),
3239 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003240 {
3241 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003242 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3243 * records is equivalent to CBC decryption of the concatenation
3244 * of the records; in other words, IVs are maintained across
3245 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003246 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003247 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003248 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Hanno Becker4c6876b2017-12-27 21:28:58 +00003252 /* Safe since data_len >= minlen + maclen + 1, so after having
3253 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003254 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3255 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003257
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 if( auth_done == 1 )
3259 {
3260 correct *= ( rec->data_len >= padlen + 1 );
3261 padlen *= ( rec->data_len >= padlen + 1 );
3262 }
3263 else
Paul Bakker45829992013-01-03 14:52:21 +01003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 if( rec->data_len < transform->maclen + padlen + 1 )
3267 {
3268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3269 rec->data_len,
3270 transform->maclen,
3271 padlen + 1 ) );
3272 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003273#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274
3275 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3276 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker4c6876b2017-12-27 21:28:58 +00003279 padlen++;
3280
3281 /* Regardless of the validity of the padding,
3282 * we have data_len >= padlen here. */
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003285 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3286 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_SSL_DEBUG_ALL)
3291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003292 "should be no more than %d",
3293 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003294#endif
Paul Bakker45829992013-01-03 14:52:21 +01003295 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 }
3297 }
3298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3300#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3301 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003302 if( mbedtls_ssl_ver_gt(
3303 mbedtls_ssl_transform_get_minor_ver( transform ),
3304 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 /* The padding check involves a series of up to 256
3307 * consecutive memory reads at the end of the record
3308 * plaintext buffer. In order to hide the length and
3309 * validity of the padding, always perform exactly
3310 * `min(256,plaintext_len)` reads (but take into account
3311 * only the last `padlen` bytes for the padding check). */
3312 size_t pad_count = 0;
3313 size_t real_count = 0;
3314 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003315
Hanno Becker4c6876b2017-12-27 21:28:58 +00003316 /* Index of first padding byte; it has been ensured above
3317 * that the subtraction is safe. */
3318 size_t const padding_idx = rec->data_len - padlen;
3319 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3320 size_t const start_idx = rec->data_len - num_checks;
3321 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003322
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003324 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003325 real_count |= ( idx >= padding_idx );
3326 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003327 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003328 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003331 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003333#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003334 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3338 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003342 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003343
Hanno Becker4c6876b2017-12-27 21:28:58 +00003344 /* If the padding was found to be invalid, padlen == 0
3345 * and the subtraction is safe. If the padding was found valid,
3346 * padlen hasn't been changed and the previous assertion
3347 * data_len >= padlen still holds. */
3348 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003352 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003358#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003360 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
3363 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003364 * Authenticate if not done yet.
3365 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003367#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003368 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003369 {
Hanno Becker992b6872017-11-09 18:57:39 +00003370 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003371
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 /* If the initial value of padlen was such that
3373 * data_len < maclen + padlen + 1, then padlen
3374 * got reset to 1, and the initial check
3375 * data_len >= minlen + maclen + 1
3376 * guarantees that at this point we still
3377 * have at least data_len >= maclen.
3378 *
3379 * If the initial value of padlen was such that
3380 * data_len >= maclen + padlen + 1, then we have
3381 * subtracted either padlen + 1 (if the padding was correct)
3382 * or 0 (if the padding was incorrect) since then,
3383 * hence data_len >= maclen in any case.
3384 */
3385 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003386 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003389 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3390 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003391 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003392 ssl_mac( &transform->md_ctx_dec,
3393 transform->mac_dec,
3394 data, rec->data_len,
3395 rec->ctr, rec->type,
3396 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 }
3398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003402 if( mbedtls_ssl_ver_gt(
3403 mbedtls_ssl_transform_get_minor_ver( transform ),
3404 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003405 {
3406 /*
3407 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003408 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 *
3410 * Known timing attacks:
3411 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3412 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003413 * To compensate for different timings for the MAC calculation
3414 * depending on how much padding was removed (which is determined
3415 * by padlen), process extra_run more blocks through the hash
3416 * function.
3417 *
3418 * The formula in the paper is
3419 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3420 * where L1 is the size of the header plus the decrypted message
3421 * plus CBC padding and L2 is the size of the header plus the
3422 * decrypted message. This is for an underlying hash function
3423 * with 64-byte blocks.
3424 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3425 * correctly. We round down instead of up, so -56 is the correct
3426 * value for our calculations instead of -55.
3427 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003428 * Repeat the formula rather than defining a block_size variable.
3429 * This avoids requiring division by a variable at runtime
3430 * (which would be marginally less efficient and would require
3431 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003432 */
3433 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003434 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003435
3436 /*
3437 * The next two sizes are the minimum and maximum values of
3438 * in_msglen over all padlen values.
3439 *
3440 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003441 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442 *
3443 * Note that max_len + maclen is never more than the buffer
3444 * length, as we previously did in_msglen -= maclen too.
3445 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003446 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003447 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3448
Hanno Becker4c6876b2017-12-27 21:28:58 +00003449 memset( tmp, 0, sizeof( tmp ) );
3450
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003451 switch( mbedtls_md_get_type(
3452 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003453 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003454#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3455 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003456 case MBEDTLS_MD_MD5:
3457 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003458 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003459 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003460 extra_run =
3461 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3462 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 break;
3464#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003465#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003468 extra_run =
3469 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3470 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 break;
3472#endif
3473 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3476 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003477
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003478 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003479
Hanno Beckere83efe62019-04-29 13:52:53 +01003480 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3481 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003482 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3483 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003484 /* Make sure we access everything even when padlen > 0. This
3485 * makes the synchronisation requirements for just-in-time
3486 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003487 ssl_read_memory( data + rec->data_len, padlen );
3488 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003489
3490 /* Call mbedtls_md_process at least once due to cache attacks
3491 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003492 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003493 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003494
Hanno Becker4c6876b2017-12-27 21:28:58 +00003495 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496
3497 /* Make sure we access all the memory that could contain the MAC,
3498 * before we check it in the next code block. This makes the
3499 * synchronisation requirements for just-in-time Prime+Probe
3500 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003501 ssl_read_memory( data + min_len,
3502 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003503 }
3504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3506 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003512#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3514 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003517 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003518 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_SSL_DEBUG_ALL)
3521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003522#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003523 correct = 0;
3524 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003525 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003526 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003527
3528 /*
3529 * Finally check the correct flag
3530 */
3531 if( correct == 0 )
3532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003533#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003534
3535 /* Make extra sure authentication was performed, exactly once */
3536 if( auth_done != 1 )
3537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Beckera5a2b082019-05-15 14:03:01 +01003542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003543 if( rec->cid_len != 0 )
3544 {
3545 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3546 &rec->type );
3547 if( ret != 0 )
3548 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3549 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 return( 0 );
3555}
3556
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003557#undef MAC_NONE
3558#undef MAC_PLAINTEXT
3559#undef MAC_CIPHERTEXT
3560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562/*
3563 * Compression/decompression functions
3564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003566{
3567 int ret;
3568 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003569 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003571 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003575 if( len_pre == 0 )
3576 return( 0 );
3577
Teppo Järvelin91d79382019-10-02 09:09:31 +03003578 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581 ssl->out_msglen ) );
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584 ssl->out_msg, ssl->out_msglen );
3585
Paul Bakker48916f92012-09-16 19:57:18 +00003586 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3587 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3588 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003589 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Paul Bakker48916f92012-09-16 19:57:18 +00003591 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592 if( ret != Z_OK )
3593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3595 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596 }
3597
Angus Grattond8213d02016-05-25 20:56:48 +10003598 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003599 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 ssl->out_msglen ) );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003605 ssl->out_msg, ssl->out_msglen );
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608
3609 return( 0 );
3610}
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003613{
3614 int ret;
3615 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003616 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003618 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003621
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003622 if( len_pre == 0 )
3623 return( 0 );
3624
Teppo Järvelin91d79382019-10-02 09:09:31 +03003625 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628 ssl->in_msglen ) );
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631 ssl->in_msg, ssl->in_msglen );
3632
Paul Bakker48916f92012-09-16 19:57:18 +00003633 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3634 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3635 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003636 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003637 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638
Paul Bakker48916f92012-09-16 19:57:18 +00003639 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 if( ret != Z_OK )
3641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3643 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003644 }
3645
Angus Grattond8213d02016-05-25 20:56:48 +10003646 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003647 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650 ssl->in_msglen ) );
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 ssl->in_msg, ssl->in_msglen );
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
3657 return( 0 );
3658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3662static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
3665static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003666{
3667 /* If renegotiation is not enforced, retransmit until we would reach max
3668 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003669 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003670 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003671 uint32_t ratio =
3672 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3673 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674 unsigned char doublings = 1;
3675
3676 while( ratio != 0 )
3677 {
3678 ++doublings;
3679 ratio >>= 1;
3680 }
3681
3682 if( ++ssl->renego_records_seen > doublings )
3683 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003685 return( 0 );
3686 }
3687 }
3688
3689 return( ssl_write_hello_request( ssl ) );
3690}
3691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003693
Paul Bakker5121ce52009-01-03 21:22:43 +00003694/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003695 * Fill the input message buffer by appending data to it.
3696 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 *
3698 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3699 * available (from this read and/or a previous one). Otherwise, an error code
3700 * is returned (possibly EOF or WANT_READ).
3701 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3703 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3704 * since we always read a whole datagram at once.
3705 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003706 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003707 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003710{
Paul Bakker23986e52011-04-24 08:57:21 +00003711 int ret;
3712 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Hanno Beckera58a8962019-06-13 16:11:15 +01003716 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3717 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003720 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003722 }
3723
Angus Grattond8213d02016-05-25 20:56:48 +10003724 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003728 }
3729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003733 uint32_t timeout;
3734
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003735 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003736 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3737 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3740 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3742 }
3743
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003744 /*
3745 * The point is, we need to always read a full datagram at once, so we
3746 * sometimes read more then requested, and handle the additional data.
3747 * It could be the rest of the current record (while fetching the
3748 * header) and/or some other records in the same datagram.
3749 */
3750
3751 /*
3752 * Move to the next record in the already read datagram if applicable
3753 */
3754 if( ssl->next_record_offset != 0 )
3755 {
3756 if( ssl->in_left < ssl->next_record_offset )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003760 }
3761
3762 ssl->in_left -= ssl->next_record_offset;
3763
3764 if( ssl->in_left != 0 )
3765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003767 ssl->next_record_offset ) );
3768 memmove( ssl->in_hdr,
3769 ssl->in_hdr + ssl->next_record_offset,
3770 ssl->in_left );
3771 }
3772
3773 ssl->next_record_offset = 0;
3774 }
3775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003778
3779 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003780 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 */
3782 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003786 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003787
3788 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003789 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790 * are not at the beginning of a new record, the caller did something
3791 * wrong.
3792 */
3793 if( ssl->in_left != 0 )
3794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 }
3798
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003799 /*
3800 * Don't even try to read if time's out already.
3801 * This avoids by-passing the timer when repeatedly receiving messages
3802 * that will end up being dropped.
3803 */
3804 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003807 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003808 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003809 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003810 {
Angus Grattond8213d02016-05-25 20:56:48 +10003811 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003814 timeout = ssl->handshake->retransmit_timeout;
3815 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003816 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003819
Hanno Beckera58a8962019-06-13 16:11:15 +01003820 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3821 {
3822 ret = mbedtls_ssl_get_recv_timeout( ssl )
3823 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3824 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003825 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003826 {
3827 ret = mbedtls_ssl_get_recv( ssl )
3828 ( ssl->p_bio, ssl->in_hdr, len );
3829 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832
3833 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835 }
3836
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003837 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003843 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003844 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003847 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003848 }
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003853 return( ret );
3854 }
3855
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003856 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003859 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3860 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003862 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003863 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003866 return( ret );
3867 }
3868
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003869 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003870 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003872 }
3873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 if( ret < 0 )
3875 return( ret );
3876
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003877 ssl->in_left = ret;
3878 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003879 MBEDTLS_SSL_TRANSPORT_ELSE
3880#endif /* MBEDTLS_SSL_PROTO_DTLS */
3881#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003884 ssl->in_left, nb_want ) );
3885
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003886 while( ssl->in_left < nb_want )
3887 {
3888 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003889
3890 if( ssl_check_timer( ssl ) != 0 )
3891 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3892 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003893 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003894 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003895 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003896 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3897 ssl->in_hdr + ssl->in_left, len,
3898 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003899 }
3900 else
3901 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003902 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003903 ssl->in_hdr + ssl->in_left, len );
3904 }
3905 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003908 ssl->in_left, nb_want ) );
3909 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003910
3911 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003913
3914 if( ret < 0 )
3915 return( ret );
3916
mohammad160352aecb92018-03-28 23:41:40 -07003917 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003918 {
Darryl Green11999bb2018-03-13 15:22:58 +00003919 MBEDTLS_SSL_DEBUG_MSG( 1,
3920 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003921 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3923 }
3924
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003925 ssl->in_left += ret;
3926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003928#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
3932 return( 0 );
3933}
3934
3935/*
3936 * Flush any data not yet written
3937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003940 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003941 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Hanno Beckera58a8962019-06-13 16:11:15 +01003945 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003948 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003950 }
3951
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003952 /* Avoid incrementing counter if data is flushed */
3953 if( ssl->out_left == 0 )
3954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003956 return( 0 );
3957 }
3958
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 while( ssl->out_left > 0 )
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003962 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Hanno Becker2b1e3542018-08-06 11:19:13 +01003964 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003965 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
3969 if( ret <= 0 )
3970 return( ret );
3971
mohammad160352aecb92018-03-28 23:41:40 -07003972 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003973 {
Darryl Green11999bb2018-03-13 15:22:58 +00003974 MBEDTLS_SSL_DEBUG_MSG( 1,
3975 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003976 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3978 }
3979
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 ssl->out_left -= ret;
3981 }
3982
Hanno Becker2b1e3542018-08-06 11:19:13 +01003983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003984 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003985 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003986 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003987 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003988 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003989#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003990#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003991 {
3992 ssl->out_hdr = ssl->out_buf + 8;
3993 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003994#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
3999 return( 0 );
4000}
4001
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002/*
4003 * Functions to handle the DTLS retransmission state machine
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004006/*
4007 * Append current handshake message to current outgoing flight
4008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4013 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4014 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015
4016 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004017 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004021 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022 }
4023
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004024 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == 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", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 mbedtls_free( msg );
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
4031 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004032 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004035 msg->next = NULL;
4036
4037 /* Append to the current flight */
4038 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004039 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 else
4041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 while( cur->next != NULL )
4044 cur = cur->next;
4045 cur->next = msg;
4046 }
4047
Hanno Becker3b235902018-08-06 09:54:53 +01004048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 return( 0 );
4050}
4051
4052/*
4053 * Free the current flight of handshake messages
4054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 mbedtls_ssl_flight_item *cur = flight;
4058 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059
4060 while( cur != NULL )
4061 {
4062 next = cur->next;
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_free( cur->p );
4065 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 cur = next;
4068 }
4069}
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4072static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004073#endif
4074
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004076 * Swap transform_out and out_ctr with the alternative ones
4077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004081 unsigned char tmp_out_ctr[8];
4082
4083 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086 return;
4087 }
4088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004091 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092 tmp_transform = ssl->transform_out;
4093 ssl->transform_out = ssl->handshake->alt_transform_out;
4094 ssl->handshake->alt_transform_out = tmp_transform;
4095
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004096 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004097 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4098 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4099 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004100
4101 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004102 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4105 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004111 }
4112 }
4113#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004114}
4115
4116/*
4117 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004118 */
4119int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4120{
4121 int ret = 0;
4122
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4124
4125 ret = mbedtls_ssl_flight_transmit( ssl );
4126
4127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4128
4129 return( ret );
4130}
4131
4132/*
4133 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004134 *
4135 * Need to remember the current message in case flush_output returns
4136 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004137 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004138 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004139int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004140{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004145 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004147
4148 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004149 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004150 ssl_swap_epochs( ssl );
4151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004153 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154
4155 while( ssl->handshake->cur_msg != NULL )
4156 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004159
Hanno Beckere1dcb032018-08-17 16:47:58 +01004160 int const is_finished =
4161 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4162 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4163
Hanno Becker04da1892018-08-14 13:22:10 +01004164 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4165 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4166
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004167 /* Swap epochs before sending Finished: we can't do it after
4168 * sending ChangeCipherSpec, in case write returns WANT_READ.
4169 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004170 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004171 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004173 ssl_swap_epochs( ssl );
4174 }
4175
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176 ret = ssl_get_remaining_payload_in_datagram( ssl );
4177 if( ret < 0 )
4178 return( ret );
4179 max_frag_len = (size_t) ret;
4180
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004181 /* CCS is copied as is, while HS messages may need fragmentation */
4182 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4183 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004184 if( max_frag_len == 0 )
4185 {
4186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4187 return( ret );
4188
4189 continue;
4190 }
4191
Teppo Järvelin91d79382019-10-02 09:09:31 +03004192 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004193 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004194 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004195
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004196 /* Update position inside current message */
4197 ssl->handshake->cur_msg_p += cur->len;
4198 }
4199 else
4200 {
4201 const unsigned char * const p = ssl->handshake->cur_msg_p;
4202 const size_t hs_len = cur->len - 12;
4203 const size_t frag_off = p - ( cur->p + 12 );
4204 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004205 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004206
Hanno Beckere1dcb032018-08-17 16:47:58 +01004207 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004208 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004209 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004211
Hanno Becker67bc7c32018-08-06 11:33:50 +01004212 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4213 return( ret );
4214
4215 continue;
4216 }
4217 max_hs_frag_len = max_frag_len - 12;
4218
4219 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4220 max_hs_frag_len : rem_len;
4221
4222 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004223 {
4224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 (unsigned) cur_hs_frag_len,
4226 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004227 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004228
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004229 /* Messages are stored with handshake headers as if not fragmented,
4230 * copy beginning of headers then fill fragmentation fields.
4231 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004232 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004233
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004234 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4235 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4236 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004237
4238 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4239
Hanno Becker3f7b9732018-08-28 09:53:25 +01004240 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004241 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004243 ssl->out_msgtype = cur->type;
4244
4245 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004247 }
4248
4249 /* If done with the current message move to the next one if any */
4250 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4251 {
4252 if( cur->next != NULL )
4253 {
4254 ssl->handshake->cur_msg = cur->next;
4255 ssl->handshake->cur_msg_p = cur->next->p + 12;
4256 }
4257 else
4258 {
4259 ssl->handshake->cur_msg = NULL;
4260 ssl->handshake->cur_msg_p = NULL;
4261 }
4262 }
4263
4264 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004265 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004268 return( ret );
4269 }
4270 }
4271
Hanno Becker67bc7c32018-08-06 11:33:50 +01004272 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4273 return( ret );
4274
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004275 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004278 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004281 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4282 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004283
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285
4286 return( 0 );
4287}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004288
4289/*
4290 * To be called when the last message of an incoming flight is received.
4291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004293{
4294 /* We won't need to resend that one any more */
4295 ssl_flight_free( ssl->handshake->flight );
4296 ssl->handshake->flight = NULL;
4297 ssl->handshake->cur_msg = NULL;
4298
4299 /* The next incoming flight will start with this msg_seq */
4300 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4301
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004302 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004303 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004304
Hanno Becker0271f962018-08-16 13:23:47 +01004305 /* Clear future message buffering structure. */
4306 ssl_buffering_free( ssl );
4307
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004308 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004309 ssl_set_timer( ssl, 0 );
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4312 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004315 }
4316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004318}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004319
4320/*
4321 * To be called when the last message of an outgoing flight is send.
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004324{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004325 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004326 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004327
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é-Gonnard7de3c9e2014-09-29 15:29:48 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004332 }
4333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004339 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004340 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004341
4342/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004343 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004344 *
4345 * - fill in handshake headers
4346 * - update handshake checksum
4347 * - DTLS: save message for resending
4348 * - then pass to the record layer
4349 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004350 * DTLS: except for HelloRequest, messages are only queued, and will only be
4351 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004352 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004353 * Inputs:
4354 * - ssl->out_msglen: 4 + actual handshake message len
4355 * (4 is the size of handshake headers for TLS)
4356 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4357 * - ssl->out_msg + 4: the handshake message body
4358 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004359 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004360 * - ssl->out_msglen: the length of the record contents
4361 * (including handshake headers but excluding record headers)
4362 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004363 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004364int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004366 int ret;
4367 const size_t hs_len = ssl->out_msglen - 4;
4368 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4371
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004372 /*
4373 * Sanity checks
4374 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004375 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004376 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4377 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004378 /* In SSLv3, the client might send a NoCertificate alert. */
4379#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004380 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004381 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004382 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4383 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004384#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4385 {
4386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4388 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004391 /* Whenever we send anything different from a
4392 * HelloRequest we should be in a handshake - double check. */
4393 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4394 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004395 ssl->handshake == NULL )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004402 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004403 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004405 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004408 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004409#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004410
Hanno Beckerb50a2532018-08-06 11:52:54 +01004411 /* Double-check that we did not exceed the bounds
4412 * of the outgoing record buffer.
4413 * This should never fail as the various message
4414 * writing functions must obey the bounds of the
4415 * outgoing record buffer, but better be safe.
4416 *
4417 * Note: We deliberately do not check for the MTU or MFL here.
4418 */
4419 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4422 "size %u, maximum %u",
4423 (unsigned) ssl->out_msglen,
4424 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4426 }
4427
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004428 /*
4429 * Fill handshake headers
4430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004433 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004435 /*
4436 * DTLS has additional fields in the Handshake layer,
4437 * between the length field and the actual payload:
4438 * uint16 message_seq;
4439 * uint24 fragment_offset;
4440 * uint24 fragment_length;
4441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004443 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004444 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004445 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004446 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4449 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004450 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004451 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4453 }
4454
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004455 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004456 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004457
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004458 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004459 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004460 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004461 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4462 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004463 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004464 }
4465 else
4466 {
4467 ssl->out_msg[4] = 0;
4468 ssl->out_msg[5] = 0;
4469 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004470
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004471 /* Handshake hashes are computed without fragmentation,
4472 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004473 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004474 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004477
Hanno Becker0207e532018-08-28 10:28:28 +01004478 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004479 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004480 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 }
4482
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004483 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004485 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004486 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4487 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004488 {
4489 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004492 return( ret );
4493 }
4494 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004495 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004496#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004498 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004499 {
4500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4501 return( ret );
4502 }
4503 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004504
4505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4506
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004507 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004508}
4509
4510/*
4511 * Record layer functions
4512 */
4513
4514/*
4515 * Write current record.
4516 *
4517 * Uses:
4518 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4519 * - ssl->out_msglen: length of the record content (excl headers)
4520 * - ssl->out_msg: record content
4521 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004522int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004523{
4524 int ret, done = 0;
4525 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004526 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004527
4528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004531 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004533 {
4534 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004537 return( ret );
4538 }
4539
4540 len = ssl->out_msglen;
4541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4545 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 ret = mbedtls_ssl_hw_record_write( ssl );
4550 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004554 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004555
4556 if( ret == 0 )
4557 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004560 if( !done )
4561 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004562 unsigned i;
4563 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004564 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004565
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004566 /* Skip writing the record content type to after the encryption,
4567 * as it may change when using the CID extension. */
4568
Hanno Becker2881d802019-05-22 14:44:53 +01004569 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4570 mbedtls_ssl_get_minor_ver( ssl ),
4571 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004572
Teppo Järvelin91d79382019-10-02 09:09:31 +03004573 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004574 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575
Paul Bakker48916f92012-09-16 19:57:18 +00004576 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 {
Hanno Becker3307b532017-12-27 21:37:21 +00004578 mbedtls_record rec;
4579
4580 rec.buf = ssl->out_iv;
4581 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4582 ( ssl->out_iv - ssl->out_buf );
4583 rec.data_len = ssl->out_msglen;
4584 rec.data_offset = ssl->out_msg - rec.buf;
4585
Teppo Järvelin91d79382019-10-02 09:09:31 +03004586 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004587 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4588 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004589 ssl->conf->transport, rec.ver );
4590 rec.type = ssl->out_msgtype;
4591
Hanno Beckera5a2b082019-05-15 14:03:01 +01004592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004593 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004594 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004595#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004596
Hanno Becker611a83b2018-01-03 14:27:32 +00004597 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004598 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004599 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004602 return( ret );
4603 }
4604
Hanno Becker3307b532017-12-27 21:37:21 +00004605 if( rec.data_offset != 0 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4609 }
4610
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004611 /* Update the record content type and CID. */
4612 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004614 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4615 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004616#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004617 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004618 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004619 encrypted_fi = 1;
4620 }
4621
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004622 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004623 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4624 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004625 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004626 }
4627
Hanno Becker43395762019-05-03 14:46:38 +01004628 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004629
4630#if defined(MBEDTLS_SSL_PROTO_DTLS)
4631 /* In case of DTLS, double-check that we don't exceed
4632 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004634 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004635 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004636 if( ret < 0 )
4637 return( ret );
4638
4639 if( protected_record_size > (size_t) ret )
4640 {
4641 /* Should never happen */
4642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4643 }
4644 }
4645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004646
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004647 /* Now write the potentially updated record content type. */
4648 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004651 "version = [%d:%d], msglen = %d",
4652 ssl->out_hdr[0], ssl->out_hdr[1],
4653 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004656 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004657
4658 ssl->out_left += protected_record_size;
4659 ssl->out_hdr += protected_record_size;
4660 ssl_update_out_pointers( ssl, ssl->transform_out );
4661
Hanno Becker04484622018-08-06 09:49:38 +01004662 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4663 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4664 break;
4665
4666 /* The loop goes to its end iff the counter is wrapping */
4667 if( i == ssl_ep_len( ssl ) )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4670 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004672 }
4673
Hanno Becker67bc7c32018-08-06 11:33:50 +01004674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004675 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004676 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004677 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004678 size_t remaining;
4679 ret = ssl_get_remaining_payload_in_datagram( ssl );
4680 if( ret < 0 )
4681 {
4682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4683 ret );
4684 return( ret );
4685 }
4686
4687 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004688 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004689 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004690 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004691 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004692 else
4693 {
Hanno Becker513815a2018-08-20 11:56:09 +01004694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004695 }
4696 }
4697#endif /* MBEDTLS_SSL_PROTO_DTLS */
4698
4699 if( ( flush == SSL_FORCE_FLUSH ) &&
4700 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 return( ret );
4704 }
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004707
4708 return( 0 );
4709}
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004712
4713static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4714{
4715 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004716 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4717 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004718 {
4719 return( 1 );
4720 }
4721 return( 0 );
4722}
Hanno Becker44650b72018-08-16 12:51:11 +01004723
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004724static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004725{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004726 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004727}
4728
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004729static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004730{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004731 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004732}
4733
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004734static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004735{
4736 uint32_t msg_len, frag_off, frag_len;
4737
4738 msg_len = ssl_get_hs_total_len( ssl );
4739 frag_off = ssl_get_hs_frag_off( ssl );
4740 frag_len = ssl_get_hs_frag_len( ssl );
4741
4742 if( frag_off > msg_len )
4743 return( -1 );
4744
4745 if( frag_len > msg_len - frag_off )
4746 return( -1 );
4747
4748 if( frag_len + 12 > ssl->in_msglen )
4749 return( -1 );
4750
4751 return( 0 );
4752}
4753
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004754/*
4755 * Mark bits in bitmask (used for DTLS HS reassembly)
4756 */
4757static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4758{
4759 unsigned int start_bits, end_bits;
4760
4761 start_bits = 8 - ( offset % 8 );
4762 if( start_bits != 8 )
4763 {
4764 size_t first_byte_idx = offset / 8;
4765
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004766 /* Special case */
4767 if( len <= start_bits )
4768 {
4769 for( ; len != 0; len-- )
4770 mask[first_byte_idx] |= 1 << ( start_bits - len );
4771
4772 /* Avoid potential issues with offset or len becoming invalid */
4773 return;
4774 }
4775
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004776 offset += start_bits; /* Now offset % 8 == 0 */
4777 len -= start_bits;
4778
4779 for( ; start_bits != 0; start_bits-- )
4780 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4781 }
4782
4783 end_bits = len % 8;
4784 if( end_bits != 0 )
4785 {
4786 size_t last_byte_idx = ( offset + len ) / 8;
4787
4788 len -= end_bits; /* Now len % 8 == 0 */
4789
4790 for( ; end_bits != 0; end_bits-- )
4791 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4792 }
4793
4794 memset( mask + offset / 8, 0xFF, len / 8 );
4795}
4796
4797/*
4798 * Check that bitmask is full
4799 */
4800static int ssl_bitmask_check( unsigned char *mask, size_t len )
4801{
4802 size_t i;
4803
4804 for( i = 0; i < len / 8; i++ )
4805 if( mask[i] != 0xFF )
4806 return( -1 );
4807
4808 for( i = 0; i < len % 8; i++ )
4809 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4810 return( -1 );
4811
4812 return( 0 );
4813}
4814
Hanno Becker56e205e2018-08-16 09:06:12 +01004815/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004816static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004817 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004818{
Hanno Becker56e205e2018-08-16 09:06:12 +01004819 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004820
Hanno Becker56e205e2018-08-16 09:06:12 +01004821 alloc_len = 12; /* Handshake header */
4822 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004823
Hanno Beckerd07df862018-08-16 09:14:58 +01004824 if( add_bitmap )
4825 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004826
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004827 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828}
Hanno Becker56e205e2018-08-16 09:06:12 +01004829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004831
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004832static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004833{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004834 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004835}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004836
Simon Butcher99000142016-10-13 17:21:01 +01004837int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004842 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004844 }
4845
Hanno Becker12555c62018-08-16 12:47:53 +01004846 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004850 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004853 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004854 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004855 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004856 unsigned int recv_msg_seq = (unsigned int)
4857 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004858
Hanno Becker44650b72018-08-16 12:51:11 +01004859 if( ssl_check_hs_header( ssl ) != 0 )
4860 {
4861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4862 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4863 }
4864
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004865 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004866 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4867 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4868 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4869 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004870 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004871 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4874 recv_msg_seq,
4875 ssl->handshake->in_msg_seq ) );
4876 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4877 }
4878
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004879 /* Retransmit only on last message from previous flight, to avoid
4880 * too many retransmissions.
4881 * Besides, No sane server ever retransmits HelloVerifyRequest */
4882 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004886 "message_seq = %d, start_of_flight = %d",
4887 recv_msg_seq,
4888 ssl->handshake->in_flight_start_seq ) );
4889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004893 return( ret );
4894 }
4895 }
4896 else
4897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004899 "message_seq = %d, expected = %d",
4900 recv_msg_seq,
4901 ssl->handshake->in_msg_seq ) );
4902 }
4903
Hanno Becker90333da2017-10-10 11:27:13 +01004904 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004905 }
4906 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004907
Hanno Becker6d97ef52018-08-16 13:09:04 +01004908 /* Message reassembly is handled alongside buffering of future
4909 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004910 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004911 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004912 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004915 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004916 }
4917 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004918 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004920#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004921 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* With TLS we don't handle fragmentation (for now) */
4923 if( ssl->in_msglen < ssl->in_hslen )
4924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4927 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004928 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004929#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004930
Simon Butcher99000142016-10-13 17:21:01 +01004931 return( 0 );
4932}
4933
4934void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4935{
Hanno Becker0271f962018-08-16 13:23:47 +01004936 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004937
Hanno Becker0271f962018-08-16 13:23:47 +01004938 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004939 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004940
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004941 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004944 ssl->handshake != NULL )
4945 {
Hanno Becker0271f962018-08-16 13:23:47 +01004946 unsigned offset;
4947 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004948
Hanno Becker0271f962018-08-16 13:23:47 +01004949 /* Increment handshake sequence number */
4950 hs->in_msg_seq++;
4951
4952 /*
4953 * Clear up handshake buffering and reassembly structure.
4954 */
4955
4956 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004957 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004958
4959 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004960 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4961 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004962 offset++, hs_buf++ )
4963 {
4964 *hs_buf = *(hs_buf + 1);
4965 }
4966
4967 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004968 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004969 }
4970#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004971}
4972
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974 * DTLS anti-replay: RFC 6347 4.1.2.6
4975 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004976 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4977 * Bit n is set iff record number in_window_top - n has been seen.
4978 *
4979 * Usually, in_window_top is the last record number seen and the lsb of
4980 * in_window is set. The only exception is the initial state (record number 0
4981 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4984static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004985{
4986 ssl->in_window_top = 0;
4987 ssl->in_window = 0;
4988}
4989
4990static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4991{
4992 return( ( (uint64_t) buf[0] << 40 ) |
4993 ( (uint64_t) buf[1] << 32 ) |
4994 ( (uint64_t) buf[2] << 24 ) |
4995 ( (uint64_t) buf[3] << 16 ) |
4996 ( (uint64_t) buf[4] << 8 ) |
4997 ( (uint64_t) buf[5] ) );
4998}
4999
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005000static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5001{
5002 int ret;
5003 unsigned char *original_in_ctr;
5004
5005 // save original in_ctr
5006 original_in_ctr = ssl->in_ctr;
5007
5008 // use counter from record
5009 ssl->in_ctr = record_in_ctr;
5010
5011 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5012
5013 // restore the counter
5014 ssl->in_ctr = original_in_ctr;
5015
5016 return ret;
5017}
5018
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005019/*
5020 * Return 0 if sequence number is acceptable, -1 otherwise
5021 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005022int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005023{
5024 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5025 uint64_t bit;
5026
Hanno Becker7f376f42019-06-12 16:20:48 +01005027 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5028 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5029 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005030 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005031 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005032
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005033 if( rec_seqnum > ssl->in_window_top )
5034 return( 0 );
5035
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005036 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005037
5038 if( bit >= 64 )
5039 return( -1 );
5040
5041 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5042 return( -1 );
5043
5044 return( 0 );
5045}
5046
5047/*
5048 * Update replay window on new validated record
5049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005051{
5052 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5053
Hanno Becker7f376f42019-06-12 16:20:48 +01005054 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5055 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5056 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005057 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005058 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005059
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005060 if( rec_seqnum > ssl->in_window_top )
5061 {
5062 /* Update window_top and the contents of the window */
5063 uint64_t shift = rec_seqnum - ssl->in_window_top;
5064
5065 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005066 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005067 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005068 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005069 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005070 ssl->in_window |= 1;
5071 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005072
5073 ssl->in_window_top = rec_seqnum;
5074 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005075 else
5076 {
5077 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005078 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079
5080 if( bit < 64 ) /* Always true, but be extra sure */
5081 ssl->in_window |= (uint64_t) 1 << bit;
5082 }
5083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005085
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005086#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005087/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005088static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5089
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005090/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005091 * Without any SSL context, check if a datagram looks like a ClientHello with
5092 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005093 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005094 *
5095 * - if cookie is valid, return 0
5096 * - if ClientHello looks superficially valid but cookie is not,
5097 * fill obuf and set olen, then
5098 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5099 * - otherwise return a specific error code
5100 */
5101static int ssl_check_dtls_clihlo_cookie(
5102 mbedtls_ssl_cookie_write_t *f_cookie_write,
5103 mbedtls_ssl_cookie_check_t *f_cookie_check,
5104 void *p_cookie,
5105 const unsigned char *cli_id, size_t cli_id_len,
5106 const unsigned char *in, size_t in_len,
5107 unsigned char *obuf, size_t buf_len, size_t *olen )
5108{
5109 size_t sid_len, cookie_len;
5110 unsigned char *p;
5111
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 /*
5113 * Structure of ClientHello with record and handshake headers,
5114 * and expected values. We don't need to check a lot, more checks will be
5115 * done when actually parsing the ClientHello - skipping those checks
5116 * avoids code duplication and does not make cookie forging any easier.
5117 *
5118 * 0-0 ContentType type; copied, must be handshake
5119 * 1-2 ProtocolVersion version; copied
5120 * 3-4 uint16 epoch; copied, must be 0
5121 * 5-10 uint48 sequence_number; copied
5122 * 11-12 uint16 length; (ignored)
5123 *
5124 * 13-13 HandshakeType msg_type; (ignored)
5125 * 14-16 uint24 length; (ignored)
5126 * 17-18 uint16 message_seq; copied
5127 * 19-21 uint24 fragment_offset; copied, must be 0
5128 * 22-24 uint24 fragment_length; (ignored)
5129 *
5130 * 25-26 ProtocolVersion client_version; (ignored)
5131 * 27-58 Random random; (ignored)
5132 * 59-xx SessionID session_id; 1 byte len + sid_len content
5133 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5134 * ...
5135 *
5136 * Minimum length is 61 bytes.
5137 */
5138 if( in_len < 61 ||
5139 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5140 in[3] != 0 || in[4] != 0 ||
5141 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5142 {
5143 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5144 }
5145
5146 sid_len = in[59];
5147 if( sid_len > in_len - 61 )
5148 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5149
5150 cookie_len = in[60 + sid_len];
5151 if( cookie_len > in_len - 60 )
5152 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5153
5154 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5155 cli_id, cli_id_len ) == 0 )
5156 {
5157 /* Valid cookie */
5158 return( 0 );
5159 }
5160
5161 /*
5162 * If we get here, we've got an invalid cookie, let's prepare HVR.
5163 *
5164 * 0-0 ContentType type; copied
5165 * 1-2 ProtocolVersion version; copied
5166 * 3-4 uint16 epoch; copied
5167 * 5-10 uint48 sequence_number; copied
5168 * 11-12 uint16 length; olen - 13
5169 *
5170 * 13-13 HandshakeType msg_type; hello_verify_request
5171 * 14-16 uint24 length; olen - 25
5172 * 17-18 uint16 message_seq; copied
5173 * 19-21 uint24 fragment_offset; copied
5174 * 22-24 uint24 fragment_length; olen - 25
5175 *
5176 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5177 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5178 *
5179 * Minimum length is 28.
5180 */
5181 if( buf_len < 28 )
5182 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5183
5184 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005185 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005186 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5187 obuf[25] = 0xfe;
5188 obuf[26] = 0xff;
5189
5190 /* Generate and write actual cookie */
5191 p = obuf + 28;
5192 if( f_cookie_write( p_cookie,
5193 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5194 {
5195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5196 }
5197
5198 *olen = p - obuf;
5199
5200 /* Go back and fill length fields */
5201 obuf[27] = (unsigned char)( *olen - 28 );
5202
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005203 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005204 obuf[22] = obuf[14];
5205 obuf[23] = obuf[15];
5206 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005207
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005208 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005209
5210 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5211}
5212
5213/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005214 * Handle possible client reconnect with the same UDP quadruplet
5215 * (RFC 6347 Section 4.2.8).
5216 *
5217 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5218 * that looks like a ClientHello.
5219 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005220 * - if the input looks like a ClientHello without cookies,
5221 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005222 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005223 * - if the input looks like a ClientHello with a valid cookie,
5224 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005225 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005227 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005228 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005229 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5230 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 */
5232static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5233{
5234 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005235 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005236
Hanno Becker87b56262019-07-10 14:37:41 +01005237 if( ssl->conf->f_cookie_write == NULL ||
5238 ssl->conf->f_cookie_check == NULL )
5239 {
5240 /* If we can't use cookies to verify reachability of the peer,
5241 * drop the record. */
5242 return( 0 );
5243 }
5244
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005245 ret = ssl_check_dtls_clihlo_cookie(
5246 ssl->conf->f_cookie_write,
5247 ssl->conf->f_cookie_check,
5248 ssl->conf->p_cookie,
5249 ssl->cli_id, ssl->cli_id_len,
5250 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005251 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005252
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5254
5255 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005256 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005257 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005258 * If the error is permanent we'll catch it later,
5259 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005260 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005261 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005262 }
5263
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005264 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005265 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005266 /* Got a valid cookie, partially reset context */
5267 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5268 {
5269 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5270 return( ret );
5271 }
5272
5273 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274 }
5275
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005276 return( ret );
5277}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005278#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005279
Hanno Becker46483f12019-05-03 13:25:54 +01005280static int ssl_check_record_type( uint8_t record_type )
5281{
5282 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5283 record_type != MBEDTLS_SSL_MSG_ALERT &&
5284 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5285 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5286 {
5287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5288 }
5289
5290 return( 0 );
5291}
5292
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005293/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294 * ContentType type;
5295 * ProtocolVersion version;
5296 * uint16 epoch; // DTLS only
5297 * uint48 sequence_number; // DTLS only
5298 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005299 *
5300 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005301 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005302 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5303 *
5304 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005305 * 1. proceed with the record if this function returns 0
5306 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5307 * 3. return CLIENT_RECONNECT if this function return that value
5308 * 4. drop the whole datagram if this function returns anything else.
5309 * Point 2 is needed when the peer is resending, and we have already received
5310 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005311 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005312static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 unsigned char *buf,
5314 size_t len,
5315 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005317 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319 size_t const rec_hdr_type_offset = 0;
5320 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005321
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005322 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5323 rec_hdr_type_len;
5324 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005326 size_t const rec_hdr_ctr_len = 8;
5327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005328 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5330 rec_hdr_version_len;
5331
Hanno Beckera5a2b082019-05-15 14:03:01 +01005332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5334 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005335 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5337#endif /* MBEDTLS_SSL_PROTO_DTLS */
5338
5339 size_t rec_hdr_len_offset; /* To be determined */
5340 size_t const rec_hdr_len_len = 2;
5341
5342 /*
5343 * Check minimum lengths for record header.
5344 */
5345
5346#if defined(MBEDTLS_SSL_PROTO_DTLS)
5347 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5348 {
5349 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5350 }
5351 MBEDTLS_SSL_TRANSPORT_ELSE
5352#endif /* MBEDTLS_SSL_PROTO_DTLS */
5353#if defined(MBEDTLS_SSL_PROTO_TLS)
5354 {
5355 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5356 }
5357#endif /* MBEDTLS_SSL_PROTO_DTLS */
5358
5359 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5362 (unsigned) len,
5363 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5365 }
5366
5367 /*
5368 * Parse and validate record content type
5369 */
5370
5371 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005372
5373 /* Check record content type */
5374#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5375 rec->cid_len = 0;
5376
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5379 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005380 {
5381 /* Shift pointers to account for record header including CID
5382 * struct {
5383 * ContentType special_type = tls12_cid;
5384 * ProtocolVersion version;
5385 * uint16 epoch;
5386 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005387 * opaque cid[cid_length]; // Additional field compared to
5388 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005389 * uint16 length;
5390 * opaque enc_content[DTLSCiphertext.length];
5391 * } DTLSCiphertext;
5392 */
5393
5394 /* So far, we only support static CID lengths
5395 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005396 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5397 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005398
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005399 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005400 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5402 (unsigned) len,
5403 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005405 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005407 /* configured CID len is guaranteed at most 255, see
5408 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5409 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005410 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5411 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005412 }
5413 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005415 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005416 if( ssl_check_record_type( rec->type ) )
5417 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5419 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5421 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005422 }
5423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 /*
5425 * Parse and validate record version
5426 */
5427
Hanno Becker8061c6e2019-07-26 08:07:03 +01005428 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5429 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005430 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5431 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005432 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005433
Hanno Becker2881d802019-05-22 14:44:53 +01005434 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5437 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005440 if( mbedtls_ssl_ver_gt( minor_ver,
5441 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5444 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 }
5446
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005447 /*
5448 * Parse/Copy record sequence number.
5449 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005450
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005451#if defined(MBEDTLS_SSL_PROTO_DTLS)
5452 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5453 {
5454 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005455 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5456 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005457 rec_hdr_ctr_len );
5458 }
5459 MBEDTLS_SSL_TRANSPORT_ELSE
5460#endif /* MBEDTLS_SSL_PROTO_DTLS */
5461#if defined(MBEDTLS_SSL_PROTO_TLS)
5462 {
5463 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005464 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5465 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005466 }
5467#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005468
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005469 /*
5470 * Parse record length.
5471 */
5472
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005473 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005474 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5476
Hanno Becker8b09b732019-05-08 12:03:28 +01005477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005478 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005479 rec->type,
5480 major_ver, minor_ver, rec->data_len ) );
5481
5482 rec->buf = buf;
5483 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005484
Hanno Beckerec014082019-07-26 08:20:27 +01005485 if( rec->data_len == 0 )
5486 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5487
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005488 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005489 * DTLS-related tests.
5490 * Check epoch before checking length constraint because
5491 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5492 * message gets duplicated before the corresponding Finished message,
5493 * the second ChangeCipherSpec should be discarded because it belongs
5494 * to an old epoch, but not because its length is shorter than
5495 * the minimum record length for packets using the new record transform.
5496 * Note that these two kinds of failures are handled differently,
5497 * as an unexpected record is silently skipped but an invalid
5498 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005499 */
5500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005501 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005502 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005503 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005504
Hanno Beckere0452772019-07-10 17:12:07 +01005505 /* Check that the datagram is large enough to contain a record
5506 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005507 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005508 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5510 (unsigned) len,
5511 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005512 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5513 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005514
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005515 /* Records from other, non-matching epochs are silently discarded.
5516 * (The case of same-port Client reconnects must be considered in
5517 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005518 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005519 {
5520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5521 "expected %d, received %d",
5522 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005523
5524 /* Records from the next epoch are considered for buffering
5525 * (concretely: early Finished messages). */
5526 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5527 {
5528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5530 }
5531
Hanno Becker87b56262019-07-10 14:37:41 +01005532 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005534#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005535 /* For records from the correct epoch, check whether their
5536 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005537 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5538 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005539 {
5540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5542 }
5543#endif
5544 }
5545#endif /* MBEDTLS_SSL_PROTO_DTLS */
5546
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005547 return( 0 );
5548}
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
Hanno Becker87b56262019-07-10 14:37:41 +01005550
5551#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5552static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5553{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005554 unsigned int rec_epoch = (unsigned int)
5555 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005556
5557 /*
5558 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5559 * access the first byte of record content (handshake type), as we
5560 * have an active transform (possibly iv_len != 0), so use the
5561 * fact that the record header len is 13 instead.
5562 */
5563 if( rec_epoch == 0 &&
5564 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5565 MBEDTLS_SSL_IS_SERVER &&
5566 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5567 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5568 ssl->in_left > 13 &&
5569 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5572 "from the same port" ) );
5573 return( ssl_handle_possible_reconnect( ssl ) );
5574 }
5575
5576 return( 0 );
5577}
5578#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5579
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005580/*
5581 * If applicable, decrypt (and decompress) record content
5582 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005583static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5584 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005585{
5586 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005589 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5592 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 ret = mbedtls_ssl_hw_record_read( ssl );
5597 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5600 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005601 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005602
5603 if( ret == 0 )
5604 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005607 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005609 unsigned char const old_msg_type = rec->type;
5610
Hanno Becker611a83b2018-01-03 14:27:32 +00005611 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005612 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005615
Hanno Beckera5a2b082019-05-15 14:03:01 +01005616#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005617 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005618 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5619 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005620 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005622 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005623 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005625
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 return( ret );
5627 }
5628
Hanno Becker106f3da2019-07-12 09:35:58 +01005629 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005630 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005631 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005632 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005634
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005636 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Hanno Beckera5a2b082019-05-15 14:03:01 +01005638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005639 /* We have already checked the record content type
5640 * in ssl_parse_record_header(), failing or silently
5641 * dropping the record in the case of an unknown type.
5642 *
5643 * Since with the use of CIDs, the record content type
5644 * might change during decryption, re-check the record
5645 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005646 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005647 {
5648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5650 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005651#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005652
Hanno Becker106f3da2019-07-12 09:35:58 +01005653 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005654 {
5655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005656 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005657 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005658 {
5659 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5662 }
5663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5664
5665 ssl->nb_zero++;
5666
5667 /*
5668 * Three or more empty messages may be a DoS attack
5669 * (excessive CPU consumption).
5670 */
5671 if( ssl->nb_zero > 3 )
5672 {
5673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005674 "messages, possible DoS attack" ) );
5675 /* Treat the records as if they were not properly authenticated,
5676 * thereby failing the connection if we see more than allowed
5677 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005678 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5679 }
5680 }
5681 else
5682 ssl->nb_zero = 0;
5683
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005684 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5685#if defined(MBEDTLS_SSL_PROTO_TLS)
5686 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005687 {
5688 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005689 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005690 if( ++ssl->in_ctr[i - 1] != 0 )
5691 break;
5692
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005693 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005694 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005695 {
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5697 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5698 }
5699 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005700#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005701
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 }
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005705 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005707 {
5708 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005711 return( ret );
5712 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005717 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005720 }
5721#endif
5722
Hanno Beckerf0242852019-07-09 17:30:02 +01005723 /* Check actual (decrypted) record content length against
5724 * configured maximum. */
5725 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5728 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5729 }
5730
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005731 return( 0 );
5732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005735
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736/*
5737 * Read a record.
5738 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005739 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5740 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5741 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005742 */
Hanno Becker1097b342018-08-15 14:09:41 +01005743
5744/* Helper functions for mbedtls_ssl_read_record(). */
5745static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005746static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5747static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005748
Hanno Becker327c93b2018-08-15 13:56:18 +01005749int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005750 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005751{
5752 int ret;
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005755
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005756 if( ssl->keep_current_message == 0 )
5757 {
5758 do {
Simon Butcher99000142016-10-13 17:21:01 +01005759
Hanno Becker26994592018-08-15 14:14:59 +01005760 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005761 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005762 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005763
Hanno Beckere74d5562018-08-15 14:26:08 +01005764 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005765 {
Hanno Becker40f50842018-08-15 14:48:01 +01005766#if defined(MBEDTLS_SSL_PROTO_DTLS)
5767 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005768
Hanno Becker40f50842018-08-15 14:48:01 +01005769 /* We only check for buffered messages if the
5770 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005771 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005772 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005773 {
Hanno Becker40f50842018-08-15 14:48:01 +01005774 if( ssl_load_buffered_message( ssl ) == 0 )
5775 have_buffered = 1;
5776 }
5777
5778 if( have_buffered == 0 )
5779#endif /* MBEDTLS_SSL_PROTO_DTLS */
5780 {
5781 ret = ssl_get_next_record( ssl );
5782 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5783 continue;
5784
5785 if( ret != 0 )
5786 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005787 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005788 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005789 return( ret );
5790 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005791 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005792 }
5793
5794 ret = mbedtls_ssl_handle_message_type( ssl );
5795
Hanno Becker40f50842018-08-15 14:48:01 +01005796#if defined(MBEDTLS_SSL_PROTO_DTLS)
5797 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5798 {
5799 /* Buffer future message */
5800 ret = ssl_buffer_message( ssl );
5801 if( ret != 0 )
5802 return( ret );
5803
5804 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5805 }
5806#endif /* MBEDTLS_SSL_PROTO_DTLS */
5807
Hanno Becker90333da2017-10-10 11:27:13 +01005808 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5809 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005810
5811 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005812 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005813 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005814 return( ret );
5815 }
5816
Hanno Becker327c93b2018-08-15 13:56:18 +01005817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005818 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005819 {
5820 mbedtls_ssl_update_handshake_status( ssl );
5821 }
Simon Butcher99000142016-10-13 17:21:01 +01005822 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005823 else
Simon Butcher99000142016-10-13 17:21:01 +01005824 {
Hanno Becker02f59072018-08-15 14:00:24 +01005825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005826 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005827 }
5828
5829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5830
5831 return( 0 );
5832}
5833
Hanno Becker40f50842018-08-15 14:48:01 +01005834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005835static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005836{
Hanno Becker40f50842018-08-15 14:48:01 +01005837 if( ssl->in_left > ssl->next_record_offset )
5838 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005839
Hanno Becker40f50842018-08-15 14:48:01 +01005840 return( 0 );
5841}
5842
5843static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5844{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005846 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 int ret = 0;
5848
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005849 if( hs == NULL )
5850 return( -1 );
5851
Hanno Beckere00ae372018-08-20 09:39:42 +01005852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5853
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005854 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5855 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5856 {
5857 /* Check if we have seen a ChangeCipherSpec before.
5858 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005859 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5862 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005863 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 }
5865
Hanno Becker39b8bc92018-08-28 17:17:13 +01005866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5868 ssl->in_msglen = 1;
5869 ssl->in_msg[0] = 1;
5870
5871 /* As long as they are equal, the exact value doesn't matter. */
5872 ssl->in_left = 0;
5873 ssl->next_record_offset = 0;
5874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005875 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 goto exit;
5877 }
Hanno Becker37f95322018-08-16 13:55:32 +01005878
Hanno Beckerb8f50142018-08-28 10:01:34 +01005879#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005880 /* Debug only */
5881 {
5882 unsigned offset;
5883 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5884 {
5885 hs_buf = &hs->buffering.hs[offset];
5886 if( hs_buf->is_valid == 1 )
5887 {
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5889 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005890 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005891 }
5892 }
5893 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005894#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005895
5896 /* Check if we have buffered and/or fully reassembled the
5897 * next handshake message. */
5898 hs_buf = &hs->buffering.hs[0];
5899 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5900 {
5901 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005902 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005903
5904 /* Double-check that we haven't accidentally buffered
5905 * a message that doesn't fit into the input buffer. */
5906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5910 }
5911
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5913 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5914 hs_buf->data, msg_len + 12 );
5915
5916 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5917 ssl->in_hslen = msg_len + 12;
5918 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005919 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005920
5921 ret = 0;
5922 goto exit;
5923 }
5924 else
5925 {
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5927 hs->in_msg_seq ) );
5928 }
5929
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005930 ret = -1;
5931
5932exit:
5933
5934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5935 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005936}
5937
Hanno Beckera02b0b42018-08-21 17:20:27 +01005938static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5939 size_t desired )
5940{
5941 int offset;
5942 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5944 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005945
Hanno Becker01315ea2018-08-21 17:22:17 +01005946 /* Get rid of future records epoch first, if such exist. */
5947 ssl_free_buffered_record( ssl );
5948
5949 /* Check if we have enough space available now. */
5950 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5951 hs->buffering.total_bytes_buffered ) )
5952 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005954 return( 0 );
5955 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956
Hanno Becker4f432ad2018-08-28 10:02:32 +01005957 /* We don't have enough space to buffer the next expected handshake
5958 * message. Remove buffers used for future messages to gain space,
5959 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005960 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5961 offset >= 0; offset-- )
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5964 offset ) );
5965
Hanno Beckerb309b922018-08-23 13:18:05 +01005966 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005967
5968 /* Check if we have enough space available now. */
5969 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5970 hs->buffering.total_bytes_buffered ) )
5971 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005973 return( 0 );
5974 }
5975 }
5976
5977 return( -1 );
5978}
5979
Hanno Becker40f50842018-08-15 14:48:01 +01005980static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5981{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005982 int ret = 0;
5983 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5984
5985 if( hs == NULL )
5986 return( 0 );
5987
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5989
5990 switch( ssl->in_msgtype )
5991 {
5992 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005994
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005995 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005996 break;
5997
5998 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005999 {
6000 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006001 unsigned recv_msg_seq = (unsigned)
6002 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006003
Hanno Becker37f95322018-08-16 13:55:32 +01006004 mbedtls_ssl_hs_buffer *hs_buf;
6005 size_t msg_len = ssl->in_hslen - 12;
6006
6007 /* We should never receive an old handshake
6008 * message - double-check nonetheless. */
6009 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6013 }
6014
6015 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6016 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6017 {
6018 /* Silently ignore -- message too far in the future */
6019 MBEDTLS_SSL_DEBUG_MSG( 2,
6020 ( "Ignore future HS message with sequence number %u, "
6021 "buffering window %u - %u",
6022 recv_msg_seq, ssl->handshake->in_msg_seq,
6023 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6024
6025 goto exit;
6026 }
6027
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6029 recv_msg_seq, recv_msg_seq_offset ) );
6030
6031 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6032
6033 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006034 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006035 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006036 size_t reassembly_buf_sz;
6037
Hanno Becker37f95322018-08-16 13:55:32 +01006038 hs_buf->is_fragmented =
6039 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6040
6041 /* We copy the message back into the input buffer
6042 * after reassembly, so check that it's not too large.
6043 * This is an implementation-specific limitation
6044 * and not one from the standard, hence it is not
6045 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006046 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006047 {
6048 /* Ignore message */
6049 goto exit;
6050 }
6051
Hanno Beckere0b150f2018-08-21 15:51:03 +01006052 /* Check if we have enough space to buffer the message. */
6053 if( hs->buffering.total_bytes_buffered >
6054 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6055 {
6056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6058 }
6059
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006060 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6061 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006062
6063 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6064 hs->buffering.total_bytes_buffered ) )
6065 {
6066 if( recv_msg_seq_offset > 0 )
6067 {
6068 /* If we can't buffer a future message because
6069 * of space limitations -- ignore. */
6070 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",
6071 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6072 (unsigned) hs->buffering.total_bytes_buffered ) );
6073 goto exit;
6074 }
Hanno Beckere1801392018-08-21 16:51:05 +01006075 else
6076 {
6077 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",
6078 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6079 (unsigned) hs->buffering.total_bytes_buffered ) );
6080 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006081
Hanno Beckera02b0b42018-08-21 17:20:27 +01006082 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006083 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006084 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",
6085 (unsigned) msg_len,
6086 (unsigned) reassembly_buf_sz,
6087 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006088 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006089 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6090 goto exit;
6091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006092 }
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6095 msg_len ) );
6096
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006097 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6098 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006099 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006100 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006101 goto exit;
6102 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006103 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006104
6105 /* Prepare final header: copy msg_type, length and message_seq,
6106 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006107 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006108 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006109 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006110
6111 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006112
6113 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006114 }
6115 else
6116 {
6117 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006118 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 +01006119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6121 /* Ignore */
6122 goto exit;
6123 }
6124 }
6125
Hanno Becker4422bbb2018-08-20 09:40:19 +01006126 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006127 {
6128 size_t frag_len, frag_off;
6129 unsigned char * const msg = hs_buf->data + 12;
6130
6131 /*
6132 * Check and copy current fragment
6133 */
6134
6135 /* Validation of header fields already done in
6136 * mbedtls_ssl_prepare_handshake_record(). */
6137 frag_off = ssl_get_hs_frag_off( ssl );
6138 frag_len = ssl_get_hs_frag_len( ssl );
6139
6140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6141 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006142 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006143
6144 if( hs_buf->is_fragmented )
6145 {
6146 unsigned char * const bitmask = msg + msg_len;
6147 ssl_bitmask_set( bitmask, frag_off, frag_len );
6148 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6149 msg_len ) == 0 );
6150 }
6151 else
6152 {
6153 hs_buf->is_complete = 1;
6154 }
6155
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6157 hs_buf->is_complete ? "" : "not yet " ) );
6158 }
6159
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006160 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006161 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006162
6163 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006164 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006165 break;
6166 }
6167
6168exit:
6169
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6171 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006172}
6173#endif /* MBEDTLS_SSL_PROTO_DTLS */
6174
Hanno Becker1097b342018-08-15 14:09:41 +01006175static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006176{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006178 * Consume last content-layer message and potentially
6179 * update in_msglen which keeps track of the contents'
6180 * consumption state.
6181 *
6182 * (1) Handshake messages:
6183 * Remove last handshake message, move content
6184 * and adapt in_msglen.
6185 *
6186 * (2) Alert messages:
6187 * Consume whole record content, in_msglen = 0.
6188 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006189 * (3) Change cipher spec:
6190 * Consume whole record content, in_msglen = 0.
6191 *
6192 * (4) Application data:
6193 * Don't do anything - the record layer provides
6194 * the application data as a stream transport
6195 * and consumes through mbedtls_ssl_read only.
6196 *
6197 */
6198
6199 /* Case (1): Handshake messages */
6200 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006202 /* Hard assertion to be sure that no application data
6203 * is in flight, as corrupting ssl->in_msglen during
6204 * ssl->in_offt != NULL is fatal. */
6205 if( ssl->in_offt != NULL )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6209 }
6210
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006211 /*
6212 * Get next Handshake message in the current record
6213 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006214
Hanno Becker4a810fb2017-05-24 16:27:30 +01006215 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006216 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006217 * current handshake content: If DTLS handshake
6218 * fragmentation is used, that's the fragment
6219 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006220 * size here is faulty and should be changed at
6221 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006222 * (2) While it doesn't seem to cause problems, one
6223 * has to be very careful not to assume that in_hslen
6224 * is always <= in_msglen in a sensible communication.
6225 * Again, it's wrong for DTLS handshake fragmentation.
6226 * The following check is therefore mandatory, and
6227 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006228 * Additionally, ssl->in_hslen might be arbitrarily out of
6229 * bounds after handling a DTLS message with an unexpected
6230 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 */
6232 if( ssl->in_hslen < ssl->in_msglen )
6233 {
6234 ssl->in_msglen -= ssl->in_hslen;
6235 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6236 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006237
Hanno Becker4a810fb2017-05-24 16:27:30 +01006238 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6239 ssl->in_msg, ssl->in_msglen );
6240 }
6241 else
6242 {
6243 ssl->in_msglen = 0;
6244 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006245
Hanno Becker4a810fb2017-05-24 16:27:30 +01006246 ssl->in_hslen = 0;
6247 }
6248 /* Case (4): Application data */
6249 else if( ssl->in_offt != NULL )
6250 {
6251 return( 0 );
6252 }
6253 /* Everything else (CCS & Alerts) */
6254 else
6255 {
6256 ssl->in_msglen = 0;
6257 }
6258
Hanno Becker1097b342018-08-15 14:09:41 +01006259 return( 0 );
6260}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006261
Hanno Beckere74d5562018-08-15 14:26:08 +01006262static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6263{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006264 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006265 return( 1 );
6266
6267 return( 0 );
6268}
6269
Hanno Becker5f066e72018-08-16 14:56:31 +01006270#if defined(MBEDTLS_SSL_PROTO_DTLS)
6271
6272static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6273{
6274 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6275 if( hs == NULL )
6276 return;
6277
Hanno Becker01315ea2018-08-21 17:22:17 +01006278 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006279 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006280 hs->buffering.total_bytes_buffered -=
6281 hs->buffering.future_record.len;
6282
6283 mbedtls_free( hs->buffering.future_record.data );
6284 hs->buffering.future_record.data = NULL;
6285 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006286}
6287
6288static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6289{
6290 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6291 unsigned char * rec;
6292 size_t rec_len;
6293 unsigned rec_epoch;
6294
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006295 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006296 return( 0 );
6297
6298 if( hs == NULL )
6299 return( 0 );
6300
Hanno Becker5f066e72018-08-16 14:56:31 +01006301 rec = hs->buffering.future_record.data;
6302 rec_len = hs->buffering.future_record.len;
6303 rec_epoch = hs->buffering.future_record.epoch;
6304
6305 if( rec == NULL )
6306 return( 0 );
6307
Hanno Becker4cb782d2018-08-20 11:19:05 +01006308 /* Only consider loading future records if the
6309 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006310 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006311 return( 0 );
6312
Hanno Becker5f066e72018-08-16 14:56:31 +01006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6314
6315 if( rec_epoch != ssl->in_epoch )
6316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6318 goto exit;
6319 }
6320
6321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6322
6323 /* Double-check that the record is not too large */
6324 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6325 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6329 }
6330
Teppo Järvelin91d79382019-10-02 09:09:31 +03006331 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 ssl->in_left = rec_len;
6333 ssl->next_record_offset = 0;
6334
6335 ssl_free_buffered_record( ssl );
6336
6337exit:
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6339 return( 0 );
6340}
6341
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006342static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6343 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006344{
6345 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006346
6347 /* Don't buffer future records outside handshakes. */
6348 if( hs == NULL )
6349 return( 0 );
6350
6351 /* Only buffer handshake records (we are only interested
6352 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006353 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006354 return( 0 );
6355
6356 /* Don't buffer more than one future epoch record. */
6357 if( hs->buffering.future_record.data != NULL )
6358 return( 0 );
6359
Hanno Becker01315ea2018-08-21 17:22:17 +01006360 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006361 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006362 hs->buffering.total_bytes_buffered ) )
6363 {
6364 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 +01006365 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006366 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006367 return( 0 );
6368 }
6369
Hanno Becker5f066e72018-08-16 14:56:31 +01006370 /* Buffer record */
6371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6372 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006373 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006374
6375 /* ssl_parse_record_header() only considers records
6376 * of the next epoch as candidates for buffering. */
6377 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006378 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006379
6380 hs->buffering.future_record.data =
6381 mbedtls_calloc( 1, hs->buffering.future_record.len );
6382 if( hs->buffering.future_record.data == NULL )
6383 {
6384 /* If we run out of RAM trying to buffer a
6385 * record from the next epoch, just ignore. */
6386 return( 0 );
6387 }
6388
Teppo Järvelin91d79382019-10-02 09:09:31 +03006389 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006390
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006391 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006392 return( 0 );
6393}
6394
6395#endif /* MBEDTLS_SSL_PROTO_DTLS */
6396
Hanno Beckere74d5562018-08-15 14:26:08 +01006397static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006398{
6399 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006400 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006401
Hanno Becker5f066e72018-08-16 14:56:31 +01006402#if defined(MBEDTLS_SSL_PROTO_DTLS)
6403 /* We might have buffered a future record; if so,
6404 * and if the epoch matches now, load it.
6405 * On success, this call will set ssl->in_left to
6406 * the length of the buffered record, so that
6407 * the calls to ssl_fetch_input() below will
6408 * essentially be no-ops. */
6409 ret = ssl_load_buffered_record( ssl );
6410 if( ret != 0 )
6411 return( ret );
6412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006413
Hanno Becker8b09b732019-05-08 12:03:28 +01006414 /* Ensure that we have enough space available for the default form
6415 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6416 * with no space for CIDs counted in). */
6417 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6418 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006421 return( ret );
6422 }
6423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006424 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6425 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006428 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006429 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006430 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6431 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006432 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006433 if( ret != 0 )
6434 return( ret );
6435
6436 /* Fall through to handling of unexpected records */
6437 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6438 }
6439
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006440 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6441 {
Hanno Becker87b56262019-07-10 14:37:41 +01006442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006443 /* Reset in pointers to default state for TLS/DTLS records,
6444 * assuming no CID and no offset between record content and
6445 * record plaintext. */
6446 ssl_update_in_pointers( ssl );
6447
Hanno Becker69412452019-07-12 08:33:49 +01006448 /* Setup internal message pointers from record structure. */
6449 ssl->in_msgtype = rec.type;
6450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6451 ssl->in_len = ssl->in_cid + rec.cid_len;
6452#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006453 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006454 ssl->in_msglen = rec.data_len;
6455
Hanno Becker87b56262019-07-10 14:37:41 +01006456 ret = ssl_check_client_reconnect( ssl );
6457 if( ret != 0 )
6458 return( ret );
6459#endif
6460
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006461 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006462 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006463
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6465 "(header)" ) );
6466 }
6467 else
6468 {
6469 /* Skip invalid record and the rest of the datagram */
6470 ssl->next_record_offset = 0;
6471 ssl->in_left = 0;
6472
6473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6474 "(header)" ) );
6475 }
6476
6477 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006478 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006479 }
Hanno Becker87b56262019-07-10 14:37:41 +01006480 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006481#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006482 {
6483 return( ret );
6484 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006485 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006488 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006489 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006490 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006491 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006492 if( ssl->next_record_offset < ssl->in_left )
6493 {
6494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6495 }
6496 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006497 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006499#if defined(MBEDTLS_SSL_PROTO_TLS)
6500 {
Hanno Beckere0452772019-07-10 17:12:07 +01006501 /*
6502 * Fetch record contents from underlying transport.
6503 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006504 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006505 if( ret != 0 )
6506 {
6507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6508 return( ret );
6509 }
6510
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006511 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006512 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006513#endif /* MBEDTLS_SSL_PROTO_TLS */
6514
6515 /*
6516 * Decrypt record contents.
6517 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006518
Hanno Beckera89610a2019-07-11 13:07:45 +01006519 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006522 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006523 {
6524 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006525 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006526 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006527 /* Except when waiting for Finished as a bad mac here
6528 * probably means something went wrong in the handshake
6529 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6530 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6531 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6532 {
6533#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6534 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6535 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006536 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006537 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6538 }
6539#endif
6540 return( ret );
6541 }
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006544 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6545 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006549 }
6550#endif
6551
Hanno Becker4a810fb2017-05-24 16:27:30 +01006552 /* As above, invalid records cause
6553 * dismissal of the whole datagram. */
6554
6555 ssl->next_record_offset = 0;
6556 ssl->in_left = 0;
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006559 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006560 }
6561
6562 return( ret );
6563 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006564 MBEDTLS_SSL_TRANSPORT_ELSE
6565#endif /* MBEDTLS_SSL_PROTO_DTLS */
6566#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006567 {
6568 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6570 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006572 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006574 }
6575#endif
6576 return( ret );
6577 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006578#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006579 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006580
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006581
6582 /* Reset in pointers to default state for TLS/DTLS records,
6583 * assuming no CID and no offset between record content and
6584 * record plaintext. */
6585 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6587 ssl->in_len = ssl->in_cid + rec.cid_len;
6588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006589 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006590
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006591 /* The record content type may change during decryption,
6592 * so re-read it. */
6593 ssl->in_msgtype = rec.type;
6594 /* Also update the input buffer, because unfortunately
6595 * the server-side ssl_parse_client_hello() reparses the
6596 * record header when receiving a ClientHello initiating
6597 * a renegotiation. */
6598 ssl->in_hdr[0] = rec.type;
6599 ssl->in_msg = rec.buf + rec.data_offset;
6600 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006601 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006602
Simon Butcher99000142016-10-13 17:21:01 +01006603 return( 0 );
6604}
6605
6606int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6607{
6608 int ret;
6609
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006610 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006611 * Handle particular types of records
6612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 {
Simon Butcher99000142016-10-13 17:21:01 +01006615 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6616 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006617 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 }
6620
Hanno Beckere678eaa2018-08-21 14:57:46 +01006621 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006622 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006623 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006624 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6626 ssl->in_msglen ) );
6627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006628 }
6629
Hanno Beckere678eaa2018-08-21 14:57:46 +01006630 if( ssl->in_msg[0] != 1 )
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6633 ssl->in_msg[0] ) );
6634 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6635 }
6636
6637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006638 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006639 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6640 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6641 {
6642 if( ssl->handshake == NULL )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6645 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6646 }
6647
6648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6649 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6650 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006651#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006652 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006656 if( ssl->in_msglen != 2 )
6657 {
6658 /* Note: Standard allows for more than one 2 byte alert
6659 to be packed in a single message, but Mbed TLS doesn't
6660 currently support this. */
6661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6662 ssl->in_msglen ) );
6663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6664 }
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 ssl->in_msg[0], ssl->in_msg[1] ) );
6668
6669 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006670 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006675 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6680 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6683 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006685
6686#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6687 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6688 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6689 {
Hanno Becker90333da2017-10-10 11:27:13 +01006690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006691 /* Will be handled when trying to parse ServerHello */
6692 return( 0 );
6693 }
6694#endif
6695
6696#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006697 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006698 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6699 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006700 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6701 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6704 /* Will be handled in mbedtls_ssl_parse_certificate() */
6705 return( 0 );
6706 }
6707#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6708
6709 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006710 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 }
6712
Hanno Beckerc76c6192017-06-06 10:03:17 +01006713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006715 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006716 /* Drop unexpected ApplicationData records,
6717 * except at the beginning of renegotiations */
6718 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6719 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6720#if defined(MBEDTLS_SSL_RENEGOTIATION)
6721 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6722 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006723#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006724 )
6725 {
6726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6727 return( MBEDTLS_ERR_SSL_NON_FATAL );
6728 }
6729
6730 if( ssl->handshake != NULL &&
6731 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6732 {
6733 ssl_handshake_wrapup_free_hs_transform( ssl );
6734 }
6735 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006737
Paul Bakker5121ce52009-01-03 21:22:43 +00006738 return( 0 );
6739}
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006742{
6743 int ret;
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6747 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006748 {
6749 return( ret );
6750 }
6751
6752 return( 0 );
6753}
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006756 unsigned char level,
6757 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006758{
6759 int ret;
6760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006761 if( ssl == NULL || ssl->conf == NULL )
6762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006768 ssl->out_msglen = 2;
6769 ssl->out_msg[0] = level;
6770 ssl->out_msg[1] = message;
6771
Hanno Becker67bc7c32018-08-06 11:33:50 +01006772 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006775 return( ret );
6776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006778
6779 return( 0 );
6780}
6781
Hanno Becker17572472019-02-08 07:19:04 +00006782#if defined(MBEDTLS_X509_CRT_PARSE_C)
6783static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6784{
6785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6786 if( session->peer_cert != NULL )
6787 {
6788 mbedtls_x509_crt_free( session->peer_cert );
6789 mbedtls_free( session->peer_cert );
6790 session->peer_cert = NULL;
6791 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006792#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006793 if( session->peer_cert_digest != NULL )
6794 {
6795 /* Zeroization is not necessary. */
6796 mbedtls_free( session->peer_cert_digest );
6797 session->peer_cert_digest = NULL;
6798 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6799 session->peer_cert_digest_len = 0;
6800 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006801#else
6802 ((void) session);
6803#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006804}
6805#endif /* MBEDTLS_X509_CRT_PARSE_C */
6806
Paul Bakker5121ce52009-01-03 21:22:43 +00006807/*
6808 * Handshake functions
6809 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006810#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006811/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813{
Hanno Beckerdf645962019-06-26 13:02:22 +01006814 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6815 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Hanno Becker5097cba2019-02-05 13:36:46 +00006819 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006822 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6823 {
6824 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6825 }
6826 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6827 {
6828 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6829 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006830 else
6831 {
6832 ssl->state = MBEDTLS_SSL_INVALID;
6833 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006834 return( 0 );
6835 }
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006839}
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006842{
Hanno Beckerdf645962019-06-26 13:02:22 +01006843 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6844 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006847
Hanno Becker5097cba2019-02-05 13:36:46 +00006848 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006851 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6852 {
6853 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6854 }
6855 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6856 {
6857 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6858 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006859 else
6860 {
6861 ssl->state = MBEDTLS_SSL_INVALID;
6862 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006863 return( 0 );
6864 }
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006868}
Gilles Peskinef9828522017-05-03 12:28:43 +02006869
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006870#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006871/* Some certificate support -> implement write and parse */
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006876 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006878 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6879 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882
Hanno Becker5097cba2019-02-05 13:36:46 +00006883 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006886 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6887 {
6888 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6889 }
6890 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6891 {
6892 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6893 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006894 else
6895 {
6896 ssl->state = MBEDTLS_SSL_INVALID;
6897 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006898 return( 0 );
6899 }
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006902 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6903 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
6905 if( ssl->client_auth == 0 )
6906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006908 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6909 {
6910 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6911 }
6912 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6913 {
6914 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6915 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006916 else
6917 {
6918 ssl->state = MBEDTLS_SSL_INVALID;
6919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 return( 0 );
6921 }
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 /*
6925 * If using SSLv3 and got no cert, send an Alert message
6926 * (otherwise an empty Certificate message will be sent).
6927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006929 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006930 {
6931 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6933 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6934 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006937 goto write_msg;
6938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#endif /* MBEDTLS_SSL_CLI_C */
6942#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006943 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6948 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 }
6950 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954
6955 /*
6956 * 0 . 0 handshake type
6957 * 1 . 3 handshake length
6958 * 4 . 6 length of all certs
6959 * 7 . 9 length of cert. 1
6960 * 10 . n-1 peer certificate
6961 * n . n+2 length of cert. 2
6962 * n+3 . ... upper level cert, etc.
6963 */
6964 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Paul Bakker29087132010-03-21 21:03:34 +00006967 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
6969 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006970 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006973 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006977 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
Teppo Järvelin91d79382019-10-02 09:09:31 +03006979 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 i += n; crt = crt->next;
6981 }
6982
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006983 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
6985 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6987 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006989#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006990write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Jarno Lamsa2b205162019-11-12 15:36:21 +02006993 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6994 {
6995 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6996 }
6997 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6998 {
6999 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7000 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007001 else
7002 {
7003 ssl->state = MBEDTLS_SSL_INVALID;
7004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007006 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 return( ret );
7010 }
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Paul Bakkered27a042013-04-18 22:46:23 +02007014 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015}
7016
Hanno Becker285ff0c2019-01-31 07:44:03 +00007017#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007018
7019#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7021 unsigned char *crt_buf,
7022 size_t crt_buf_len )
7023{
7024 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7025
7026 if( peer_crt == NULL )
7027 return( -1 );
7028
7029 if( peer_crt->raw.len != crt_buf_len )
7030 return( -1 );
7031
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007032 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007033}
Hanno Becker5882dd02019-06-06 16:25:57 +01007034#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007035static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7036 unsigned char *crt_buf,
7037 size_t crt_buf_len )
7038{
7039 int ret;
7040 unsigned char const * const peer_cert_digest =
7041 ssl->session->peer_cert_digest;
7042 mbedtls_md_type_t const peer_cert_digest_type =
7043 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007044 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007045 mbedtls_md_info_from_type( peer_cert_digest_type );
7046 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7047 size_t digest_len;
7048
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007049 if( peer_cert_digest == NULL ||
7050 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7051 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007052 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007053 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007054
7055 digest_len = mbedtls_md_get_size( digest_info );
7056 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7057 return( -1 );
7058
7059 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7060 if( ret != 0 )
7061 return( -1 );
7062
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007063 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007064}
Hanno Becker5882dd02019-06-06 16:25:57 +01007065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007066#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007067
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007068/*
7069 * Once the certificate message is read, parse it into a cert chain and
7070 * perform basic checks, but leave actual verification to the caller
7071 */
Hanno Becker35e41772019-02-05 15:37:23 +00007072static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7073 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007075 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007076#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7077 int crt_cnt=0;
7078#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007079 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007080 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007085 mbedtls_ssl_pend_fatal_alert( ssl,
7086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 }
7089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7091 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007094 mbedtls_ssl_pend_fatal_alert( ssl,
7095 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 }
7098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007100
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007104 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007106 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007110 mbedtls_ssl_pend_fatal_alert( ssl,
7111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Hanno Becker33c3dc82019-01-30 14:46:46 +00007115 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7116 i += 3;
7117
Hanno Becker33c3dc82019-01-30 14:46:46 +00007118 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 while( i < ssl->in_hslen )
7120 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007121 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007122 if ( i + 3 > ssl->in_hslen ) {
7123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007124 mbedtls_ssl_pend_fatal_alert( ssl,
7125 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007126 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7127 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007128 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7129 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 if( ssl->in_msg[i] != 0 )
7131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007133 mbedtls_ssl_pend_fatal_alert( ssl,
7134 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 }
7137
Hanno Becker33c3dc82019-01-30 14:46:46 +00007138 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007139 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 i += 3;
7141
7142 if( n < 128 || i + n > ssl->in_hslen )
7143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007145 mbedtls_ssl_pend_fatal_alert( ssl,
7146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 }
7149
Hanno Becker33c3dc82019-01-30 14:46:46 +00007150 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007151#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7152 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007153 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7154 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007155 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007156 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007157 /* During client-side renegotiation, check that the server's
7158 * end-CRTs hasn't changed compared to the initial handshake,
7159 * mitigating the triple handshake attack. On success, reuse
7160 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7162 if( ssl_check_peer_crt_unchanged( ssl,
7163 &ssl->in_msg[i],
7164 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007165 {
Hanno Becker35e41772019-02-05 15:37:23 +00007166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007167 mbedtls_ssl_pend_fatal_alert( ssl,
7168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007169 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007170 }
Hanno Becker35e41772019-02-05 15:37:23 +00007171
7172 /* Now we can safely free the original chain. */
7173 ssl_clear_peer_cert( ssl->session );
7174 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007175#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7176
Hanno Becker33c3dc82019-01-30 14:46:46 +00007177 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007178#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007179 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007180#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007181 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007182 * it in-place from the input buffer instead of making a copy. */
7183 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7184#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007185 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 case 0: /*ok*/
7188 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7189 /* Ignore certificate with an unknown algorithm: maybe a
7190 prior certificate was already trusted. */
7191 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007192
Hanno Becker33c3dc82019-01-30 14:46:46 +00007193 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7194 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7195 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007196
Hanno Becker33c3dc82019-01-30 14:46:46 +00007197 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7198 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7199 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007200
Hanno Becker33c3dc82019-01-30 14:46:46 +00007201 default:
7202 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7203 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007204 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007205 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 }
7208
7209 i += n;
7210 }
7211
Hanno Becker35e41772019-02-05 15:37:23 +00007212 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007213 return( 0 );
7214}
7215
Hanno Beckerb8a08572019-02-05 12:49:06 +00007216#if defined(MBEDTLS_SSL_SRV_C)
7217static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7218{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007219 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007220 return( -1 );
7221
7222#if defined(MBEDTLS_SSL_PROTO_SSL3)
7223 /*
7224 * Check if the client sent an empty certificate
7225 */
Hanno Becker2881d802019-05-22 14:44:53 +01007226 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007227 {
7228 if( ssl->in_msglen == 2 &&
7229 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7230 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7231 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7232 {
7233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7234 return( 0 );
7235 }
7236
7237 return( -1 );
7238 }
7239#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7240
7241#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7242 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7243 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7244 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7245 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007246 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 +00007247 {
7248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7249 return( 0 );
7250 }
7251
Hanno Beckerb8a08572019-02-05 12:49:06 +00007252#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7253 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007254
7255 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007256}
7257#endif /* MBEDTLS_SSL_SRV_C */
7258
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007259/* Check if a certificate message is expected.
7260 * Return either
7261 * - SSL_CERTIFICATE_EXPECTED, or
7262 * - SSL_CERTIFICATE_SKIP
7263 * indicating whether a Certificate message is expected or not.
7264 */
7265#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007266#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007267static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7268 int authmode )
7269{
Hanno Becker473f98f2019-06-26 10:27:32 +01007270 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007271 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007272
7273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7274 return( SSL_CERTIFICATE_SKIP );
7275
7276#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007277 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007278 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007279 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7280 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7281 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007282 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007283 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007284
7285 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7286 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007287 ssl->session_negotiate->verify_result =
7288 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7289 return( SSL_CERTIFICATE_SKIP );
7290 }
7291 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007292#else
7293 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007294#endif /* MBEDTLS_SSL_SRV_C */
7295
7296 return( SSL_CERTIFICATE_EXPECTED );
7297}
7298
Hanno Becker3cf50612019-02-05 14:36:34 +00007299static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007300 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007301 mbedtls_x509_crt *chain,
7302 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007303{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007304 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7305 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7306 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007307 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007308 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007309 mbedtls_x509_crt *ca_chain;
7310 mbedtls_x509_crl *ca_crl;
7311
7312 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007313 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007314 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007315 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007316
Jarno Lamsae1621d42019-12-19 08:58:56 +02007317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007318#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7319 if( ssl->handshake->sni_ca_chain != NULL )
7320 {
7321 ca_chain = ssl->handshake->sni_ca_chain;
7322 ca_crl = ssl->handshake->sni_ca_crl;
7323 }
7324 else
7325#endif
7326 {
7327 ca_chain = ssl->conf->ca_chain;
7328 ca_crl = ssl->conf->ca_crl;
7329 }
7330
7331 /*
7332 * Main check: verify certificate
7333 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007334 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007335 chain,
7336 ca_chain, ca_crl,
7337 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007338#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007339 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007340#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007341 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007342#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7343 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7344#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7345 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007346
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007347 if( verify_ret == 0 )
7348 {
7349 mbedtls_platform_enforce_volatile_reads();
7350 if( verify_ret == 0 )
7351 {
7352 flow_counter++;
7353 }
7354 else
7355 {
7356 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7357 }
7358 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007359 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007360 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007361 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007362 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007363 }
7364
7365#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007366 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007367 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7368#endif
7369
7370 /*
7371 * Secondary checks: always done, but change 'ret' only if it was 0
7372 */
7373
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007374#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007375 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007376#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007377 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007378#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007379 mbedtls_pk_context *pk;
7380 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7381 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007382 {
7383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007384 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007385 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007386
7387 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007388 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007389 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007390 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007391 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007392
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007393 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007394#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007395
7396 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007397 {
7398 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7399
7400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401 if( verify_ret == 0 )
7402 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007403 flow_counter++;
7404 }
7405 if( ret == 0 )
7406 {
7407 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007408 }
7409 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007410#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007411
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007412 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007414 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7415 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007416 &ssl->session_negotiate->verify_result );
7417 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007418 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007419 flow_counter++;
7420 }
7421 else
7422 {
7423 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007425 if( verify_ret == 0 )
7426 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007427 }
7428
7429 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7430 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7431 * with details encoded in the verification flags. All other kinds
7432 * of error codes, including those from the user provided f_vrfy
7433 * functions, are treated as fatal and lead to a failure of
7434 * ssl_parse_certificate even if verification was optional. */
7435 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007436 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7437 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007438 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007439 mbedtls_platform_enforce_volatile_reads();
7440 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7441 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7442 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7443 {
7444 verify_ret = 0;
7445 flow_counter++;
7446 }
7447 else
7448 {
7449 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7450 }
7451 } else {
7452 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 }
7454
7455 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7456 {
7457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007458 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007459 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007460 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007461 else
7462 {
7463 flow_counter++;
7464 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007465
Hanno Becker8c13ee62019-02-26 16:48:17 +00007466 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007467 {
7468 uint8_t alert;
7469
7470 /* The certificate may have been rejected for several reasons.
7471 Pick one and send the corresponding alert. Which alert to send
7472 may be a subject of debate in some cases. */
7473 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7474 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7475 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7476 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7477 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7478 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7479 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7480 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7481 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7482 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7483 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7484 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7485 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7486 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7487 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7488 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7489 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7490 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7491 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7492 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7493 else
7494 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007495 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007496 }
7497
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007498 if( verify_ret == 0 &&
7499#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7500 flow_counter == 5 )
7501#else
7502 flow_counter == 4 )
7503#endif
7504 {
7505 mbedtls_platform_enforce_volatile_reads();
7506 if( verify_ret == 0 &&
7507#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7508 flow_counter == 5 )
7509#else
7510 flow_counter == 4 )
7511#endif
7512 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007514 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7515 }
7516 else
7517 {
7518 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7519 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007520 } else {
7521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007522 }
7523
Hanno Becker3cf50612019-02-05 14:36:34 +00007524#if defined(MBEDTLS_DEBUG_C)
7525 if( ssl->session_negotiate->verify_result != 0 )
7526 {
7527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7528 ssl->session_negotiate->verify_result ) );
7529 }
7530 else
7531 {
7532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7533 }
7534#endif /* MBEDTLS_DEBUG_C */
7535
Hanno Becker8c13ee62019-02-26 16:48:17 +00007536 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007537}
7538
Hanno Becker34106f62019-02-08 14:59:05 +00007539#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007540
7541#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007542static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7543 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007544{
7545 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007546 /* Remember digest of the peer's end-CRT. */
7547 ssl->session_negotiate->peer_cert_digest =
7548 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7549 if( ssl->session_negotiate->peer_cert_digest == NULL )
7550 {
7551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7552 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007553 mbedtls_ssl_pend_fatal_alert( ssl,
7554 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007555
7556 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7557 }
7558
7559 ret = mbedtls_md( mbedtls_md_info_from_type(
7560 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7561 start, len,
7562 ssl->session_negotiate->peer_cert_digest );
7563
7564 ssl->session_negotiate->peer_cert_digest_type =
7565 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7566 ssl->session_negotiate->peer_cert_digest_len =
7567 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7568
7569 return( ret );
7570}
Hanno Becker5882dd02019-06-06 16:25:57 +01007571#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007572
7573static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7574 unsigned char *start, size_t len )
7575{
7576 unsigned char *end = start + len;
7577 int ret;
7578
7579 /* Make a copy of the peer's raw public key. */
7580 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7581 ret = mbedtls_pk_parse_subpubkey( &start, end,
7582 &ssl->handshake->peer_pubkey );
7583 if( ret != 0 )
7584 {
7585 /* We should have parsed the public key before. */
7586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7587 }
7588
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007589 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007590 return( 0 );
7591}
7592#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7593
Hanno Becker3cf50612019-02-05 14:36:34 +00007594int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7595{
7596 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007597 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007598#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7599 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7600 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007601 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007602#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007603 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007604#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007605 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007606 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007607
7608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7609
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007610 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7611 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007612 {
7613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007614 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007615 }
7616
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007617#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7618 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007619 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007620 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007621 chain = ssl->handshake->ecrs_peer_cert;
7622 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007623 goto crt_verify;
7624 }
7625#endif
7626
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007627 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007628 {
7629 /* mbedtls_ssl_read_record may have sent an alert already. We
7630 let it decide whether to alert. */
7631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007632 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007633 }
7634
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007636 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7637 {
7638 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007639
Hanno Beckerb8a08572019-02-05 12:49:06 +00007640 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007641 ret = 0;
7642 else
7643 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007644
Hanno Becker613d4902019-02-05 13:11:17 +00007645 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007646 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007647#endif /* MBEDTLS_SSL_SRV_C */
7648
Hanno Becker35e41772019-02-05 15:37:23 +00007649 /* Clear existing peer CRT structure in case we tried to
7650 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007651 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007652
7653 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7654 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007655 {
7656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7657 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007658 mbedtls_ssl_pend_fatal_alert( ssl,
7659 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007660
Hanno Beckere4aeb762019-02-05 17:19:52 +00007661 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7662 goto exit;
7663 }
7664 mbedtls_x509_crt_init( chain );
7665
7666 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007667 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007668 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007669
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007670#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7671 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007672 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007673
7674crt_verify:
7675 if( ssl->handshake->ecrs_enabled)
7676 rs_ctx = &ssl->handshake->ecrs_ctx;
7677#endif
7678
Hanno Becker3cf50612019-02-05 14:36:34 +00007679 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007680 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007681 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007682 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
Hanno Becker3008d282019-02-05 17:02:28 +00007684#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007685 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007686 size_t pk_len;
7687 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Hanno Becker34106f62019-02-08 14:59:05 +00007689 /* We parse the CRT chain without copying, so
7690 * these pointers point into the input buffer,
7691 * and are hence still valid after freeing the
7692 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Hanno Becker5882dd02019-06-06 16:25:57 +01007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
7695 unsigned char *crt_start;
7696 size_t crt_len;
7697
Hanno Becker34106f62019-02-08 14:59:05 +00007698 crt_start = chain->raw.p;
7699 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007700#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Hanno Becker8c13ee62019-02-26 16:48:17 +00007702 pk_start = chain->cache->pk_raw.p;
7703 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007704
7705 /* Free the CRT structures before computing
7706 * digest and copying the peer's public key. */
7707 mbedtls_x509_crt_free( chain );
7708 mbedtls_free( chain );
7709 chain = NULL;
7710
Hanno Becker5882dd02019-06-06 16:25:57 +01007711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007712 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007714 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007715#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007716
Hanno Becker34106f62019-02-08 14:59:05 +00007717 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007719 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 }
Hanno Becker34106f62019-02-08 14:59:05 +00007721#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7722 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007723 ssl->session_negotiate->peer_cert = chain;
7724 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007725#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Hanno Becker613d4902019-02-05 13:11:17 +00007729exit:
7730
Hanno Beckere4aeb762019-02-05 17:19:52 +00007731 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007732 {
7733 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7734 {
7735 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7736 }
7737 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7738 {
7739 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7740 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007741 else
7742 {
7743 ssl->state = MBEDTLS_SSL_INVALID;
7744 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007745 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007746
7747#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7748 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7749 {
7750 ssl->handshake->ecrs_peer_cert = chain;
7751 chain = NULL;
7752 }
7753#endif
7754
7755 if( chain != NULL )
7756 {
7757 mbedtls_x509_crt_free( chain );
7758 mbedtls_free( chain );
7759 }
7760
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 return( ret );
7762}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007763#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766{
7767 int ret;
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 ssl->out_msglen = 1;
7773 ssl->out_msg[0] = 1;
7774
Jarno Lamsa2b205162019-11-12 15:36:21 +02007775 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7776 {
7777 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7778 }
7779 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7780 {
7781 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7782 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007783 else
7784 {
7785 ssl->state = MBEDTLS_SSL_INVALID;
7786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007788 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007789 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 return( ret );
7792 }
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
7796 return( 0 );
7797}
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800{
7801 int ret;
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Hanno Becker327c93b2018-08-15 13:56:18 +01007805 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 return( ret );
7809 }
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007814 mbedtls_ssl_pend_fatal_alert( ssl,
7815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 }
7818
Hanno Beckere678eaa2018-08-21 14:57:46 +01007819 /* CCS records are only accepted if they have length 1 and content '1',
7820 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007822 /*
7823 * Switch to our negotiated transform and session parameters for inbound
7824 * data.
7825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007827 ssl->transform_in = ssl->transform_negotiate;
7828 ssl->session_in = ssl->session_negotiate;
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007831 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007834 ssl_dtls_replay_reset( ssl );
7835#endif
7836
7837 /* Increment epoch */
7838 if( ++ssl->in_epoch == 0 )
7839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007841 /* This is highly unlikely to happen for legitimate reasons, so
7842 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007844 }
7845 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848#if defined(MBEDTLS_SSL_PROTO_TLS)
7849 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007850 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851 }
7852#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007853
Hanno Beckerf5970a02019-05-08 09:38:41 +01007854 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7857 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007862 mbedtls_ssl_pend_fatal_alert( ssl,
7863 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007865 }
7866 }
7867#endif
7868
Jarno Lamsa2b205162019-11-12 15:36:21 +02007869 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7870 {
7871 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7872 }
7873 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7874 {
7875 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7876 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007877 else
7878 {
7879 ssl->state = MBEDTLS_SSL_INVALID;
7880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
7884 return( 0 );
7885}
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7890 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007891 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007892 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7895#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007896 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007907
7908 /*
7909 * Free our handshake params
7910 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007911 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007913 ssl->handshake = NULL;
7914
7915 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007916 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007917 */
7918 if( ssl->transform )
7919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 mbedtls_ssl_transform_free( ssl->transform );
7921 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007922 }
7923 ssl->transform = ssl->transform_negotiate;
7924 ssl->transform_negotiate = NULL;
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927}
7928
Jarno Lamsae1621d42019-12-19 08:58:56 +02007929int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007930{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007931 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007932
7933#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007934 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007935 ? ssl->handshake->sni_authmode
7936 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7937#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007938 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007939#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007940#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7941 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7942 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_RENEGOTIATION)
7947 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007950 ssl->renego_records_seen = 0;
7951 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007952#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007953
7954 /*
7955 * Free the previous session and switch in the current one
7956 */
Paul Bakker0a597072012-09-25 21:55:46 +00007957 if( ssl->session )
7958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007960 /* RFC 7366 3.1: keep the EtM state */
7961 ssl->session_negotiate->encrypt_then_mac =
7962 ssl->session->encrypt_then_mac;
7963#endif
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_ssl_session_free( ssl->session );
7966 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007967 }
7968 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007969 ssl->session_negotiate = NULL;
7970
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007971#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007972 /*
7973 * Add cache entry
7974 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007976 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007977 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007978 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007979 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007981 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007982#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007983
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007984 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
7985 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
7986#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7987 crt_expected == SSL_CERTIFICATE_SKIP )
7988#else
7989 1 )
7990#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007991 {
Jarno Lamsa015aa442019-12-20 12:09:37 +02007992 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007993 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
7994 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
7995#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7996 crt_expected == SSL_CERTIFICATE_SKIP )
7997#else
7998 1 )
7999#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008000 {
8001 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8002 }
8003 else
8004 {
8005 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8006 goto cleanup;
8007 }
8008 }
8009
Jarno Lamsae1621d42019-12-19 08:58:56 +02008010#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008011 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008012 {
8013 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008014 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008015 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008016 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008017 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008018 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008019 }
8020 else
8021 {
8022 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008023 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008024 }
8025 }
8026#endif
8027
8028 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8029 {
8030 mbedtls_platform_enforce_volatile_reads();
8031 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8032 {
8033 ret = 0;
8034 }
8035 else
8036 {
8037 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008038 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008039 }
8040 }
8041 else
8042 {
8043 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008044 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008045 }
8046
Jarno Lamsa06164052019-12-19 14:40:36 +02008047 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8048 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8049 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8050 {
8051 mbedtls_platform_enforce_volatile_reads();
8052 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8053 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8054 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8055 {
8056 ret = 0;
8057 }
8058 else
8059 {
8060 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8061 goto cleanup;
8062 }
8063 }
8064 else
8065 {
8066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8069 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8070 }
8071
8072cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008074 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008075 ssl->handshake->flight != NULL )
8076 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008077 /* Cancel handshake timer */
8078 ssl_set_timer( ssl, 0 );
8079
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008080 /* Keep last flight around in case we need to resend it:
8081 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008083 }
8084 else
8085#endif
8086 ssl_handshake_wrapup_free_hs_transform( ssl );
8087
Jarno Lamsa2b205162019-11-12 15:36:21 +02008088 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008091 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008092}
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008095{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008096 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008099
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008100 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008101
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008102 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8103 mbedtls_ssl_suite_get_mac(
8104 mbedtls_ssl_ciphersuite_from_id(
8105 mbedtls_ssl_session_get_ciphersuite(
8106 ssl->session_negotiate ) ) ),
8107 ssl, ssl->out_msg + 4,
8108 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008109
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008110 /*
8111 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8112 * may define some other value. Currently (early 2016), no defined
8113 * ciphersuite does this (and this is unlikely to change as activity has
8114 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8115 */
Hanno Becker2881d802019-05-22 14:44:53 +01008116 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008119 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008120 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008121#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008122
Paul Bakker5121ce52009-01-03 21:22:43 +00008123 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8125 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008126
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008127#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008128 /*
8129 * In case of session resuming, invert the client and server
8130 * ChangeCipherSpec messages order.
8131 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008132 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008135 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8136 MBEDTLS_SSL_IS_CLIENT )
8137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008139 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008142 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8143 MBEDTLS_SSL_IS_SERVER )
8144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008146 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008148 }
8149 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008150#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008151 {
8152 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8153 {
8154 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8155 }
8156 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8157 {
8158 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8159 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008160 else
8161 {
8162 ssl->state = MBEDTLS_SSL_INVALID;
8163 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008165
Paul Bakker48916f92012-09-16 19:57:18 +00008166 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008167 * Switch to our negotiated transform and session parameters for outbound
8168 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008173 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008174 {
8175 unsigned char i;
8176
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008177 /* Remember current epoch settings for resending */
8178 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008179 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008180
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008181 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008182 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008183
8184 /* Increment epoch */
8185 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008186 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008187 break;
8188
8189 /* The loop goes to its end iff the counter is wrapping */
8190 if( i == 0 )
8191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8193 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008194 }
8195 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008196 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008198#if defined(MBEDTLS_SSL_PROTO_TLS)
8199 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008200 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008201 }
8202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008203
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008204 ssl->transform_out = ssl->transform_negotiate;
8205 ssl->session_out = ssl->session_negotiate;
8206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8208 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8213 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008214 }
8215 }
8216#endif
8217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008219 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008221#endif
8222
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008223 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008224 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008226 return( ret );
8227 }
8228
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008230 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008231 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8232 {
8233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8234 return( ret );
8235 }
8236#endif
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008239
8240 return( 0 );
8241}
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008244#define SSL_MAX_HASH_LEN 36
8245#else
8246#define SSL_MAX_HASH_LEN 12
8247#endif
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008250{
Paul Bakker23986e52011-04-24 08:57:21 +00008251 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008252 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008253 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008256
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008257 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8258 mbedtls_ssl_suite_get_mac(
8259 mbedtls_ssl_ciphersuite_from_id(
8260 mbedtls_ssl_session_get_ciphersuite(
8261 ssl->session_negotiate ) ) ),
8262 ssl, buf,
8263 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008264
Hanno Becker327c93b2018-08-15 13:56:18 +01008265 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008268 return( ret );
8269 }
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008274 mbedtls_ssl_pend_fatal_alert( ssl,
8275 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008277 }
8278
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008279 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008281 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008282 hash_len = 36;
8283 else
8284#endif
8285 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8288 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008291 mbedtls_ssl_pend_fatal_alert( ssl,
8292 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008294 }
8295
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008296 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008297 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008300 mbedtls_ssl_pend_fatal_alert( ssl,
8301 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 }
8304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008306 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008307 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008308#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008309
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008310#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008311 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008314 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008318 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008320#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008321 }
8322 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008323#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008324 {
8325 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8326 {
8327 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8328 }
8329 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8330 {
8331 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8332 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008333 else
8334 {
8335 ssl->state = MBEDTLS_SSL_INVALID;
8336 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008340 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008342#endif
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008345
8346 return( 0 );
8347}
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8354 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8355 mbedtls_md5_init( &handshake->fin_md5 );
8356 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008357 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8358 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8361#if defined(MBEDTLS_SHA256_C)
8362 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008363 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SHA512_C)
8366 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008367 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008370
Hanno Becker7e5437a2017-04-28 17:15:26 +01008371#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8372 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8373 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8374#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_DHM_C)
8377 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379#if defined(MBEDTLS_ECDH_C)
8380 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008381#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008382#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008383 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008384#if defined(MBEDTLS_SSL_CLI_C)
8385 handshake->ecjpake_cache = NULL;
8386 handshake->ecjpake_cache_len = 0;
8387#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008388#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008389
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008390#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008391 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008392#endif
8393
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008394#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8395 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8396#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008397
8398#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8399 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8400 mbedtls_pk_init( &handshake->peer_pubkey );
8401#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008402}
8403
Hanno Becker611a83b2018-01-03 14:27:32 +00008404void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8409 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008410
Hanno Becker92231322018-01-03 15:32:51 +00008411#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 mbedtls_md_init( &transform->md_ctx_enc );
8413 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008414#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008415}
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008420}
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008423{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008424 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008425 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008427 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008429 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008430 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008431
8432 /*
8433 * Either the pointers are now NULL or cleared properly and can be freed.
8434 * Now allocate missing structures.
8435 */
8436 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008437 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008438 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008439 }
Paul Bakker48916f92012-09-16 19:57:18 +00008440
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008441 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008442 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008443 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008444 }
Paul Bakker48916f92012-09-16 19:57:18 +00008445
Paul Bakker82788fb2014-10-20 13:59:19 +02008446 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008447 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008448 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008449 }
Paul Bakker48916f92012-09-16 19:57:18 +00008450
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008451 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008452 if( ssl->handshake == NULL ||
8453 ssl->transform_negotiate == NULL ||
8454 ssl->session_negotiate == NULL )
8455 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 mbedtls_free( ssl->handshake );
8459 mbedtls_free( ssl->transform_negotiate );
8460 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008461
8462 ssl->handshake = NULL;
8463 ssl->transform_negotiate = NULL;
8464 ssl->session_negotiate = NULL;
8465
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008466 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008467 }
8468
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008469 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008471 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008472 ssl_handshake_params_init( ssl->handshake );
8473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008475 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008476 {
8477 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008478
Hanno Becker2d9623f2019-06-13 12:07:05 +01008479 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008480 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8481 else
8482 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8483 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008484#endif
8485
Paul Bakker48916f92012-09-16 19:57:18 +00008486 return( 0 );
8487}
8488
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008489#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008490/* Dummy cookie callbacks for defaults */
8491static int ssl_cookie_write_dummy( void *ctx,
8492 unsigned char **p, unsigned char *end,
8493 const unsigned char *cli_id, size_t cli_id_len )
8494{
8495 ((void) ctx);
8496 ((void) p);
8497 ((void) end);
8498 ((void) cli_id);
8499 ((void) cli_id_len);
8500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008502}
8503
8504static int ssl_cookie_check_dummy( void *ctx,
8505 const unsigned char *cookie, size_t cookie_len,
8506 const unsigned char *cli_id, size_t cli_id_len )
8507{
8508 ((void) ctx);
8509 ((void) cookie);
8510 ((void) cookie_len);
8511 ((void) cli_id);
8512 ((void) cli_id_len);
8513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008515}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008516#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008517
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008518/* Once ssl->out_hdr as the address of the beginning of the
8519 * next outgoing record is set, deduce the other pointers.
8520 *
8521 * Note: For TLS, we save the implicit record sequence number
8522 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8523 * and the caller has to make sure there's space for this.
8524 */
8525
8526static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8527 mbedtls_ssl_transform *transform )
8528{
8529#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008530 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008531 {
8532 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008533#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008534 ssl->out_cid = ssl->out_ctr + 8;
8535 ssl->out_len = ssl->out_cid;
8536 if( transform != NULL )
8537 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008538#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008539 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008540#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008541 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008542 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008543 MBEDTLS_SSL_TRANSPORT_ELSE
8544#endif /* MBEDTLS_SSL_PROTO_DTLS */
8545#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008546 {
8547 ssl->out_ctr = ssl->out_hdr - 8;
8548 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008550 ssl->out_cid = ssl->out_len;
8551#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008552 ssl->out_iv = ssl->out_hdr + 5;
8553 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008554#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008555
8556 /* Adjust out_msg to make space for explicit IV, if used. */
8557 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008558 mbedtls_ssl_ver_geq(
8559 mbedtls_ssl_get_minor_ver( ssl ),
8560 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008561 {
8562 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8563 }
8564 else
8565 ssl->out_msg = ssl->out_iv;
8566}
8567
8568/* Once ssl->in_hdr as the address of the beginning of the
8569 * next incoming record is set, deduce the other pointers.
8570 *
8571 * Note: For TLS, we save the implicit record sequence number
8572 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8573 * and the caller has to make sure there's space for this.
8574 */
8575
Hanno Beckerf5970a02019-05-08 09:38:41 +01008576static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008578 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008579 * of unprotected TLS/DTLS records, with ssl->in_msg
8580 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008581 *
8582 * When decrypting a protected record, ssl->in_msg
8583 * will be shifted to point to the beginning of the
8584 * record plaintext.
8585 */
8586
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008588 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008589 {
Hanno Becker70e79282019-05-03 14:34:53 +01008590 /* This sets the header pointers to match records
8591 * without CID. When we receive a record containing
8592 * a CID, the fields are shifted accordingly in
8593 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008594 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008595#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008596 ssl->in_cid = ssl->in_ctr + 8;
8597 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008598#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008599 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008600#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008601 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008602 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008603 MBEDTLS_SSL_TRANSPORT_ELSE
8604#endif /* MBEDTLS_SSL_PROTO_DTLS */
8605#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008606 {
8607 ssl->in_ctr = ssl->in_hdr - 8;
8608 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008610 ssl->in_cid = ssl->in_len;
8611#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008612 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008613 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008614#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008615}
8616
Paul Bakker5121ce52009-01-03 21:22:43 +00008617/*
8618 * Initialize an SSL context
8619 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008620void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8621{
8622 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8623}
8624
8625/*
8626 * Setup an SSL context
8627 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008628
8629static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8630{
8631 /* Set the incoming and outgoing record pointers. */
8632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008634 {
8635 ssl->out_hdr = ssl->out_buf;
8636 ssl->in_hdr = ssl->in_buf;
8637 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008638 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008639#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008640#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008641 {
8642 ssl->out_hdr = ssl->out_buf + 8;
8643 ssl->in_hdr = ssl->in_buf + 8;
8644 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008645#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008646
8647 /* Derive other internal pointers. */
8648 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008649 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008650}
8651
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008652int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008653 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008654{
Paul Bakker48916f92012-09-16 19:57:18 +00008655 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008656
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008657 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008658
Hanno Beckeref982d52019-07-23 15:56:18 +01008659#if defined(MBEDTLS_USE_TINYCRYPT)
8660 uECC_set_rng( &uecc_rng_wrapper );
8661#endif
8662
Paul Bakker62f2dee2012-09-28 07:31:51 +00008663 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008664 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008665 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008666
8667 /* Set to NULL in case of an error condition */
8668 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008669
Angus Grattond8213d02016-05-25 20:56:48 +10008670 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8671 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008672 {
Angus Grattond8213d02016-05-25 20:56:48 +10008673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008674 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008675 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008676 }
8677
8678 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8679 if( ssl->out_buf == NULL )
8680 {
8681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008682 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008683 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008684 }
8685
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008686 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008687
Paul Bakker48916f92012-09-16 19:57:18 +00008688 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008689 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008690
Hanno Beckerc8f52992019-07-25 11:15:08 +01008691 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008692
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008694
8695error:
8696 mbedtls_free( ssl->in_buf );
8697 mbedtls_free( ssl->out_buf );
8698
8699 ssl->conf = NULL;
8700
8701 ssl->in_buf = NULL;
8702 ssl->out_buf = NULL;
8703
8704 ssl->in_hdr = NULL;
8705 ssl->in_ctr = NULL;
8706 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008707 ssl->in_msg = NULL;
8708
8709 ssl->out_hdr = NULL;
8710 ssl->out_ctr = NULL;
8711 ssl->out_len = NULL;
8712 ssl->out_iv = NULL;
8713 ssl->out_msg = NULL;
8714
k-stachowiak9f7798e2018-07-31 16:52:32 +02008715 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008716}
8717
8718/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008719 * Reset an initialized and used SSL context for re-use while retaining
8720 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008721 *
8722 * If partial is non-zero, keep data in the input buffer and client ID.
8723 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008724 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008725static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008726{
Paul Bakker48916f92012-09-16 19:57:18 +00008727 int ret;
8728
Hanno Becker7e772132018-08-10 12:38:21 +01008729#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8730 !defined(MBEDTLS_SSL_SRV_C)
8731 ((void) partial);
8732#endif
8733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008735
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008736 /* Cancel any possibly running timer */
8737 ssl_set_timer( ssl, 0 );
8738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739#if defined(MBEDTLS_SSL_RENEGOTIATION)
8740 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008741 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008742
8743 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008744 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8745 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008748
Paul Bakker7eb013f2011-10-06 12:37:39 +00008749 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008750 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008751
8752 ssl->in_msgtype = 0;
8753 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008755 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008756 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008759 ssl_dtls_replay_reset( ssl );
8760#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008761
8762 ssl->in_hslen = 0;
8763 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008764
8765 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008766
8767 ssl->out_msgtype = 0;
8768 ssl->out_msglen = 0;
8769 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8771 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008772 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008773#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008774
Hanno Becker19859472018-08-06 09:40:20 +01008775 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8776
Paul Bakker48916f92012-09-16 19:57:18 +00008777 ssl->transform_in = NULL;
8778 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008779
Hanno Becker78640902018-08-13 16:35:15 +01008780 ssl->session_in = NULL;
8781 ssl->session_out = NULL;
8782
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008783 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008784
8785#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008786 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008787#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8788 {
8789 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008790 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008791 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8794 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8797 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8800 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008801 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008802 }
8803#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008804
Paul Bakker48916f92012-09-16 19:57:18 +00008805 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 mbedtls_ssl_transform_free( ssl->transform );
8808 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008809 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008810 }
Paul Bakker48916f92012-09-16 19:57:18 +00008811
Paul Bakkerc0463502013-02-14 11:19:38 +01008812 if( ssl->session )
8813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 mbedtls_ssl_session_free( ssl->session );
8815 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008816 ssl->session = NULL;
8817 }
8818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008820 ssl->alpn_chosen = NULL;
8821#endif
8822
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008823#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008824#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008825 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008826#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008827 {
8828 mbedtls_free( ssl->cli_id );
8829 ssl->cli_id = NULL;
8830 ssl->cli_id_len = 0;
8831 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008832#endif
8833
Paul Bakker48916f92012-09-16 19:57:18 +00008834 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8835 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008836
8837 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008838}
8839
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008840/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008841 * Reset an initialized and used SSL context for re-use while retaining
8842 * all application-set variables, function pointers and data.
8843 */
8844int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8845{
8846 return( ssl_session_reset_int( ssl, 0 ) );
8847}
8848
8849/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008850 * SSL set accessors
8851 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008852#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008853void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008855 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008856}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008857#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008859void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008861 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008862}
8863
Hanno Becker7f376f42019-06-12 16:20:48 +01008864#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8865 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008866void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008867{
Hanno Becker7f376f42019-06-12 16:20:48 +01008868 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008869}
Hanno Becker7f376f42019-06-12 16:20:48 +01008870#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008871
Hanno Beckerde671542019-06-12 16:30:46 +01008872#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8873 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8874void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8875 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008877 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008878}
Hanno Beckerde671542019-06-12 16:30:46 +01008879#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008882
Hanno Becker1841b0a2018-08-24 11:13:57 +01008883void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8884 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008885{
8886 ssl->disable_datagram_packing = !allow_packing;
8887}
8888
Hanno Becker1f835fa2019-06-13 10:14:59 +01008889#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8890 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008891void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8892 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008893{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008894 conf->hs_timeout_min = min;
8895 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008896}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008897#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8898 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8899void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8900 uint32_t min, uint32_t max )
8901{
8902 ((void) conf);
8903 ((void) min);
8904 ((void) max);
8905}
8906#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8907 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8908
8909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008910
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008911void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008912{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008913#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8914 conf->authmode = authmode;
8915#else
8916 ((void) conf);
8917 ((void) authmode);
8918#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008919}
8920
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008921#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8922 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008923void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008924 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008925 void *p_vrfy )
8926{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008927 conf->f_vrfy = f_vrfy;
8928 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008929}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008930#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008931
Hanno Beckerece325c2019-06-13 15:39:27 +01008932#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008933void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008934 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008935 void *p_rng )
8936{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008937 conf->f_rng = f_rng;
8938 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008939}
Hanno Beckerece325c2019-06-13 15:39:27 +01008940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008941
Hanno Becker14a4a442019-07-02 17:00:34 +01008942#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008943void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008944 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008945 void *p_dbg )
8946{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008947 conf->f_dbg = f_dbg;
8948 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008949}
Hanno Becker14a4a442019-07-02 17:00:34 +01008950#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008951
Hanno Beckera58a8962019-06-13 16:11:15 +01008952#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8953 !defined(MBEDTLS_SSL_CONF_SEND) && \
8954 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008956 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008957 mbedtls_ssl_send_t *f_send,
8958 mbedtls_ssl_recv_t *f_recv,
8959 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008960{
Hanno Beckera58a8962019-06-13 16:11:15 +01008961 ssl->p_bio = p_bio;
8962 ssl->f_send = f_send;
8963 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008964 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008965}
Hanno Beckera58a8962019-06-13 16:11:15 +01008966#else
8967void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8968 void *p_bio )
8969{
8970 ssl->p_bio = p_bio;
8971}
8972#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008973
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008974#if defined(MBEDTLS_SSL_PROTO_DTLS)
8975void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8976{
8977 ssl->mtu = mtu;
8978}
8979#endif
8980
Hanno Becker1f835fa2019-06-13 10:14:59 +01008981#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008982void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008983{
8984 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008985}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008986#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008987
Hanno Becker0ae6b242019-06-13 16:45:36 +01008988#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8989 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008990void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8991 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008992 mbedtls_ssl_set_timer_t *f_set_timer,
8993 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008994{
8995 ssl->p_timer = p_timer;
8996 ssl->f_set_timer = f_set_timer;
8997 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008998 /* Make sure we start with no timer running */
8999 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009000}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009001#else
9002void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9003 void *p_timer )
9004{
9005 ssl->p_timer = p_timer;
9006 /* Make sure we start with no timer running */
9007 ssl_set_timer( ssl, 0 );
9008}
9009#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009010
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009011#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009012void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009013 void *p_cache,
9014 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9015 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009016{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009017 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009018 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009019 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009020}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009021#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009022
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009023#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009025{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009026 int ret;
9027
9028 if( ssl == NULL ||
9029 session == NULL ||
9030 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009031 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009034 }
9035
Hanno Becker58fccf22019-02-06 14:30:46 +00009036 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9037 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009038 return( ret );
9039
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009040 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009041
9042 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009043}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009044#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009045
Hanno Becker73f4cb12019-06-27 13:51:07 +01009046#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009047void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009048 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009049{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009050 conf->ciphersuite_list[0] = ciphersuites;
9051 conf->ciphersuite_list[1] = ciphersuites;
9052 conf->ciphersuite_list[2] = ciphersuites;
9053 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009054}
9055
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009057 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009058 int major, int minor )
9059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009061 return;
9062
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009063 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9064 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9065 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009066 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009067 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009068
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009069 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9070 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009071}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009072#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009075void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009076 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009077{
9078 conf->cert_profile = profile;
9079}
9080
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009081/* Append a new keycert entry to a (possibly empty) list */
9082static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9083 mbedtls_x509_crt *cert,
9084 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009085{
niisato8ee24222018-06-25 19:05:48 +09009086 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009087
niisato8ee24222018-06-25 19:05:48 +09009088 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9089 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009090 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009091
niisato8ee24222018-06-25 19:05:48 +09009092 new_cert->cert = cert;
9093 new_cert->key = key;
9094 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009095
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009096 /* Update head is the list was null, else add to the end */
9097 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009098 {
niisato8ee24222018-06-25 19:05:48 +09009099 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009100 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009101 else
9102 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009103 mbedtls_ssl_key_cert *cur = *head;
9104 while( cur->next != NULL )
9105 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009106 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009107 }
9108
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009109 return( 0 );
9110}
9111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009113 mbedtls_x509_crt *own_cert,
9114 mbedtls_pk_context *pk_key )
9115{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009116 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009117}
9118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009120 mbedtls_x509_crt *ca_chain,
9121 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009122{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009123 conf->ca_chain = ca_chain;
9124 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009127
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009128#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9129int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9130 mbedtls_x509_crt *own_cert,
9131 mbedtls_pk_context *pk_key )
9132{
9133 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9134 own_cert, pk_key ) );
9135}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009136
9137void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9138 mbedtls_x509_crt *ca_chain,
9139 mbedtls_x509_crl *ca_crl )
9140{
9141 ssl->handshake->sni_ca_chain = ca_chain;
9142 ssl->handshake->sni_ca_crl = ca_crl;
9143}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009144
9145void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9146 int authmode )
9147{
9148 ssl->handshake->sni_authmode = authmode;
9149}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009150#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9151
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009152#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009153/*
9154 * Set EC J-PAKE password for current handshake
9155 */
9156int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9157 const unsigned char *pw,
9158 size_t pw_len )
9159{
9160 mbedtls_ecjpake_role role;
9161
Janos Follath8eb64132016-06-03 15:40:57 +01009162 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9164
Hanno Becker2d9623f2019-06-13 12:07:05 +01009165 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009166 role = MBEDTLS_ECJPAKE_SERVER;
9167 else
9168 role = MBEDTLS_ECJPAKE_CLIENT;
9169
9170 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9171 role,
9172 MBEDTLS_MD_SHA256,
9173 MBEDTLS_ECP_DP_SECP256R1,
9174 pw, pw_len ) );
9175}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009176#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009179int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009180 const unsigned char *psk, size_t psk_len,
9181 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009182{
Paul Bakker6db455e2013-09-18 17:29:31 +02009183 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009188
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009189 /* Identity len will be encoded on two bytes */
9190 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009191 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009192 {
9193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9194 }
9195
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009196 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009197 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009198 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009199
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009200 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009201 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009202 conf->psk_len = 0;
9203 }
9204 if( conf->psk_identity != NULL )
9205 {
9206 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009207 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009208 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009209 }
9210
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009211 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9212 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009213 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009214 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009215 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009216 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009217 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009218 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009219 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009220
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009221 conf->psk_len = psk_len;
9222 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009223
Teppo Järvelin91d79382019-10-02 09:09:31 +03009224 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9225 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009226
9227 return( 0 );
9228}
9229
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009230int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9231 const unsigned char *psk, size_t psk_len )
9232{
9233 if( psk == NULL || ssl->handshake == NULL )
9234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9235
9236 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9238
9239 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009240 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009241 mbedtls_platform_zeroize( ssl->handshake->psk,
9242 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009243 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009244 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009245 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009246
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009247 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009248 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009249
9250 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009251 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009252
9253 return( 0 );
9254}
9255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009256void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009258 size_t),
9259 void *p_psk )
9260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009261 conf->f_psk = f_psk;
9262 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009265
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009266#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009267
9268#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009269int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009270{
9271 int ret;
9272
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009273 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9274 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9275 {
9276 mbedtls_mpi_free( &conf->dhm_P );
9277 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009278 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009280
9281 return( 0 );
9282}
Hanno Becker470a8c42017-10-04 15:28:46 +01009283#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009284
Hanno Beckera90658f2017-10-04 15:29:08 +01009285int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9286 const unsigned char *dhm_P, size_t P_len,
9287 const unsigned char *dhm_G, size_t G_len )
9288{
9289 int ret;
9290
9291 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9292 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9293 {
9294 mbedtls_mpi_free( &conf->dhm_P );
9295 mbedtls_mpi_free( &conf->dhm_G );
9296 return( ret );
9297 }
9298
9299 return( 0 );
9300}
Paul Bakker5121ce52009-01-03 21:22:43 +00009301
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009302int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009303{
9304 int ret;
9305
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009306 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9307 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9308 {
9309 mbedtls_mpi_free( &conf->dhm_P );
9310 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009311 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009312 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009313
9314 return( 0 );
9315}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009316#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009317
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009318#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9319/*
9320 * Set the minimum length for Diffie-Hellman parameters
9321 */
9322void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9323 unsigned int bitlen )
9324{
9325 conf->dhm_min_bitlen = bitlen;
9326}
9327#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9328
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009329#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009330/*
9331 * Set allowed/preferred hashes for handshake signatures
9332 */
9333void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9334 const int *hashes )
9335{
Hanno Becker56595f42019-06-19 16:31:38 +01009336#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009337 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009338#else
9339 ((void) conf);
9340 ((void) hashes);
9341#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009342}
Hanno Becker947194e2017-04-07 13:25:49 +01009343#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009344
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009345#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009346#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009347/*
9348 * Set the allowed elliptic curves
9349 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009350void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009351 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009352{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009353 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009354}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009355#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009356#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009357
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009358#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009360{
Hanno Becker947194e2017-04-07 13:25:49 +01009361 /* Initialize to suppress unnecessary compiler warning */
9362 size_t hostname_len = 0;
9363
9364 /* Check if new hostname is valid before
9365 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009366 if( hostname != NULL )
9367 {
9368 hostname_len = strlen( hostname );
9369
9370 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9372 }
9373
9374 /* Now it's clear that we will overwrite the old hostname,
9375 * so we can free it safely */
9376
9377 if( ssl->hostname != NULL )
9378 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009379 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009380 mbedtls_free( ssl->hostname );
9381 }
9382
9383 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009384
Paul Bakker5121ce52009-01-03 21:22:43 +00009385 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009386 {
9387 ssl->hostname = NULL;
9388 }
9389 else
9390 {
9391 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009392 if( ssl->hostname == NULL )
9393 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009394
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009395 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9396 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009397
Hanno Becker947194e2017-04-07 13:25:49 +01009398 ssl->hostname[hostname_len] = '\0';
9399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009400
9401 return( 0 );
9402}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009403#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009404
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009405#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009406void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009408 const unsigned char *, size_t),
9409 void *p_sni )
9410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009411 conf->f_sni = f_sni;
9412 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009417int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009418{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009419 size_t cur_len, tot_len;
9420 const char **p;
9421
9422 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009423 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9424 * MUST NOT be truncated."
9425 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009426 */
9427 tot_len = 0;
9428 for( p = protos; *p != NULL; p++ )
9429 {
9430 cur_len = strlen( *p );
9431 tot_len += cur_len;
9432
9433 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009435 }
9436
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009437 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009438
9439 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009440}
9441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009443{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009444 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009447
Hanno Becker33b9b252019-07-05 11:23:25 +01009448#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9449 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9450void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9451 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009453 conf->max_major_ver = major;
9454 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009455}
Hanno Becker33b9b252019-07-05 11:23:25 +01009456#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9457 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009458
Hanno Becker33b9b252019-07-05 11:23:25 +01009459#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9460 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9461void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9462 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009463{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009464 conf->min_major_ver = major;
9465 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009466}
Hanno Becker33b9b252019-07-05 11:23:25 +01009467#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9468 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009471void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009472{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009473 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009474}
9475#endif
9476
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009477#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009478void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9479 char cert_req_ca_list )
9480{
9481 conf->cert_req_ca_list = cert_req_ca_list;
9482}
9483#endif
9484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009486void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009487{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009488 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009489}
9490#endif
9491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009493#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009494void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009495{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009496 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009497}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009498#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9499#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009500void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009501 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009502{
9503 conf->enforce_extended_master_secret = ems_enf;
9504}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009505#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009506#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009507
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009508#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009509void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009511 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009512}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009513#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009516int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009519 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009522 }
9523
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009524 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009525
9526 return( 0 );
9527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009531void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009532{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009533 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009538void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009539{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009540 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009541}
9542#endif
9543
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009544#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009545void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009546{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009547 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009548}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009549#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009552void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009554 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009555}
9556
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009557void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009558{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009559 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009560}
9561
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009562void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009563 const unsigned char period[8] )
9564{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009565 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009570#if defined(MBEDTLS_SSL_CLI_C)
9571void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009572{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009573 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009574}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009575#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009576
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009577#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009578void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9579 mbedtls_ssl_ticket_write_t *f_ticket_write,
9580 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9581 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009582{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009583 conf->f_ticket_write = f_ticket_write;
9584 conf->f_ticket_parse = f_ticket_parse;
9585 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009586}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009587#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009589
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009590#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9591void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9592 mbedtls_ssl_export_keys_t *f_export_keys,
9593 void *p_export_keys )
9594{
9595 conf->f_export_keys = f_export_keys;
9596 conf->p_export_keys = p_export_keys;
9597}
9598#endif
9599
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009600#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009601void mbedtls_ssl_conf_async_private_cb(
9602 mbedtls_ssl_config *conf,
9603 mbedtls_ssl_async_sign_t *f_async_sign,
9604 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9605 mbedtls_ssl_async_resume_t *f_async_resume,
9606 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009607 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009608{
9609 conf->f_async_sign_start = f_async_sign;
9610 conf->f_async_decrypt_start = f_async_decrypt;
9611 conf->f_async_resume = f_async_resume;
9612 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009613 conf->p_async_config_data = async_config_data;
9614}
9615
Gilles Peskine8f97af72018-04-26 11:46:10 +02009616void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9617{
9618 return( conf->p_async_config_data );
9619}
9620
Gilles Peskine1febfef2018-04-30 11:54:39 +02009621void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009622{
9623 if( ssl->handshake == NULL )
9624 return( NULL );
9625 else
9626 return( ssl->handshake->user_async_ctx );
9627}
9628
Gilles Peskine1febfef2018-04-30 11:54:39 +02009629void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009630 void *ctx )
9631{
9632 if( ssl->handshake != NULL )
9633 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009634}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009635#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009636
Paul Bakker5121ce52009-01-03 21:22:43 +00009637/*
9638 * SSL get accessors
9639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009641{
9642 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9643}
9644
Hanno Becker8b170a02017-10-10 11:51:19 +01009645int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9646{
9647 /*
9648 * Case A: We're currently holding back
9649 * a message for further processing.
9650 */
9651
9652 if( ssl->keep_current_message == 1 )
9653 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009655 return( 1 );
9656 }
9657
9658 /*
9659 * Case B: Further records are pending in the current datagram.
9660 */
9661
9662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009663 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009664 ssl->in_left > ssl->next_record_offset )
9665 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009667 return( 1 );
9668 }
9669#endif /* MBEDTLS_SSL_PROTO_DTLS */
9670
9671 /*
9672 * Case C: A handshake message is being processed.
9673 */
9674
Hanno Becker8b170a02017-10-10 11:51:19 +01009675 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9676 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009678 return( 1 );
9679 }
9680
9681 /*
9682 * Case D: An application data message is being processed
9683 */
9684 if( ssl->in_offt != NULL )
9685 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009687 return( 1 );
9688 }
9689
9690 /*
9691 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009692 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009693 * we implement support for multiple alerts in single records.
9694 */
9695
9696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9697 return( 0 );
9698}
9699
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009700uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009701{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009702 if( ssl->session != NULL )
9703 return( ssl->session->verify_result );
9704
9705 if( ssl->session_negotiate != NULL )
9706 return( ssl->session_negotiate->verify_result );
9707
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009708 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009709}
9710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009712{
Hanno Beckere02758c2019-06-26 15:31:31 +01009713 int suite;
9714
Paul Bakker926c8e42013-03-06 10:23:34 +01009715 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009716 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009717
Hanno Beckere02758c2019-06-26 15:31:31 +01009718 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9719 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009720}
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009725 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009726 {
Hanno Becker2881d802019-05-22 14:44:53 +01009727 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009730 return( "DTLSv1.0" );
9731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009733 return( "DTLSv1.2" );
9734
9735 default:
9736 return( "unknown (DTLS)" );
9737 }
9738 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009739 MBEDTLS_SSL_TRANSPORT_ELSE
9740#endif /* MBEDTLS_SSL_PROTO_DTLS */
9741#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009742 {
Hanno Becker2881d802019-05-22 14:44:53 +01009743 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009744 {
9745 case MBEDTLS_SSL_MINOR_VERSION_0:
9746 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009747
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009748 case MBEDTLS_SSL_MINOR_VERSION_1:
9749 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009750
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009751 case MBEDTLS_SSL_MINOR_VERSION_2:
9752 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009753
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009754 case MBEDTLS_SSL_MINOR_VERSION_3:
9755 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009756
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009757 default:
9758 return( "unknown" );
9759 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009760 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009761#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009762}
9763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009765{
Hanno Becker3136ede2018-08-17 15:28:19 +01009766 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009768
Hanno Becker43395762019-05-03 14:46:38 +01009769 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9770
Hanno Becker78640902018-08-13 16:35:15 +01009771 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009772 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_ZLIB_SUPPORT)
9775 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9776 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009777#endif
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009780 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009781#if defined(MBEDTLS_GCM_C) || \
9782 defined(MBEDTLS_CCM_C) || \
9783 defined(MBEDTLS_CHACHAPOLY_C)
9784#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009786#endif
9787#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009789#endif
9790#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009791 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009792#endif
9793 transform_expansion =
9794 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009795 break;
9796
Hanno Beckera9d5c452019-07-25 16:47:12 +01009797#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9798 MBEDTLS_CHACHAPOLY_C */
9799
9800#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9801 case MBEDTLS_MODE_STREAM:
9802 transform_expansion = transform->maclen;
9803 break;
9804#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9805
9806#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009808 {
9809 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009810
9811 block_size = mbedtls_cipher_get_block_size(
9812 &transform->cipher_ctx_enc );
9813
Hanno Becker3136ede2018-08-17 15:28:19 +01009814 /* Expansion due to the addition of the MAC. */
9815 transform_expansion += transform->maclen;
9816
9817 /* Expansion due to the addition of CBC padding;
9818 * Theoretically up to 256 bytes, but we never use
9819 * more than the block size of the underlying cipher. */
9820 transform_expansion += block_size;
9821
9822 /* For TLS 1.1 or higher, an explicit IV is added
9823 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009824#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009825 if( mbedtls_ssl_ver_geq(
9826 mbedtls_ssl_get_minor_ver( ssl ),
9827 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9828 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009829 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009830 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009831#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009832
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009833 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009834 }
9835#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009836
9837 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009840 }
9841
Hanno Beckera5a2b082019-05-15 14:03:01 +01009842#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009843 if( transform->out_cid_len != 0 )
9844 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009845#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009846
Hanno Becker43395762019-05-03 14:46:38 +01009847 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009848}
9849
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009850#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9851size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9852{
9853 size_t max_len;
9854
9855 /*
9856 * Assume mfl_code is correct since it was checked when set
9857 */
Angus Grattond8213d02016-05-25 20:56:48 +10009858 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009859
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009860 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009861 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009862 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009863 {
Angus Grattond8213d02016-05-25 20:56:48 +10009864 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009865 }
9866
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009867 /* During a handshake, use the value being negotiated */
9868 if( ssl->session_negotiate != NULL &&
9869 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9870 {
9871 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9872 }
9873
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009874 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009875}
9876#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9877
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009878#if defined(MBEDTLS_SSL_PROTO_DTLS)
9879static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9880{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009881 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009882 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009883 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9884 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009885 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009886
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009887 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9888 return( ssl->mtu );
9889
9890 if( ssl->mtu == 0 )
9891 return( ssl->handshake->mtu );
9892
9893 return( ssl->mtu < ssl->handshake->mtu ?
9894 ssl->mtu : ssl->handshake->mtu );
9895}
9896#endif /* MBEDTLS_SSL_PROTO_DTLS */
9897
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009898int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9899{
9900 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9901
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009902#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9903 !defined(MBEDTLS_SSL_PROTO_DTLS)
9904 (void) ssl;
9905#endif
9906
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009907#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9908 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9909
9910 if( max_len > mfl )
9911 max_len = mfl;
9912#endif
9913
9914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009915 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009916 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009917 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009918 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9919 const size_t overhead = (size_t) ret;
9920
9921 if( ret < 0 )
9922 return( ret );
9923
9924 if( mtu <= overhead )
9925 {
9926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9927 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9928 }
9929
9930 if( max_len > mtu - overhead )
9931 max_len = mtu - overhead;
9932 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009934
Hanno Becker0defedb2018-08-10 12:35:02 +01009935#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9936 !defined(MBEDTLS_SSL_PROTO_DTLS)
9937 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009938#endif
9939
9940 return( (int) max_len );
9941}
9942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_X509_CRT_PARSE_C)
9944const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009945{
9946 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009947 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009948
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009949#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009950 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009951#else
9952 return( NULL );
9953#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009958int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9959 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009960{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009961 if( ssl == NULL ||
9962 dst == NULL ||
9963 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009964 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009967 }
9968
Hanno Becker58fccf22019-02-06 14:30:46 +00009969 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009972
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009973const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9974{
9975 if( ssl == NULL )
9976 return( NULL );
9977
9978 return( ssl->session );
9979}
9980
Paul Bakker5121ce52009-01-03 21:22:43 +00009981/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009982 * Define ticket header determining Mbed TLS version
9983 * and structure of the ticket.
9984 */
9985
Hanno Becker41527622019-05-16 12:50:45 +01009986/*
Hanno Becker26829e92019-05-28 14:30:45 +01009987 * Define bitflag determining compile-time settings influencing
9988 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009989 */
9990
Hanno Becker26829e92019-05-28 14:30:45 +01009991#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009992#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009993#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009994#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009995#endif /* MBEDTLS_HAVE_TIME */
9996
9997#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009998#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009999#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010000#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010001#endif /* MBEDTLS_X509_CRT_PARSE_C */
10002
10003#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010004#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010005#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010006#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010007#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10008
10009#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010010#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010011#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010012#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010013#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10014
10015#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010016#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010017#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010018#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010019#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10020
10021#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010022#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010023#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010024#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010025#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10026
Hanno Becker41527622019-05-16 12:50:45 +010010027#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10028#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10029#else
10030#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10031#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10032
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010033#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10034#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10035#else
10036#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10037#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10038
Hanno Becker88440552019-07-03 14:16:13 +010010039#if defined(MBEDTLS_ZLIB_SUPPORT)
10040#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10041#else
10042#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10043#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10044
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010045#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10046#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10047#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10048#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10049#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10050#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10051#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010052#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010053#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010054
Hanno Becker26829e92019-05-28 14:30:45 +010010055#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010056 ( (uint16_t) ( \
10057 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10058 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10059 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10060 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10061 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10062 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010063 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010064 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010065 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010066
Hanno Becker557fe9f2019-05-16 12:41:07 +010010067static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010068 MBEDTLS_VERSION_MAJOR,
10069 MBEDTLS_VERSION_MINOR,
10070 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010071 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10072 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010073};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010074
10075/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010076 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010077 * (in the presentation language of TLS, RFC 8446 section 3)
10078 *
Hanno Becker26829e92019-05-28 14:30:45 +010010079 * opaque mbedtls_version[3]; // major, minor, patch
10080 * opaque session_format[2]; // version-specific 16-bit field determining
10081 * // the format of the remaining
10082 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010083 *
10084 * Note: When updating the format, remember to keep
10085 * these version+format bytes.
10086 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010087 * // In this version, `session_format` determines
10088 * // the setting of those compile-time
10089 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010090 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010091 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010092 * uint8 ciphersuite[2]; // defined by the standard
10093 * uint8 compression; // 0 or 1
10094 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010095 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010096 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010097 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010098 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10099 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10100 * case disabled: uint8_t peer_cert_digest_type;
10101 * opaque peer_cert_digest<0..2^8-1>;
10102 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010103 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010104 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010105 * uint8 mfl_code; // up to 255 according to standard
10106 * uint8 trunc_hmac; // 0 or 1
10107 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010108 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010109 * The order is the same as in the definition of the structure, except
10110 * verify_result is put before peer_cert so that all mandatory fields come
10111 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010112 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010113static int ssl_session_save( const mbedtls_ssl_session *session,
10114 unsigned char omit_header,
10115 unsigned char *buf,
10116 size_t buf_len,
10117 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010118{
10119 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010120 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010121#if defined(MBEDTLS_HAVE_TIME)
10122 uint64_t start;
10123#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010124#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010125#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010126 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010127#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010128#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010129
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010130 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010131 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010132 /*
10133 * Add version identifier
10134 */
10135
10136 used += sizeof( ssl_serialized_session_header );
10137
10138 if( used <= buf_len )
10139 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010140 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010141 sizeof( ssl_serialized_session_header ) );
10142 p += sizeof( ssl_serialized_session_header );
10143 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010144 }
10145
10146 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010147 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010148 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010149#if defined(MBEDTLS_HAVE_TIME)
10150 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010151
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010152 if( used <= buf_len )
10153 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010154 start = (uint64_t) session->start;
10155
10156 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10157 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10158 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10159 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10160 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10161 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10162 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10163 *p++ = (unsigned char)( ( start ) & 0xFF );
10164 }
10165#endif /* MBEDTLS_HAVE_TIME */
10166
10167 /*
10168 * Basic mandatory fields
10169 */
Hanno Becker88440552019-07-03 14:16:13 +010010170 {
10171 size_t const ciphersuite_len = 2;
10172#if defined(MBEDTLS_ZLIB_SUPPORT)
10173 size_t const compression_len = 1;
10174#else
10175 size_t const compression_len = 0;
10176#endif
10177 size_t const id_len_len = 1;
10178 size_t const id_len = 32;
10179 size_t const master_len = 48;
10180 size_t const verif_result_len = 4;
10181
10182 size_t const basic_len =
10183 ciphersuite_len +
10184 compression_len +
10185 id_len_len +
10186 id_len +
10187 master_len +
10188 verif_result_len;
10189
10190 used += basic_len;
10191 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010192
10193 if( used <= buf_len )
10194 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010195 const int ciphersuite =
10196 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010197 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010198
Hanno Becker88440552019-07-03 14:16:13 +010010199#if defined(MBEDTLS_ZLIB_SUPPORT)
10200 *p++ = (unsigned char)(
10201 mbedtls_ssl_session_get_compression( session ) );
10202#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010203
10204 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010205 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10206 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010207 p += 32;
10208
Teppo Järvelin91d79382019-10-02 09:09:31 +030010209 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010210 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010211 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010212 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010213
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010214 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010215 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010216 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010217#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010218#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010219 if( session->peer_cert == NULL )
10220 cert_len = 0;
10221 else
10222 cert_len = session->peer_cert->raw.len;
10223
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010224 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010225
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010226 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010227 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010228 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010229
10230 if( session->peer_cert != NULL )
10231 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010232 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010233 p += cert_len;
10234 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010235 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010236
Hanno Becker5882dd02019-06-06 16:25:57 +010010237#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010238 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010239 if( session->peer_cert_digest != NULL )
10240 {
10241 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10242 if( used <= buf_len )
10243 {
10244 *p++ = (unsigned char) session->peer_cert_digest_type;
10245 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010246 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010247 session->peer_cert_digest_len );
10248 p += session->peer_cert_digest_len;
10249 }
10250 }
10251 else
10252 {
10253 used += 2;
10254 if( used <= buf_len )
10255 {
10256 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10257 *p++ = 0;
10258 }
10259 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010260#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010261#endif /* MBEDTLS_X509_CRT_PARSE_C */
10262
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010263 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010264 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010265 */
10266#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010267 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010268
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010269 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010270 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010271 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010272
10273 if( session->ticket != NULL )
10274 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010275 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010276 p += session->ticket_len;
10277 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010278
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010279 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010280 }
10281#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10282
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010283 /*
10284 * Misc extension-related info
10285 */
10286#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10287 used += 1;
10288
10289 if( used <= buf_len )
10290 *p++ = session->mfl_code;
10291#endif
10292
10293#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10294 used += 1;
10295
10296 if( used <= buf_len )
10297 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10298#endif
10299
10300#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10301 used += 1;
10302
10303 if( used <= buf_len )
10304 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10305#endif
10306
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010307 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010308 *olen = used;
10309
10310 if( used > buf_len )
10311 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010312
10313 return( 0 );
10314}
10315
10316/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010317 * Public wrapper for ssl_session_save()
10318 */
10319int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10320 unsigned char *buf,
10321 size_t buf_len,
10322 size_t *olen )
10323{
10324 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10325}
10326
10327/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010328 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010329 *
10330 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010331 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010332 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010333static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010334 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010335 const unsigned char *buf,
10336 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010337{
10338 const unsigned char *p = buf;
10339 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010340 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010341#if defined(MBEDTLS_HAVE_TIME)
10342 uint64_t start;
10343#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010344#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010345#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010346 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010347#endif
10348#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010349
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010350 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010351 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010352 /*
10353 * Check version identifier
10354 */
10355
10356 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10358
Teppo Järvelin0efac532019-10-04 13:21:08 +030010359 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010360 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010361 sizeof( ssl_serialized_session_header ) ) != 0 )
10362 {
10363 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10364 }
10365 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010366 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010367
10368 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010369 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010370 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010371#if defined(MBEDTLS_HAVE_TIME)
10372 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10374
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010375 start = ( (uint64_t) p[0] << 56 ) |
10376 ( (uint64_t) p[1] << 48 ) |
10377 ( (uint64_t) p[2] << 40 ) |
10378 ( (uint64_t) p[3] << 32 ) |
10379 ( (uint64_t) p[4] << 24 ) |
10380 ( (uint64_t) p[5] << 16 ) |
10381 ( (uint64_t) p[6] << 8 ) |
10382 ( (uint64_t) p[7] );
10383 p += 8;
10384
10385 session->start = (time_t) start;
10386#endif /* MBEDTLS_HAVE_TIME */
10387
10388 /*
10389 * Basic mandatory fields
10390 */
Hanno Becker88440552019-07-03 14:16:13 +010010391 {
10392 size_t const ciphersuite_len = 2;
10393#if defined(MBEDTLS_ZLIB_SUPPORT)
10394 size_t const compression_len = 1;
10395#else
10396 size_t const compression_len = 0;
10397#endif
10398 size_t const id_len_len = 1;
10399 size_t const id_len = 32;
10400 size_t const master_len = 48;
10401 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010402
Hanno Becker88440552019-07-03 14:16:13 +010010403 size_t const basic_len =
10404 ciphersuite_len +
10405 compression_len +
10406 id_len_len +
10407 id_len +
10408 master_len +
10409 verif_result_len;
10410
10411 if( basic_len > (size_t)( end - p ) )
10412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10413 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010414
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010415 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010416 p += 2;
10417
Hanno Becker73f4cb12019-06-27 13:51:07 +010010418#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010419 session->ciphersuite = ciphersuite;
10420#else
10421 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010422 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010423 {
10424 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10425 }
10426#endif
10427
Hanno Becker88440552019-07-03 14:16:13 +010010428#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010429 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010430#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010431
10432 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010433 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10434 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010435 p += 32;
10436
Teppo Järvelin91d79382019-10-02 09:09:31 +030010437 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010438 p += 48;
10439
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010440 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010441 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010442
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010443 /* Immediately clear invalid pointer values that have been read, in case
10444 * we exit early before we replaced them with valid ones. */
10445#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010446#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010447 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010448#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010449 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010450#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010451#endif
10452#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10453 session->ticket = NULL;
10454#endif
10455
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010456 /*
10457 * Peer certificate
10458 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010459#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010460#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010461 if( 3 > (size_t)( end - p ) )
10462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10463
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010464 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10465
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010466 p += 3;
10467
10468 if( cert_len == 0 )
10469 {
10470 session->peer_cert = NULL;
10471 }
10472 else
10473 {
10474 int ret;
10475
10476 if( cert_len > (size_t)( end - p ) )
10477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10478
10479 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10480
10481 if( session->peer_cert == NULL )
10482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10483
10484 mbedtls_x509_crt_init( session->peer_cert );
10485
10486 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10487 p, cert_len ) ) != 0 )
10488 {
10489 mbedtls_x509_crt_free( session->peer_cert );
10490 mbedtls_free( session->peer_cert );
10491 session->peer_cert = NULL;
10492 return( ret );
10493 }
10494
10495 p += cert_len;
10496 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010497#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010498 /* Deserialize CRT digest from the end of the ticket. */
10499 if( 2 > (size_t)( end - p ) )
10500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10501
10502 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10503 session->peer_cert_digest_len = (size_t) *p++;
10504
10505 if( session->peer_cert_digest_len != 0 )
10506 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010507 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010508 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010509 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10511 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10513
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010514 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10516
10517 session->peer_cert_digest =
10518 mbedtls_calloc( 1, session->peer_cert_digest_len );
10519 if( session->peer_cert_digest == NULL )
10520 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10521
Teppo Järvelin91d79382019-10-02 09:09:31 +030010522 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010523 session->peer_cert_digest_len );
10524 p += session->peer_cert_digest_len;
10525 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010526#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010527#endif /* MBEDTLS_X509_CRT_PARSE_C */
10528
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010529 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010530 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010531 */
10532#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10533 if( 3 > (size_t)( end - p ) )
10534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10535
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010536 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010537 p += 3;
10538
10539 if( session->ticket_len != 0 )
10540 {
10541 if( session->ticket_len > (size_t)( end - p ) )
10542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10543
10544 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10545 if( session->ticket == NULL )
10546 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10547
Teppo Järvelin91d79382019-10-02 09:09:31 +030010548 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010549 p += session->ticket_len;
10550 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010551
10552 if( 4 > (size_t)( end - p ) )
10553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10554
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010555 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010556 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010557#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10558
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010559 /*
10560 * Misc extension-related info
10561 */
10562#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10563 if( 1 > (size_t)( end - p ) )
10564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10565
10566 session->mfl_code = *p++;
10567#endif
10568
10569#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10570 if( 1 > (size_t)( end - p ) )
10571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10572
10573 session->trunc_hmac = *p++;
10574#endif
10575
10576#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10577 if( 1 > (size_t)( end - p ) )
10578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10579
10580 session->encrypt_then_mac = *p++;
10581#endif
10582
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010583 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010584 if( p != end )
10585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10586
10587 return( 0 );
10588}
10589
10590/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010591 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010592 */
10593int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10594 const unsigned char *buf,
10595 size_t len )
10596{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010597 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010598
10599 if( ret != 0 )
10600 mbedtls_ssl_session_free( session );
10601
10602 return( ret );
10603}
10604
10605/*
Paul Bakker1961b702013-01-25 14:49:24 +010010606 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010611
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010612 if( ssl == NULL || ssl->conf == NULL )
10613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010616 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010620 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010622#endif
10623
Hanno Beckerb82350b2019-07-26 07:24:05 +010010624 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010625 return( ret );
10626}
10627
10628/*
10629 * Perform the SSL handshake
10630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010632{
10633 int ret = 0;
10634
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010635 if( ssl == NULL || ssl->conf == NULL )
10636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010643
10644 if( ret != 0 )
10645 break;
10646 }
10647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010649
10650 return( ret );
10651}
10652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653#if defined(MBEDTLS_SSL_RENEGOTIATION)
10654#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010655/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010656 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010659{
10660 int ret;
10661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010663
10664 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10666 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010667
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010668 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010669 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010671 return( ret );
10672 }
10673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010675
10676 return( 0 );
10677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010679
10680/*
10681 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 * - any side: calling mbedtls_ssl_renegotiate(),
10683 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10684 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010685 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010686 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010690{
10691 int ret;
10692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010694
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010695 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10696 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010697
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010698 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10699 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010701 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010703 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010704 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10705 MBEDTLS_SSL_IS_SERVER )
10706 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010707 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010708 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010709 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010710 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010711 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010712 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010713 }
10714#endif
10715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10717 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010722 return( ret );
10723 }
10724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010726
10727 return( 0 );
10728}
10729
10730/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010731 * Renegotiate current connection on client,
10732 * or request renegotiation on server
10733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010737
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010738 if( ssl == NULL || ssl->conf == NULL )
10739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010742 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010743 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010749
10750 /* Did we already try/start sending HelloRequest? */
10751 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010753
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010754 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010759 /*
10760 * On client, either start the renegotiation process or,
10761 * if already in progress, continue the handshake
10762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010767
10768 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010771 return( ret );
10772 }
10773 }
10774 else
10775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010779 return( ret );
10780 }
10781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010783
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010784 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010785}
10786
10787/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010788 * Check record counters and renegotiate if they're above the limit.
10789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010791{
Andres AG2196c7f2016-12-15 17:01:16 +000010792 size_t ep_len = ssl_ep_len( ssl );
10793 int in_ctr_cmp;
10794 int out_ctr_cmp;
10795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10797 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010798 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010799 {
10800 return( 0 );
10801 }
10802
Teppo Järvelin0efac532019-10-04 13:21:08 +030010803 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010804 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010805 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010806 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010807 ssl->conf->renego_period + ep_len, 8 - ep_len );
10808
10809 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010810 {
10811 return( 0 );
10812 }
10813
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010816}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010817#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010818
10819/*
10820 * Receive application data decrypted from the SSL layer
10821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010822int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010823{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010824 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010825 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010826
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010827 if( ssl == NULL || ssl->conf == NULL )
10828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010832#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010833 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010836 return( ret );
10837
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010838 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010840 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010841 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010842 return( ret );
10843 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010844 }
10845#endif
10846
Hanno Becker4a810fb2017-05-24 16:27:30 +010010847 /*
10848 * Check if renegotiation is necessary and/or handshake is
10849 * in process. If yes, perform/continue, and fall through
10850 * if an unexpected packet is received while the client
10851 * is waiting for the ServerHello.
10852 *
10853 * (There is no equivalent to the last condition on
10854 * the server-side as it is not treated as within
10855 * a handshake while waiting for the ClientHello
10856 * after a renegotiation request.)
10857 */
10858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010860 ret = ssl_check_ctr_renegotiate( ssl );
10861 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10862 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010865 return( ret );
10866 }
10867#endif
10868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010872 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10873 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010876 return( ret );
10877 }
10878 }
10879
Hanno Beckere41158b2017-10-23 13:30:32 +010010880 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010881 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010882 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010883 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010884 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10885 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010886 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010887 ssl_set_timer( ssl,
10888 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010889 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010890
Hanno Becker327c93b2018-08-15 13:56:18 +010010891 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010892 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010893 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10894 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010895
Hanno Becker4a810fb2017-05-24 16:27:30 +010010896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10897 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010898 }
10899
10900 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010902 {
10903 /*
10904 * OpenSSL sends empty messages to randomize the IV
10905 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010906 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010909 return( 0 );
10910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010912 return( ret );
10913 }
10914 }
10915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010919
Hanno Becker4a810fb2017-05-24 16:27:30 +010010920 /*
10921 * - For client-side, expect SERVER_HELLO_REQUEST.
10922 * - For server-side, expect CLIENT_HELLO.
10923 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10924 */
10925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010927 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10928 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010930 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010933
10934 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010936 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010937 {
10938 continue;
10939 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010940 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010941#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010942#if defined(MBEDTLS_SSL_PROTO_TLS)
10943 {
10944 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10945 }
10946#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010947 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010948#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010949
Hanno Becker4a810fb2017-05-24 16:27:30 +010010950#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010951 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10952 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010956
10957 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010959 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010960 {
10961 continue;
10962 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010963 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010964#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010965#if defined(MBEDTLS_SSL_PROTO_TLS)
10966 {
10967 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10968 }
10969#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010970 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010971#endif /* MBEDTLS_SSL_SRV_C */
10972
Hanno Becker21df7f92017-10-17 11:03:26 +010010973#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010974 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010975 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10976 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010977 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010978 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10979 {
10980 /*
10981 * Accept renegotiation request
10982 */
Paul Bakker48916f92012-09-16 19:57:18 +000010983
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010984 /* DTLS clients need to know renego is server-initiated */
10985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010986 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010987 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10988 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010989 {
10990 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10991 }
10992#endif
10993 ret = ssl_start_renegotiation( ssl );
10994 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10995 ret != 0 )
10996 {
10997 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10998 return( ret );
10999 }
11000 }
11001 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011002#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011003 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011004 /*
11005 * Refuse renegotiation
11006 */
11007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011011 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011012 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011013 /* SSLv3 does not have a "no_renegotiation" warning, so
11014 we send a fatal alert and abort the connection. */
11015 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11016 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11017 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011018 }
11019 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11021#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11022 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011023 if( mbedtls_ssl_ver_geq(
11024 mbedtls_ssl_get_minor_ver( ssl ),
11025 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011026 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011027 ret = mbedtls_ssl_send_alert_message( ssl,
11028 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11029 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11030 if( ret != 0 )
11031 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011032 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11035 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011039 }
Paul Bakker48916f92012-09-16 19:57:18 +000011040 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011041
Hanno Becker90333da2017-10-10 11:27:13 +010011042 /* At this point, we don't know whether the renegotiation has been
11043 * completed or not. The cases to consider are the following:
11044 * 1) The renegotiation is complete. In this case, no new record
11045 * has been read yet.
11046 * 2) The renegotiation is incomplete because the client received
11047 * an application data record while awaiting the ServerHello.
11048 * 3) The renegotiation is incomplete because the client received
11049 * a non-handshake, non-application data message while awaiting
11050 * the ServerHello.
11051 * In each of these case, looping will be the proper action:
11052 * - For 1), the next iteration will read a new record and check
11053 * if it's application data.
11054 * - For 2), the loop condition isn't satisfied as application data
11055 * is present, hence continue is the same as break
11056 * - For 3), the loop condition is satisfied and read_record
11057 * will re-deliver the message that was held back by the client
11058 * when expecting the ServerHello.
11059 */
11060 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011061 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011062#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011064 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011065 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011066 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011067 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011070 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011072 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011073 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011075#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011081 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011082 }
11083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011084 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011088 }
11089
11090 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011091
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011092 /* We're going to return something now, cancel timer,
11093 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011094 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011095 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011096
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011098 /* If we requested renego but received AppData, resend HelloRequest.
11099 * Do it now, after setting in_offt, to avoid taking this branch
11100 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011102 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11103 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011105 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011106 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011109 return( ret );
11110 }
11111 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011113#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011114 }
11115
11116 n = ( len < ssl->in_msglen )
11117 ? len : ssl->in_msglen;
11118
Teppo Järvelin91d79382019-10-02 09:09:31 +030011119 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011120 ssl->in_msglen -= n;
11121
11122 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011123 {
11124 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011125 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011126 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011127 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011128 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011129 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011130 /* more data available */
11131 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011132 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011135
Paul Bakker23986e52011-04-24 08:57:21 +000011136 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011137}
11138
11139/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011140 * Send application data to be encrypted by the SSL layer, taking care of max
11141 * fragment length and buffer size.
11142 *
11143 * According to RFC 5246 Section 6.2.1:
11144 *
11145 * Zero-length fragments of Application data MAY be sent as they are
11146 * potentially useful as a traffic analysis countermeasure.
11147 *
11148 * Therefore, it is possible that the input message length is 0 and the
11149 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011150 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011151static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011152 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011153{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011154 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11155 const size_t max_len = (size_t) ret;
11156
11157 if( ret < 0 )
11158 {
11159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11160 return( ret );
11161 }
11162
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011163 if( len > max_len )
11164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011166 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011169 "maximum fragment length: %d > %d",
11170 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011172 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011173 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011174#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011175#if defined(MBEDTLS_SSL_PROTO_TLS)
11176 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011177 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011178 }
11179#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011180 }
Paul Bakker887bd502011-06-08 13:10:54 +000011181
Paul Bakker5121ce52009-01-03 21:22:43 +000011182 if( ssl->out_left != 0 )
11183 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011184 /*
11185 * The user has previously tried to send the data and
11186 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11187 * written. In this case, we expect the high-level write function
11188 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011193 return( ret );
11194 }
11195 }
Paul Bakker887bd502011-06-08 13:10:54 +000011196 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011197 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011198 /*
11199 * The user is trying to send a message the first time, so we need to
11200 * copy the data into the internal buffers and setup the data structure
11201 * to keep track of partial writes
11202 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011203 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011205 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011206
Hanno Becker67bc7c32018-08-06 11:33:50 +010011207 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011210 return( ret );
11211 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011212 }
11213
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011214 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011215}
11216
11217/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011218 * Write application data, doing 1/n-1 splitting if necessary.
11219 *
11220 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011221 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011222 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011225static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011226 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011227{
11228 int ret;
11229
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011230 if( ssl->conf->cbc_record_splitting ==
11231 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011232 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011233 mbedtls_ssl_ver_gt(
11234 mbedtls_ssl_get_minor_ver( ssl ),
11235 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11237 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011238 {
11239 return( ssl_write_real( ssl, buf, len ) );
11240 }
11241
11242 if( ssl->split_done == 0 )
11243 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011244 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011245 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011246 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011247 }
11248
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011249 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11250 return( ret );
11251 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011252
11253 return( ret + 1 );
11254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011255#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011256
11257/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011258 * Write application data (public-facing wrapper)
11259 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011260int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011261{
11262 int ret;
11263
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011265
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011266 if( ssl == NULL || ssl->conf == NULL )
11267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11268
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011269#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011270 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11271 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011273 return( ret );
11274 }
11275#endif
11276
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011277 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011278 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011279 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011280 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011282 return( ret );
11283 }
11284 }
11285
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011286#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011287 ret = ssl_write_split( ssl, buf, len );
11288#else
11289 ret = ssl_write_real( ssl, buf, len );
11290#endif
11291
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011293
11294 return( ret );
11295}
11296
11297/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011298 * Notify the peer that the connection is being closed
11299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011300int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011301{
11302 int ret;
11303
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011304 if( ssl == NULL || ssl->conf == NULL )
11305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011308
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011309 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011312 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011314 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11315 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11316 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011319 return( ret );
11320 }
11321 }
11322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011324
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011325 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011326}
11327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011328void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011329{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011330 if( transform == NULL )
11331 return;
11332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011333#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011334 deflateEnd( &transform->ctx_deflate );
11335 inflateEnd( &transform->ctx_inflate );
11336#endif
11337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011338 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11339 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011340
Hanno Becker92231322018-01-03 15:32:51 +000011341#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011342 mbedtls_md_free( &transform->md_ctx_enc );
11343 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011344#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011346 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011347}
11348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011349#if defined(MBEDTLS_X509_CRT_PARSE_C)
11350static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011353
11354 while( cur != NULL )
11355 {
11356 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011357 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011358 cur = next;
11359 }
11360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011362
Hanno Becker0271f962018-08-16 13:23:47 +010011363#if defined(MBEDTLS_SSL_PROTO_DTLS)
11364
11365static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11366{
11367 unsigned offset;
11368 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11369
11370 if( hs == NULL )
11371 return;
11372
Hanno Becker283f5ef2018-08-24 09:34:47 +010011373 ssl_free_buffered_record( ssl );
11374
Hanno Becker0271f962018-08-16 13:23:47 +010011375 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011376 ssl_buffering_free_slot( ssl, offset );
11377}
11378
11379static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11380 uint8_t slot )
11381{
11382 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11383 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011384
11385 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11386 return;
11387
Hanno Beckere605b192018-08-21 15:59:07 +010011388 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011389 {
Hanno Beckere605b192018-08-21 15:59:07 +010011390 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011391 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011392 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011393 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011394 }
11395}
11396
11397#endif /* MBEDTLS_SSL_PROTO_DTLS */
11398
Gilles Peskine9b562d52018-04-25 20:32:43 +020011399void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011400{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011401 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11402
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011403 if( handshake == NULL )
11404 return;
11405
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011406#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11407 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11408 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011409 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011410 handshake->async_in_progress = 0;
11411 }
11412#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11413
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011414#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11415 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11416 mbedtls_md5_free( &handshake->fin_md5 );
11417 mbedtls_sha1_free( &handshake->fin_sha1 );
11418#endif
11419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11420#if defined(MBEDTLS_SHA256_C)
11421 mbedtls_sha256_free( &handshake->fin_sha256 );
11422#endif
11423#if defined(MBEDTLS_SHA512_C)
11424 mbedtls_sha512_free( &handshake->fin_sha512 );
11425#endif
11426#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011428#if defined(MBEDTLS_DHM_C)
11429 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011431#if defined(MBEDTLS_ECDH_C)
11432 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011433#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011434#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011435 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011436#if defined(MBEDTLS_SSL_CLI_C)
11437 mbedtls_free( handshake->ecjpake_cache );
11438 handshake->ecjpake_cache = NULL;
11439 handshake->ecjpake_cache_len = 0;
11440#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011441#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011442
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011443#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11444 if( handshake->psk != NULL )
11445 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011446 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011447 mbedtls_free( handshake->psk );
11448 }
11449#endif
11450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011451#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11452 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011453 /*
11454 * Free only the linked list wrapper, not the keys themselves
11455 * since the belong to the SNI callback
11456 */
11457 if( handshake->sni_key_cert != NULL )
11458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011459 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011460
11461 while( cur != NULL )
11462 {
11463 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011464 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011465 cur = next;
11466 }
11467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011468#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011469
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011470#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011471 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011472 if( handshake->ecrs_peer_cert != NULL )
11473 {
11474 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11475 mbedtls_free( handshake->ecrs_peer_cert );
11476 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011477#endif
11478
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011479#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11480 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11481 mbedtls_pk_free( &handshake->peer_pubkey );
11482#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011484#if defined(MBEDTLS_SSL_PROTO_DTLS)
11485 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011486 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011487 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011488#endif
11489
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011490 mbedtls_platform_zeroize( handshake,
11491 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011492}
11493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011494void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011495{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011496 if( session == NULL )
11497 return;
11498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011499#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011500 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011501#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011502
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011503#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011504 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011505#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011506
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011507 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011508}
11509
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011510#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011511
11512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11513#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11514#else
11515#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11516#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11517
11518#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11519#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11520#else
11521#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11522#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11523
11524#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11525#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11526#else
11527#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11528#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11529
11530#if defined(MBEDTLS_SSL_ALPN)
11531#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11532#else
11533#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11534#endif /* MBEDTLS_SSL_ALPN */
11535
11536#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11537#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11538#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11539#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11540
11541#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11542 ( (uint32_t) ( \
11543 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11544 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11545 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11546 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11547 0u ) )
11548
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011549static unsigned char ssl_serialized_context_header[] = {
11550 MBEDTLS_VERSION_MAJOR,
11551 MBEDTLS_VERSION_MINOR,
11552 MBEDTLS_VERSION_PATCH,
11553 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11554 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011555 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11556 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11557 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011558};
11559
Paul Bakker5121ce52009-01-03 21:22:43 +000011560/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011561 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011562 *
11563 * The format of the serialized data is:
11564 * (in the presentation language of TLS, RFC 8446 section 3)
11565 *
11566 * // header
11567 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011568 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011569 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011570 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011571 * Note: When updating the format, remember to keep these
11572 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011573 *
11574 * // session sub-structure
11575 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11576 * // transform sub-structure
11577 * uint8 random[64]; // ServerHello.random+ClientHello.random
11578 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11579 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11580 * // fields from ssl_context
11581 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11582 * uint64 in_window_top; // DTLS: last validated record seq_num
11583 * uint64 in_window; // DTLS: bitmask for replay protection
11584 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11585 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11586 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11587 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11588 *
11589 * Note that many fields of the ssl_context or sub-structures are not
11590 * serialized, as they fall in one of the following categories:
11591 *
11592 * 1. forced value (eg in_left must be 0)
11593 * 2. pointer to dynamically-allocated memory (eg session, transform)
11594 * 3. value can be re-derived from other data (eg session keys from MS)
11595 * 4. value was temporary (eg content of input buffer)
11596 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011597 */
11598int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11599 unsigned char *buf,
11600 size_t buf_len,
11601 size_t *olen )
11602{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011603 unsigned char *p = buf;
11604 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011605 size_t session_len;
11606 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011607
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011608 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011609 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11610 * this function's documentation.
11611 *
11612 * These are due to assumptions/limitations in the implementation. Some of
11613 * them are likely to stay (no handshake in progress) some might go away
11614 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011615 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011616 /* The initial handshake must be over */
11617 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011619 if( ssl->handshake != NULL )
11620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11621 /* Double-check that sub-structures are indeed ready */
11622 if( ssl->transform == NULL || ssl->session == NULL )
11623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11624 /* There must be no pending incoming or outgoing data */
11625 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11627 if( ssl->out_left != 0 )
11628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11629 /* Protocol must be DLTS, not TLS */
11630 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11632 /* Version must be 1.2 */
11633 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11635 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11637 /* We must be using an AEAD ciphersuite */
11638 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11640 /* Renegotiation must not be enabled */
11641 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011643
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011644 /*
11645 * Version and format identifier
11646 */
11647 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011648
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011649 if( used <= buf_len )
11650 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011651 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011652 sizeof( ssl_serialized_context_header ) );
11653 p += sizeof( ssl_serialized_context_header );
11654 }
11655
11656 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011657 * Session (length + data)
11658 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011659 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011660 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11661 return( ret );
11662
11663 used += 4 + session_len;
11664 if( used <= buf_len )
11665 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011666 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011667
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011668 ret = ssl_session_save( ssl->session, 1,
11669 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011670 if( ret != 0 )
11671 return( ret );
11672
11673 p += session_len;
11674 }
11675
11676 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011677 * Transform
11678 */
11679 used += sizeof( ssl->transform->randbytes );
11680 if( used <= buf_len )
11681 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011682 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011683 sizeof( ssl->transform->randbytes ) );
11684 p += sizeof( ssl->transform->randbytes );
11685 }
11686
11687#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11688 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11689 if( used <= buf_len )
11690 {
11691 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011692 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11693 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011694 p += ssl->transform->in_cid_len;
11695
11696 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011697 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11698 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011699 p += ssl->transform->out_cid_len;
11700 }
11701#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11702
11703 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011704 * Saved fields from top-level ssl_context structure
11705 */
11706#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11707 used += 4;
11708 if( used <= buf_len )
11709 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011710 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011711 }
11712#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11713
11714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11715 used += 16;
11716 if( used <= buf_len )
11717 {
11718 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11719 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11720 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11721 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11722 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11723 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11724 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11725 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11726
11727 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11728 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11729 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11730 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11731 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11732 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11733 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11734 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11735 }
11736#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11737
11738#if defined(MBEDTLS_SSL_PROTO_DTLS)
11739 used += 1;
11740 if( used <= buf_len )
11741 {
11742 *p++ = ssl->disable_datagram_packing;
11743 }
11744#endif /* MBEDTLS_SSL_PROTO_DTLS */
11745
11746 used += 8;
11747 if( used <= buf_len )
11748 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011749 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011750 p += 8;
11751 }
11752
11753#if defined(MBEDTLS_SSL_PROTO_DTLS)
11754 used += 2;
11755 if( used <= buf_len )
11756 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011757 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011758 }
11759#endif /* MBEDTLS_SSL_PROTO_DTLS */
11760
11761#if defined(MBEDTLS_SSL_ALPN)
11762 {
11763 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011764 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011765 : 0;
11766
11767 used += 1 + alpn_len;
11768 if( used <= buf_len )
11769 {
11770 *p++ = alpn_len;
11771
11772 if( ssl->alpn_chosen != NULL )
11773 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011774 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011775 p += alpn_len;
11776 }
11777 }
11778 }
11779#endif /* MBEDTLS_SSL_ALPN */
11780
11781 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011782 * Done
11783 */
11784 *olen = used;
11785
11786 if( used > buf_len )
11787 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011788
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011789 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11790
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011791 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011792}
11793
11794/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011795 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011796 *
11797 * This internal version is wrapped by a public function that cleans up in
11798 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011799 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011800static int ssl_context_load( mbedtls_ssl_context *ssl,
11801 const unsigned char *buf,
11802 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011803{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011804 const unsigned char *p = buf;
11805 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011806 size_t session_len;
11807 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011808
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011809 /*
11810 * The context should have been freshly setup or reset.
11811 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011812 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011813 * renegotiating, or if the user mistakenly loaded a session first.)
11814 */
11815 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11816 ssl->session != NULL )
11817 {
11818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11819 }
11820
11821 /*
11822 * We can't check that the config matches the initial one, but we can at
11823 * least check it matches the requirements for serializing.
11824 */
11825 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011826 mbedtls_ssl_ver_lt(
11827 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11828 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11829 mbedtls_ssl_ver_gt(
11830 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11831 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11832 mbedtls_ssl_ver_lt(
11833 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11834 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11835 mbedtls_ssl_ver_gt(
11836 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11837 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011838 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011839 {
11840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11841 }
11842
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011843 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11844
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011845 /*
11846 * Check version identifier
11847 */
11848 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11850
Teppo Järvelin650343c2019-10-03 15:36:59 +030011851 // use regular memcmp as header is not that critical
11852 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011853 sizeof( ssl_serialized_context_header ) ) != 0 )
11854 {
11855 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11856 }
11857 p += sizeof( ssl_serialized_context_header );
11858
11859 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011860 * Session
11861 */
11862 if( (size_t)( end - p ) < 4 )
11863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11864
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011865 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011866 p += 4;
11867
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011868 /* This has been allocated by ssl_handshake_init(), called by
11869 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11870 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011871 ssl->session_in = ssl->session;
11872 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011873 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011874
11875 if( (size_t)( end - p ) < session_len )
11876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11877
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011878 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011879 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011880 {
11881 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011882 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011883 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011884
11885 p += session_len;
11886
11887 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011888 * Transform
11889 */
11890
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011891 /* This has been allocated by ssl_handshake_init(), called by
11892 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11893 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011894 ssl->transform_in = ssl->transform;
11895 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011896 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011897
11898 /* Read random bytes and populate structure */
11899 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11901
11902 ret = ssl_populate_transform( ssl->transform,
11903 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11904 ssl->session->master,
11905#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11906#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11907 ssl->session->encrypt_then_mac,
11908#endif
11909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11910 ssl->session->trunc_hmac,
11911#endif
11912#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11913#if defined(MBEDTLS_ZLIB_SUPPORT)
11914 ssl->session->compression,
11915#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011916 p, /* currently pointing to randbytes */
11917 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11918 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11919 ssl );
11920 if( ret != 0 )
11921 return( ret );
11922
11923 p += sizeof( ssl->transform->randbytes );
11924
11925#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11926 /* Read connection IDs and store them */
11927 if( (size_t)( end - p ) < 1 )
11928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11929
11930 ssl->transform->in_cid_len = *p++;
11931
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011932 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11934
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011935 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11936 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011937 p += ssl->transform->in_cid_len;
11938
11939 ssl->transform->out_cid_len = *p++;
11940
11941 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11943
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011944 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11945 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011946 p += ssl->transform->out_cid_len;
11947#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11948
11949 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011950 * Saved fields from top-level ssl_context structure
11951 */
11952#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11953 if( (size_t)( end - p ) < 4 )
11954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11955
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011956 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011957 p += 4;
11958#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11959
11960#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11961 if( (size_t)( end - p ) < 16 )
11962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11963
11964 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11965 ( (uint64_t) p[1] << 48 ) |
11966 ( (uint64_t) p[2] << 40 ) |
11967 ( (uint64_t) p[3] << 32 ) |
11968 ( (uint64_t) p[4] << 24 ) |
11969 ( (uint64_t) p[5] << 16 ) |
11970 ( (uint64_t) p[6] << 8 ) |
11971 ( (uint64_t) p[7] );
11972 p += 8;
11973
11974 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11975 ( (uint64_t) p[1] << 48 ) |
11976 ( (uint64_t) p[2] << 40 ) |
11977 ( (uint64_t) p[3] << 32 ) |
11978 ( (uint64_t) p[4] << 24 ) |
11979 ( (uint64_t) p[5] << 16 ) |
11980 ( (uint64_t) p[6] << 8 ) |
11981 ( (uint64_t) p[7] );
11982 p += 8;
11983#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11984
11985#if defined(MBEDTLS_SSL_PROTO_DTLS)
11986 if( (size_t)( end - p ) < 1 )
11987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11988
11989 ssl->disable_datagram_packing = *p++;
11990#endif /* MBEDTLS_SSL_PROTO_DTLS */
11991
11992 if( (size_t)( end - p ) < 8 )
11993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11994
Teppo Järvelin91d79382019-10-02 09:09:31 +030011995 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011996 p += 8;
11997
11998#if defined(MBEDTLS_SSL_PROTO_DTLS)
11999 if( (size_t)( end - p ) < 2 )
12000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012001 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012002 p += 2;
12003#endif /* MBEDTLS_SSL_PROTO_DTLS */
12004
12005#if defined(MBEDTLS_SSL_ALPN)
12006 {
12007 uint8_t alpn_len;
12008 const char **cur;
12009
12010 if( (size_t)( end - p ) < 1 )
12011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12012
12013 alpn_len = *p++;
12014
12015 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12016 {
12017 /* alpn_chosen should point to an item in the configured list */
12018 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12019 {
12020 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012021 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012022 {
12023 ssl->alpn_chosen = *cur;
12024 break;
12025 }
12026 }
12027 }
12028
12029 /* can only happen on conf mismatch */
12030 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12032
12033 p += alpn_len;
12034 }
12035#endif /* MBEDTLS_SSL_ALPN */
12036
12037 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012038 * Forced fields from top-level ssl_context structure
12039 *
12040 * Most of them already set to the correct value by mbedtls_ssl_init() and
12041 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12042 */
12043 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12044
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012045#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012046 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012047#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12048#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012049 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012050#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012051
Hanno Becker83985822019-08-30 10:42:49 +010012052 /* Adjust pointers for header fields of outgoing records to
12053 * the given transform, accounting for explicit IV and CID. */
12054 ssl_update_out_pointers( ssl, ssl->transform );
12055
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012056#if defined(MBEDTLS_SSL_PROTO_DTLS)
12057 ssl->in_epoch = 1;
12058#endif
12059
12060 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12061 * which we don't want - otherwise we'd end up freeing the wrong transform
12062 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12063 if( ssl->handshake != NULL )
12064 {
12065 mbedtls_ssl_handshake_free( ssl );
12066 mbedtls_free( ssl->handshake );
12067 ssl->handshake = NULL;
12068 }
12069
12070 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012071 * Done - should have consumed entire buffer
12072 */
12073 if( p != end )
12074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012075
12076 return( 0 );
12077}
12078
12079/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012080 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012081 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012082int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012083 const unsigned char *buf,
12084 size_t len )
12085{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012086 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012087
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012088 if( ret != 0 )
12089 mbedtls_ssl_free( context );
12090
12091 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012092}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012093#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012094
12095/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012096 * Free an SSL context
12097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012098void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012099{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012100 if( ssl == NULL )
12101 return;
12102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012104
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012105 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012106 {
Angus Grattond8213d02016-05-25 20:56:48 +100012107 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012108 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012109 }
12110
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012111 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012112 {
Angus Grattond8213d02016-05-25 20:56:48 +100012113 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012114 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012115 }
12116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012117#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012118 if( ssl->compress_buf != NULL )
12119 {
Angus Grattond8213d02016-05-25 20:56:48 +100012120 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012121 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012122 }
12123#endif
12124
Paul Bakker48916f92012-09-16 19:57:18 +000012125 if( ssl->transform )
12126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012127 mbedtls_ssl_transform_free( ssl->transform );
12128 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012129 }
12130
12131 if( ssl->handshake )
12132 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012133 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012134 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12135 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012137 mbedtls_free( ssl->handshake );
12138 mbedtls_free( ssl->transform_negotiate );
12139 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012140 }
12141
Paul Bakkerc0463502013-02-14 11:19:38 +010012142 if( ssl->session )
12143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012144 mbedtls_ssl_session_free( ssl->session );
12145 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012146 }
12147
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012148#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012149 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012150 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012151 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012152 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012153 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012156#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12157 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12160 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012161 }
12162#endif
12163
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012164#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012165 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012166#endif
12167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012169
Paul Bakker86f04f42013-02-14 11:20:09 +010012170 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012171 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012172}
12173
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012174/*
12175 * Initialze mbedtls_ssl_config
12176 */
12177void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12178{
12179 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012180
12181#if !defined(MBEDTLS_SSL_PROTO_TLS)
12182 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12183#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012184}
12185
Simon Butcherc97b6972015-12-27 23:48:17 +000012186#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012187#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012188static int ssl_preset_default_hashes[] = {
12189#if defined(MBEDTLS_SHA512_C)
12190 MBEDTLS_MD_SHA512,
12191 MBEDTLS_MD_SHA384,
12192#endif
12193#if defined(MBEDTLS_SHA256_C)
12194 MBEDTLS_MD_SHA256,
12195 MBEDTLS_MD_SHA224,
12196#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012197#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012198 MBEDTLS_MD_SHA1,
12199#endif
12200 MBEDTLS_MD_NONE
12201};
Simon Butcherc97b6972015-12-27 23:48:17 +000012202#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012203#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012204
Hanno Becker73f4cb12019-06-27 13:51:07 +010012205#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012206static int ssl_preset_suiteb_ciphersuites[] = {
12207 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12208 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12209 0
12210};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012211#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012212
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012213#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012214#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012215static int ssl_preset_suiteb_hashes[] = {
12216 MBEDTLS_MD_SHA256,
12217 MBEDTLS_MD_SHA384,
12218 MBEDTLS_MD_NONE
12219};
12220#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012221#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012222
Hanno Beckerc1096e72019-06-19 12:30:41 +010012223#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012224static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012225#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012226 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012227#endif
12228#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012229 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012230#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012231 MBEDTLS_ECP_DP_NONE
12232};
12233#endif
12234
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012235/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012236 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012237 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012238int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012239 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012240{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012241#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012242 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012243#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012244
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012245 /* Use the functions here so that they are covered in tests,
12246 * but otherwise access member directly for efficiency */
12247 mbedtls_ssl_conf_endpoint( conf, endpoint );
12248 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012249
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012250 /*
12251 * Things that are common to all presets
12252 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012253#if defined(MBEDTLS_SSL_CLI_C)
12254 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12255 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012256#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012257 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012258#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012259#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12260 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12261#endif
12262 }
12263#endif
12264
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012265#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012266 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012267#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012268
12269#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12270 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12271#endif
12272
12273#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012274#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012275 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012276#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12277#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012278 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012279 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012280#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012281#endif
12282
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012283#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12284 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12285#endif
12286
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012287#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012288 conf->f_cookie_write = ssl_cookie_write_dummy;
12289 conf->f_cookie_check = ssl_cookie_check_dummy;
12290#endif
12291
Hanno Becker7f376f42019-06-12 16:20:48 +010012292#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12293 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012294 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12295#endif
12296
Janos Follath088ce432017-04-10 12:42:31 +010012297#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012298#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012299 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012300#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12301#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012302
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012304#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012305 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012306#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12307#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012308 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012309#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12310#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012311
12312#if defined(MBEDTLS_SSL_RENEGOTIATION)
12313 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012314 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12315 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012316#endif
12317
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012318#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12319 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12320 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012321 const unsigned char dhm_p[] =
12322 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12323 const unsigned char dhm_g[] =
12324 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12325
Hanno Beckera90658f2017-10-04 15:29:08 +010012326 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12327 dhm_p, sizeof( dhm_p ),
12328 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012329 {
12330 return( ret );
12331 }
12332 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012333#endif
12334
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012335 /*
12336 * Preset-specific defaults
12337 */
12338 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012339 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012340 /*
12341 * NSA Suite B
12342 */
12343 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012344#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012345 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012346#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12347#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012348 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012349#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12350#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012351 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012352#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12353#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012354 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012355#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012356
Hanno Becker73f4cb12019-06-27 13:51:07 +010012357#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012358 conf->ciphersuite_list[0] =
12359 conf->ciphersuite_list[1] =
12360 conf->ciphersuite_list[2] =
12361 conf->ciphersuite_list[3] =
12362 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012363#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012364
12365#if defined(MBEDTLS_X509_CRT_PARSE_C)
12366 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012367#endif
12368
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012369#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012370#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012371 conf->sig_hashes = ssl_preset_suiteb_hashes;
12372#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012373#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012374
12375#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012376#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012377 conf->curve_list = ssl_preset_suiteb_curves;
12378#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012379#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012380 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012381
12382 /*
12383 * Default
12384 */
12385 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012386#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012387 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12388 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12389 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12390 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012391#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12392#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012393 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12394 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12395 MBEDTLS_SSL_MIN_MINOR_VERSION :
12396 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012398 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012399 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12400#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012401#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12402#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12403 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12404#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12405#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12406 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12407#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012408
Hanno Becker73f4cb12019-06-27 13:51:07 +010012409#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012410 conf->ciphersuite_list[0] =
12411 conf->ciphersuite_list[1] =
12412 conf->ciphersuite_list[2] =
12413 conf->ciphersuite_list[3] =
12414 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012415#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012416
12417#if defined(MBEDTLS_X509_CRT_PARSE_C)
12418 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12419#endif
12420
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012421#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012422#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012423 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012424#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012425#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012426
12427#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012428#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012429 conf->curve_list = mbedtls_ecp_grp_id_list();
12430#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012431#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012432
12433#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12434 conf->dhm_min_bitlen = 1024;
12435#endif
12436 }
12437
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012438 return( 0 );
12439}
12440
12441/*
12442 * Free mbedtls_ssl_config
12443 */
12444void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12445{
12446#if defined(MBEDTLS_DHM_C)
12447 mbedtls_mpi_free( &conf->dhm_P );
12448 mbedtls_mpi_free( &conf->dhm_G );
12449#endif
12450
12451#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12452 if( conf->psk != NULL )
12453 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012454 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012455 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012456 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012457 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012458 }
12459
12460 if( conf->psk_identity != NULL )
12461 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012462 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012463 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012464 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012465 conf->psk_identity_len = 0;
12466 }
12467#endif
12468
12469#if defined(MBEDTLS_X509_CRT_PARSE_C)
12470 ssl_key_cert_free( conf->key_cert );
12471#endif
12472
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012473 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012474}
12475
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012476#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012477 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12478 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012479/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012480 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012482unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012484#if defined(MBEDTLS_RSA_C)
12485 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12486 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012487#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012488#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012489 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12490 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012492 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012493}
12494
Hanno Becker7e5437a2017-04-28 17:15:26 +010012495unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12496{
12497 switch( type ) {
12498 case MBEDTLS_PK_RSA:
12499 return( MBEDTLS_SSL_SIG_RSA );
12500 case MBEDTLS_PK_ECDSA:
12501 case MBEDTLS_PK_ECKEY:
12502 return( MBEDTLS_SSL_SIG_ECDSA );
12503 default:
12504 return( MBEDTLS_SSL_SIG_ANON );
12505 }
12506}
12507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012508mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012509{
12510 switch( sig )
12511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012512#if defined(MBEDTLS_RSA_C)
12513 case MBEDTLS_SSL_SIG_RSA:
12514 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012515#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012516#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012517 case MBEDTLS_SSL_SIG_ECDSA:
12518 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012519#endif
12520 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012521 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012522 }
12523}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012524#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012525
Hanno Becker7e5437a2017-04-28 17:15:26 +010012526#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12527 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12528
12529/* Find an entry in a signature-hash set matching a given hash algorithm. */
12530mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12531 mbedtls_pk_type_t sig_alg )
12532{
12533 switch( sig_alg )
12534 {
12535 case MBEDTLS_PK_RSA:
12536 return( set->rsa );
12537 case MBEDTLS_PK_ECDSA:
12538 return( set->ecdsa );
12539 default:
12540 return( MBEDTLS_MD_NONE );
12541 }
12542}
12543
12544/* Add a signature-hash-pair to a signature-hash set */
12545void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12546 mbedtls_pk_type_t sig_alg,
12547 mbedtls_md_type_t md_alg )
12548{
12549 switch( sig_alg )
12550 {
12551 case MBEDTLS_PK_RSA:
12552 if( set->rsa == MBEDTLS_MD_NONE )
12553 set->rsa = md_alg;
12554 break;
12555
12556 case MBEDTLS_PK_ECDSA:
12557 if( set->ecdsa == MBEDTLS_MD_NONE )
12558 set->ecdsa = md_alg;
12559 break;
12560
12561 default:
12562 break;
12563 }
12564}
12565
12566/* Allow exactly one hash algorithm for each signature. */
12567void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12568 mbedtls_md_type_t md_alg )
12569{
12570 set->rsa = md_alg;
12571 set->ecdsa = md_alg;
12572}
12573
12574#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12575 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12576
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012577/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012578 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012580mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012581{
12582 switch( hash )
12583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012584#if defined(MBEDTLS_MD5_C)
12585 case MBEDTLS_SSL_HASH_MD5:
12586 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012587#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012588#if defined(MBEDTLS_SHA1_C)
12589 case MBEDTLS_SSL_HASH_SHA1:
12590 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012591#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012592#if defined(MBEDTLS_SHA256_C)
12593 case MBEDTLS_SSL_HASH_SHA224:
12594 return( MBEDTLS_MD_SHA224 );
12595 case MBEDTLS_SSL_HASH_SHA256:
12596 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012598#if defined(MBEDTLS_SHA512_C)
12599 case MBEDTLS_SSL_HASH_SHA384:
12600 return( MBEDTLS_MD_SHA384 );
12601 case MBEDTLS_SSL_HASH_SHA512:
12602 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012603#endif
12604 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012605 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012606 }
12607}
12608
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012609/*
12610 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12611 */
12612unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12613{
12614 switch( md )
12615 {
12616#if defined(MBEDTLS_MD5_C)
12617 case MBEDTLS_MD_MD5:
12618 return( MBEDTLS_SSL_HASH_MD5 );
12619#endif
12620#if defined(MBEDTLS_SHA1_C)
12621 case MBEDTLS_MD_SHA1:
12622 return( MBEDTLS_SSL_HASH_SHA1 );
12623#endif
12624#if defined(MBEDTLS_SHA256_C)
12625 case MBEDTLS_MD_SHA224:
12626 return( MBEDTLS_SSL_HASH_SHA224 );
12627 case MBEDTLS_MD_SHA256:
12628 return( MBEDTLS_SSL_HASH_SHA256 );
12629#endif
12630#if defined(MBEDTLS_SHA512_C)
12631 case MBEDTLS_MD_SHA384:
12632 return( MBEDTLS_SSL_HASH_SHA384 );
12633 case MBEDTLS_MD_SHA512:
12634 return( MBEDTLS_SSL_HASH_SHA512 );
12635#endif
12636 default:
12637 return( MBEDTLS_SSL_HASH_NONE );
12638 }
12639}
12640
Hanno Beckeree902df2019-08-23 13:47:47 +010012641#if defined(MBEDTLS_USE_TINYCRYPT)
12642/*
12643 * Check if a curve proposed by the peer is in our list.
12644 * Return 0 if we're willing to use it, -1 otherwise.
12645 */
12646int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12647 mbedtls_uecc_group_id grp_id )
12648{
12649 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12650 if( own_ec_id == grp_id )
12651 return( 0 );
12652 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12653
12654 return( -1 );
12655}
12656#endif /* MBEDTLS_USE_TINYCRYPT */
12657
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012658#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012659/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012660 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012661 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012662 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012663int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12664 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012665{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012666 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12667 if( own_ec_id == grp_id )
12668 return( 0 );
12669 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012670
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012671 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012672}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012673#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012674
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012675#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012676/*
12677 * Check if a hash 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_sig_hash( const mbedtls_ssl_context *ssl,
12681 mbedtls_md_type_t md )
12682{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012683 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12684 if( md_alg == md )
12685 return( 0 );
12686 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012687
12688 return( -1 );
12689}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012690#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012692#if defined(MBEDTLS_X509_CRT_PARSE_C)
12693int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012694 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012695 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012696 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012697{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012698 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012699#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012700 int usage = 0;
12701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012702#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012703 const char *ext_oid;
12704 size_t ext_len;
12705#endif
12706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012707#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12708 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012709 ((void) cert);
12710 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012711 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012712#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012714#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12715 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012716 {
12717 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012718 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012720 case MBEDTLS_KEY_EXCHANGE_RSA:
12721 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012722 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012723 break;
12724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012725 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12726 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12727 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12728 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012729 break;
12730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012731 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12732 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012733 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012734 break;
12735
12736 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012737 case MBEDTLS_KEY_EXCHANGE_NONE:
12738 case MBEDTLS_KEY_EXCHANGE_PSK:
12739 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12740 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012741 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012742 usage = 0;
12743 }
12744 }
12745 else
12746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012747 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12748 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012749 }
12750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012751 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012752 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012753 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012754 ret = -1;
12755 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012756#else
12757 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012758#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012760#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12761 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012763 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12764 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012765 }
12766 else
12767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012768 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12769 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012770 }
12771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012772 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012773 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012774 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012775 ret = -1;
12776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012777#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012778
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012779 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012781#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012782
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012783#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12784 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12785int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12786 unsigned char *output,
12787 unsigned char *data, size_t data_len )
12788{
12789 int ret = 0;
12790 mbedtls_md5_context mbedtls_md5;
12791 mbedtls_sha1_context mbedtls_sha1;
12792
12793 mbedtls_md5_init( &mbedtls_md5 );
12794 mbedtls_sha1_init( &mbedtls_sha1 );
12795
12796 /*
12797 * digitally-signed struct {
12798 * opaque md5_hash[16];
12799 * opaque sha_hash[20];
12800 * };
12801 *
12802 * md5_hash
12803 * MD5(ClientHello.random + ServerHello.random
12804 * + ServerParams);
12805 * sha_hash
12806 * SHA(ClientHello.random + ServerHello.random
12807 * + ServerParams);
12808 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012809 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012812 goto exit;
12813 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012814 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012815 ssl->handshake->randbytes, 64 ) ) != 0 )
12816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012818 goto exit;
12819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012820 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012821 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012823 goto exit;
12824 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012825 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012826 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012828 goto exit;
12829 }
12830
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012831 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012832 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012834 goto exit;
12835 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012836 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012837 ssl->handshake->randbytes, 64 ) ) != 0 )
12838 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012840 goto exit;
12841 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012842 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012843 data_len ) ) != 0 )
12844 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012849 output + 16 ) ) != 0 )
12850 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 goto exit;
12853 }
12854
12855exit:
12856 mbedtls_md5_free( &mbedtls_md5 );
12857 mbedtls_sha1_free( &mbedtls_sha1 );
12858
12859 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012860 mbedtls_ssl_pend_fatal_alert( ssl,
12861 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012862
12863 return( ret );
12864
12865}
12866#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12867 MBEDTLS_SSL_PROTO_TLS1_1 */
12868
12869#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12870 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12871int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012872 unsigned char *hash, size_t *hashlen,
12873 unsigned char *data, size_t data_len,
12874 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012875{
12876 int ret = 0;
12877 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012878 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012879 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012880
12881 mbedtls_md_init( &ctx );
12882
12883 /*
12884 * digitally-signed struct {
12885 * opaque client_random[32];
12886 * opaque server_random[32];
12887 * ServerDHParams params;
12888 * };
12889 */
12890 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12891 {
12892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12893 goto exit;
12894 }
12895 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12896 {
12897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12898 goto exit;
12899 }
12900 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12901 {
12902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12903 goto exit;
12904 }
12905 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12906 {
12907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12908 goto exit;
12909 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012910 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012911 {
12912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12913 goto exit;
12914 }
12915
12916exit:
12917 mbedtls_md_free( &ctx );
12918
12919 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012920 mbedtls_ssl_pend_fatal_alert( ssl,
12921 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012922
12923 return( ret );
12924}
12925#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12926 MBEDTLS_SSL_PROTO_TLS1_2 */
12927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012928#endif /* MBEDTLS_SSL_TLS_C */