blob: 146a8f1e90177bc1be955186d15ca76eecd31cd2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Jarno Lamsaaf60cd72019-12-19 16:45:23 +020051#include "mbedtls/platform.h"
52
Paul Bakker0be444a2013-08-27 21:55:01 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Janos Follath23bdca02016-10-07 14:47:14 +010056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020058#endif
59
Hanno Beckeref982d52019-07-23 15:56:18 +010060#if defined(MBEDTLS_USE_TINYCRYPT)
61static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
62{
Hanno Beckerd089fad2019-07-24 09:05:05 +010063 int ret;
64 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
65 if( ret == 0 )
66 return( (int) size );
67
68 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010069}
Hanno Becker75f12d12019-07-23 16:16:15 +010070
71int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
72 unsigned char **p, unsigned char *end )
73{
74 size_t const secp256r1_uncompressed_point_length =
75 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
76
77 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
78 {
79 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010081 }
82
83 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
84 (*p)[1] != 0x04 )
85 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
87 2 * NUM_ECC_BYTES + 1,
88 0x04,
89 (unsigned) (*p)[0],
90 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010092 }
93
Teppo Järvelin91d79382019-10-02 09:09:31 +030094 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010095
96 *p += secp256r1_uncompressed_point_length;
97 return( 0 );
98}
Hanno Beckeref982d52019-07-23 15:56:18 +010099#endif /* MBEDTLS_USE_TINYCRYPT */
100
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100102static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100103
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100106{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200107#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100108 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100109#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200110
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
112 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
113 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200115#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200118#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100119}
120
Hanno Beckerb82350b2019-07-26 07:24:05 +0100121static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
122{
123 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
124 return;
125
126 mbedtls_ssl_send_alert_message( ssl,
127 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
128 ssl->pending_fatal_alert_msg );
129 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
130}
131
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200132/*
133 * Start a timer.
134 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200137{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100138 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200139 return;
140
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100142 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
143 millisecs / 4,
144 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200145}
146
147/*
148 * Return -1 is timer is expired, 0 if it isn't.
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200151{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100152 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200153 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200154
Hanno Becker0ae6b242019-06-13 16:45:36 +0100155 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200156 {
157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200159 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200160
161 return( 0 );
162}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200163
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100164static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
165 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100166static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100167
Hanno Becker02f26092019-07-03 16:13:00 +0100168#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100169static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
170 unsigned char *buf,
171 size_t len,
172 mbedtls_record *rec );
173
Hanno Becker02f26092019-07-03 16:13:00 +0100174int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
175 unsigned char *buf,
176 size_t buflen )
177{
Hanno Becker03e2db62019-07-12 14:40:00 +0100178 int ret = 0;
179 mbedtls_record rec;
180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
181 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
182
183 /* We don't support record checking in TLS because
184 * (a) there doesn't seem to be a usecase for it, and
185 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
186 * and we'd need to backup the transform here.
187 */
188#if defined(MBEDTLS_SSL_PROTO_TLS)
189 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
190 {
191 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
192 goto exit;
193 }
194 MBEDTLS_SSL_TRANSPORT_ELSE
195#endif /* MBEDTLS_SSL_PROTO_TLS */
196#if defined(MBEDTLS_SSL_PROTO_DTLS)
197 {
198 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
199 if( ret != 0 )
200 {
201 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
202 goto exit;
203 }
204
205 if( ssl->transform_in != NULL )
206 {
207 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
208 if( ret != 0 )
209 {
210 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
211 goto exit;
212 }
213 }
214 }
215#endif /* MBEDTLS_SSL_PROTO_DTLS */
216
217exit:
218 /* On success, we have decrypted the buffer in-place, so make
219 * sure we don't leak any plaintext data. */
220 mbedtls_platform_zeroize( buf, buflen );
221
222 /* For the purpose of this API, treat messages with unexpected CID
223 * as well as such from future epochs as unexpected. */
224 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
225 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
226 {
227 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
228 }
229
230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
231 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100232}
233#endif /* MBEDTLS_SSL_RECORD_CHECKING */
234
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235#define SSL_DONT_FORCE_FLUSH 0
236#define SSL_FORCE_FLUSH 1
237
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100239
Hanno Beckera5a2b082019-05-15 14:03:01 +0100240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100241/* Top-level Connection ID API */
242
Hanno Beckere0200da2019-06-13 09:23:43 +0100243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
244 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
245 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100246int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
247 size_t len,
248 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100249{
250 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
252
Hanno Becker791ec6b2019-05-14 11:45:26 +0100253 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
254 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
255 {
256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
257 }
258
259 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100260 conf->cid_len = len;
261 return( 0 );
262}
Hanno Beckere0200da2019-06-13 09:23:43 +0100263#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
264 !MBEDTLS_SSL_CONF_CID_LEN &&
265 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
266
267#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
268#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
269#endif
270#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
271 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
272#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
273#endif
274
275#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
276 !MBEDTLS_SSL_CONF_CID_LEN &&
277 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100278
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100279int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
280 int enable,
281 unsigned char const *own_cid,
282 size_t own_cid_len )
283{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200284 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
286
Hanno Becker07489862019-04-25 16:01:49 +0100287 ssl->negotiate_cid = enable;
288 if( enable == MBEDTLS_SSL_CID_DISABLED )
289 {
290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
291 return( 0 );
292 }
293 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100294 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100295
Hanno Beckere0200da2019-06-13 09:23:43 +0100296 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100297 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
299 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100300 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
302 }
303
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300304 /* Not using more secure mbedtls_platform_memcpy as cid is public */
305 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100306 /* Truncation is not an issue here because
307 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
308 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100309
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100310 return( 0 );
311}
312
313int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
314 int *enabled,
315 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
316 size_t *peer_cid_len )
317{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100318 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100319
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200320 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100321 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
322 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100324 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100325
Hanno Beckercb063f52019-05-03 12:54:52 +0100326 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
327 * were used, but client and server requested the empty CID.
328 * This is indistinguishable from not using the CID extension
329 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100330 if( ssl->transform_in->in_cid_len == 0 &&
331 ssl->transform_in->out_cid_len == 0 )
332 {
333 return( 0 );
334 }
335
Hanno Becker633d6042019-05-22 16:50:35 +0100336 if( peer_cid_len != NULL )
337 {
338 *peer_cid_len = ssl->transform_in->out_cid_len;
339 if( peer_cid != NULL )
340 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300341 /* Not using more secure mbedtls_platform_memcpy as cid is public */
342 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100343 ssl->transform_in->out_cid_len );
344 }
345 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100346
347 *enabled = MBEDTLS_SSL_CID_ENABLED;
348
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100349 return( 0 );
350}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100352
Hanno Beckerd5847772018-08-28 10:09:23 +0100353/* Forward declarations for functions related to message buffering. */
354static void ssl_buffering_free( mbedtls_ssl_context *ssl );
355static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
356 uint8_t slot );
357static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
358static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
359static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
360static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100361static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
362 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100363static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100364
Hanno Beckera67dee22018-08-22 10:05:20 +0100365static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100366static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100367{
Hanno Becker11682cc2018-08-22 14:41:02 +0100368 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100369
370 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100371 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100372
373 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
374}
375
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
377{
Hanno Becker11682cc2018-08-22 14:41:02 +0100378 size_t const bytes_written = ssl->out_left;
379 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380
381 /* Double-check that the write-index hasn't gone
382 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100383 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100384 {
385 /* Should never happen... */
386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
387 }
388
389 return( (int) ( mtu - bytes_written ) );
390}
391
392static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
393{
394 int ret;
395 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400396 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100397
398#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
399 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
400
401 if( max_len > mfl )
402 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100403
404 /* By the standard (RFC 6066 Sect. 4), the MFL extension
405 * only limits the maximum record payload size, so in theory
406 * we would be allowed to pack multiple records of payload size
407 * MFL into a single datagram. However, this would mean that there's
408 * no way to explicitly communicate MTU restrictions to the peer.
409 *
410 * The following reduction of max_len makes sure that we never
411 * write datagrams larger than MFL + Record Expansion Overhead.
412 */
413 if( max_len <= ssl->out_left )
414 return( 0 );
415
416 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100417#endif
418
419 ret = ssl_get_remaining_space_in_datagram( ssl );
420 if( ret < 0 )
421 return( ret );
422 remaining = (size_t) ret;
423
424 ret = mbedtls_ssl_get_record_expansion( ssl );
425 if( ret < 0 )
426 return( ret );
427 expansion = (size_t) ret;
428
429 if( remaining <= expansion )
430 return( 0 );
431
432 remaining -= expansion;
433 if( remaining >= max_len )
434 remaining = max_len;
435
436 return( (int) remaining );
437}
438
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200439/*
440 * Double the retransmit timeout value, within the allowed range,
441 * returning -1 if the maximum value has already been reached.
442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200444{
445 uint32_t new_timeout;
446
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 if( ssl->handshake->retransmit_timeout >=
448 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
449 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200450 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100451 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200452
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200453 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
454 * in the following way: after the initial transmission and a first
455 * retransmission, back off to a temporary estimated MTU of 508 bytes.
456 * This value is guaranteed to be deliverable (if not guaranteed to be
457 * delivered) of any compliant IPv4 (and IPv6) network, and should work
458 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100459 if( ssl->handshake->retransmit_timeout !=
460 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400461 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200462 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
464 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200465
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200466 new_timeout = 2 * ssl->handshake->retransmit_timeout;
467
468 /* Avoid arithmetic overflow and range overflow */
469 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100470 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200471 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100472 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 }
474
475 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200477 ssl->handshake->retransmit_timeout ) );
478
479 return( 0 );
480}
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200483{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100484 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200486 ssl->handshake->retransmit_timeout ) );
487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200491/*
492 * Convert max_fragment_length codes to length.
493 * RFC 6066 says:
494 * enum{
495 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
496 * } MaxFragmentLength;
497 * and we add 0 -> extension unused
498 */
Angus Grattond8213d02016-05-25 20:56:48 +1000499static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200500{
Angus Grattond8213d02016-05-25 20:56:48 +1000501 switch( mfl )
502 {
503 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300504 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000505 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
506 return 512;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
508 return 1024;
509 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
510 return 2048;
511 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
512 return 4096;
513 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300514 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000515 }
516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200518
Hanno Becker58fccf22019-02-06 14:30:46 +0000519int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
520 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300523 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000526
527#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200528 if( src->peer_cert != NULL )
529 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200530 int ret;
531
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200532 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200533 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200534 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200539 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200542 dst->peer_cert = NULL;
543 return( ret );
544 }
545 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100546#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( src->peer_cert_digest != NULL )
548 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000549 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000550 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000551 if( dst->peer_cert_digest == NULL )
552 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
553
Teppo Järvelin91d79382019-10-02 09:09:31 +0300554 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000555 src->peer_cert_digest_len );
556 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000557 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000558 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200562
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200563#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200564 if( src->ticket != NULL )
565 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200566 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200567 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
Teppo Järvelin91d79382019-10-02 09:09:31 +0300570 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200571 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200572#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200573
574 return( 0 );
575}
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
578int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200579 const unsigned char *key_enc, const unsigned char *key_dec,
580 size_t keylen,
581 const unsigned char *iv_enc, const unsigned char *iv_dec,
582 size_t ivlen,
583 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200584 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
586int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
587int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
588int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
589int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
590#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000591
Paul Bakker5121ce52009-01-03 21:22:43 +0000592/*
593 * Key material generation
594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100596MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200597 const char *label,
598 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000599 unsigned char *dstbuf, size_t dlen )
600{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100601 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000602 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200603 mbedtls_md5_context md5;
604 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000605 unsigned char padding[16];
606 unsigned char sha1sum[20];
607 ((void)label);
608
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200609 mbedtls_md5_init( &md5 );
610 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200611
Paul Bakker5f70b252012-09-13 14:23:06 +0000612 /*
613 * SSLv3:
614 * block =
615 * MD5( secret + SHA1( 'A' + secret + random ) ) +
616 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
617 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
618 * ...
619 */
620 for( i = 0; i < dlen / 16; i++ )
621 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200622 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000623
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100630 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100631 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100632 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100633 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100639 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100640 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100641 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100642 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000643 }
644
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100645exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200646 mbedtls_md5_free( &md5 );
647 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000648
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500649 mbedtls_platform_zeroize( padding, sizeof( padding ) );
650 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100652 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100657MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200658 const char *label,
659 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000660 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000661{
Paul Bakker23986e52011-04-24 08:57:21 +0000662 size_t nb, hs;
663 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200664 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 unsigned char tmp[128];
666 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100667 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669 int ret;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 hs = ( slen + 1 ) / 2;
677 S1 = secret;
678 S2 = secret + slen - hs;
679
680 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300681 mbedtls_platform_memcpy( tmp + 20, label, nb );
682 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 nb += rlen;
684
685 /*
686 * First compute P_md5(secret,label+random)[0..dlen]
687 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100688 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
689 MBEDTLS_MD_INVALID_HANDLE )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100692 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100695 return( ret );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
698 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
699 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 for( i = 0; i < dlen; i += 16 )
702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
705 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_hmac_reset ( &md_ctx );
708 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
709 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
712
713 for( j = 0; j < k; j++ )
714 dstbuf[i + j] = h_i[j];
715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100718
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 /*
720 * XOR out with P_sha1(secret,label+random)[0..dlen]
721 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100722 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
723 MBEDTLS_MD_INVALID_HANDLE )
724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100726 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729 return( ret );
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
732 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 for( i = 0; i < dlen; i += 20 )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
739 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_hmac_reset ( &md_ctx );
742 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
743 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
746
747 for( j = 0; j < k; j++ )
748 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100752
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500753 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
754 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 return( 0 );
757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100761#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
762MBEDTLS_ALWAYS_INLINE static inline
763#else
764static
765#endif
766int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100767 const unsigned char *secret, size_t slen,
768 const char *label,
769 const unsigned char *random, size_t rlen,
770 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771{
772 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100773 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100776 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100778 int ret;
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100782 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
783 MBEDTLS_MD_INVALID_HANDLE )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100786 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789
790 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
793 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200794 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
795 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 nb += rlen;
797
798 /*
799 * Compute P_<hash>(secret, label + random)[0..dlen]
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100802 return( ret );
803
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200804 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
805 return( ret );
806 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
807 return( ret );
808 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
809 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100810
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100811 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200813 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
814 return( ret );
815 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
816 return( ret );
817 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
818 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100819
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200820 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
821 return( ret );
822 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
823 return( ret );
824 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
825 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 for( j = 0; j < k; j++ )
830 dstbuf[i + j] = h_i[j];
831 }
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100834
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200835 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
836 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
838 return( 0 );
839}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100842MBEDTLS_NO_INLINE static int tls_prf_sha256(
843 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100844 const char *label,
845 const unsigned char *random, size_t rlen,
846 unsigned char *dstbuf, size_t dlen )
847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100849 label, random, rlen, dstbuf, dlen ) );
850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100854MBEDTLS_NO_INLINE static int tls_prf_sha384(
855 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200856 const char *label,
857 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858 unsigned char *dstbuf, size_t dlen )
859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100861 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_SHA512_C */
864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000865
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100866/*
867 * Call the appropriate PRF function
868 */
Hanno Becker2793f742019-08-16 14:28:43 +0100869MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100870 mbedtls_md_type_t hash,
871 const unsigned char *secret, size_t slen,
872 const char *label,
873 const unsigned char *random, size_t rlen,
874 unsigned char *dstbuf, size_t dlen )
875{
876#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
877 (void) hash;
878#endif
879
880#if defined(MBEDTLS_SSL_PROTO_SSL3)
881 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
882 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
883 else
884#endif
885#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100886 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100887 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
888 else
889#endif
890#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
891#if defined(MBEDTLS_SHA512_C)
892 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
893 hash == MBEDTLS_MD_SHA384 )
894 {
895 return( tls_prf_sha384( secret, slen, label, random, rlen,
896 dstbuf, dlen ) );
897 }
898 else
899#endif
900#if defined(MBEDTLS_SHA256_C)
901 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
902 {
903 return( tls_prf_sha256( secret, slen, label, random, rlen,
904 dstbuf, dlen ) );
905 }
906#endif
907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
908
909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
910}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200911
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100912#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100913MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100914 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
915{
916 const char *sender;
917 mbedtls_md5_context md5;
918 mbedtls_sha1_context sha1;
919
920 unsigned char padbuf[48];
921 unsigned char md5sum[16];
922 unsigned char sha1sum[20];
923
924 mbedtls_ssl_session *session = ssl->session_negotiate;
925 if( !session )
926 session = ssl->session;
927
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
929
930 mbedtls_md5_init( &md5 );
931 mbedtls_sha1_init( &sha1 );
932
933 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
934 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
935
936 /*
937 * SSLv3:
938 * hash =
939 * MD5( master + pad2 +
940 * MD5( handshake + sender + master + pad1 ) )
941 * + SHA1( master + pad2 +
942 * SHA1( handshake + sender + master + pad1 ) )
943 */
944
945#if !defined(MBEDTLS_MD5_ALT)
946 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
947 md5.state, sizeof( md5.state ) );
948#endif
949
950#if !defined(MBEDTLS_SHA1_ALT)
951 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
952 sha1.state, sizeof( sha1.state ) );
953#endif
954
955 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
956 : "SRVR";
957
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200958 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100959
960 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
961 mbedtls_md5_update_ret( &md5, session->master, 48 );
962 mbedtls_md5_update_ret( &md5, padbuf, 48 );
963 mbedtls_md5_finish_ret( &md5, md5sum );
964
965 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
966 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
967 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
968 mbedtls_sha1_finish_ret( &sha1, sha1sum );
969
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200970 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100971
972 mbedtls_md5_starts_ret( &md5 );
973 mbedtls_md5_update_ret( &md5, session->master, 48 );
974 mbedtls_md5_update_ret( &md5, padbuf, 48 );
975 mbedtls_md5_update_ret( &md5, md5sum, 16 );
976 mbedtls_md5_finish_ret( &md5, buf );
977
978 mbedtls_sha1_starts_ret( &sha1 );
979 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
980 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
981 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
982 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
983
984 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
985
986 mbedtls_md5_free( &md5 );
987 mbedtls_sha1_free( &sha1 );
988
989 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
990 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
991 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
992
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
994}
995#endif /* MBEDTLS_SSL_PROTO_SSL3 */
996
997#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100998MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100999 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1000{
1001 int len = 12;
1002 const char *sender;
1003 mbedtls_md5_context md5;
1004 mbedtls_sha1_context sha1;
1005 unsigned char padbuf[36];
1006
1007 mbedtls_ssl_session *session = ssl->session_negotiate;
1008 if( !session )
1009 session = ssl->session;
1010
1011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1012
1013 mbedtls_md5_init( &md5 );
1014 mbedtls_sha1_init( &sha1 );
1015
1016 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1017 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1018
1019 /*
1020 * TLSv1:
1021 * hash = PRF( master, finished_label,
1022 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1023 */
1024
1025#if !defined(MBEDTLS_MD5_ALT)
1026 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1027 md5.state, sizeof( md5.state ) );
1028#endif
1029
1030#if !defined(MBEDTLS_SHA1_ALT)
1031 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1032 sha1.state, sizeof( sha1.state ) );
1033#endif
1034
1035 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1036 ? "client finished"
1037 : "server finished";
1038
1039 mbedtls_md5_finish_ret( &md5, padbuf );
1040 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1041
1042 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1043 mbedtls_ssl_suite_get_mac(
1044 mbedtls_ssl_ciphersuite_from_id(
1045 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1046 session->master, 48, sender,
1047 padbuf, 36, buf, len );
1048
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1050
1051 mbedtls_md5_free( &md5 );
1052 mbedtls_sha1_free( &sha1 );
1053
1054 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1055
1056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1057}
1058#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1059
1060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1061#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001062MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001063 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1064{
1065 int len = 12;
1066 const char *sender;
1067 mbedtls_sha256_context sha256;
1068 unsigned char padbuf[32];
1069
1070 mbedtls_ssl_session *session = ssl->session_negotiate;
1071 if( !session )
1072 session = ssl->session;
1073
1074 mbedtls_sha256_init( &sha256 );
1075
1076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1077
1078 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1079
1080 /*
1081 * TLSv1.2:
1082 * hash = PRF( master, finished_label,
1083 * Hash( handshake ) )[0.11]
1084 */
1085
1086#if !defined(MBEDTLS_SHA256_ALT)
1087 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1088 sha256.state, sizeof( sha256.state ) );
1089#endif
1090
1091 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1092 ? "client finished"
1093 : "server finished";
1094
1095 mbedtls_sha256_finish_ret( &sha256, padbuf );
1096
1097 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1098 mbedtls_ssl_suite_get_mac(
1099 mbedtls_ssl_ciphersuite_from_id(
1100 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1101 session->master, 48, sender,
1102 padbuf, 32, buf, len );
1103
1104 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1105
1106 mbedtls_sha256_free( &sha256 );
1107
1108 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1109
1110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1111}
1112#endif /* MBEDTLS_SHA256_C */
1113
1114#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001115MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1117{
1118 int len = 12;
1119 const char *sender;
1120 mbedtls_sha512_context sha512;
1121 unsigned char padbuf[48];
1122
1123 mbedtls_ssl_session *session = ssl->session_negotiate;
1124 if( !session )
1125 session = ssl->session;
1126
1127 mbedtls_sha512_init( &sha512 );
1128
1129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1130
1131 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1132
1133 /*
1134 * TLSv1.2:
1135 * hash = PRF( master, finished_label,
1136 * Hash( handshake ) )[0.11]
1137 */
1138
1139#if !defined(MBEDTLS_SHA512_ALT)
1140 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1141 sha512.state, sizeof( sha512.state ) );
1142#endif
1143
1144 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1145 ? "client finished"
1146 : "server finished";
1147
1148 mbedtls_sha512_finish_ret( &sha512, padbuf );
1149
1150 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1151 mbedtls_ssl_suite_get_mac(
1152 mbedtls_ssl_ciphersuite_from_id(
1153 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1154 session->master, 48, sender,
1155 padbuf, 48, buf, len );
1156
1157 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1158
1159 mbedtls_sha512_free( &sha512 );
1160
1161 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1162
1163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1164}
1165#endif /* MBEDTLS_SHA512_C */
1166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1167
Hanno Becker2793f742019-08-16 14:28:43 +01001168MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1169 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001170 mbedtls_md_type_t hash,
1171 mbedtls_ssl_context *ssl,
1172 unsigned char *buf,
1173 int from )
1174{
1175#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1176 (void) hash;
1177#endif
1178
1179#if defined(MBEDTLS_SSL_PROTO_SSL3)
1180 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1181 ssl_calc_finished_ssl( ssl, buf, from );
1182 else
1183#endif
1184#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001185 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001186 ssl_calc_finished_tls( ssl, buf, from );
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1190#if defined(MBEDTLS_SHA512_C)
1191 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1192 hash == MBEDTLS_MD_SHA384 )
1193 {
1194 ssl_calc_finished_tls_sha384( ssl, buf, from );
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SHA256_C)
1199 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1200 ssl_calc_finished_tls_sha256( ssl, buf, from );
1201 else
1202#endif
1203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1205
1206 return( 0 );
1207}
1208
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001209/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001210 * Populate a transform structure with session keys and all the other
1211 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001212 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001213 * Parameters:
1214 * - [in/out]: transform: structure to populate
1215 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001216 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001217 * - [in] ciphersuite
1218 * - [in] master
1219 * - [in] encrypt_then_mac
1220 * - [in] trunc_hmac
1221 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001222 * - [in] tls_prf: pointer to PRF to use for key derivation
1223 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001224 * - [in] minor_ver: SSL/TLS minor version
1225 * - [in] endpoint: client or server
1226 * - [in] ssl: optionally used for:
1227 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1228 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1229 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001230 */
Hanno Becker298a4702019-08-16 10:21:32 +01001231/* Force compilers to inline this function if it's used only
1232 * from one place, because at least ARMC5 doesn't do that
1233 * automatically. */
1234#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1235MBEDTLS_ALWAYS_INLINE static inline
1236#else
1237static
1238#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1239int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001240 int ciphersuite,
1241 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001242#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001243#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1244 int encrypt_then_mac,
1245#endif
1246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1247 int trunc_hmac,
1248#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001249#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001250#if defined(MBEDTLS_ZLIB_SUPPORT)
1251 int compression,
1252#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001253 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001254 int minor_ver,
1255 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001256 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001258 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 unsigned char keyblk[256];
1260 unsigned char *key1;
1261 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001262 unsigned char *mac_enc;
1263 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001265 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001266 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001267 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001269 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001270
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001271#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1272 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1273 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001274 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001275 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001276#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001277
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001278 /*
1279 * Some data just needs copying into the structure
1280 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001281#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1282 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001283 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001284#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001285
1286#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001287 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001288#else
1289 ((void) minor_ver);
1290#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001292#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001293 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001294#endif
1295
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001296 /*
1297 * Get various info structures
1298 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001299 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001300 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001301 {
1302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001303 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1305 }
1306
Hanno Becker473f98f2019-06-26 10:27:32 +01001307 cipher_info = mbedtls_cipher_info_from_type(
1308 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001309 if( cipher_info == NULL )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001312 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
1315
Hanno Becker473f98f2019-06-26 10:27:32 +01001316 md_info = mbedtls_md_info_from_type(
1317 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001318 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001321 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001323 }
1324
Hanno Beckera5a2b082019-05-15 14:03:01 +01001325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001326 /* Copy own and peer's CID if the use of the CID
1327 * extension has been negotiated. */
1328 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001331
Hanno Becker4932f9f2019-05-03 15:23:51 +01001332 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001333 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1334 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001335 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001336 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001337
1338 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001339 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1340 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001341 ssl->handshake->peer_cid_len );
1342 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1343 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001344 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001346
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001348 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001349 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001350 ret = ssl_prf( minor_ver,
1351 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1352 master, 48, "key expansion", randbytes, 64,
1353 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001354 if( ret != 0 )
1355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001357 return( ret );
1358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001361 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001363 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 /*
1367 * Determine the appropriate key, IV and MAC length.
1368 */
Paul Bakker68884e32013-01-07 18:20:04 +01001369
Hanno Beckere7f2df02017-12-27 08:17:40 +00001370 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001371
Hanno Beckerf1229442018-01-03 15:32:31 +00001372#if defined(MBEDTLS_GCM_C) || \
1373 defined(MBEDTLS_CCM_C) || \
1374 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001376 cipher_info->mode == MBEDTLS_MODE_CCM ||
1377 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001379 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001381 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1382 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001383
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001384 /* All modes haves 96-bit IVs;
1385 * GCM and CCM has 4 implicit and 8 explicit bytes
1386 * ChachaPoly has all 12 bytes implicit
1387 */
Paul Bakker68884e32013-01-07 18:20:04 +01001388 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001389 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1390 transform->fixed_ivlen = 12;
1391 else
1392 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001393 }
1394 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001395#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1396#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1397 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1398 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001399 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001400 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1402 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001405 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001408 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001409 mac_key_len = mbedtls_md_get_size( md_info );
1410 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001413 /*
1414 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1415 * (rfc 6066 page 13 or rfc 2104 section 4),
1416 * so we only need to adjust the length here.
1417 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001418 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001421
1422#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1423 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001424 * HMAC implementation which also truncates the key
1425 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001426 mac_key_len = transform->maclen;
1427#endif
1428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001430
1431 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001432 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001434 else
1435#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1436 {
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Hanno Beckera9d5c452019-07-25 16:47:12 +01001441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001443 (unsigned) transform->ivlen,
1444 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
1446 /*
1447 * Finally setup the cipher contexts, IVs and MAC secrets.
1448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001450 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001452 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001453 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Paul Bakker68884e32013-01-07 18:20:04 +01001455 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001458 /*
1459 * This is not used in TLS v1.1.
1460 */
Paul Bakker48916f92012-09-16 19:57:18 +00001461 iv_copy_len = ( transform->fixed_ivlen ) ?
1462 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001463 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1464 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001465 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 }
1467 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_SSL_CLI_C */
1469#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001470 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001472 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001473 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Hanno Becker81c7b182017-11-09 18:39:33 +00001475 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001476 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 /*
1479 * This is not used in TLS v1.1.
1480 */
Paul Bakker48916f92012-09-16 19:57:18 +00001481 iv_copy_len = ( transform->fixed_ivlen ) ?
1482 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001483 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1484 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1491 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Hanno Becker92231322018-01-03 15:32:51 +00001494#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001496 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001497 {
Hanno Becker92231322018-01-03 15:32:51 +00001498 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001502 }
1503
Teppo Järvelin91d79382019-10-02 09:09:31 +03001504 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1505 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001506 }
1507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001511 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001512 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001513 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1514 For AEAD-based ciphersuites, there is nothing to do here. */
1515 if( mac_key_len != 0 )
1516 {
1517 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1518 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1519 }
Paul Bakker68884e32013-01-07 18:20:04 +01001520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521 else
1522#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001526 }
Hanno Becker92231322018-01-03 15:32:51 +00001527#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1530 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001531 {
1532 int ret = 0;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001535
Hanno Beckere7f2df02017-12-27 08:17:40 +00001536 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001537 transform->iv_enc, transform->iv_dec,
1538 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001539 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001540 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1543 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001544 }
1545 }
Hanno Becker92231322018-01-03 15:32:51 +00001546#else
1547 ((void) mac_dec);
1548 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001550
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001551#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1552 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001553 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001554 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001555 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001556 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001557 iv_copy_len );
1558 }
1559#endif
1560
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001561 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 return( ret );
1566 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001567
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001568 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 cipher_info ) ) != 0 )
1570 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001571 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572 return( ret );
1573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001576 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001580 return( ret );
1581 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001584 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 return( ret );
1589 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_CIPHER_MODE_CBC)
1592 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1595 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001599 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1602 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001605 return( ret );
1606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001610 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001612 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001614 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001618 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1619 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001621 if( deflateInit( &transform->ctx_deflate,
1622 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001623 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1626 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001627 }
1628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001630
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 return( 0 );
1632}
1633
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001634#if defined(MBEDTLS_SSL_PROTO_SSL3)
1635static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1636 unsigned char hash[36],
1637 size_t *hlen )
1638{
1639 mbedtls_md5_context md5;
1640 mbedtls_sha1_context sha1;
1641 unsigned char pad_1[48];
1642 unsigned char pad_2[48];
1643
1644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1645
1646 mbedtls_md5_init( &md5 );
1647 mbedtls_sha1_init( &sha1 );
1648
1649 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1650 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1651
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001652 mbedtls_platform_memset( pad_1, 0x36, 48 );
1653 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001654
1655 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1656 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1657 mbedtls_md5_finish_ret( &md5, hash );
1658
1659 mbedtls_md5_starts_ret( &md5 );
1660 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1661 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1662 mbedtls_md5_update_ret( &md5, hash, 16 );
1663 mbedtls_md5_finish_ret( &md5, hash );
1664
1665 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1666 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1667 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1668
1669 mbedtls_sha1_starts_ret( &sha1 );
1670 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1671 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1672 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1673 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1674
1675 *hlen = 36;
1676
1677 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1679
1680 mbedtls_md5_free( &md5 );
1681 mbedtls_sha1_free( &sha1 );
1682
1683 return;
1684}
1685#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1686
1687#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1688static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1689 unsigned char hash[36],
1690 size_t *hlen )
1691{
1692 mbedtls_md5_context md5;
1693 mbedtls_sha1_context sha1;
1694
1695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1696
1697 mbedtls_md5_init( &md5 );
1698 mbedtls_sha1_init( &sha1 );
1699
1700 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1701 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1702
1703 mbedtls_md5_finish_ret( &md5, hash );
1704 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1705
1706 *hlen = 36;
1707
1708 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1710
1711 mbedtls_md5_free( &md5 );
1712 mbedtls_sha1_free( &sha1 );
1713
1714 return;
1715}
1716#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1717
1718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1719#if defined(MBEDTLS_SHA256_C)
1720static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1721 unsigned char hash[32],
1722 size_t *hlen )
1723{
1724 mbedtls_sha256_context sha256;
1725
1726 mbedtls_sha256_init( &sha256 );
1727
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1729
1730 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1731 mbedtls_sha256_finish_ret( &sha256, hash );
1732
1733 *hlen = 32;
1734
1735 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1737
1738 mbedtls_sha256_free( &sha256 );
1739
1740 return;
1741}
1742#endif /* MBEDTLS_SHA256_C */
1743
1744#if defined(MBEDTLS_SHA512_C)
1745static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1746 unsigned char hash[48],
1747 size_t *hlen )
1748{
1749 mbedtls_sha512_context sha512;
1750
1751 mbedtls_sha512_init( &sha512 );
1752
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1754
1755 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1756 mbedtls_sha512_finish_ret( &sha512, hash );
1757
1758 *hlen = 48;
1759
1760 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1762
1763 mbedtls_sha512_free( &sha512 );
1764
1765 return;
1766}
1767#endif /* MBEDTLS_SHA512_C */
1768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1769
Hanno Becker2f41b242019-08-15 17:29:43 +01001770int mbedtls_ssl_calc_verify( int minor_ver,
1771 mbedtls_md_type_t hash,
1772 mbedtls_ssl_context const *ssl,
1773 unsigned char *dst,
1774 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001775{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1777 (void) hash;
1778#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001780#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001781 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001782 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001783 else
1784#endif
1785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001786 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001787 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001788 else
1789#endif
1790#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1791#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001792 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1793 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001794 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001795 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001796 }
1797 else
1798#endif
1799#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001800 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001801 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001802 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001803 }
1804 else
1805#endif
1806#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1807 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1809 }
1810
1811 return( 0 );
1812}
1813
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001814/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001816 *
1817 * Parameters:
1818 * [in/out] handshake
1819 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1820 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001821 * [out] master
1822 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001823 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001824static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001825 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001826 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001827{
1828 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001829
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001830/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1831/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1832/* (void) ssl; */
1833/* #endif */
1834
1835 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1836 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001837
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001838#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001839 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001840 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1842 return( 0 );
1843 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001844#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001845
1846 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1847 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001848
1849#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001850 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1851 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001852 {
1853 unsigned char session_hash[48];
1854 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001855
Hanno Becker2f41b242019-08-15 17:29:43 +01001856 mbedtls_ssl_calc_verify(
1857 mbedtls_ssl_get_minor_ver( ssl ),
1858 mbedtls_ssl_suite_get_mac( ciphersuite ),
1859 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001860
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001861 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1862 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001863
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001864 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1865 mbedtls_ssl_suite_get_mac( ciphersuite ),
1866 handshake->premaster, handshake->pmslen,
1867 "extended master secret",
1868 session_hash, hash_len,
1869 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001870 }
1871 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001872#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001873 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001874 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1875 mbedtls_ssl_suite_get_mac( ciphersuite ),
1876 handshake->premaster, handshake->pmslen,
1877 "master secret",
1878 handshake->randbytes, 64,
1879 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001880 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001881 if( ret != 0 )
1882 {
1883 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1884 return( ret );
1885 }
1886
1887 mbedtls_platform_zeroize( handshake->premaster,
1888 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001889
1890 return( 0 );
1891}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001892
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001893int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1894{
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02001895 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001896
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001898 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001899 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001900 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001901 ssl->session_negotiate->master,
1902 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001903 if( ret != 0 )
1904 {
1905 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1906 return( ret );
1907 }
1908
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001909 /* Swap the client and server random values:
1910 * - MS derivation wanted client+server (RFC 5246 8.1)
1911 * - key derivation wants server+client (RFC 5246 6.3) */
1912 {
1913 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001914 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1915 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1916 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001917 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1918 }
1919
1920 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001921 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001922 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1923 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001924#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001926 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001927#endif
1928#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001929 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001930#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001931#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001932#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001933 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001934#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001935 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001936 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001937 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1938 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001939 if( ret == 0 )
1940 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001941 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001942 if( ret == 0 )
1943 {
1944 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1945 }
1946 else
1947 {
1948 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1949 }
1950 }
1951 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001952 {
1953 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1954 return( ret );
1955 }
1956
1957 /* We no longer need Server/ClientHello.random values */
1958 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1959 sizeof( ssl->handshake->randbytes ) );
1960
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001961 /* Allocate compression buffer */
1962#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001963 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001964 ssl->compress_buf == NULL )
1965 {
1966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1967 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1968 if( ssl->compress_buf == NULL )
1969 {
1970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001971 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001972 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1973 }
1974 }
1975#endif
1976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1978
1979 return( 0 );
1980}
1981
Hanno Becker09d23642019-07-22 17:18:18 +01001982int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1983{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001984 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001985
1986 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1987 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001988#if defined(MBEDTLS_USE_TINYCRYPT)
1989 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001990 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001991 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001992 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1993 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1994 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1995 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1996 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001997 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001998 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1999 ssl->handshake->ecdh_privkey,
2000 ssl->handshake->premaster );
2001 if( ret == UECC_FAULT_DETECTED )
2002 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2003 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002004 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002005 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002006
2007 ssl->handshake->pmslen = NUM_ECC_BYTES;
2008 }
2009 else
2010#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002011#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2012 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2013 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2014 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002015 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002016 ssl->handshake->premaster,
2017 MBEDTLS_PREMASTER_SIZE,
2018 &ssl->handshake->pmslen,
2019 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002020 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2021 if( ret == 0 )
2022 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002023 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002024 if( ret == 0 )
2025 {
2026 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2027 }
2028 else
2029 {
2030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2031 return( ret );
2032 }
2033 }
2034 else
Hanno Becker09d23642019-07-22 17:18:18 +01002035 {
2036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2037 return( ret );
2038 }
2039
2040 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2041 }
2042 else
2043#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002044#if defined(MBEDTLS_ECDH_C) && \
2045 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2046 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2047 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2048 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2050 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2051 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2052 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2054 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2055 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2056 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2057 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002058 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002059 &ssl->handshake->pmslen,
2060 ssl->handshake->premaster,
2061 MBEDTLS_MPI_MAX_SIZE,
2062 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002063 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2064 if( ret == 0 )
2065 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002066 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002067 if( ret == 0 )
2068 {
2069 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2070 }
2071 else
2072 {
2073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002074 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002075 }
2076 }
2077 else
Hanno Becker09d23642019-07-22 17:18:18 +01002078 {
2079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2080 return( ret );
2081 }
2082
2083 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2084 }
2085 else
2086#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2087 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2088 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2089 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2090#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2091 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2092 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002093 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2094 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2095 if( ret == 0 )
2096 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002097 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002098 if( ret == 0 )
2099 {
2100 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2101 }
2102 else
2103 {
2104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002105 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002106 }
2107 }
2108 else
Hanno Becker09d23642019-07-22 17:18:18 +01002109 {
2110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2111 return( ret );
2112 }
2113 }
2114 else
2115#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2116#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2117 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2118 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2119 {
2120 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2121 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2122 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002123 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002124 if( ret == 0 )
2125 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002126 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002127 if( ret == 0 )
2128 {
2129 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2130 }
2131 else
2132 {
2133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002134 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002135 }
2136 }
2137 else
Hanno Becker09d23642019-07-22 17:18:18 +01002138 {
2139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2140 return( ret );
2141 }
2142 }
2143 else
2144#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2145#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2146 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2147 == MBEDTLS_KEY_EXCHANGE_RSA )
2148 {
2149 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002150 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002151 * ssl_rsa_generate_partial_pms(). Only the
2152 * PMS length needs to be set. */
2153 ssl->handshake->pmslen = 48;
2154 }
2155 else
2156#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2157 {
2158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2160 }
2161
2162 return( 0 );
2163}
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2166int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002167{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002168 unsigned char *p = ssl->handshake->premaster;
2169 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002170 const unsigned char *psk = ssl->conf->psk;
2171 size_t psk_len = ssl->conf->psk_len;
2172
2173 /* If the psk callback was called, use its result */
2174 if( ssl->handshake->psk != NULL )
2175 {
2176 psk = ssl->handshake->psk;
2177 psk_len = ssl->handshake->psk_len;
2178 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179
2180 /*
2181 * PMS = struct {
2182 * opaque other_secret<0..2^16-1>;
2183 * opaque psk<0..2^16-1>;
2184 * };
2185 * with "other_secret" depending on the particular key exchange
2186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2188 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002189 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002190 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002193 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002194
2195 if( end < p || (size_t)( end - p ) < psk_len )
2196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2197
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002198 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002199 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002200 }
2201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2203#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2204 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002205 {
2206 /*
2207 * other_secret already set by the ClientKeyExchange message,
2208 * and is 48 bytes long
2209 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002210 if( end - p < 2 )
2211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2212
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002213 *p++ = 0;
2214 *p++ = 48;
2215 p += 48;
2216 }
2217 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2219#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2220 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002222 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002223 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002224
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002225 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002227 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002228 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002229 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002232 return( ret );
2233 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002234 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002235 p += len;
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002238 }
2239 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2241#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2242 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002243 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002244 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002245 size_t zlen;
2246
Hanno Becker982da7e2019-09-02 09:47:39 +01002247#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002248 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2249 ssl->handshake->ecdh_privkey,
2250 p + 2 );
2251 if( ret == UECC_FAULT_DETECTED )
2252 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2253 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002254 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002255
2256 zlen = NUM_ECC_BYTES;
2257#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002259 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002260 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002261 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 return( ret );
2265 }
2266
Hanno Becker982da7e2019-09-02 09:47:39 +01002267 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2268 MBEDTLS_DEBUG_ECDH_Z );
2269#endif /* MBEDTLS_USE_TINYCRYPT */
2270
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002271 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002272 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002273 }
2274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002279 }
2280
2281 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002282 if( end - p < 2 )
2283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002284
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002285 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002286
2287 if( end < p || (size_t)( end - p ) < psk_len )
2288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2289
Teppo Järvelin91d79382019-10-02 09:09:31 +03002290 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002291 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002292
2293 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2294
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002295 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2296
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002297 return( 0 );
2298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002302/*
2303 * SSLv3.0 MAC functions
2304 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002305#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002306static void ssl_mac( mbedtls_md_context_t *md_ctx,
2307 const unsigned char *secret,
2308 const unsigned char *buf, size_t len,
2309 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002310 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002311{
2312 unsigned char header[11];
2313 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002314 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2316 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002317
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002318 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002320 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002321 else
Paul Bakker68884e32013-01-07 18:20:04 +01002322 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Teppo Järvelin91d79382019-10-02 09:09:31 +03002324 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002325 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002326 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002328 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 mbedtls_md_starts( md_ctx );
2330 mbedtls_md_update( md_ctx, secret, md_size );
2331 mbedtls_md_update( md_ctx, padding, padlen );
2332 mbedtls_md_update( md_ctx, header, 11 );
2333 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002334 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002336 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 mbedtls_md_starts( md_ctx );
2338 mbedtls_md_update( md_ctx, secret, md_size );
2339 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002340 mbedtls_md_update( md_ctx, out, md_size );
2341 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002344
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002345/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002346 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002347#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002348 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2349 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2350 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2351/* This function makes sure every byte in the memory region is accessed
2352 * (in ascending addresses order) */
2353static void ssl_read_memory( unsigned char *p, size_t len )
2354{
2355 unsigned char acc = 0;
2356 volatile unsigned char force;
2357
2358 for( ; len != 0; p++, len-- )
2359 acc ^= *p;
2360
2361 force = acc;
2362 (void) force;
2363}
2364#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2365
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002366/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002368 */
Hanno Becker3307b532017-12-27 21:37:21 +00002369
Hanno Beckera5a2b082019-05-15 14:03:01 +01002370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002371/* This functions transforms a DTLS plaintext fragment and a record content
2372 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002373 *
2374 * struct {
2375 * opaque content[DTLSPlaintext.length];
2376 * ContentType real_type;
2377 * uint8 zeros[length_of_padding];
2378 * } DTLSInnerPlaintext;
2379 *
2380 * Input:
2381 * - `content`: The beginning of the buffer holding the
2382 * plaintext to be wrapped.
2383 * - `*content_size`: The length of the plaintext in Bytes.
2384 * - `max_len`: The number of Bytes available starting from
2385 * `content`. This must be `>= *content_size`.
2386 * - `rec_type`: The desired record content type.
2387 *
2388 * Output:
2389 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2390 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2391 *
2392 * Returns:
2393 * - `0` on success.
2394 * - A negative error code if `max_len` didn't offer enough space
2395 * for the expansion.
2396 */
2397static int ssl_cid_build_inner_plaintext( unsigned char *content,
2398 size_t *content_size,
2399 size_t remaining,
2400 uint8_t rec_type )
2401{
2402 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002403 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2404 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2405 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002406
2407 /* Write real content type */
2408 if( remaining == 0 )
2409 return( -1 );
2410 content[ len ] = rec_type;
2411 len++;
2412 remaining--;
2413
2414 if( remaining < pad )
2415 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002416 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002417 len += pad;
2418 remaining -= pad;
2419
2420 *content_size = len;
2421 return( 0 );
2422}
2423
Hanno Becker7dc25772019-05-20 15:08:01 +01002424/* This function parses a DTLSInnerPlaintext structure.
2425 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002426static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2427 size_t *content_size,
2428 uint8_t *rec_type )
2429{
2430 size_t remaining = *content_size;
2431
2432 /* Determine length of padding by skipping zeroes from the back. */
2433 do
2434 {
2435 if( remaining == 0 )
2436 return( -1 );
2437 remaining--;
2438 } while( content[ remaining ] == 0 );
2439
2440 *content_size = remaining;
2441 *rec_type = content[ remaining ];
2442
2443 return( 0 );
2444}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002446
Hanno Becker99abf512019-05-20 14:50:53 +01002447/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002448 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002449static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002450 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002451 mbedtls_record *rec )
2452{
Hanno Becker99abf512019-05-20 14:50:53 +01002453 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002454 *
2455 * additional_data = seq_num + TLSCompressed.type +
2456 * TLSCompressed.version + TLSCompressed.length;
2457 *
Hanno Becker99abf512019-05-20 14:50:53 +01002458 * For the CID extension, this is extended as follows
2459 * (quoting draft-ietf-tls-dtls-connection-id-05,
2460 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002461 *
2462 * additional_data = seq_num + DTLSPlaintext.type +
2463 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002464 * cid +
2465 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002466 * length_of_DTLSInnerPlaintext;
2467 */
2468
Teppo Järvelin91d79382019-10-02 09:09:31 +03002469 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002470 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002471 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002472
Hanno Beckera5a2b082019-05-15 14:03:01 +01002473#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002474 if( rec->cid_len != 0 )
2475 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002476 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002478 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2479 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002480 *add_data_len = 13 + 1 + rec->cid_len;
2481 }
2482 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002483#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002484 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002485 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002486 *add_data_len = 13;
2487 }
Hanno Becker3307b532017-12-27 21:37:21 +00002488}
2489
Hanno Becker611a83b2018-01-03 14:27:32 +00002490int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2491 mbedtls_ssl_transform *transform,
2492 mbedtls_record *rec,
2493 int (*f_rng)(void *, unsigned char *, size_t),
2494 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002497 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002498 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002499 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002500 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002501 size_t post_avail;
2502
2503 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002504#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002505 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002506 ((void) ssl);
2507#endif
2508
2509 /* The PRNG is used for dynamic IV generation that's used
2510 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2511#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2512 ( defined(MBEDTLS_AES_C) || \
2513 defined(MBEDTLS_ARIA_C) || \
2514 defined(MBEDTLS_CAMELLIA_C) ) && \
2515 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2516 ((void) f_rng);
2517 ((void) p_rng);
2518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Hanno Becker3307b532017-12-27 21:37:21 +00002522 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002523 {
Hanno Becker3307b532017-12-27 21:37:21 +00002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2526 }
Hanno Becker505089d2019-05-01 09:45:57 +01002527 if( rec == NULL
2528 || rec->buf == NULL
2529 || rec->buf_len < rec->data_offset
2530 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002532 || rec->cid_len != 0
2533#endif
2534 )
Hanno Becker3307b532017-12-27 21:37:21 +00002535 {
2536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002538 }
2539
Hanno Becker3307b532017-12-27 21:37:21 +00002540 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002541 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002543 data, rec->data_len );
2544
2545 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2546
2547 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2548 {
2549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2550 (unsigned) rec->data_len,
2551 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2553 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002554
Hanno Beckera5a2b082019-05-15 14:03:01 +01002555#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002556 /*
2557 * Add CID information
2558 */
2559 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002560 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2561 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002562 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002563
2564 if( rec->cid_len != 0 )
2565 {
2566 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002567 * Wrap plaintext into DTLSInnerPlaintext structure.
2568 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002569 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002570 * Note that this changes `rec->data_len`, and hence
2571 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002572 */
2573 if( ssl_cid_build_inner_plaintext( data,
2574 &rec->data_len,
2575 post_avail,
2576 rec->type ) != 0 )
2577 {
2578 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2579 }
2580
2581 rec->type = MBEDTLS_SSL_MSG_CID;
2582 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002584
Hanno Becker92c930f2019-04-29 17:31:37 +01002585 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2586
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002588 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002590#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( mode == MBEDTLS_MODE_STREAM ||
2592 ( mode == MBEDTLS_MODE_CBC
2593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002594 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002595#endif
2596 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Hanno Becker3307b532017-12-27 21:37:21 +00002598 if( post_avail < transform->maclen )
2599 {
2600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2601 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2602 }
2603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002605 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2606 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002607 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002608 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002609 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2610 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002611 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002612 }
2613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2616 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002617 if( mbedtls_ssl_ver_geq(
2618 mbedtls_ssl_transform_get_minor_ver( transform ),
2619 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002620 {
Hanno Becker992b6872017-11-09 18:57:39 +00002621 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2622
Hanno Beckere83efe62019-04-29 13:52:53 +01002623 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002624
Hanno Becker3307b532017-12-27 21:37:21 +00002625 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002626 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002627 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2628 data, rec->data_len );
2629 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2630 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2631
Teppo Järvelin91d79382019-10-02 09:09:31 +03002632 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002633 }
2634 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002635#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2638 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 }
2640
Hanno Becker3307b532017-12-27 21:37:21 +00002641 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2642 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002643
Hanno Becker3307b532017-12-27 21:37:21 +00002644 rec->data_len += transform->maclen;
2645 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002646 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002647 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002648#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002650 /*
2651 * Encrypt
2652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2654 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002656 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002657 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002659 "including %d bytes of padding",
2660 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Hanno Becker3307b532017-12-27 21:37:21 +00002662 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2663 transform->iv_enc, transform->ivlen,
2664 data, rec->data_len,
2665 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002668 return( ret );
2669 }
2670
Hanno Becker3307b532017-12-27 21:37:21 +00002671 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
Paul Bakker68884e32013-01-07 18:20:04 +01002677 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002679
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002680#if defined(MBEDTLS_GCM_C) || \
2681 defined(MBEDTLS_CCM_C) || \
2682 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002684 mode == MBEDTLS_MODE_CCM ||
2685 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002686 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002687 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002688 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002689 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690
Hanno Becker3307b532017-12-27 21:37:21 +00002691 /* Check that there's space for both the authentication tag
2692 * and the explicit IV before and after the record content. */
2693 if( post_avail < transform->taglen ||
2694 rec->data_offset < explicit_iv_len )
2695 {
2696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2697 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2698 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002699
Paul Bakker68884e32013-01-07 18:20:04 +01002700 /*
2701 * Generate IV
2702 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002703 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2704 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002705 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002706 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2707 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002708 explicit_iv_len );
2709 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002710 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002711 }
2712 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002714 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002715 unsigned char i;
2716
Teppo Järvelin91d79382019-10-02 09:09:31 +03002717 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002718
2719 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002720 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721 }
2722 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002723 {
2724 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002727 }
2728
Hanno Beckere83efe62019-04-29 13:52:53 +01002729 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002730
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002731 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2732 iv, transform->ivlen );
2733 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002734 data - explicit_iv_len, explicit_iv_len );
2735 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002736 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002738 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002739 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002740
Paul Bakker68884e32013-01-07 18:20:04 +01002741 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002742 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002743 */
Hanno Becker3307b532017-12-27 21:37:21 +00002744
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002745 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002746 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002747 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002748 data, rec->data_len, /* source */
2749 data, &rec->data_len, /* destination */
2750 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002753 return( ret );
2754 }
2755
Hanno Becker3307b532017-12-27 21:37:21 +00002756 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2757 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002758
Hanno Becker3307b532017-12-27 21:37:21 +00002759 rec->data_len += transform->taglen + explicit_iv_len;
2760 rec->data_offset -= explicit_iv_len;
2761 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002762 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2766#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002767 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002770 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002771 size_t padlen, i;
2772 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002773
Hanno Becker3307b532017-12-27 21:37:21 +00002774 /* Currently we're always using minimal padding
2775 * (up to 255 bytes would be allowed). */
2776 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2777 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 padlen = 0;
2779
Hanno Becker3307b532017-12-27 21:37:21 +00002780 /* Check there's enough space in the buffer for the padding. */
2781 if( post_avail < padlen + 1 )
2782 {
2783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2784 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2785 }
2786
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002788 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Hanno Becker3307b532017-12-27 21:37:21 +00002790 rec->data_len += padlen + 1;
2791 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2796 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002798 if( mbedtls_ssl_ver_geq(
2799 mbedtls_ssl_transform_get_minor_ver( transform ),
2800 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801 {
Hanno Becker3307b532017-12-27 21:37:21 +00002802 if( f_rng == NULL )
2803 {
2804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2806 }
2807
2808 if( rec->data_offset < transform->ivlen )
2809 {
2810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2811 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2812 }
2813
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814 /*
2815 * Generate IV
2816 */
Hanno Becker3307b532017-12-27 21:37:21 +00002817 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002818 if( ret != 0 )
2819 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002820
Teppo Järvelin91d79382019-10-02 09:09:31 +03002821 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002822 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002828 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002829 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002830 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Hanno Becker3307b532017-12-27 21:37:21 +00002832 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2833 transform->iv_enc,
2834 transform->ivlen,
2835 data, rec->data_len,
2836 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002839 return( ret );
2840 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002841
Hanno Becker3307b532017-12-27 21:37:21 +00002842 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002846 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002849 if( mbedtls_ssl_ver_lt(
2850 mbedtls_ssl_transform_get_minor_ver( transform ),
2851 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002852 {
2853 /*
2854 * Save IV in SSL3 and TLS1
2855 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002856 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002857 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 }
Hanno Becker3307b532017-12-27 21:37:21 +00002859 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002860#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002861 {
2862 data -= transform->ivlen;
2863 rec->data_offset -= transform->ivlen;
2864 rec->data_len += transform->ivlen;
2865 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002868 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002869 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002870 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2871
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002872 /*
2873 * MAC(MAC_write_key, seq_num +
2874 * TLSCipherText.type +
2875 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002876 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877 * IV + // except for TLS 1.0
2878 * ENC(content + padding + padding_length));
2879 */
Hanno Becker3307b532017-12-27 21:37:21 +00002880
2881 if( post_avail < transform->maclen)
2882 {
2883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2884 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2885 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886
Hanno Beckere83efe62019-04-29 13:52:53 +01002887 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002890 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002891 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002892
Hanno Becker3307b532017-12-27 21:37:21 +00002893 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002894 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002895 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2896 data, rec->data_len );
2897 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2898 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002899
Teppo Järvelin91d79382019-10-02 09:09:31 +03002900 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002901
Hanno Becker3307b532017-12-27 21:37:21 +00002902 rec->data_len += transform->maclen;
2903 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002904 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002910 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002916 /* Make extra sure authentication was performed, exactly once */
2917 if( auth_done != 1 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002921 }
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
2925 return( 0 );
2926}
2927
Hanno Becker40478be2019-07-12 08:23:59 +01002928int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002929 mbedtls_ssl_transform *transform,
2930 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002931{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002934 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002935#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002936 size_t padlen = 0, correct = 1;
2937#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002938 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002939 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002940 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002941
Hanno Becker611a83b2018-01-03 14:27:32 +00002942#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002943 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002944 ((void) ssl);
2945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 if( rec == NULL ||
2949 rec->buf == NULL ||
2950 rec->buf_len < rec->data_offset ||
2951 rec->buf_len - rec->data_offset < rec->data_len )
2952 {
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955 }
2956
Hanno Becker4c6876b2017-12-27 21:28:58 +00002957 data = rec->buf + rec->data_offset;
2958 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Hanno Beckera5a2b082019-05-15 14:03:01 +01002960#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002961 /*
2962 * Match record's CID with incoming CID.
2963 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002964 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002965 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002966 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002967 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002968 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002969#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2972 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002973 {
2974 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002975 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2976 transform->iv_dec,
2977 transform->ivlen,
2978 data, rec->data_len,
2979 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002982 return( ret );
2983 }
2984
Hanno Becker4c6876b2017-12-27 21:28:58 +00002985 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
Paul Bakker68884e32013-01-07 18:20:04 +01002991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002993#if defined(MBEDTLS_GCM_C) || \
2994 defined(MBEDTLS_CCM_C) || \
2995 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002997 mode == MBEDTLS_MODE_CCM ||
2998 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003000 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003001 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003003 /*
3004 * Prepare IV from explicit and implicit data.
3005 */
3006
3007 /* Check that there's enough space for the explicit IV
3008 * (at the beginning of the record) and the MAC (at the
3009 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003010 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003013 "+ taglen (%d)", rec->data_len,
3014 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003016 }
Paul Bakker68884e32013-01-07 18:20:04 +01003017
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003018#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003019 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3020 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003021 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003022
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003023 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003024 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003025 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003026 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003027 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003028 else
3029#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3030#if defined(MBEDTLS_CHACHAPOLY_C)
3031 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003032 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003033 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 unsigned char i;
3035
Teppo Järvelin91d79382019-10-02 09:09:31 +03003036 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003037
3038 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003039 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003040 }
3041 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003042#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003043 {
3044 /* Reminder if we ever add an AEAD mode with a different size */
3045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3047 }
3048
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003049 /* Group changes to data, data_len, and add_data, because
3050 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003051 data += explicit_iv_len;
3052 rec->data_offset += explicit_iv_len;
3053 rec->data_len -= explicit_iv_len + transform->taglen;
3054
Hanno Beckere83efe62019-04-29 13:52:53 +01003055 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003056 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003057 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003058
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003059 /* Because of the check above, we know that there are
3060 * explicit_iv_len Bytes preceeding data, and taglen
3061 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003062 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003063 * mbedtls_cipher_auth_decrypt() below. */
3064
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003065 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003067 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003068
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003069 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003070 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003071 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003072 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3073 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003074 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 data, rec->data_len,
3076 data, &olen,
3077 data + rec->data_len,
3078 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3083 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003084
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003085 return( ret );
3086 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003087 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003089 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003090 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003094 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3098#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003099 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003102 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003103
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 /*
Paul Bakker45829992013-01-03 14:52:21 +01003105 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003108 if( mbedtls_ssl_ver_geq(
3109 mbedtls_ssl_transform_get_minor_ver( transform ),
3110 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003111 {
3112 /* The ciphertext is prefixed with the CBC IV. */
3113 minlen += transform->ivlen;
3114 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003115#endif
Paul Bakker45829992013-01-03 14:52:21 +01003116
Hanno Becker4c6876b2017-12-27 21:28:58 +00003117 /* Size considerations:
3118 *
3119 * - The CBC cipher text must not be empty and hence
3120 * at least of size transform->ivlen.
3121 *
3122 * Together with the potential IV-prefix, this explains
3123 * the first of the two checks below.
3124 *
3125 * - The record must contain a MAC, either in plain or
3126 * encrypted, depending on whether Encrypt-then-MAC
3127 * is used or not.
3128 * - If it is, the message contains the IV-prefix,
3129 * the CBC ciphertext, and the MAC.
3130 * - If it is not, the padded plaintext, and hence
3131 * the CBC ciphertext, has at least length maclen + 1
3132 * because there is at least the padding length byte.
3133 *
3134 * As the CBC ciphertext is not empty, both cases give the
3135 * lower bound minlen + maclen + 1 on the record size, which
3136 * we test for in the second check below.
3137 */
3138 if( rec->data_len < minlen + transform->ivlen ||
3139 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003142 "+ 1 ) ( + expl IV )", rec->data_len,
3143 transform->ivlen,
3144 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003146 }
3147
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003148 /*
3149 * Authenticate before decrypt if enabled
3150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003152 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003153 {
Hanno Becker992b6872017-11-09 18:57:39 +00003154 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003157
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003158 /* Update data_len in tandem with add_data.
3159 *
3160 * The subtraction is safe because of the previous check
3161 * data_len >= minlen + maclen + 1.
3162 *
3163 * Afterwards, we know that data + data_len is followed by at
3164 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003165 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003166 *
3167 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003168 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003169 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003170
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003171 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003172 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3173 add_data_len );
3174 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3175 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3177 data, rec->data_len );
3178 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3179 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003180
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3182 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003183 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003184 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003186 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003187 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003192 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003193 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196
3197 /*
3198 * Check length sanity
3199 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003200
3201 /* We know from above that data_len > minlen >= 0,
3202 * so the following check in particular implies that
3203 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003204 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003209 }
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003214 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003215 if( mbedtls_ssl_ver_geq(
3216 mbedtls_ssl_transform_get_minor_ver( transform ),
3217 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003218 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003219 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003220 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003221
Hanno Becker4c6876b2017-12-27 21:28:58 +00003222 data += transform->ivlen;
3223 rec->data_offset += transform->ivlen;
3224 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003227
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003228 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3229
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3231 transform->iv_dec, transform->ivlen,
3232 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003235 return( ret );
3236 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003237
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003238 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003243 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003246 if( mbedtls_ssl_ver_lt(
3247 mbedtls_ssl_transform_get_minor_ver( transform ),
3248 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003249 {
3250 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003251 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3252 * records is equivalent to CBC decryption of the concatenation
3253 * of the records; in other words, IVs are maintained across
3254 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003255 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003256 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003257 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
Hanno Becker4c6876b2017-12-27 21:28:58 +00003261 /* Safe since data_len >= minlen + maclen + 1, so after having
3262 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003263 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3264 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003266
Hanno Becker4c6876b2017-12-27 21:28:58 +00003267 if( auth_done == 1 )
3268 {
3269 correct *= ( rec->data_len >= padlen + 1 );
3270 padlen *= ( rec->data_len >= padlen + 1 );
3271 }
3272 else
Paul Bakker45829992013-01-03 14:52:21 +01003273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003275 if( rec->data_len < transform->maclen + padlen + 1 )
3276 {
3277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3278 rec->data_len,
3279 transform->maclen,
3280 padlen + 1 ) );
3281 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003282#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003283
3284 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3285 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 padlen++;
3289
3290 /* Regardless of the validity of the padding,
3291 * we have data_len >= padlen here. */
3292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003294 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3295 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003297 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_SSL_DEBUG_ALL)
3300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003301 "should be no more than %d",
3302 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003303#endif
Paul Bakker45829992013-01-03 14:52:21 +01003304 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
3306 }
3307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3309#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3310 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003311 if( mbedtls_ssl_ver_gt(
3312 mbedtls_ssl_transform_get_minor_ver( transform ),
3313 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003315 /* The padding check involves a series of up to 256
3316 * consecutive memory reads at the end of the record
3317 * plaintext buffer. In order to hide the length and
3318 * validity of the padding, always perform exactly
3319 * `min(256,plaintext_len)` reads (but take into account
3320 * only the last `padlen` bytes for the padding check). */
3321 size_t pad_count = 0;
3322 size_t real_count = 0;
3323 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003324
Hanno Becker4c6876b2017-12-27 21:28:58 +00003325 /* Index of first padding byte; it has been ensured above
3326 * that the subtraction is safe. */
3327 size_t const padding_idx = rec->data_len - padlen;
3328 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3329 size_t const start_idx = rec->data_len - num_checks;
3330 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003331
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003333 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 real_count |= ( idx >= padding_idx );
3335 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003336 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003337 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003340 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003342#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003343 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3347 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003351 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003352
Hanno Becker4c6876b2017-12-27 21:28:58 +00003353 /* If the padding was found to be invalid, padlen == 0
3354 * and the subtraction is safe. If the padding was found valid,
3355 * padlen hasn't been changed and the previous assertion
3356 * data_len >= padlen still holds. */
3357 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003359 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003361 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003367#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003369 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
3372 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003373 * Authenticate if not done yet.
3374 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003376#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003377 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003378 {
Hanno Becker992b6872017-11-09 18:57:39 +00003379 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003380
Hanno Becker4c6876b2017-12-27 21:28:58 +00003381 /* If the initial value of padlen was such that
3382 * data_len < maclen + padlen + 1, then padlen
3383 * got reset to 1, and the initial check
3384 * data_len >= minlen + maclen + 1
3385 * guarantees that at this point we still
3386 * have at least data_len >= maclen.
3387 *
3388 * If the initial value of padlen was such that
3389 * data_len >= maclen + padlen + 1, then we have
3390 * subtracted either padlen + 1 (if the padding was correct)
3391 * or 0 (if the padding was incorrect) since then,
3392 * hence data_len >= maclen in any case.
3393 */
3394 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003395 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003398 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3399 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003400 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003401 ssl_mac( &transform->md_ctx_dec,
3402 transform->mac_dec,
3403 data, rec->data_len,
3404 rec->ctr, rec->type,
3405 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003406 }
3407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3409#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3410 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003411 if( mbedtls_ssl_ver_gt(
3412 mbedtls_ssl_transform_get_minor_ver( transform ),
3413 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003414 {
3415 /*
3416 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003417 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003418 *
3419 * Known timing attacks:
3420 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3421 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003422 * To compensate for different timings for the MAC calculation
3423 * depending on how much padding was removed (which is determined
3424 * by padlen), process extra_run more blocks through the hash
3425 * function.
3426 *
3427 * The formula in the paper is
3428 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3429 * where L1 is the size of the header plus the decrypted message
3430 * plus CBC padding and L2 is the size of the header plus the
3431 * decrypted message. This is for an underlying hash function
3432 * with 64-byte blocks.
3433 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3434 * correctly. We round down instead of up, so -56 is the correct
3435 * value for our calculations instead of -55.
3436 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003437 * Repeat the formula rather than defining a block_size variable.
3438 * This avoids requiring division by a variable at runtime
3439 * (which would be marginally less efficient and would require
3440 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003441 */
3442 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003443 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003444
3445 /*
3446 * The next two sizes are the minimum and maximum values of
3447 * in_msglen over all padlen values.
3448 *
3449 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003450 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003451 *
3452 * Note that max_len + maclen is never more than the buffer
3453 * length, as we previously did in_msglen -= maclen too.
3454 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003455 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003456 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3457
Hanno Becker4c6876b2017-12-27 21:28:58 +00003458 memset( tmp, 0, sizeof( tmp ) );
3459
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003460 switch( mbedtls_md_get_type(
3461 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003462 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003463#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3464 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 case MBEDTLS_MD_MD5:
3466 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003468 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003469 extra_run =
3470 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3471 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003472 break;
3473#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003474#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003476 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003477 extra_run =
3478 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3479 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003480 break;
3481#endif
3482 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3485 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003486
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003487 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003488
Hanno Beckere83efe62019-04-29 13:52:53 +01003489 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3490 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003491 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3492 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003493 /* Make sure we access everything even when padlen > 0. This
3494 * makes the synchronisation requirements for just-in-time
3495 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003496 ssl_read_memory( data + rec->data_len, padlen );
3497 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003498
3499 /* Call mbedtls_md_process at least once due to cache attacks
3500 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003501 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003502 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003503
Hanno Becker4c6876b2017-12-27 21:28:58 +00003504 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003505
3506 /* Make sure we access all the memory that could contain the MAC,
3507 * before we check it in the next code block. This makes the
3508 * synchronisation requirements for just-in-time Prime+Probe
3509 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003510 ssl_read_memory( data + min_len,
3511 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003512 }
3513 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3515 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003521#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003522 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3523 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003526 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003527 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529#if defined(MBEDTLS_SSL_DEBUG_ALL)
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003531#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003532 correct = 0;
3533 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003534 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003535 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003536
3537 /*
3538 * Finally check the correct flag
3539 */
3540 if( correct == 0 )
3541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003542#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003543
3544 /* Make extra sure authentication was performed, exactly once */
3545 if( auth_done != 1 )
3546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Hanno Beckera5a2b082019-05-15 14:03:01 +01003551#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003552 if( rec->cid_len != 0 )
3553 {
3554 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3555 &rec->type );
3556 if( ret != 0 )
3557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3558 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003559#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
3563 return( 0 );
3564}
3565
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003566#undef MAC_NONE
3567#undef MAC_PLAINTEXT
3568#undef MAC_CIPHERTEXT
3569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003571/*
3572 * Compression/decompression functions
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003575{
3576 int ret;
3577 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003578 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003580 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003583
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003584 if( len_pre == 0 )
3585 return( 0 );
3586
Teppo Järvelin91d79382019-10-02 09:09:31 +03003587 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590 ssl->out_msglen ) );
3591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003593 ssl->out_msg, ssl->out_msglen );
3594
Paul Bakker48916f92012-09-16 19:57:18 +00003595 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3596 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3597 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003598 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003599
Paul Bakker48916f92012-09-16 19:57:18 +00003600 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003601 if( ret != Z_OK )
3602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3604 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003605 }
3606
Angus Grattond8213d02016-05-25 20:56:48 +10003607 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003608 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003611 ssl->out_msglen ) );
3612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003614 ssl->out_msg, ssl->out_msglen );
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617
3618 return( 0 );
3619}
3620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003622{
3623 int ret;
3624 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003625 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003627 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003630
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003631 if( len_pre == 0 )
3632 return( 0 );
3633
Teppo Järvelin91d79382019-10-02 09:09:31 +03003634 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003637 ssl->in_msglen ) );
3638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 ssl->in_msg, ssl->in_msglen );
3641
Paul Bakker48916f92012-09-16 19:57:18 +00003642 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3643 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3644 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003645 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003646 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003647
Paul Bakker48916f92012-09-16 19:57:18 +00003648 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649 if( ret != Z_OK )
3650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3652 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 }
3654
Angus Grattond8213d02016-05-25 20:56:48 +10003655 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003656 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003659 ssl->in_msglen ) );
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662 ssl->in_msg, ssl->in_msglen );
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003665
3666 return( 0 );
3667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3671static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_PROTO_DTLS)
3674static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003675{
3676 /* If renegotiation is not enforced, retransmit until we would reach max
3677 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003678 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003679 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003680 uint32_t ratio =
3681 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3682 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003683 unsigned char doublings = 1;
3684
3685 while( ratio != 0 )
3686 {
3687 ++doublings;
3688 ratio >>= 1;
3689 }
3690
3691 if( ++ssl->renego_records_seen > doublings )
3692 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003694 return( 0 );
3695 }
3696 }
3697
3698 return( ssl_write_hello_request( ssl ) );
3699}
3700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003702
Paul Bakker5121ce52009-01-03 21:22:43 +00003703/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003704 * Fill the input message buffer by appending data to it.
3705 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003706 *
3707 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3708 * available (from this read and/or a previous one). Otherwise, an error code
3709 * is returned (possibly EOF or WANT_READ).
3710 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003711 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3712 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3713 * since we always read a whole datagram at once.
3714 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003715 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003716 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003719{
Paul Bakker23986e52011-04-24 08:57:21 +00003720 int ret;
3721 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Hanno Beckera58a8962019-06-13 16:11:15 +01003725 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3726 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003729 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003731 }
3732
Angus Grattond8213d02016-05-25 20:56:48 +10003733 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003737 }
3738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003742 uint32_t timeout;
3743
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003744 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003745 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3746 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003747 {
3748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3749 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3751 }
3752
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003753 /*
3754 * The point is, we need to always read a full datagram at once, so we
3755 * sometimes read more then requested, and handle the additional data.
3756 * It could be the rest of the current record (while fetching the
3757 * header) and/or some other records in the same datagram.
3758 */
3759
3760 /*
3761 * Move to the next record in the already read datagram if applicable
3762 */
3763 if( ssl->next_record_offset != 0 )
3764 {
3765 if( ssl->in_left < ssl->next_record_offset )
3766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003769 }
3770
3771 ssl->in_left -= ssl->next_record_offset;
3772
3773 if( ssl->in_left != 0 )
3774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003776 ssl->next_record_offset ) );
3777 memmove( ssl->in_hdr,
3778 ssl->in_hdr + ssl->next_record_offset,
3779 ssl->in_left );
3780 }
3781
3782 ssl->next_record_offset = 0;
3783 }
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003787
3788 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003789 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790 */
3791 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003795 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796
3797 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003798 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003799 * are not at the beginning of a new record, the caller did something
3800 * wrong.
3801 */
3802 if( ssl->in_left != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3805 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003806 }
3807
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003808 /*
3809 * Don't even try to read if time's out already.
3810 * This avoids by-passing the timer when repeatedly receiving messages
3811 * that will end up being dropped.
3812 */
3813 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003814 {
3815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003816 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003817 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003818 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003819 {
Angus Grattond8213d02016-05-25 20:56:48 +10003820 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003823 timeout = ssl->handshake->retransmit_timeout;
3824 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003825 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003828
Hanno Beckera58a8962019-06-13 16:11:15 +01003829 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3830 {
3831 ret = mbedtls_ssl_get_recv_timeout( ssl )
3832 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3833 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003834 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003835 {
3836 ret = mbedtls_ssl_get_recv( ssl )
3837 ( ssl->p_bio, ssl->in_hdr, len );
3838 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003841
3842 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003844 }
3845
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003846 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003849 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003852 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003853 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003856 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003857 }
3858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003862 return( ret );
3863 }
3864
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003865 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003868 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3869 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003871 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003872 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003875 return( ret );
3876 }
3877
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003878 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003881 }
3882
Paul Bakker5121ce52009-01-03 21:22:43 +00003883 if( ret < 0 )
3884 return( ret );
3885
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003886 ssl->in_left = ret;
3887 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003888 MBEDTLS_SSL_TRANSPORT_ELSE
3889#endif /* MBEDTLS_SSL_PROTO_DTLS */
3890#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003893 ssl->in_left, nb_want ) );
3894
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003895 while( ssl->in_left < nb_want )
3896 {
3897 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003898
3899 if( ssl_check_timer( ssl ) != 0 )
3900 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3901 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003902 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003903 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003904 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003905 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3906 ssl->in_hdr + ssl->in_left, len,
3907 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003908 }
3909 else
3910 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003911 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003912 ssl->in_hdr + ssl->in_left, len );
3913 }
3914 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003917 ssl->in_left, nb_want ) );
3918 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003919
3920 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003922
3923 if( ret < 0 )
3924 return( ret );
3925
mohammad160352aecb92018-03-28 23:41:40 -07003926 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003927 {
Darryl Green11999bb2018-03-13 15:22:58 +00003928 MBEDTLS_SSL_DEBUG_MSG( 1,
3929 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003930 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3932 }
3933
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003934 ssl->in_left += ret;
3935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003937#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
3941 return( 0 );
3942}
3943
3944/*
3945 * Flush any data not yet written
3946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003948{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003949 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003950 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
Hanno Beckera58a8962019-06-13 16:11:15 +01003954 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003957 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003959 }
3960
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003961 /* Avoid incrementing counter if data is flushed */
3962 if( ssl->out_left == 0 )
3963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003965 return( 0 );
3966 }
3967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 while( ssl->out_left > 0 )
3969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003971 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
Hanno Becker2b1e3542018-08-06 11:19:13 +01003973 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003974 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
3978 if( ret <= 0 )
3979 return( ret );
3980
mohammad160352aecb92018-03-28 23:41:40 -07003981 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003982 {
Darryl Green11999bb2018-03-13 15:22:58 +00003983 MBEDTLS_SSL_DEBUG_MSG( 1,
3984 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003985 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3987 }
3988
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 ssl->out_left -= ret;
3990 }
3991
Hanno Becker2b1e3542018-08-06 11:19:13 +01003992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003993 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003994 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003997 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003999#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004000 {
4001 ssl->out_hdr = ssl->out_buf + 8;
4002 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004003#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004004 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
4008 return( 0 );
4009}
4010
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011/*
4012 * Functions to handle the DTLS retransmission state machine
4013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015/*
4016 * Append current handshake message to current outgoing flight
4017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4022 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4023 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004024
4025 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004026 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004030 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004031 }
4032
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004033 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004034 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004037 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004038 }
4039
4040 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004041 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004043 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004044 msg->next = NULL;
4045
4046 /* Append to the current flight */
4047 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 else
4050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052 while( cur->next != NULL )
4053 cur = cur->next;
4054 cur->next = msg;
4055 }
4056
Hanno Becker3b235902018-08-06 09:54:53 +01004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058 return( 0 );
4059}
4060
4061/*
4062 * Free the current flight of handshake messages
4063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 mbedtls_ssl_flight_item *cur = flight;
4067 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068
4069 while( cur != NULL )
4070 {
4071 next = cur->next;
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 mbedtls_free( cur->p );
4074 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075
4076 cur = next;
4077 }
4078}
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4081static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004082#endif
4083
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004085 * Swap transform_out and out_ctr with the alternative ones
4086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090 unsigned char tmp_out_ctr[8];
4091
4092 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004095 return;
4096 }
4097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004099
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004100 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004101 tmp_transform = ssl->transform_out;
4102 ssl->transform_out = ssl->handshake->alt_transform_out;
4103 ssl->handshake->alt_transform_out = tmp_transform;
4104
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004105 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004106 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4107 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4108 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004109
4110 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004111 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4114 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4119 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004120 }
4121 }
4122#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004123}
4124
4125/*
4126 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004127 */
4128int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4129{
4130 int ret = 0;
4131
4132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4133
4134 ret = mbedtls_ssl_flight_transmit( ssl );
4135
4136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4137
4138 return( ret );
4139}
4140
4141/*
4142 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143 *
4144 * Need to remember the current message in case flush_output returns
4145 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004146 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004147 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004148int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004150 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004154 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004156
4157 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004159 ssl_swap_epochs( ssl );
4160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004162 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163
4164 while( ssl->handshake->cur_msg != NULL )
4165 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004166 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004167 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004168
Hanno Beckere1dcb032018-08-17 16:47:58 +01004169 int const is_finished =
4170 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4171 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4172
Hanno Becker04da1892018-08-14 13:22:10 +01004173 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4174 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4175
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004176 /* Swap epochs before sending Finished: we can't do it after
4177 * sending ChangeCipherSpec, in case write returns WANT_READ.
4178 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004179 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004180 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004182 ssl_swap_epochs( ssl );
4183 }
4184
Hanno Becker67bc7c32018-08-06 11:33:50 +01004185 ret = ssl_get_remaining_payload_in_datagram( ssl );
4186 if( ret < 0 )
4187 return( ret );
4188 max_frag_len = (size_t) ret;
4189
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004190 /* CCS is copied as is, while HS messages may need fragmentation */
4191 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4192 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004193 if( max_frag_len == 0 )
4194 {
4195 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4196 return( ret );
4197
4198 continue;
4199 }
4200
Teppo Järvelin91d79382019-10-02 09:09:31 +03004201 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004202 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004203 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004204
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004205 /* Update position inside current message */
4206 ssl->handshake->cur_msg_p += cur->len;
4207 }
4208 else
4209 {
4210 const unsigned char * const p = ssl->handshake->cur_msg_p;
4211 const size_t hs_len = cur->len - 12;
4212 const size_t frag_off = p - ( cur->p + 12 );
4213 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004214 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004215
Hanno Beckere1dcb032018-08-17 16:47:58 +01004216 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004217 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004218 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004219 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220
Hanno Becker67bc7c32018-08-06 11:33:50 +01004221 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4222 return( ret );
4223
4224 continue;
4225 }
4226 max_hs_frag_len = max_frag_len - 12;
4227
4228 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4229 max_hs_frag_len : rem_len;
4230
4231 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 (unsigned) cur_hs_frag_len,
4235 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004236 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004237
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004238 /* Messages are stored with handshake headers as if not fragmented,
4239 * copy beginning of headers then fill fragmentation fields.
4240 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004241 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004242
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004243 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4244 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4245 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004246
4247 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4248
Hanno Becker3f7b9732018-08-28 09:53:25 +01004249 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004250 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004251 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004252 ssl->out_msgtype = cur->type;
4253
4254 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004255 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004256 }
4257
4258 /* If done with the current message move to the next one if any */
4259 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4260 {
4261 if( cur->next != NULL )
4262 {
4263 ssl->handshake->cur_msg = cur->next;
4264 ssl->handshake->cur_msg_p = cur->next->p + 12;
4265 }
4266 else
4267 {
4268 ssl->handshake->cur_msg = NULL;
4269 ssl->handshake->cur_msg_p = NULL;
4270 }
4271 }
4272
4273 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004274 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004277 return( ret );
4278 }
4279 }
4280
Hanno Becker67bc7c32018-08-06 11:33:50 +01004281 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4282 return( ret );
4283
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004284 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4286 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004287 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004290 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4291 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004292
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004294
4295 return( 0 );
4296}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004297
4298/*
4299 * To be called when the last message of an incoming flight is received.
4300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004302{
4303 /* We won't need to resend that one any more */
4304 ssl_flight_free( ssl->handshake->flight );
4305 ssl->handshake->flight = NULL;
4306 ssl->handshake->cur_msg = NULL;
4307
4308 /* The next incoming flight will start with this msg_seq */
4309 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4310
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004311 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004312 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004313
Hanno Becker0271f962018-08-16 13:23:47 +01004314 /* Clear future message buffering structure. */
4315 ssl_buffering_free( ssl );
4316
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004317 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004318 ssl_set_timer( ssl, 0 );
4319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4321 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004324 }
4325 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004327}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004328
4329/*
4330 * To be called when the last message of an outgoing flight is send.
4331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004333{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004334 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004335 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4338 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004341 }
4342 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004346
Paul Bakker5121ce52009-01-03 21:22:43 +00004347/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004348 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004350
4351/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004352 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004353 *
4354 * - fill in handshake headers
4355 * - update handshake checksum
4356 * - DTLS: save message for resending
4357 * - then pass to the record layer
4358 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004359 * DTLS: except for HelloRequest, messages are only queued, and will only be
4360 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004361 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004362 * Inputs:
4363 * - ssl->out_msglen: 4 + actual handshake message len
4364 * (4 is the size of handshake headers for TLS)
4365 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4366 * - ssl->out_msg + 4: the handshake message body
4367 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004368 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004369 * - ssl->out_msglen: the length of the record contents
4370 * (including handshake headers but excluding record headers)
4371 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004372 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004373int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004374{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004375 int ret;
4376 const size_t hs_len = ssl->out_msglen - 4;
4377 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4380
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004381 /*
4382 * Sanity checks
4383 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004384 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004385 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4386 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004387 /* In SSLv3, the client might send a NoCertificate alert. */
4388#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004389 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004390 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004391 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4392 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004393#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4394 {
4395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4397 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004400 /* Whenever we send anything different from a
4401 * HelloRequest we should be in a handshake - double check. */
4402 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4403 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004404 ssl->handshake == NULL )
4405 {
4406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004411 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004412 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004414 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004417 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004418#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004419
Hanno Beckerb50a2532018-08-06 11:52:54 +01004420 /* Double-check that we did not exceed the bounds
4421 * of the outgoing record buffer.
4422 * This should never fail as the various message
4423 * writing functions must obey the bounds of the
4424 * outgoing record buffer, but better be safe.
4425 *
4426 * Note: We deliberately do not check for the MTU or MFL here.
4427 */
4428 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4431 "size %u, maximum %u",
4432 (unsigned) ssl->out_msglen,
4433 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4435 }
4436
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004437 /*
4438 * Fill handshake headers
4439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004442 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004443
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004444 /*
4445 * DTLS has additional fields in the Handshake layer,
4446 * between the length field and the actual payload:
4447 * uint16 message_seq;
4448 * uint24 fragment_offset;
4449 * uint24 fragment_length;
4450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004452 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004453 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004454 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004455 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004456 {
4457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4458 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004459 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004460 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4462 }
4463
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004464 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004465 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004466
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004467 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004468 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004469 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004470 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4471 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004472 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004473 }
4474 else
4475 {
4476 ssl->out_msg[4] = 0;
4477 ssl->out_msg[5] = 0;
4478 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004479
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004480 /* Handshake hashes are computed without fragmentation,
4481 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004482 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004483 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004486
Hanno Becker0207e532018-08-28 10:28:28 +01004487 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004488 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004489 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 }
4491
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004492 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004494 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004495 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4496 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004497 {
4498 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004501 return( ret );
4502 }
4503 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004504 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004505#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004506 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004507 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004508 {
4509 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4510 return( ret );
4511 }
4512 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004513
4514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4515
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004516 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004517}
4518
4519/*
4520 * Record layer functions
4521 */
4522
4523/*
4524 * Write current record.
4525 *
4526 * Uses:
4527 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4528 * - ssl->out_msglen: length of the record content (excl headers)
4529 * - ssl->out_msg: record content
4530 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004531int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004532{
4533 int ret, done = 0;
4534 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004535 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004536
4537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004540 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004541 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004542 {
4543 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004546 return( ret );
4547 }
4548
4549 len = ssl->out_msglen;
4550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4554 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 ret = mbedtls_ssl_hw_record_write( ssl );
4559 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4562 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004563 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004564
4565 if( ret == 0 )
4566 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004569 if( !done )
4570 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004571 unsigned i;
4572 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004573 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004574
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004575 /* Skip writing the record content type to after the encryption,
4576 * as it may change when using the CID extension. */
4577
Hanno Becker2881d802019-05-22 14:44:53 +01004578 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4579 mbedtls_ssl_get_minor_ver( ssl ),
4580 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004581
Teppo Järvelin91d79382019-10-02 09:09:31 +03004582 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004583 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004584
Paul Bakker48916f92012-09-16 19:57:18 +00004585 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004586 {
Hanno Becker3307b532017-12-27 21:37:21 +00004587 mbedtls_record rec;
4588
4589 rec.buf = ssl->out_iv;
4590 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4591 ( ssl->out_iv - ssl->out_buf );
4592 rec.data_len = ssl->out_msglen;
4593 rec.data_offset = ssl->out_msg - rec.buf;
4594
Teppo Järvelin91d79382019-10-02 09:09:31 +03004595 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004596 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4597 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004598 ssl->conf->transport, rec.ver );
4599 rec.type = ssl->out_msgtype;
4600
Hanno Beckera5a2b082019-05-15 14:03:01 +01004601#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004602 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004603 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004604#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004605
Hanno Becker611a83b2018-01-03 14:27:32 +00004606 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004607 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004608 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004611 return( ret );
4612 }
4613
Hanno Becker3307b532017-12-27 21:37:21 +00004614 if( rec.data_offset != 0 )
4615 {
4616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4618 }
4619
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004620 /* Update the record content type and CID. */
4621 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004622#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004623 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4624 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004625#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004626 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004627 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004628 encrypted_fi = 1;
4629 }
4630
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004631 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004632 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4633 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004634 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004635 }
4636
Hanno Becker43395762019-05-03 14:46:38 +01004637 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004638
4639#if defined(MBEDTLS_SSL_PROTO_DTLS)
4640 /* In case of DTLS, double-check that we don't exceed
4641 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004642 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004643 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004644 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004645 if( ret < 0 )
4646 return( ret );
4647
4648 if( protected_record_size > (size_t) ret )
4649 {
4650 /* Should never happen */
4651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4652 }
4653 }
4654#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004655
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004656 /* Now write the potentially updated record content type. */
4657 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004660 "version = [%d:%d], msglen = %d",
4661 ssl->out_hdr[0], ssl->out_hdr[1],
4662 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004665 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004666
4667 ssl->out_left += protected_record_size;
4668 ssl->out_hdr += protected_record_size;
4669 ssl_update_out_pointers( ssl, ssl->transform_out );
4670
Hanno Becker04484622018-08-06 09:49:38 +01004671 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4672 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4673 break;
4674
4675 /* The loop goes to its end iff the counter is wrapping */
4676 if( i == ssl_ep_len( ssl ) )
4677 {
4678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4679 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 }
4682
Hanno Becker67bc7c32018-08-06 11:33:50 +01004683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004684 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004685 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004686 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004687 size_t remaining;
4688 ret = ssl_get_remaining_payload_in_datagram( ssl );
4689 if( ret < 0 )
4690 {
4691 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4692 ret );
4693 return( ret );
4694 }
4695
4696 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004697 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004698 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004699 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004700 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004701 else
4702 {
Hanno Becker513815a2018-08-20 11:56:09 +01004703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004704 }
4705 }
4706#endif /* MBEDTLS_SSL_PROTO_DTLS */
4707
4708 if( ( flush == SSL_FORCE_FLUSH ) &&
4709 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 return( ret );
4713 }
4714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004716
4717 return( 0 );
4718}
4719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004721
4722static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4723{
4724 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004725 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4726 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004727 {
4728 return( 1 );
4729 }
4730 return( 0 );
4731}
Hanno Becker44650b72018-08-16 12:51:11 +01004732
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004733static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004734{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004735 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004736}
4737
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004738static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004739{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004740 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004741}
4742
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004743static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004744{
4745 uint32_t msg_len, frag_off, frag_len;
4746
4747 msg_len = ssl_get_hs_total_len( ssl );
4748 frag_off = ssl_get_hs_frag_off( ssl );
4749 frag_len = ssl_get_hs_frag_len( ssl );
4750
4751 if( frag_off > msg_len )
4752 return( -1 );
4753
4754 if( frag_len > msg_len - frag_off )
4755 return( -1 );
4756
4757 if( frag_len + 12 > ssl->in_msglen )
4758 return( -1 );
4759
4760 return( 0 );
4761}
4762
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004763/*
4764 * Mark bits in bitmask (used for DTLS HS reassembly)
4765 */
4766static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4767{
4768 unsigned int start_bits, end_bits;
4769
4770 start_bits = 8 - ( offset % 8 );
4771 if( start_bits != 8 )
4772 {
4773 size_t first_byte_idx = offset / 8;
4774
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004775 /* Special case */
4776 if( len <= start_bits )
4777 {
4778 for( ; len != 0; len-- )
4779 mask[first_byte_idx] |= 1 << ( start_bits - len );
4780
4781 /* Avoid potential issues with offset or len becoming invalid */
4782 return;
4783 }
4784
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004785 offset += start_bits; /* Now offset % 8 == 0 */
4786 len -= start_bits;
4787
4788 for( ; start_bits != 0; start_bits-- )
4789 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4790 }
4791
4792 end_bits = len % 8;
4793 if( end_bits != 0 )
4794 {
4795 size_t last_byte_idx = ( offset + len ) / 8;
4796
4797 len -= end_bits; /* Now len % 8 == 0 */
4798
4799 for( ; end_bits != 0; end_bits-- )
4800 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4801 }
4802
4803 memset( mask + offset / 8, 0xFF, len / 8 );
4804}
4805
4806/*
4807 * Check that bitmask is full
4808 */
4809static int ssl_bitmask_check( unsigned char *mask, size_t len )
4810{
4811 size_t i;
4812
4813 for( i = 0; i < len / 8; i++ )
4814 if( mask[i] != 0xFF )
4815 return( -1 );
4816
4817 for( i = 0; i < len % 8; i++ )
4818 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4819 return( -1 );
4820
4821 return( 0 );
4822}
4823
Hanno Becker56e205e2018-08-16 09:06:12 +01004824/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004825static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004826 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004827{
Hanno Becker56e205e2018-08-16 09:06:12 +01004828 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004829
Hanno Becker56e205e2018-08-16 09:06:12 +01004830 alloc_len = 12; /* Handshake header */
4831 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004832
Hanno Beckerd07df862018-08-16 09:14:58 +01004833 if( add_bitmap )
4834 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004835
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004836 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004837}
Hanno Becker56e205e2018-08-16 09:06:12 +01004838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004840
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004841static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004842{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004843 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004844}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004845
Simon Butcher99000142016-10-13 17:21:01 +01004846int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004851 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004853 }
4854
Hanno Becker12555c62018-08-16 12:47:53 +01004855 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004858 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004859 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004862 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004863 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004864 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004865 unsigned int recv_msg_seq = (unsigned int)
4866 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004867
Hanno Becker44650b72018-08-16 12:51:11 +01004868 if( ssl_check_hs_header( ssl ) != 0 )
4869 {
4870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4871 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4872 }
4873
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004874 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004875 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4876 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4877 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4878 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004879 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004880 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4881 {
4882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4883 recv_msg_seq,
4884 ssl->handshake->in_msg_seq ) );
4885 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4886 }
4887
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004888 /* Retransmit only on last message from previous flight, to avoid
4889 * too many retransmissions.
4890 * Besides, No sane server ever retransmits HelloVerifyRequest */
4891 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004895 "message_seq = %d, start_of_flight = %d",
4896 recv_msg_seq,
4897 ssl->handshake->in_flight_start_seq ) );
4898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004902 return( ret );
4903 }
4904 }
4905 else
4906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004908 "message_seq = %d, expected = %d",
4909 recv_msg_seq,
4910 ssl->handshake->in_msg_seq ) );
4911 }
4912
Hanno Becker90333da2017-10-10 11:27:13 +01004913 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004914 }
4915 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004916
Hanno Becker6d97ef52018-08-16 13:09:04 +01004917 /* Message reassembly is handled alongside buffering of future
4918 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004919 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004920 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004921 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004924 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004925 }
4926 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004927 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004929#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004930 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004931 /* With TLS we don't handle fragmentation (for now) */
4932 if( ssl->in_msglen < ssl->in_hslen )
4933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4935 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4936 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004937 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004938#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004939
Simon Butcher99000142016-10-13 17:21:01 +01004940 return( 0 );
4941}
4942
4943void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4944{
Hanno Becker0271f962018-08-16 13:23:47 +01004945 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004946
Hanno Becker0271f962018-08-16 13:23:47 +01004947 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004948 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004949
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004950 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004952 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004953 ssl->handshake != NULL )
4954 {
Hanno Becker0271f962018-08-16 13:23:47 +01004955 unsigned offset;
4956 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004957
Hanno Becker0271f962018-08-16 13:23:47 +01004958 /* Increment handshake sequence number */
4959 hs->in_msg_seq++;
4960
4961 /*
4962 * Clear up handshake buffering and reassembly structure.
4963 */
4964
4965 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004966 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004967
4968 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004969 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4970 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004971 offset++, hs_buf++ )
4972 {
4973 *hs_buf = *(hs_buf + 1);
4974 }
4975
4976 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004977 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004978 }
4979#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004980}
4981
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004982/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983 * DTLS anti-replay: RFC 6347 4.1.2.6
4984 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004985 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4986 * Bit n is set iff record number in_window_top - n has been seen.
4987 *
4988 * Usually, in_window_top is the last record number seen and the lsb of
4989 * in_window is set. The only exception is the initial state (record number 0
4990 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4993static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004994{
4995 ssl->in_window_top = 0;
4996 ssl->in_window = 0;
4997}
4998
4999static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5000{
5001 return( ( (uint64_t) buf[0] << 40 ) |
5002 ( (uint64_t) buf[1] << 32 ) |
5003 ( (uint64_t) buf[2] << 24 ) |
5004 ( (uint64_t) buf[3] << 16 ) |
5005 ( (uint64_t) buf[4] << 8 ) |
5006 ( (uint64_t) buf[5] ) );
5007}
5008
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005009static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5010{
5011 int ret;
5012 unsigned char *original_in_ctr;
5013
5014 // save original in_ctr
5015 original_in_ctr = ssl->in_ctr;
5016
5017 // use counter from record
5018 ssl->in_ctr = record_in_ctr;
5019
5020 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5021
5022 // restore the counter
5023 ssl->in_ctr = original_in_ctr;
5024
5025 return ret;
5026}
5027
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005028/*
5029 * Return 0 if sequence number is acceptable, -1 otherwise
5030 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005031int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005032{
5033 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5034 uint64_t bit;
5035
Hanno Becker7f376f42019-06-12 16:20:48 +01005036 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5037 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5038 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005039 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005040 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005041
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005042 if( rec_seqnum > ssl->in_window_top )
5043 return( 0 );
5044
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005045 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005046
5047 if( bit >= 64 )
5048 return( -1 );
5049
5050 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5051 return( -1 );
5052
5053 return( 0 );
5054}
5055
5056/*
5057 * Update replay window on new validated record
5058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005060{
5061 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5062
Hanno Becker7f376f42019-06-12 16:20:48 +01005063 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5064 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5065 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005066 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005067 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005068
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005069 if( rec_seqnum > ssl->in_window_top )
5070 {
5071 /* Update window_top and the contents of the window */
5072 uint64_t shift = rec_seqnum - ssl->in_window_top;
5073
5074 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005075 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005076 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005077 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005078 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005079 ssl->in_window |= 1;
5080 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005081
5082 ssl->in_window_top = rec_seqnum;
5083 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005084 else
5085 {
5086 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005087 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005088
5089 if( bit < 64 ) /* Always true, but be extra sure */
5090 ssl->in_window |= (uint64_t) 1 << bit;
5091 }
5092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005094
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005095#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005096/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005097static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5098
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005099/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005100 * Without any SSL context, check if a datagram looks like a ClientHello with
5101 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005102 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005103 *
5104 * - if cookie is valid, return 0
5105 * - if ClientHello looks superficially valid but cookie is not,
5106 * fill obuf and set olen, then
5107 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5108 * - otherwise return a specific error code
5109 */
5110static int ssl_check_dtls_clihlo_cookie(
5111 mbedtls_ssl_cookie_write_t *f_cookie_write,
5112 mbedtls_ssl_cookie_check_t *f_cookie_check,
5113 void *p_cookie,
5114 const unsigned char *cli_id, size_t cli_id_len,
5115 const unsigned char *in, size_t in_len,
5116 unsigned char *obuf, size_t buf_len, size_t *olen )
5117{
5118 size_t sid_len, cookie_len;
5119 unsigned char *p;
5120
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005121 /*
5122 * Structure of ClientHello with record and handshake headers,
5123 * and expected values. We don't need to check a lot, more checks will be
5124 * done when actually parsing the ClientHello - skipping those checks
5125 * avoids code duplication and does not make cookie forging any easier.
5126 *
5127 * 0-0 ContentType type; copied, must be handshake
5128 * 1-2 ProtocolVersion version; copied
5129 * 3-4 uint16 epoch; copied, must be 0
5130 * 5-10 uint48 sequence_number; copied
5131 * 11-12 uint16 length; (ignored)
5132 *
5133 * 13-13 HandshakeType msg_type; (ignored)
5134 * 14-16 uint24 length; (ignored)
5135 * 17-18 uint16 message_seq; copied
5136 * 19-21 uint24 fragment_offset; copied, must be 0
5137 * 22-24 uint24 fragment_length; (ignored)
5138 *
5139 * 25-26 ProtocolVersion client_version; (ignored)
5140 * 27-58 Random random; (ignored)
5141 * 59-xx SessionID session_id; 1 byte len + sid_len content
5142 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5143 * ...
5144 *
5145 * Minimum length is 61 bytes.
5146 */
5147 if( in_len < 61 ||
5148 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5149 in[3] != 0 || in[4] != 0 ||
5150 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5151 {
5152 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5153 }
5154
5155 sid_len = in[59];
5156 if( sid_len > in_len - 61 )
5157 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5158
5159 cookie_len = in[60 + sid_len];
5160 if( cookie_len > in_len - 60 )
5161 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5162
5163 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5164 cli_id, cli_id_len ) == 0 )
5165 {
5166 /* Valid cookie */
5167 return( 0 );
5168 }
5169
5170 /*
5171 * If we get here, we've got an invalid cookie, let's prepare HVR.
5172 *
5173 * 0-0 ContentType type; copied
5174 * 1-2 ProtocolVersion version; copied
5175 * 3-4 uint16 epoch; copied
5176 * 5-10 uint48 sequence_number; copied
5177 * 11-12 uint16 length; olen - 13
5178 *
5179 * 13-13 HandshakeType msg_type; hello_verify_request
5180 * 14-16 uint24 length; olen - 25
5181 * 17-18 uint16 message_seq; copied
5182 * 19-21 uint24 fragment_offset; copied
5183 * 22-24 uint24 fragment_length; olen - 25
5184 *
5185 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5186 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5187 *
5188 * Minimum length is 28.
5189 */
5190 if( buf_len < 28 )
5191 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5192
5193 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005194 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005195 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5196 obuf[25] = 0xfe;
5197 obuf[26] = 0xff;
5198
5199 /* Generate and write actual cookie */
5200 p = obuf + 28;
5201 if( f_cookie_write( p_cookie,
5202 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5203 {
5204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5205 }
5206
5207 *olen = p - obuf;
5208
5209 /* Go back and fill length fields */
5210 obuf[27] = (unsigned char)( *olen - 28 );
5211
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005212 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005213 obuf[22] = obuf[14];
5214 obuf[23] = obuf[15];
5215 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005216
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005217 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005218
5219 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5220}
5221
5222/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005223 * Handle possible client reconnect with the same UDP quadruplet
5224 * (RFC 6347 Section 4.2.8).
5225 *
5226 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5227 * that looks like a ClientHello.
5228 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005229 * - if the input looks like a ClientHello without cookies,
5230 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005231 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005232 * - if the input looks like a ClientHello with a valid cookie,
5233 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005234 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005235 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005236 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005237 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005238 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5239 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005240 */
5241static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5242{
5243 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005244 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005245
Hanno Becker87b56262019-07-10 14:37:41 +01005246 if( ssl->conf->f_cookie_write == NULL ||
5247 ssl->conf->f_cookie_check == NULL )
5248 {
5249 /* If we can't use cookies to verify reachability of the peer,
5250 * drop the record. */
5251 return( 0 );
5252 }
5253
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005254 ret = ssl_check_dtls_clihlo_cookie(
5255 ssl->conf->f_cookie_write,
5256 ssl->conf->f_cookie_check,
5257 ssl->conf->p_cookie,
5258 ssl->cli_id, ssl->cli_id_len,
5259 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005260 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005261
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005262 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5263
5264 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005265 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005266 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005267 * If the error is permanent we'll catch it later,
5268 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005269 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005270 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005271 }
5272
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005273 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005275 /* Got a valid cookie, partially reset context */
5276 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5277 {
5278 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5279 return( ret );
5280 }
5281
5282 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005283 }
5284
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005285 return( ret );
5286}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005287#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005288
Hanno Becker46483f12019-05-03 13:25:54 +01005289static int ssl_check_record_type( uint8_t record_type )
5290{
5291 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5292 record_type != MBEDTLS_SSL_MSG_ALERT &&
5293 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5294 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5295 {
5296 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5297 }
5298
5299 return( 0 );
5300}
5301
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005302/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005303 * ContentType type;
5304 * ProtocolVersion version;
5305 * uint16 epoch; // DTLS only
5306 * uint48 sequence_number; // DTLS only
5307 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005308 *
5309 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005310 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005311 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5312 *
5313 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005314 * 1. proceed with the record if this function returns 0
5315 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5316 * 3. return CLIENT_RECONNECT if this function return that value
5317 * 4. drop the whole datagram if this function returns anything else.
5318 * Point 2 is needed when the peer is resending, and we have already received
5319 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005321static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005322 unsigned char *buf,
5323 size_t len,
5324 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005325{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005326 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005327
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005328 size_t const rec_hdr_type_offset = 0;
5329 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005330
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005331 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5332 rec_hdr_type_len;
5333 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005334
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005335 size_t const rec_hdr_ctr_len = 8;
5336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005337 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005338 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5339 rec_hdr_version_len;
5340
Hanno Beckera5a2b082019-05-15 14:03:01 +01005341#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005342 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5343 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005344 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5346#endif /* MBEDTLS_SSL_PROTO_DTLS */
5347
5348 size_t rec_hdr_len_offset; /* To be determined */
5349 size_t const rec_hdr_len_len = 2;
5350
5351 /*
5352 * Check minimum lengths for record header.
5353 */
5354
5355#if defined(MBEDTLS_SSL_PROTO_DTLS)
5356 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5357 {
5358 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5359 }
5360 MBEDTLS_SSL_TRANSPORT_ELSE
5361#endif /* MBEDTLS_SSL_PROTO_DTLS */
5362#if defined(MBEDTLS_SSL_PROTO_TLS)
5363 {
5364 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5365 }
5366#endif /* MBEDTLS_SSL_PROTO_DTLS */
5367
5368 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5371 (unsigned) len,
5372 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5373 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5374 }
5375
5376 /*
5377 * Parse and validate record content type
5378 */
5379
5380 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005381
5382 /* Check record content type */
5383#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5384 rec->cid_len = 0;
5385
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005386 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005387 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5388 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005389 {
5390 /* Shift pointers to account for record header including CID
5391 * struct {
5392 * ContentType special_type = tls12_cid;
5393 * ProtocolVersion version;
5394 * uint16 epoch;
5395 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005396 * opaque cid[cid_length]; // Additional field compared to
5397 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005398 * uint16 length;
5399 * opaque enc_content[DTLSCiphertext.length];
5400 * } DTLSCiphertext;
5401 */
5402
5403 /* So far, we only support static CID lengths
5404 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005405 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5406 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005407
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005409 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5411 (unsigned) len,
5412 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005414 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005415
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005416 /* configured CID len is guaranteed at most 255, see
5417 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5418 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005419 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5420 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005421 }
5422 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005423#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005424 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005425 if( ssl_check_record_type( rec->type ) )
5426 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5428 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005429 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5430 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005431 }
5432
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005433 /*
5434 * Parse and validate record version
5435 */
5436
Hanno Becker8061c6e2019-07-26 08:07:03 +01005437 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5438 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005439 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5440 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005441 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005442
Hanno Becker2881d802019-05-22 14:44:53 +01005443 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5446 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 }
5448
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005449 if( mbedtls_ssl_ver_gt( minor_ver,
5450 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5453 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 }
5455
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005456 /*
5457 * Parse/Copy record sequence number.
5458 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005459
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005460#if defined(MBEDTLS_SSL_PROTO_DTLS)
5461 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5462 {
5463 /* Copy explicit record sequence number from input buffer. */
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], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005466 rec_hdr_ctr_len );
5467 }
5468 MBEDTLS_SSL_TRANSPORT_ELSE
5469#endif /* MBEDTLS_SSL_PROTO_DTLS */
5470#if defined(MBEDTLS_SSL_PROTO_TLS)
5471 {
5472 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005473 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5474 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475 }
5476#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005477
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005478 /*
5479 * Parse record length.
5480 */
5481
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005482 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005483 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005484 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5485
Hanno Becker8b09b732019-05-08 12:03:28 +01005486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005487 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005488 rec->type,
5489 major_ver, minor_ver, rec->data_len ) );
5490
5491 rec->buf = buf;
5492 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005493
Hanno Beckerec014082019-07-26 08:20:27 +01005494 if( rec->data_len == 0 )
5495 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5496
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005497 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005498 * DTLS-related tests.
5499 * Check epoch before checking length constraint because
5500 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5501 * message gets duplicated before the corresponding Finished message,
5502 * the second ChangeCipherSpec should be discarded because it belongs
5503 * to an old epoch, but not because its length is shorter than
5504 * the minimum record length for packets using the new record transform.
5505 * Note that these two kinds of failures are handled differently,
5506 * as an unexpected record is silently skipped but an invalid
5507 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005508 */
5509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005510 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005511 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005512 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005513
Hanno Beckere0452772019-07-10 17:12:07 +01005514 /* Check that the datagram is large enough to contain a record
5515 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005516 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005517 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5519 (unsigned) len,
5520 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5522 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005523
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005524 /* Records from other, non-matching epochs are silently discarded.
5525 * (The case of same-port Client reconnects must be considered in
5526 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005527 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005528 {
5529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5530 "expected %d, received %d",
5531 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005532
5533 /* Records from the next epoch are considered for buffering
5534 * (concretely: early Finished messages). */
5535 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5536 {
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5538 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5539 }
5540
Hanno Becker87b56262019-07-10 14:37:41 +01005541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005542 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005544 /* For records from the correct epoch, check whether their
5545 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005546 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5547 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5550 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5551 }
5552#endif
5553 }
5554#endif /* MBEDTLS_SSL_PROTO_DTLS */
5555
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005556 return( 0 );
5557}
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
Hanno Becker87b56262019-07-10 14:37:41 +01005559
5560#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5561static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5562{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005563 unsigned int rec_epoch = (unsigned int)
5564 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005565
5566 /*
5567 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5568 * access the first byte of record content (handshake type), as we
5569 * have an active transform (possibly iv_len != 0), so use the
5570 * fact that the record header len is 13 instead.
5571 */
5572 if( rec_epoch == 0 &&
5573 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5574 MBEDTLS_SSL_IS_SERVER &&
5575 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5576 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5577 ssl->in_left > 13 &&
5578 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5581 "from the same port" ) );
5582 return( ssl_handle_possible_reconnect( ssl ) );
5583 }
5584
5585 return( 0 );
5586}
5587#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5588
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005589/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005590 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005591 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005592static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5593 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005594{
5595 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005598 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5601 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 ret = mbedtls_ssl_hw_record_read( ssl );
5606 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5609 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005610 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005611
5612 if( ret == 0 )
5613 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005614 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005616 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005618 unsigned char const old_msg_type = rec->type;
5619
Hanno Becker611a83b2018-01-03 14:27:32 +00005620 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005621 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005624
Hanno Beckera5a2b082019-05-15 14:03:01 +01005625#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005626 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005627 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5628 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005629 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005631 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005632 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005633#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005634
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 return( ret );
5636 }
5637
Hanno Becker106f3da2019-07-12 09:35:58 +01005638 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005639 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005640 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005641 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005643
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005645 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005646
Hanno Beckera5a2b082019-05-15 14:03:01 +01005647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005648 /* We have already checked the record content type
5649 * in ssl_parse_record_header(), failing or silently
5650 * dropping the record in the case of an unknown type.
5651 *
5652 * Since with the use of CIDs, the record content type
5653 * might change during decryption, re-check the record
5654 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005655 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005656 {
5657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5659 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005660#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005661
Hanno Becker106f3da2019-07-12 09:35:58 +01005662 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005663 {
5664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005665 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005666 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005667 {
5668 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5671 }
5672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5673
5674 ssl->nb_zero++;
5675
5676 /*
5677 * Three or more empty messages may be a DoS attack
5678 * (excessive CPU consumption).
5679 */
5680 if( ssl->nb_zero > 3 )
5681 {
5682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005683 "messages, possible DoS attack" ) );
5684 /* Treat the records as if they were not properly authenticated,
5685 * thereby failing the connection if we see more than allowed
5686 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005687 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5688 }
5689 }
5690 else
5691 ssl->nb_zero = 0;
5692
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005693 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5694#if defined(MBEDTLS_SSL_PROTO_TLS)
5695 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005696 {
5697 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005698 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005699 if( ++ssl->in_ctr[i - 1] != 0 )
5700 break;
5701
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005702 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005703 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005704 {
5705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5706 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5707 }
5708 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005709#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005710
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005717 }
5718#endif
5719
Hanno Beckerf0242852019-07-09 17:30:02 +01005720 /* Check actual (decrypted) record content length against
5721 * configured maximum. */
5722 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5723 {
5724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5725 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5726 }
5727
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728 return( 0 );
5729}
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005732
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733/*
5734 * Read a record.
5735 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005736 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5737 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5738 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005739 */
Hanno Becker1097b342018-08-15 14:09:41 +01005740
5741/* Helper functions for mbedtls_ssl_read_record(). */
5742static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005743static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5744static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005745
Hanno Becker327c93b2018-08-15 13:56:18 +01005746int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005747 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005748{
5749 int ret;
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005752
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005753 if( ssl->keep_current_message == 0 )
5754 {
5755 do {
Simon Butcher99000142016-10-13 17:21:01 +01005756
Hanno Becker26994592018-08-15 14:14:59 +01005757 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005758 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005759 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005760
Hanno Beckere74d5562018-08-15 14:26:08 +01005761 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005762 {
Hanno Becker40f50842018-08-15 14:48:01 +01005763#if defined(MBEDTLS_SSL_PROTO_DTLS)
5764 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005765
Hanno Becker40f50842018-08-15 14:48:01 +01005766 /* We only check for buffered messages if the
5767 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005768 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005769 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005770 {
Hanno Becker40f50842018-08-15 14:48:01 +01005771 if( ssl_load_buffered_message( ssl ) == 0 )
5772 have_buffered = 1;
5773 }
5774
5775 if( have_buffered == 0 )
5776#endif /* MBEDTLS_SSL_PROTO_DTLS */
5777 {
5778 ret = ssl_get_next_record( ssl );
5779 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5780 continue;
5781
5782 if( ret != 0 )
5783 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005784 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005785 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005786 return( ret );
5787 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005788 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005789 }
5790
5791 ret = mbedtls_ssl_handle_message_type( ssl );
5792
Hanno Becker40f50842018-08-15 14:48:01 +01005793#if defined(MBEDTLS_SSL_PROTO_DTLS)
5794 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5795 {
5796 /* Buffer future message */
5797 ret = ssl_buffer_message( ssl );
5798 if( ret != 0 )
5799 return( ret );
5800
5801 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5802 }
5803#endif /* MBEDTLS_SSL_PROTO_DTLS */
5804
Hanno Becker90333da2017-10-10 11:27:13 +01005805 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5806 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005807
5808 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005809 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005810 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005811 return( ret );
5812 }
5813
Hanno Becker327c93b2018-08-15 13:56:18 +01005814 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005815 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005816 {
5817 mbedtls_ssl_update_handshake_status( ssl );
5818 }
Simon Butcher99000142016-10-13 17:21:01 +01005819 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005820 else
Simon Butcher99000142016-10-13 17:21:01 +01005821 {
Hanno Becker02f59072018-08-15 14:00:24 +01005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005823 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005824 }
5825
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5827
5828 return( 0 );
5829}
5830
Hanno Becker40f50842018-08-15 14:48:01 +01005831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005832static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005833{
Hanno Becker40f50842018-08-15 14:48:01 +01005834 if( ssl->in_left > ssl->next_record_offset )
5835 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005836
Hanno Becker40f50842018-08-15 14:48:01 +01005837 return( 0 );
5838}
5839
5840static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5841{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005843 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005844 int ret = 0;
5845
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005846 if( hs == NULL )
5847 return( -1 );
5848
Hanno Beckere00ae372018-08-20 09:39:42 +01005849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5850
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005851 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5852 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5853 {
5854 /* Check if we have seen a ChangeCipherSpec before.
5855 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005856 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5859 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005860 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005861 }
5862
Hanno Becker39b8bc92018-08-28 17:17:13 +01005863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5865 ssl->in_msglen = 1;
5866 ssl->in_msg[0] = 1;
5867
5868 /* As long as they are equal, the exact value doesn't matter. */
5869 ssl->in_left = 0;
5870 ssl->next_record_offset = 0;
5871
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005872 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005873 goto exit;
5874 }
Hanno Becker37f95322018-08-16 13:55:32 +01005875
Hanno Beckerb8f50142018-08-28 10:01:34 +01005876#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005877 /* Debug only */
5878 {
5879 unsigned offset;
5880 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5881 {
5882 hs_buf = &hs->buffering.hs[offset];
5883 if( hs_buf->is_valid == 1 )
5884 {
5885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5886 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005887 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005888 }
5889 }
5890 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005891#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005892
5893 /* Check if we have buffered and/or fully reassembled the
5894 * next handshake message. */
5895 hs_buf = &hs->buffering.hs[0];
5896 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5897 {
5898 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005899 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005900
5901 /* Double-check that we haven't accidentally buffered
5902 * a message that doesn't fit into the input buffer. */
5903 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5904 {
5905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5907 }
5908
5909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5910 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5911 hs_buf->data, msg_len + 12 );
5912
5913 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5914 ssl->in_hslen = msg_len + 12;
5915 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005916 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005917
5918 ret = 0;
5919 goto exit;
5920 }
5921 else
5922 {
5923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5924 hs->in_msg_seq ) );
5925 }
5926
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005927 ret = -1;
5928
5929exit:
5930
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5932 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005933}
5934
Hanno Beckera02b0b42018-08-21 17:20:27 +01005935static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5936 size_t desired )
5937{
5938 int offset;
5939 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5941 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005942
Hanno Becker01315ea2018-08-21 17:22:17 +01005943 /* Get rid of future records epoch first, if such exist. */
5944 ssl_free_buffered_record( ssl );
5945
5946 /* Check if we have enough space available now. */
5947 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5948 hs->buffering.total_bytes_buffered ) )
5949 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005951 return( 0 );
5952 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005953
Hanno Becker4f432ad2018-08-28 10:02:32 +01005954 /* We don't have enough space to buffer the next expected handshake
5955 * message. Remove buffers used for future messages to gain space,
5956 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005957 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5958 offset >= 0; offset-- )
5959 {
5960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5961 offset ) );
5962
Hanno Beckerb309b922018-08-23 13:18:05 +01005963 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005964
5965 /* Check if we have enough space available now. */
5966 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5967 hs->buffering.total_bytes_buffered ) )
5968 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005970 return( 0 );
5971 }
5972 }
5973
5974 return( -1 );
5975}
5976
Hanno Becker40f50842018-08-15 14:48:01 +01005977static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5978{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005979 int ret = 0;
5980 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5981
5982 if( hs == NULL )
5983 return( 0 );
5984
5985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5986
5987 switch( ssl->in_msgtype )
5988 {
5989 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005991
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005992 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005993 break;
5994
5995 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005996 {
5997 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005998 unsigned recv_msg_seq = (unsigned)
5999 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006000
Hanno Becker37f95322018-08-16 13:55:32 +01006001 mbedtls_ssl_hs_buffer *hs_buf;
6002 size_t msg_len = ssl->in_hslen - 12;
6003
6004 /* We should never receive an old handshake
6005 * message - double-check nonetheless. */
6006 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6007 {
6008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6010 }
6011
6012 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6013 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6014 {
6015 /* Silently ignore -- message too far in the future */
6016 MBEDTLS_SSL_DEBUG_MSG( 2,
6017 ( "Ignore future HS message with sequence number %u, "
6018 "buffering window %u - %u",
6019 recv_msg_seq, ssl->handshake->in_msg_seq,
6020 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6021
6022 goto exit;
6023 }
6024
6025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6026 recv_msg_seq, recv_msg_seq_offset ) );
6027
6028 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6029
6030 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006031 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006032 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006033 size_t reassembly_buf_sz;
6034
Hanno Becker37f95322018-08-16 13:55:32 +01006035 hs_buf->is_fragmented =
6036 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6037
6038 /* We copy the message back into the input buffer
6039 * after reassembly, so check that it's not too large.
6040 * This is an implementation-specific limitation
6041 * and not one from the standard, hence it is not
6042 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006043 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006044 {
6045 /* Ignore message */
6046 goto exit;
6047 }
6048
Hanno Beckere0b150f2018-08-21 15:51:03 +01006049 /* Check if we have enough space to buffer the message. */
6050 if( hs->buffering.total_bytes_buffered >
6051 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6052 {
6053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6054 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6055 }
6056
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006057 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6058 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006059
6060 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6061 hs->buffering.total_bytes_buffered ) )
6062 {
6063 if( recv_msg_seq_offset > 0 )
6064 {
6065 /* If we can't buffer a future message because
6066 * of space limitations -- ignore. */
6067 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",
6068 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6069 (unsigned) hs->buffering.total_bytes_buffered ) );
6070 goto exit;
6071 }
Hanno Beckere1801392018-08-21 16:51:05 +01006072 else
6073 {
6074 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",
6075 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6076 (unsigned) hs->buffering.total_bytes_buffered ) );
6077 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006078
Hanno Beckera02b0b42018-08-21 17:20:27 +01006079 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006080 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006081 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",
6082 (unsigned) msg_len,
6083 (unsigned) reassembly_buf_sz,
6084 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006085 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006086 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6087 goto exit;
6088 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006089 }
6090
6091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6092 msg_len ) );
6093
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006094 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6095 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006096 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006097 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006098 goto exit;
6099 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006100 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006101
6102 /* Prepare final header: copy msg_type, length and message_seq,
6103 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006104 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006105 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006106 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006107
6108 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006109
6110 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006111 }
6112 else
6113 {
6114 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006115 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 +01006116 {
6117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6118 /* Ignore */
6119 goto exit;
6120 }
6121 }
6122
Hanno Becker4422bbb2018-08-20 09:40:19 +01006123 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006124 {
6125 size_t frag_len, frag_off;
6126 unsigned char * const msg = hs_buf->data + 12;
6127
6128 /*
6129 * Check and copy current fragment
6130 */
6131
6132 /* Validation of header fields already done in
6133 * mbedtls_ssl_prepare_handshake_record(). */
6134 frag_off = ssl_get_hs_frag_off( ssl );
6135 frag_len = ssl_get_hs_frag_len( ssl );
6136
6137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6138 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006139 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006140
6141 if( hs_buf->is_fragmented )
6142 {
6143 unsigned char * const bitmask = msg + msg_len;
6144 ssl_bitmask_set( bitmask, frag_off, frag_len );
6145 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6146 msg_len ) == 0 );
6147 }
6148 else
6149 {
6150 hs_buf->is_complete = 1;
6151 }
6152
6153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6154 hs_buf->is_complete ? "" : "not yet " ) );
6155 }
6156
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006157 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006158 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006159
6160 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006161 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006162 break;
6163 }
6164
6165exit:
6166
6167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6168 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006169}
6170#endif /* MBEDTLS_SSL_PROTO_DTLS */
6171
Hanno Becker1097b342018-08-15 14:09:41 +01006172static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006173{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006174 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006175 * Consume last content-layer message and potentially
6176 * update in_msglen which keeps track of the contents'
6177 * consumption state.
6178 *
6179 * (1) Handshake messages:
6180 * Remove last handshake message, move content
6181 * and adapt in_msglen.
6182 *
6183 * (2) Alert messages:
6184 * Consume whole record content, in_msglen = 0.
6185 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006186 * (3) Change cipher spec:
6187 * Consume whole record content, in_msglen = 0.
6188 *
6189 * (4) Application data:
6190 * Don't do anything - the record layer provides
6191 * the application data as a stream transport
6192 * and consumes through mbedtls_ssl_read only.
6193 *
6194 */
6195
6196 /* Case (1): Handshake messages */
6197 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006198 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006199 /* Hard assertion to be sure that no application data
6200 * is in flight, as corrupting ssl->in_msglen during
6201 * ssl->in_offt != NULL is fatal. */
6202 if( ssl->in_offt != NULL )
6203 {
6204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6206 }
6207
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006208 /*
6209 * Get next Handshake message in the current record
6210 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006211
Hanno Becker4a810fb2017-05-24 16:27:30 +01006212 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006213 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006214 * current handshake content: If DTLS handshake
6215 * fragmentation is used, that's the fragment
6216 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006217 * size here is faulty and should be changed at
6218 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006219 * (2) While it doesn't seem to cause problems, one
6220 * has to be very careful not to assume that in_hslen
6221 * is always <= in_msglen in a sensible communication.
6222 * Again, it's wrong for DTLS handshake fragmentation.
6223 * The following check is therefore mandatory, and
6224 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006225 * Additionally, ssl->in_hslen might be arbitrarily out of
6226 * bounds after handling a DTLS message with an unexpected
6227 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006228 */
6229 if( ssl->in_hslen < ssl->in_msglen )
6230 {
6231 ssl->in_msglen -= ssl->in_hslen;
6232 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6233 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006234
Hanno Becker4a810fb2017-05-24 16:27:30 +01006235 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6236 ssl->in_msg, ssl->in_msglen );
6237 }
6238 else
6239 {
6240 ssl->in_msglen = 0;
6241 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006242
Hanno Becker4a810fb2017-05-24 16:27:30 +01006243 ssl->in_hslen = 0;
6244 }
6245 /* Case (4): Application data */
6246 else if( ssl->in_offt != NULL )
6247 {
6248 return( 0 );
6249 }
6250 /* Everything else (CCS & Alerts) */
6251 else
6252 {
6253 ssl->in_msglen = 0;
6254 }
6255
Hanno Becker1097b342018-08-15 14:09:41 +01006256 return( 0 );
6257}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006258
Hanno Beckere74d5562018-08-15 14:26:08 +01006259static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6260{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006261 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006262 return( 1 );
6263
6264 return( 0 );
6265}
6266
Hanno Becker5f066e72018-08-16 14:56:31 +01006267#if defined(MBEDTLS_SSL_PROTO_DTLS)
6268
6269static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6270{
6271 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6272 if( hs == NULL )
6273 return;
6274
Hanno Becker01315ea2018-08-21 17:22:17 +01006275 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006276 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006277 hs->buffering.total_bytes_buffered -=
6278 hs->buffering.future_record.len;
6279
6280 mbedtls_free( hs->buffering.future_record.data );
6281 hs->buffering.future_record.data = NULL;
6282 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006283}
6284
6285static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6286{
6287 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6288 unsigned char * rec;
6289 size_t rec_len;
6290 unsigned rec_epoch;
6291
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006292 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006293 return( 0 );
6294
6295 if( hs == NULL )
6296 return( 0 );
6297
Hanno Becker5f066e72018-08-16 14:56:31 +01006298 rec = hs->buffering.future_record.data;
6299 rec_len = hs->buffering.future_record.len;
6300 rec_epoch = hs->buffering.future_record.epoch;
6301
6302 if( rec == NULL )
6303 return( 0 );
6304
Hanno Becker4cb782d2018-08-20 11:19:05 +01006305 /* Only consider loading future records if the
6306 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006307 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006308 return( 0 );
6309
Hanno Becker5f066e72018-08-16 14:56:31 +01006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6311
6312 if( rec_epoch != ssl->in_epoch )
6313 {
6314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6315 goto exit;
6316 }
6317
6318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6319
6320 /* Double-check that the record is not too large */
6321 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6322 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6323 {
6324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6326 }
6327
Teppo Järvelin91d79382019-10-02 09:09:31 +03006328 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006329 ssl->in_left = rec_len;
6330 ssl->next_record_offset = 0;
6331
6332 ssl_free_buffered_record( ssl );
6333
6334exit:
6335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6336 return( 0 );
6337}
6338
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006339static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6340 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006341{
6342 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006343
6344 /* Don't buffer future records outside handshakes. */
6345 if( hs == NULL )
6346 return( 0 );
6347
6348 /* Only buffer handshake records (we are only interested
6349 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006350 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006351 return( 0 );
6352
6353 /* Don't buffer more than one future epoch record. */
6354 if( hs->buffering.future_record.data != NULL )
6355 return( 0 );
6356
Hanno Becker01315ea2018-08-21 17:22:17 +01006357 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006358 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006359 hs->buffering.total_bytes_buffered ) )
6360 {
6361 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 +01006362 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006363 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006364 return( 0 );
6365 }
6366
Hanno Becker5f066e72018-08-16 14:56:31 +01006367 /* Buffer record */
6368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6369 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006370 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006371
6372 /* ssl_parse_record_header() only considers records
6373 * of the next epoch as candidates for buffering. */
6374 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006375 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006376
6377 hs->buffering.future_record.data =
6378 mbedtls_calloc( 1, hs->buffering.future_record.len );
6379 if( hs->buffering.future_record.data == NULL )
6380 {
6381 /* If we run out of RAM trying to buffer a
6382 * record from the next epoch, just ignore. */
6383 return( 0 );
6384 }
6385
Teppo Järvelin91d79382019-10-02 09:09:31 +03006386 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006387
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006388 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006389 return( 0 );
6390}
6391
6392#endif /* MBEDTLS_SSL_PROTO_DTLS */
6393
Hanno Beckere74d5562018-08-15 14:26:08 +01006394static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006395{
6396 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006397 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006398
Hanno Becker5f066e72018-08-16 14:56:31 +01006399#if defined(MBEDTLS_SSL_PROTO_DTLS)
6400 /* We might have buffered a future record; if so,
6401 * and if the epoch matches now, load it.
6402 * On success, this call will set ssl->in_left to
6403 * the length of the buffered record, so that
6404 * the calls to ssl_fetch_input() below will
6405 * essentially be no-ops. */
6406 ret = ssl_load_buffered_record( ssl );
6407 if( ret != 0 )
6408 return( ret );
6409#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006410
Hanno Becker8b09b732019-05-08 12:03:28 +01006411 /* Ensure that we have enough space available for the default form
6412 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6413 * with no space for CIDs counted in). */
6414 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6415 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006418 return( ret );
6419 }
6420
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006421 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6422 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006425 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006426 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006427 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6428 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006429 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006430 if( ret != 0 )
6431 return( ret );
6432
6433 /* Fall through to handling of unexpected records */
6434 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6435 }
6436
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006437 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6438 {
Hanno Becker87b56262019-07-10 14:37:41 +01006439#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006440 /* Reset in pointers to default state for TLS/DTLS records,
6441 * assuming no CID and no offset between record content and
6442 * record plaintext. */
6443 ssl_update_in_pointers( ssl );
6444
Hanno Becker69412452019-07-12 08:33:49 +01006445 /* Setup internal message pointers from record structure. */
6446 ssl->in_msgtype = rec.type;
6447#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6448 ssl->in_len = ssl->in_cid + rec.cid_len;
6449#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006450 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006451 ssl->in_msglen = rec.data_len;
6452
Hanno Becker87b56262019-07-10 14:37:41 +01006453 ret = ssl_check_client_reconnect( ssl );
6454 if( ret != 0 )
6455 return( ret );
6456#endif
6457
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006458 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006459 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006460
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6462 "(header)" ) );
6463 }
6464 else
6465 {
6466 /* Skip invalid record and the rest of the datagram */
6467 ssl->next_record_offset = 0;
6468 ssl->in_left = 0;
6469
6470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6471 "(header)" ) );
6472 }
6473
6474 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006475 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006476 }
Hanno Becker87b56262019-07-10 14:37:41 +01006477 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006478#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006479 {
6480 return( ret );
6481 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006482 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006485 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006486 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006487 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006488 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006489 if( ssl->next_record_offset < ssl->in_left )
6490 {
6491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6492 }
6493 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006494 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006496#if defined(MBEDTLS_SSL_PROTO_TLS)
6497 {
Hanno Beckere0452772019-07-10 17:12:07 +01006498 /*
6499 * Fetch record contents from underlying transport.
6500 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006501 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006502 if( ret != 0 )
6503 {
6504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6505 return( ret );
6506 }
6507
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006508 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006509 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006510#endif /* MBEDTLS_SSL_PROTO_TLS */
6511
6512 /*
6513 * Decrypt record contents.
6514 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006515
Hanno Beckera89610a2019-07-11 13:07:45 +01006516 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006519 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006520 {
6521 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006522 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006523 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006524 /* Except when waiting for Finished as a bad mac here
6525 * probably means something went wrong in the handshake
6526 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6527 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6528 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6529 {
6530#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6531 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6532 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006533 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006534 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6535 }
6536#endif
6537 return( ret );
6538 }
6539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006541 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6542 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6545 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006546 }
6547#endif
6548
Hanno Becker4a810fb2017-05-24 16:27:30 +01006549 /* As above, invalid records cause
6550 * dismissal of the whole datagram. */
6551
6552 ssl->next_record_offset = 0;
6553 ssl->in_left = 0;
6554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006556 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006557 }
6558
6559 return( ret );
6560 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006561 MBEDTLS_SSL_TRANSPORT_ELSE
6562#endif /* MBEDTLS_SSL_PROTO_DTLS */
6563#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006564 {
6565 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6567 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006568 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006569 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 }
6572#endif
6573 return( ret );
6574 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006575#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006576 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006577
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006578
6579 /* Reset in pointers to default state for TLS/DTLS records,
6580 * assuming no CID and no offset between record content and
6581 * record plaintext. */
6582 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006583#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6584 ssl->in_len = ssl->in_cid + rec.cid_len;
6585#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006586 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006587
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006588 /* The record content type may change during decryption,
6589 * so re-read it. */
6590 ssl->in_msgtype = rec.type;
6591 /* Also update the input buffer, because unfortunately
6592 * the server-side ssl_parse_client_hello() reparses the
6593 * record header when receiving a ClientHello initiating
6594 * a renegotiation. */
6595 ssl->in_hdr[0] = rec.type;
6596 ssl->in_msg = rec.buf + rec.data_offset;
6597 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006598 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006599
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006600#if defined(MBEDTLS_ZLIB_SUPPORT)
6601 if( ssl->transform_in != NULL &&
6602 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6603 {
6604 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6605 {
6606 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6607 return( ret );
6608 }
6609
6610 /* Check actual (decompress) record content length against
6611 * configured maximum. */
6612 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6613 {
6614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6615 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6616 }
6617 }
6618#endif /* MBEDTLS_ZLIB_SUPPORT */
6619
Simon Butcher99000142016-10-13 17:21:01 +01006620 return( 0 );
6621}
6622
6623int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6624{
6625 int ret;
6626
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006627 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006628 * Handle particular types of records
6629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006631 {
Simon Butcher99000142016-10-13 17:21:01 +01006632 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6633 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006634 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 }
6637
Hanno Beckere678eaa2018-08-21 14:57:46 +01006638 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006639 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006640 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006641 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6643 ssl->in_msglen ) );
6644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006645 }
6646
Hanno Beckere678eaa2018-08-21 14:57:46 +01006647 if( ssl->in_msg[0] != 1 )
6648 {
6649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6650 ssl->in_msg[0] ) );
6651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6652 }
6653
6654#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006655 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006656 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6657 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6658 {
6659 if( ssl->handshake == NULL )
6660 {
6661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6662 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6663 }
6664
6665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6666 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6667 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006668#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006669 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006673 if( ssl->in_msglen != 2 )
6674 {
6675 /* Note: Standard allows for more than one 2 byte alert
6676 to be packed in a single message, but Mbed TLS doesn't
6677 currently support this. */
6678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6679 ssl->in_msglen ) );
6680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6681 }
6682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006684 ssl->in_msg[0], ssl->in_msg[1] ) );
6685
6686 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006687 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006692 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694 }
6695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6697 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6700 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006702
6703#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6704 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6705 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6706 {
Hanno Becker90333da2017-10-10 11:27:13 +01006707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006708 /* Will be handled when trying to parse ServerHello */
6709 return( 0 );
6710 }
6711#endif
6712
6713#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006714 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006715 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6716 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006717 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6718 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6719 {
6720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6721 /* Will be handled in mbedtls_ssl_parse_certificate() */
6722 return( 0 );
6723 }
6724#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6725
6726 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006727 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006728 }
6729
Hanno Beckerc76c6192017-06-06 10:03:17 +01006730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006732 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006733 /* Drop unexpected ApplicationData records,
6734 * except at the beginning of renegotiations */
6735 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6736 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6737#if defined(MBEDTLS_SSL_RENEGOTIATION)
6738 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6739 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006740#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006741 )
6742 {
6743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6744 return( MBEDTLS_ERR_SSL_NON_FATAL );
6745 }
6746
6747 if( ssl->handshake != NULL &&
6748 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6749 {
6750 ssl_handshake_wrapup_free_hs_transform( ssl );
6751 }
6752 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006753#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006754
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 return( 0 );
6756}
6757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006759{
6760 int ret;
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6763 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6764 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006765 {
6766 return( ret );
6767 }
6768
6769 return( 0 );
6770}
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006773 unsigned char level,
6774 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006775{
6776 int ret;
6777
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006778 if( ssl == NULL || ssl->conf == NULL )
6779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006785 ssl->out_msglen = 2;
6786 ssl->out_msg[0] = level;
6787 ssl->out_msg[1] = message;
6788
Hanno Becker67bc7c32018-08-06 11:33:50 +01006789 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006792 return( ret );
6793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006795
6796 return( 0 );
6797}
6798
Hanno Becker17572472019-02-08 07:19:04 +00006799#if defined(MBEDTLS_X509_CRT_PARSE_C)
6800static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6801{
6802#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6803 if( session->peer_cert != NULL )
6804 {
6805 mbedtls_x509_crt_free( session->peer_cert );
6806 mbedtls_free( session->peer_cert );
6807 session->peer_cert = NULL;
6808 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006809#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006810 if( session->peer_cert_digest != NULL )
6811 {
6812 /* Zeroization is not necessary. */
6813 mbedtls_free( session->peer_cert_digest );
6814 session->peer_cert_digest = NULL;
6815 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6816 session->peer_cert_digest_len = 0;
6817 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006818#else
6819 ((void) session);
6820#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006821}
6822#endif /* MBEDTLS_X509_CRT_PARSE_C */
6823
Paul Bakker5121ce52009-01-03 21:22:43 +00006824/*
6825 * Handshake functions
6826 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006827#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006828/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006830{
Hanno Beckerdf645962019-06-26 13:02:22 +01006831 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6832 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
Hanno Becker5097cba2019-02-05 13:36:46 +00006836 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006839 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6840 {
6841 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6842 }
6843 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6844 {
6845 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6846 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006847 else
6848 {
6849 ssl->state = MBEDTLS_SSL_INVALID;
6850 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006851 return( 0 );
6852 }
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006856}
6857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006859{
Hanno Beckerdf645962019-06-26 13:02:22 +01006860 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6861 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006864
Hanno Becker5097cba2019-02-05 13:36:46 +00006865 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006868 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6869 {
6870 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6871 }
6872 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6873 {
6874 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6875 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006876 else
6877 {
6878 ssl->state = MBEDTLS_SSL_INVALID;
6879 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880 return( 0 );
6881 }
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006885}
Gilles Peskinef9828522017-05-03 12:28:43 +02006886
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006887#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006888/* Some certificate support -> implement write and parse */
6889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006893 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006895 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6896 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006899
Hanno Becker5097cba2019-02-05 13:36:46 +00006900 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006903 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6904 {
6905 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6906 }
6907 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6908 {
6909 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6910 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006911 else
6912 {
6913 ssl->state = MBEDTLS_SSL_INVALID;
6914 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006915 return( 0 );
6916 }
6917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006919 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6920 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006921 {
6922 if( ssl->client_auth == 0 )
6923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006925 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6926 {
6927 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6928 }
6929 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6930 {
6931 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6932 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006933 else
6934 {
6935 ssl->state = MBEDTLS_SSL_INVALID;
6936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006937 return( 0 );
6938 }
6939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006941 /*
6942 * If using SSLv3 and got no cert, send an Alert message
6943 * (otherwise an empty Certificate message will be sent).
6944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006946 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 {
6948 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6950 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6951 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 goto write_msg;
6955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#endif /* MBEDTLS_SSL_CLI_C */
6959#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006960 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6965 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 }
6967 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006968#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971
6972 /*
6973 * 0 . 0 handshake type
6974 * 1 . 3 handshake length
6975 * 4 . 6 length of all certs
6976 * 7 . 9 length of cert. 1
6977 * 10 . n-1 peer certificate
6978 * n . n+2 length of cert. 2
6979 * n+3 . ... upper level cert, etc.
6980 */
6981 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983
Paul Bakker29087132010-03-21 21:03:34 +00006984 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 {
6986 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006987 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006990 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 }
6993
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006994 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006995
Teppo Järvelin91d79382019-10-02 09:09:31 +03006996 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997 i += n; crt = crt->next;
6998 }
6999
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007000 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007001
7002 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7004 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007006#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007007write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007008#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Jarno Lamsa2b205162019-11-12 15:36:21 +02007010 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7011 {
7012 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7013 }
7014 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7015 {
7016 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7017 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007018 else
7019 {
7020 ssl->state = MBEDTLS_SSL_INVALID;
7021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007023 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007024 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007026 return( ret );
7027 }
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
Paul Bakkered27a042013-04-18 22:46:23 +02007031 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032}
7033
Hanno Becker285ff0c2019-01-31 07:44:03 +00007034#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007035
7036#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007037static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7038 unsigned char *crt_buf,
7039 size_t crt_buf_len )
7040{
7041 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7042
7043 if( peer_crt == NULL )
7044 return( -1 );
7045
7046 if( peer_crt->raw.len != crt_buf_len )
7047 return( -1 );
7048
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007049 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007050}
Hanno Becker5882dd02019-06-06 16:25:57 +01007051#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007052static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7053 unsigned char *crt_buf,
7054 size_t crt_buf_len )
7055{
7056 int ret;
7057 unsigned char const * const peer_cert_digest =
7058 ssl->session->peer_cert_digest;
7059 mbedtls_md_type_t const peer_cert_digest_type =
7060 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007061 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007062 mbedtls_md_info_from_type( peer_cert_digest_type );
7063 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7064 size_t digest_len;
7065
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007066 if( peer_cert_digest == NULL ||
7067 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7068 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007069 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007070 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007071
7072 digest_len = mbedtls_md_get_size( digest_info );
7073 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7074 return( -1 );
7075
7076 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7077 if( ret != 0 )
7078 return( -1 );
7079
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007080 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007081}
Hanno Becker5882dd02019-06-06 16:25:57 +01007082#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007083#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007084
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007085/*
7086 * Once the certificate message is read, parse it into a cert chain and
7087 * perform basic checks, but leave actual verification to the caller
7088 */
Hanno Becker35e41772019-02-05 15:37:23 +00007089static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7090 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007091{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007092 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007093#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7094 int crt_cnt=0;
7095#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007096 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007097 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007102 mbedtls_ssl_pend_fatal_alert( ssl,
7103 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105 }
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7108 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007111 mbedtls_ssl_pend_fatal_alert( ssl,
7112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 }
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007117
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007121 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007123 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007127 mbedtls_ssl_pend_fatal_alert( ssl,
7128 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 }
7131
Hanno Becker33c3dc82019-01-30 14:46:46 +00007132 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7133 i += 3;
7134
Hanno Becker33c3dc82019-01-30 14:46:46 +00007135 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 while( i < ssl->in_hslen )
7137 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007138 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007139 if ( i + 3 > ssl->in_hslen ) {
7140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007141 mbedtls_ssl_pend_fatal_alert( ssl,
7142 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007143 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7144 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007145 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7146 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007147 if( ssl->in_msg[i] != 0 )
7148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007150 mbedtls_ssl_pend_fatal_alert( ssl,
7151 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007153 }
7154
Hanno Becker33c3dc82019-01-30 14:46:46 +00007155 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007156 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007157 i += 3;
7158
7159 if( n < 128 || i + n > ssl->in_hslen )
7160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007162 mbedtls_ssl_pend_fatal_alert( ssl,
7163 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007165 }
7166
Hanno Becker33c3dc82019-01-30 14:46:46 +00007167 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007168#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7169 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007170 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7171 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007172 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007173 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007174 /* During client-side renegotiation, check that the server's
7175 * end-CRTs hasn't changed compared to the initial handshake,
7176 * mitigating the triple handshake attack. On success, reuse
7177 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7179 if( ssl_check_peer_crt_unchanged( ssl,
7180 &ssl->in_msg[i],
7181 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007182 {
Hanno Becker35e41772019-02-05 15:37:23 +00007183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007184 mbedtls_ssl_pend_fatal_alert( ssl,
7185 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007186 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 }
Hanno Becker35e41772019-02-05 15:37:23 +00007188
7189 /* Now we can safely free the original chain. */
7190 ssl_clear_peer_cert( ssl->session );
7191 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007192#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7193
Hanno Becker33c3dc82019-01-30 14:46:46 +00007194 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007195#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007196 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007197#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007198 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007199 * it in-place from the input buffer instead of making a copy. */
7200 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7201#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007202 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007204 case 0: /*ok*/
7205 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7206 /* Ignore certificate with an unknown algorithm: maybe a
7207 prior certificate was already trusted. */
7208 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007209
Hanno Becker33c3dc82019-01-30 14:46:46 +00007210 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7211 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7212 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007213
Hanno Becker33c3dc82019-01-30 14:46:46 +00007214 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7215 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7216 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007217
Hanno Becker33c3dc82019-01-30 14:46:46 +00007218 default:
7219 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7220 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007221 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007222 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7223 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007224 }
7225
7226 i += n;
7227 }
7228
Hanno Becker35e41772019-02-05 15:37:23 +00007229 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007230 return( 0 );
7231}
7232
Hanno Beckerb8a08572019-02-05 12:49:06 +00007233#if defined(MBEDTLS_SSL_SRV_C)
7234static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7235{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007236 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007237 return( -1 );
7238
7239#if defined(MBEDTLS_SSL_PROTO_SSL3)
7240 /*
7241 * Check if the client sent an empty certificate
7242 */
Hanno Becker2881d802019-05-22 14:44:53 +01007243 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007244 {
7245 if( ssl->in_msglen == 2 &&
7246 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7247 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7248 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7249 {
7250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7251 return( 0 );
7252 }
7253
7254 return( -1 );
7255 }
7256#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7257
7258#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7259 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7260 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7261 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7262 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007263 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 +00007264 {
7265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7266 return( 0 );
7267 }
7268
Hanno Beckerb8a08572019-02-05 12:49:06 +00007269#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7270 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007271
7272 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007273}
7274#endif /* MBEDTLS_SSL_SRV_C */
7275
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007276/* Check if a certificate message is expected.
7277 * Return either
7278 * - SSL_CERTIFICATE_EXPECTED, or
7279 * - SSL_CERTIFICATE_SKIP
7280 * indicating whether a Certificate message is expected or not.
7281 */
7282#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007283#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007284static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7285 int authmode )
7286{
Hanno Becker473f98f2019-06-26 10:27:32 +01007287 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007288 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007289
7290 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7291 return( SSL_CERTIFICATE_SKIP );
7292
7293#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007294 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007295 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007296 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7297 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7298 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007299 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007300 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007301
7302 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7303 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007304 ssl->session_negotiate->verify_result =
7305 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7306 return( SSL_CERTIFICATE_SKIP );
7307 }
7308 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007309#else
7310 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007311#endif /* MBEDTLS_SSL_SRV_C */
7312
7313 return( SSL_CERTIFICATE_EXPECTED );
7314}
7315
Hanno Becker3cf50612019-02-05 14:36:34 +00007316static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007317 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007318 mbedtls_x509_crt *chain,
7319 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007320{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007321 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7322 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7323 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007324 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007325 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007326 mbedtls_x509_crt *ca_chain;
7327 mbedtls_x509_crl *ca_crl;
7328
7329 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007330 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007331 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007332 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007333
Jarno Lamsae1621d42019-12-19 08:58:56 +02007334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007335#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7336 if( ssl->handshake->sni_ca_chain != NULL )
7337 {
7338 ca_chain = ssl->handshake->sni_ca_chain;
7339 ca_crl = ssl->handshake->sni_ca_crl;
7340 }
7341 else
7342#endif
7343 {
7344 ca_chain = ssl->conf->ca_chain;
7345 ca_crl = ssl->conf->ca_crl;
7346 }
7347
7348 /*
7349 * Main check: verify certificate
7350 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007351 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007352 chain,
7353 ca_chain, ca_crl,
7354 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007355#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007356 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007357#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007358 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007359#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7360 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7361#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7362 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007363
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007364 if( verify_ret == 0 )
7365 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007366 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007367 if( verify_ret == 0 )
7368 {
7369 flow_counter++;
7370 }
7371 else
7372 {
7373 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7374 }
7375 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007376 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007377 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007378 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007379 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007380 }
7381
7382#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007383 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007384 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7385#endif
7386
7387 /*
7388 * Secondary checks: always done, but change 'ret' only if it was 0
7389 */
7390
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007391#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007392 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007393#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007394 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007395#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007396 mbedtls_pk_context *pk;
7397 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7398 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007399 {
7400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007402 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007403
7404 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007405 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007406 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007407 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007408 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007409
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007410 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007411#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007412
7413 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007414 {
7415 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7416
7417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007418 if( verify_ret == 0 )
7419 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007420 flow_counter++;
7421 }
7422 if( ret == 0 )
7423 {
7424 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007425 }
7426 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007427#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007428
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007429 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007430 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007431 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7432 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007433 &ssl->session_negotiate->verify_result );
7434 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007435 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007436 flow_counter++;
7437 }
7438 else
7439 {
7440 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007442 if( verify_ret == 0 )
7443 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007444 }
7445
7446 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7447 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7448 * with details encoded in the verification flags. All other kinds
7449 * of error codes, including those from the user provided f_vrfy
7450 * functions, are treated as fatal and lead to a failure of
7451 * ssl_parse_certificate even if verification was optional. */
7452 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007453 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7454 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007455 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007456 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007457 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7458 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7459 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7460 {
7461 verify_ret = 0;
7462 flow_counter++;
7463 }
7464 else
7465 {
7466 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7467 }
7468 } else {
7469 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007470 }
7471
7472 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7473 {
7474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007475 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007476 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007477 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007478 else
7479 {
7480 flow_counter++;
7481 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007482
Hanno Becker8c13ee62019-02-26 16:48:17 +00007483 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007484 {
7485 uint8_t alert;
7486
7487 /* The certificate may have been rejected for several reasons.
7488 Pick one and send the corresponding alert. Which alert to send
7489 may be a subject of debate in some cases. */
7490 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7491 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7492 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7493 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7494 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7495 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7496 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7497 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7498 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7499 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7500 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7501 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7502 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7503 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7504 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7505 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7506 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7507 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7508 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7509 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7510 else
7511 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007512 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007513 }
7514
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007515 if( verify_ret == 0 &&
7516#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7517 flow_counter == 5 )
7518#else
7519 flow_counter == 4 )
7520#endif
7521 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007522 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007523 if( verify_ret == 0 &&
7524#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7525 flow_counter == 5 )
7526#else
7527 flow_counter == 4 )
7528#endif
7529 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007531 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7532 }
7533 else
7534 {
7535 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7536 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007537 } else {
7538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007539 }
7540
Hanno Becker3cf50612019-02-05 14:36:34 +00007541#if defined(MBEDTLS_DEBUG_C)
7542 if( ssl->session_negotiate->verify_result != 0 )
7543 {
7544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7545 ssl->session_negotiate->verify_result ) );
7546 }
7547 else
7548 {
7549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7550 }
7551#endif /* MBEDTLS_DEBUG_C */
7552
Hanno Becker8c13ee62019-02-26 16:48:17 +00007553 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007554}
7555
Hanno Becker34106f62019-02-08 14:59:05 +00007556#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007557
7558#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007559static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7560 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007561{
7562 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007563 /* Remember digest of the peer's end-CRT. */
7564 ssl->session_negotiate->peer_cert_digest =
7565 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7566 if( ssl->session_negotiate->peer_cert_digest == NULL )
7567 {
7568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7569 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007570 mbedtls_ssl_pend_fatal_alert( ssl,
7571 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007572
7573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7574 }
7575
7576 ret = mbedtls_md( mbedtls_md_info_from_type(
7577 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7578 start, len,
7579 ssl->session_negotiate->peer_cert_digest );
7580
7581 ssl->session_negotiate->peer_cert_digest_type =
7582 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7583 ssl->session_negotiate->peer_cert_digest_len =
7584 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7585
7586 return( ret );
7587}
Hanno Becker5882dd02019-06-06 16:25:57 +01007588#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007589
7590static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7591 unsigned char *start, size_t len )
7592{
7593 unsigned char *end = start + len;
7594 int ret;
7595
7596 /* Make a copy of the peer's raw public key. */
7597 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7598 ret = mbedtls_pk_parse_subpubkey( &start, end,
7599 &ssl->handshake->peer_pubkey );
7600 if( ret != 0 )
7601 {
7602 /* We should have parsed the public key before. */
7603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7604 }
7605
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007606 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007607 return( 0 );
7608}
7609#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7610
Hanno Becker3cf50612019-02-05 14:36:34 +00007611int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7612{
7613 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007614 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007615#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7616 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7617 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007618 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007619#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007620 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007621#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007622 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007623 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007624
7625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7626
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007627 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7628 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007629 {
7630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007631 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007632 }
7633
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007634#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7635 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007636 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007637 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007638 chain = ssl->handshake->ecrs_peer_cert;
7639 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007640 goto crt_verify;
7641 }
7642#endif
7643
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007644 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007645 {
7646 /* mbedtls_ssl_read_record may have sent an alert already. We
7647 let it decide whether to alert. */
7648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007649 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007650 }
7651
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007652#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007653 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7654 {
7655 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007656
Hanno Beckerb8a08572019-02-05 12:49:06 +00007657 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007658 ret = 0;
7659 else
7660 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007661
Hanno Becker613d4902019-02-05 13:11:17 +00007662 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007663 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007664#endif /* MBEDTLS_SSL_SRV_C */
7665
Hanno Becker35e41772019-02-05 15:37:23 +00007666 /* Clear existing peer CRT structure in case we tried to
7667 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007668 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007669
7670 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7671 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007672 {
7673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7674 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007675 mbedtls_ssl_pend_fatal_alert( ssl,
7676 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007677
Hanno Beckere4aeb762019-02-05 17:19:52 +00007678 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7679 goto exit;
7680 }
7681 mbedtls_x509_crt_init( chain );
7682
7683 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007684 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007685 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007686
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007687#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7688 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007689 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007690
7691crt_verify:
7692 if( ssl->handshake->ecrs_enabled)
7693 rs_ctx = &ssl->handshake->ecrs_ctx;
7694#endif
7695
Hanno Becker3cf50612019-02-05 14:36:34 +00007696 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007697 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007698 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007699 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007700
Hanno Becker3008d282019-02-05 17:02:28 +00007701#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007702 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007703 size_t pk_len;
7704 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007705
Hanno Becker34106f62019-02-08 14:59:05 +00007706 /* We parse the CRT chain without copying, so
7707 * these pointers point into the input buffer,
7708 * and are hence still valid after freeing the
7709 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007710
Hanno Becker5882dd02019-06-06 16:25:57 +01007711#if defined(MBEDTLS_SSL_RENEGOTIATION)
7712 unsigned char *crt_start;
7713 size_t crt_len;
7714
Hanno Becker34106f62019-02-08 14:59:05 +00007715 crt_start = chain->raw.p;
7716 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Hanno Becker8c13ee62019-02-26 16:48:17 +00007719 pk_start = chain->cache->pk_raw.p;
7720 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007721
7722 /* Free the CRT structures before computing
7723 * digest and copying the peer's public key. */
7724 mbedtls_x509_crt_free( chain );
7725 mbedtls_free( chain );
7726 chain = NULL;
7727
Hanno Becker5882dd02019-06-06 16:25:57 +01007728#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007729 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007731 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007732#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007733
Hanno Becker34106f62019-02-08 14:59:05 +00007734 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007736 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007737 }
Hanno Becker34106f62019-02-08 14:59:05 +00007738#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7739 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007740 ssl->session_negotiate->peer_cert = chain;
7741 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007742#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007745
Hanno Becker613d4902019-02-05 13:11:17 +00007746exit:
7747
Hanno Beckere4aeb762019-02-05 17:19:52 +00007748 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007749 {
7750 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7751 {
7752 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7753 }
7754 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7755 {
7756 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7757 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007758 else
7759 {
7760 ssl->state = MBEDTLS_SSL_INVALID;
7761 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007762 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007763
7764#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7765 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7766 {
7767 ssl->handshake->ecrs_peer_cert = chain;
7768 chain = NULL;
7769 }
7770#endif
7771
7772 if( chain != NULL )
7773 {
7774 mbedtls_x509_crt_free( chain );
7775 mbedtls_free( chain );
7776 }
7777
Paul Bakker5121ce52009-01-03 21:22:43 +00007778 return( ret );
7779}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007780#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007783{
7784 int ret;
7785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007789 ssl->out_msglen = 1;
7790 ssl->out_msg[0] = 1;
7791
Jarno Lamsa2b205162019-11-12 15:36:21 +02007792 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7793 {
7794 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7795 }
7796 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7797 {
7798 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7799 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007800 else
7801 {
7802 ssl->state = MBEDTLS_SSL_INVALID;
7803 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007805 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 return( ret );
7809 }
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007812
7813 return( 0 );
7814}
7815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007817{
7818 int ret;
7819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Hanno Becker327c93b2018-08-15 13:56:18 +01007822 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825 return( ret );
7826 }
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007831 mbedtls_ssl_pend_fatal_alert( ssl,
7832 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834 }
7835
Hanno Beckere678eaa2018-08-21 14:57:46 +01007836 /* CCS records are only accepted if they have length 1 and content '1',
7837 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007838
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007839 /*
7840 * Switch to our negotiated transform and session parameters for inbound
7841 * data.
7842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007844 ssl->transform_in = ssl->transform_negotiate;
7845 ssl->session_in = ssl->session_negotiate;
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007851 ssl_dtls_replay_reset( ssl );
7852#endif
7853
7854 /* Increment epoch */
7855 if( ++ssl->in_epoch == 0 )
7856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007858 /* This is highly unlikely to happen for legitimate reasons, so
7859 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007861 }
7862 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007863 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007865#if defined(MBEDTLS_SSL_PROTO_TLS)
7866 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007867 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007868 }
7869#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007870
Hanno Beckerf5970a02019-05-08 09:38:41 +01007871 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7874 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007879 mbedtls_ssl_pend_fatal_alert( ssl,
7880 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007882 }
7883 }
7884#endif
7885
Jarno Lamsa2b205162019-11-12 15:36:21 +02007886 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7887 {
7888 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7889 }
7890 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7891 {
7892 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7893 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007894 else
7895 {
7896 ssl->state = MBEDTLS_SSL_INVALID;
7897 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007900
7901 return( 0 );
7902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7907 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007908 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007909 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7912#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007913 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007916 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007919}
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007924
7925 /*
7926 * Free our handshake params
7927 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007928 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007930 ssl->handshake = NULL;
7931
7932 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007933 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007934 */
7935 if( ssl->transform )
7936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 mbedtls_ssl_transform_free( ssl->transform );
7938 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007939 }
7940 ssl->transform = ssl->transform_negotiate;
7941 ssl->transform_negotiate = NULL;
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007944}
7945
Jarno Lamsae1621d42019-12-19 08:58:56 +02007946int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007947{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007948 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007949
7950#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007951 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007952 ? ssl->handshake->sni_authmode
7953 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7954#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007955 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007956#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007957#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7958 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7959 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_RENEGOTIATION)
7964 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007967 ssl->renego_records_seen = 0;
7968 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007969#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007970
7971 /*
7972 * Free the previous session and switch in the current one
7973 */
Paul Bakker0a597072012-09-25 21:55:46 +00007974 if( ssl->session )
7975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007977 /* RFC 7366 3.1: keep the EtM state */
7978 ssl->session_negotiate->encrypt_then_mac =
7979 ssl->session->encrypt_then_mac;
7980#endif
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 mbedtls_ssl_session_free( ssl->session );
7983 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007984 }
7985 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007986 ssl->session_negotiate = NULL;
7987
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007988#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007989 /*
7990 * Add cache entry
7991 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007992 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007993 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007994 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007995 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007996 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007998 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007999#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008000
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008001 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8002 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8003#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8004 crt_expected == SSL_CERTIFICATE_SKIP )
8005#else
8006 1 )
8007#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008008 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008009 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008010 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8011 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8012#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8013 crt_expected == SSL_CERTIFICATE_SKIP )
8014#else
8015 1 )
8016#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008017 {
8018 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8019 }
8020 else
8021 {
8022 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8023 goto cleanup;
8024 }
8025 }
8026
Jarno Lamsae1621d42019-12-19 08:58:56 +02008027#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008028 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008029 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008030 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008031 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008032 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008033 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008034 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008035 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008036 }
8037 else
8038 {
8039 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008040 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008041 }
8042 }
8043#endif
8044
8045 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8046 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008047 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008048 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8049 {
8050 ret = 0;
8051 }
8052 else
8053 {
8054 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008055 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008056 }
8057 }
8058 else
8059 {
8060 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008061 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008062 }
8063
Jarno Lamsa06164052019-12-19 14:40:36 +02008064 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8065 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8066 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8067 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008068 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008069 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8070 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8071 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8072 {
8073 ret = 0;
8074 }
8075 else
8076 {
8077 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8078 goto cleanup;
8079 }
8080 }
8081 else
8082 {
8083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8086 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8087 }
8088
8089cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008091 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008092 ssl->handshake->flight != NULL )
8093 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008094 /* Cancel handshake timer */
8095 ssl_set_timer( ssl, 0 );
8096
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008097 /* Keep last flight around in case we need to resend it:
8098 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008100 }
8101 else
8102#endif
8103 ssl_handshake_wrapup_free_hs_transform( ssl );
8104
Jarno Lamsa2b205162019-11-12 15:36:21 +02008105 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008108 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008109}
8110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008112{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008113 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008116
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008117 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008118
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008119 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8120 mbedtls_ssl_suite_get_mac(
8121 mbedtls_ssl_ciphersuite_from_id(
8122 mbedtls_ssl_session_get_ciphersuite(
8123 ssl->session_negotiate ) ) ),
8124 ssl, ssl->out_msg + 4,
8125 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008126
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008127 /*
8128 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8129 * may define some other value. Currently (early 2016), no defined
8130 * ciphersuite does this (and this is unlikely to change as activity has
8131 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8132 */
Hanno Becker2881d802019-05-22 14:44:53 +01008133 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008136 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008137 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008138#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008139
Paul Bakker5121ce52009-01-03 21:22:43 +00008140 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8142 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008143
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008144#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008145 /*
8146 * In case of session resuming, invert the client and server
8147 * ChangeCipherSpec messages order.
8148 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008149 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008152 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8153 MBEDTLS_SSL_IS_CLIENT )
8154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008156 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008159 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8160 MBEDTLS_SSL_IS_SERVER )
8161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008163 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008165 }
8166 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008167#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008168 {
8169 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8170 {
8171 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8172 }
8173 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8174 {
8175 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8176 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008177 else
8178 {
8179 ssl->state = MBEDTLS_SSL_INVALID;
8180 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008181 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008182
Paul Bakker48916f92012-09-16 19:57:18 +00008183 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008184 * Switch to our negotiated transform and session parameters for outbound
8185 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008190 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008191 {
8192 unsigned char i;
8193
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008194 /* Remember current epoch settings for resending */
8195 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008196 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008197
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008198 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008199 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008200
8201 /* Increment epoch */
8202 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008203 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008204 break;
8205
8206 /* The loop goes to its end iff the counter is wrapping */
8207 if( i == 0 )
8208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8210 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008211 }
8212 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008213 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008215#if defined(MBEDTLS_SSL_PROTO_TLS)
8216 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008217 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008218 }
8219#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008220
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008221 ssl->transform_out = ssl->transform_negotiate;
8222 ssl->session_out = ssl->session_negotiate;
8223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8225 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8230 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008231 }
8232 }
8233#endif
8234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008236 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008238#endif
8239
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008240 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008241 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008243 return( ret );
8244 }
8245
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008247 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008248 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8249 {
8250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8251 return( ret );
8252 }
8253#endif
8254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008256
8257 return( 0 );
8258}
8259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008261#define SSL_MAX_HASH_LEN 36
8262#else
8263#define SSL_MAX_HASH_LEN 12
8264#endif
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008267{
Paul Bakker23986e52011-04-24 08:57:21 +00008268 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008269 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008270 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008273
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008274 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8275 mbedtls_ssl_suite_get_mac(
8276 mbedtls_ssl_ciphersuite_from_id(
8277 mbedtls_ssl_session_get_ciphersuite(
8278 ssl->session_negotiate ) ) ),
8279 ssl, buf,
8280 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008281
Hanno Becker327c93b2018-08-15 13:56:18 +01008282 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008285 return( ret );
8286 }
8287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008294 }
8295
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008296 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008298 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008299 hash_len = 36;
8300 else
8301#endif
8302 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8305 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008308 mbedtls_ssl_pend_fatal_alert( ssl,
8309 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 }
8312
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008313 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008314 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008317 mbedtls_ssl_pend_fatal_alert( ssl,
8318 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008320 }
8321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008323 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008324 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008325#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008326
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008327#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008328 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008331 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008335 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 }
8339 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008340#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008341 {
8342 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8343 {
8344 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8345 }
8346 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8347 {
8348 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8349 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008350 else
8351 {
8352 ssl->state = MBEDTLS_SSL_INVALID;
8353 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008357 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008359#endif
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008362
8363 return( 0 );
8364}
8365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8371 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8372 mbedtls_md5_init( &handshake->fin_md5 );
8373 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008374 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8375 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8378#if defined(MBEDTLS_SHA256_C)
8379 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008380 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SHA512_C)
8383 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008384 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008387
Hanno Becker7e5437a2017-04-28 17:15:26 +01008388#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8389 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8390 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8391#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393#if defined(MBEDTLS_DHM_C)
8394 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_ECDH_C)
8397 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008398#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008399#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008400 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008401#if defined(MBEDTLS_SSL_CLI_C)
8402 handshake->ecjpake_cache = NULL;
8403 handshake->ecjpake_cache_len = 0;
8404#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008405#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008406
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008407#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008408 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008409#endif
8410
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008411#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8412 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8413#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008414
8415#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8416 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8417 mbedtls_pk_init( &handshake->peer_pubkey );
8418#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008419}
8420
Hanno Becker611a83b2018-01-03 14:27:32 +00008421void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8426 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008427
Hanno Becker92231322018-01-03 15:32:51 +00008428#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 mbedtls_md_init( &transform->md_ctx_enc );
8430 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008431#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008432}
8433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008437}
8438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008440{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008441 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008442 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008444 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008446 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008447 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008448
8449 /*
8450 * Either the pointers are now NULL or cleared properly and can be freed.
8451 * Now allocate missing structures.
8452 */
8453 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008454 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008455 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008456 }
Paul Bakker48916f92012-09-16 19:57:18 +00008457
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008458 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008459 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008460 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008461 }
Paul Bakker48916f92012-09-16 19:57:18 +00008462
Paul Bakker82788fb2014-10-20 13:59:19 +02008463 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008464 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008465 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008466 }
Paul Bakker48916f92012-09-16 19:57:18 +00008467
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008468 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008469 if( ssl->handshake == NULL ||
8470 ssl->transform_negotiate == NULL ||
8471 ssl->session_negotiate == NULL )
8472 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 mbedtls_free( ssl->handshake );
8476 mbedtls_free( ssl->transform_negotiate );
8477 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008478
8479 ssl->handshake = NULL;
8480 ssl->transform_negotiate = NULL;
8481 ssl->session_negotiate = NULL;
8482
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008483 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008484 }
8485
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008486 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008488 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008489 ssl_handshake_params_init( ssl->handshake );
8490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008492 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008493 {
8494 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008495
Hanno Becker2d9623f2019-06-13 12:07:05 +01008496 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008497 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8498 else
8499 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8500 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008501#endif
8502
Paul Bakker48916f92012-09-16 19:57:18 +00008503 return( 0 );
8504}
8505
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008506#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008507/* Dummy cookie callbacks for defaults */
8508static int ssl_cookie_write_dummy( void *ctx,
8509 unsigned char **p, unsigned char *end,
8510 const unsigned char *cli_id, size_t cli_id_len )
8511{
8512 ((void) ctx);
8513 ((void) p);
8514 ((void) end);
8515 ((void) cli_id);
8516 ((void) cli_id_len);
8517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008519}
8520
8521static int ssl_cookie_check_dummy( void *ctx,
8522 const unsigned char *cookie, size_t cookie_len,
8523 const unsigned char *cli_id, size_t cli_id_len )
8524{
8525 ((void) ctx);
8526 ((void) cookie);
8527 ((void) cookie_len);
8528 ((void) cli_id);
8529 ((void) cli_id_len);
8530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008532}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008533#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008534
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008535/* Once ssl->out_hdr as the address of the beginning of the
8536 * next outgoing record is set, deduce the other pointers.
8537 *
8538 * Note: For TLS, we save the implicit record sequence number
8539 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8540 * and the caller has to make sure there's space for this.
8541 */
8542
8543static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8544 mbedtls_ssl_transform *transform )
8545{
8546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008547 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008548 {
8549 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008550#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008551 ssl->out_cid = ssl->out_ctr + 8;
8552 ssl->out_len = ssl->out_cid;
8553 if( transform != NULL )
8554 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008555#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008556 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008558 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008559 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008560 MBEDTLS_SSL_TRANSPORT_ELSE
8561#endif /* MBEDTLS_SSL_PROTO_DTLS */
8562#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008563 {
8564 ssl->out_ctr = ssl->out_hdr - 8;
8565 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008566#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008567 ssl->out_cid = ssl->out_len;
8568#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008569 ssl->out_iv = ssl->out_hdr + 5;
8570 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008571#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008572
8573 /* Adjust out_msg to make space for explicit IV, if used. */
8574 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008575 mbedtls_ssl_ver_geq(
8576 mbedtls_ssl_get_minor_ver( ssl ),
8577 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008578 {
8579 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8580 }
8581 else
8582 ssl->out_msg = ssl->out_iv;
8583}
8584
8585/* Once ssl->in_hdr as the address of the beginning of the
8586 * next incoming record is set, deduce the other pointers.
8587 *
8588 * Note: For TLS, we save the implicit record sequence number
8589 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8590 * and the caller has to make sure there's space for this.
8591 */
8592
Hanno Beckerf5970a02019-05-08 09:38:41 +01008593static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008594{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008595 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008596 * of unprotected TLS/DTLS records, with ssl->in_msg
8597 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008598 *
8599 * When decrypting a protected record, ssl->in_msg
8600 * will be shifted to point to the beginning of the
8601 * record plaintext.
8602 */
8603
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008606 {
Hanno Becker70e79282019-05-03 14:34:53 +01008607 /* This sets the header pointers to match records
8608 * without CID. When we receive a record containing
8609 * a CID, the fields are shifted accordingly in
8610 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008611 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008613 ssl->in_cid = ssl->in_ctr + 8;
8614 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008615#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008616 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008617#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008618 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008619 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008620 MBEDTLS_SSL_TRANSPORT_ELSE
8621#endif /* MBEDTLS_SSL_PROTO_DTLS */
8622#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008623 {
8624 ssl->in_ctr = ssl->in_hdr - 8;
8625 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008627 ssl->in_cid = ssl->in_len;
8628#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008629 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008630 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008631#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008632}
8633
Paul Bakker5121ce52009-01-03 21:22:43 +00008634/*
8635 * Initialize an SSL context
8636 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008637void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8638{
8639 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8640}
8641
8642/*
8643 * Setup an SSL context
8644 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008645
8646static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8647{
8648 /* Set the incoming and outgoing record pointers. */
8649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008651 {
8652 ssl->out_hdr = ssl->out_buf;
8653 ssl->in_hdr = ssl->in_buf;
8654 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008655 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008657#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008658 {
8659 ssl->out_hdr = ssl->out_buf + 8;
8660 ssl->in_hdr = ssl->in_buf + 8;
8661 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008662#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008663
8664 /* Derive other internal pointers. */
8665 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008666 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008667}
8668
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008669int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008670 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008671{
Paul Bakker48916f92012-09-16 19:57:18 +00008672 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008673
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008674 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008675
Hanno Beckeref982d52019-07-23 15:56:18 +01008676#if defined(MBEDTLS_USE_TINYCRYPT)
8677 uECC_set_rng( &uecc_rng_wrapper );
8678#endif
8679
Paul Bakker62f2dee2012-09-28 07:31:51 +00008680 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008681 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008682 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008683
8684 /* Set to NULL in case of an error condition */
8685 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008686
Angus Grattond8213d02016-05-25 20:56:48 +10008687 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8688 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008689 {
Angus Grattond8213d02016-05-25 20:56:48 +10008690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008691 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008692 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008693 }
8694
8695 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8696 if( ssl->out_buf == NULL )
8697 {
8698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008700 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008701 }
8702
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008703 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008704
Paul Bakker48916f92012-09-16 19:57:18 +00008705 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008706 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008707
Hanno Beckerc8f52992019-07-25 11:15:08 +01008708 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008709
Paul Bakker5121ce52009-01-03 21:22:43 +00008710 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008711
8712error:
8713 mbedtls_free( ssl->in_buf );
8714 mbedtls_free( ssl->out_buf );
8715
8716 ssl->conf = NULL;
8717
8718 ssl->in_buf = NULL;
8719 ssl->out_buf = NULL;
8720
8721 ssl->in_hdr = NULL;
8722 ssl->in_ctr = NULL;
8723 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008724 ssl->in_msg = NULL;
8725
8726 ssl->out_hdr = NULL;
8727 ssl->out_ctr = NULL;
8728 ssl->out_len = NULL;
8729 ssl->out_iv = NULL;
8730 ssl->out_msg = NULL;
8731
k-stachowiak9f7798e2018-07-31 16:52:32 +02008732 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008733}
8734
8735/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008736 * Reset an initialized and used SSL context for re-use while retaining
8737 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008738 *
8739 * If partial is non-zero, keep data in the input buffer and client ID.
8740 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008741 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008742static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008743{
Paul Bakker48916f92012-09-16 19:57:18 +00008744 int ret;
8745
Hanno Becker7e772132018-08-10 12:38:21 +01008746#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8747 !defined(MBEDTLS_SSL_SRV_C)
8748 ((void) partial);
8749#endif
8750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008751 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008752
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008753 /* Cancel any possibly running timer */
8754 ssl_set_timer( ssl, 0 );
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_SSL_RENEGOTIATION)
8757 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008758 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008759
8760 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008761 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8762 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008765
Paul Bakker7eb013f2011-10-06 12:37:39 +00008766 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008767 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008768
8769 ssl->in_msgtype = 0;
8770 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008772 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008773 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008776 ssl_dtls_replay_reset( ssl );
8777#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008778
8779 ssl->in_hslen = 0;
8780 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008781
8782 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008783
8784 ssl->out_msgtype = 0;
8785 ssl->out_msglen = 0;
8786 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8788 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008789 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008790#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008791
Hanno Becker19859472018-08-06 09:40:20 +01008792 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8793
Paul Bakker48916f92012-09-16 19:57:18 +00008794 ssl->transform_in = NULL;
8795 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008796
Hanno Becker78640902018-08-13 16:35:15 +01008797 ssl->session_in = NULL;
8798 ssl->session_out = NULL;
8799
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008800 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008801
8802#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008803 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008804#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8805 {
8806 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008807 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008808 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8811 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8814 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008818 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008819 }
8820#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008821
Paul Bakker48916f92012-09-16 19:57:18 +00008822 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 mbedtls_ssl_transform_free( ssl->transform );
8825 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008826 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008827 }
Paul Bakker48916f92012-09-16 19:57:18 +00008828
Paul Bakkerc0463502013-02-14 11:19:38 +01008829 if( ssl->session )
8830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 mbedtls_ssl_session_free( ssl->session );
8832 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008833 ssl->session = NULL;
8834 }
8835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008837 ssl->alpn_chosen = NULL;
8838#endif
8839
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008840#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008841#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008842 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008843#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008844 {
8845 mbedtls_free( ssl->cli_id );
8846 ssl->cli_id = NULL;
8847 ssl->cli_id_len = 0;
8848 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008849#endif
8850
Paul Bakker48916f92012-09-16 19:57:18 +00008851 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8852 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008853
8854 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008855}
8856
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008857/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008858 * Reset an initialized and used SSL context for re-use while retaining
8859 * all application-set variables, function pointers and data.
8860 */
8861int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8862{
8863 return( ssl_session_reset_int( ssl, 0 ) );
8864}
8865
8866/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008867 * SSL set accessors
8868 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008869#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008870void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008871{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008872 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008873}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008874#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008876void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008877{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008878 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008879}
8880
Hanno Becker7f376f42019-06-12 16:20:48 +01008881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8882 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008883void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008884{
Hanno Becker7f376f42019-06-12 16:20:48 +01008885 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008886}
Hanno Becker7f376f42019-06-12 16:20:48 +01008887#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008888
Hanno Beckerde671542019-06-12 16:30:46 +01008889#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8890 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8891void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8892 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008893{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008894 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008895}
Hanno Beckerde671542019-06-12 16:30:46 +01008896#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008899
Hanno Becker1841b0a2018-08-24 11:13:57 +01008900void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8901 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008902{
8903 ssl->disable_datagram_packing = !allow_packing;
8904}
8905
Hanno Becker1f835fa2019-06-13 10:14:59 +01008906#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8907 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008908void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8909 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008910{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008911 conf->hs_timeout_min = min;
8912 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008913}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008914#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8915 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8916void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8917 uint32_t min, uint32_t max )
8918{
8919 ((void) conf);
8920 ((void) min);
8921 ((void) max);
8922}
8923#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8924 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8925
8926#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008927
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008928void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008929{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008930#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8931 conf->authmode = authmode;
8932#else
8933 ((void) conf);
8934 ((void) authmode);
8935#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008936}
8937
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008938#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8939 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008940void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008941 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008942 void *p_vrfy )
8943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008944 conf->f_vrfy = f_vrfy;
8945 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008946}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008947#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008948
Hanno Beckerece325c2019-06-13 15:39:27 +01008949#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008950void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008951 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008952 void *p_rng )
8953{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008954 conf->f_rng = f_rng;
8955 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008956}
Hanno Beckerece325c2019-06-13 15:39:27 +01008957#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008958
Hanno Becker14a4a442019-07-02 17:00:34 +01008959#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008960void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008961 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008962 void *p_dbg )
8963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008964 conf->f_dbg = f_dbg;
8965 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008966}
Hanno Becker14a4a442019-07-02 17:00:34 +01008967#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008968
Hanno Beckera58a8962019-06-13 16:11:15 +01008969#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8970 !defined(MBEDTLS_SSL_CONF_SEND) && \
8971 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008973 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008974 mbedtls_ssl_send_t *f_send,
8975 mbedtls_ssl_recv_t *f_recv,
8976 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008977{
Hanno Beckera58a8962019-06-13 16:11:15 +01008978 ssl->p_bio = p_bio;
8979 ssl->f_send = f_send;
8980 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008981 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008982}
Hanno Beckera58a8962019-06-13 16:11:15 +01008983#else
8984void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8985 void *p_bio )
8986{
8987 ssl->p_bio = p_bio;
8988}
8989#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008990
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008991#if defined(MBEDTLS_SSL_PROTO_DTLS)
8992void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8993{
8994 ssl->mtu = mtu;
8995}
8996#endif
8997
Hanno Becker1f835fa2019-06-13 10:14:59 +01008998#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008999void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009000{
9001 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009002}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009003#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009004
Hanno Becker0ae6b242019-06-13 16:45:36 +01009005#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9006 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009007void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9008 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009009 mbedtls_ssl_set_timer_t *f_set_timer,
9010 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009011{
9012 ssl->p_timer = p_timer;
9013 ssl->f_set_timer = f_set_timer;
9014 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009015 /* Make sure we start with no timer running */
9016 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009017}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009018#else
9019void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9020 void *p_timer )
9021{
9022 ssl->p_timer = p_timer;
9023 /* Make sure we start with no timer running */
9024 ssl_set_timer( ssl, 0 );
9025}
9026#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009027
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009028#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009029void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009030 void *p_cache,
9031 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9032 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009033{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009034 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009035 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009036 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009037}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009038#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009039
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009040#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009042{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009043 int ret;
9044
9045 if( ssl == NULL ||
9046 session == NULL ||
9047 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009048 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009051 }
9052
Hanno Becker58fccf22019-02-06 14:30:46 +00009053 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9054 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009055 return( ret );
9056
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009057 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009058
9059 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009060}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009061#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009062
Hanno Becker73f4cb12019-06-27 13:51:07 +01009063#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009064void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009066{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009067 conf->ciphersuite_list[0] = ciphersuites;
9068 conf->ciphersuite_list[1] = ciphersuites;
9069 conf->ciphersuite_list[2] = ciphersuites;
9070 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009071}
9072
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009074 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009075 int major, int minor )
9076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009078 return;
9079
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009080 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9081 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9082 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009083 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009084 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009085
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009086 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9087 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009088}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009089#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009092void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009093 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009094{
9095 conf->cert_profile = profile;
9096}
9097
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009098/* Append a new keycert entry to a (possibly empty) list */
9099static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9100 mbedtls_x509_crt *cert,
9101 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009102{
niisato8ee24222018-06-25 19:05:48 +09009103 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009104
niisato8ee24222018-06-25 19:05:48 +09009105 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9106 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009108
niisato8ee24222018-06-25 19:05:48 +09009109 new_cert->cert = cert;
9110 new_cert->key = key;
9111 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009112
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009113 /* Update head is the list was null, else add to the end */
9114 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009115 {
niisato8ee24222018-06-25 19:05:48 +09009116 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009117 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009118 else
9119 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009120 mbedtls_ssl_key_cert *cur = *head;
9121 while( cur->next != NULL )
9122 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009123 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009124 }
9125
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009126 return( 0 );
9127}
9128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009129int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009130 mbedtls_x509_crt *own_cert,
9131 mbedtls_pk_context *pk_key )
9132{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009133 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009134}
9135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009137 mbedtls_x509_crt *ca_chain,
9138 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009139{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009140 conf->ca_chain = ca_chain;
9141 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009144
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009145#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9146int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9147 mbedtls_x509_crt *own_cert,
9148 mbedtls_pk_context *pk_key )
9149{
9150 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9151 own_cert, pk_key ) );
9152}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009153
9154void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9155 mbedtls_x509_crt *ca_chain,
9156 mbedtls_x509_crl *ca_crl )
9157{
9158 ssl->handshake->sni_ca_chain = ca_chain;
9159 ssl->handshake->sni_ca_crl = ca_crl;
9160}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009161
9162void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9163 int authmode )
9164{
9165 ssl->handshake->sni_authmode = authmode;
9166}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009167#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9168
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009169#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009170/*
9171 * Set EC J-PAKE password for current handshake
9172 */
9173int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9174 const unsigned char *pw,
9175 size_t pw_len )
9176{
9177 mbedtls_ecjpake_role role;
9178
Janos Follath8eb64132016-06-03 15:40:57 +01009179 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9181
Hanno Becker2d9623f2019-06-13 12:07:05 +01009182 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009183 role = MBEDTLS_ECJPAKE_SERVER;
9184 else
9185 role = MBEDTLS_ECJPAKE_CLIENT;
9186
9187 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9188 role,
9189 MBEDTLS_MD_SHA256,
9190 MBEDTLS_ECP_DP_SECP256R1,
9191 pw, pw_len ) );
9192}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009193#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009196int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009197 const unsigned char *psk, size_t psk_len,
9198 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009199{
Paul Bakker6db455e2013-09-18 17:29:31 +02009200 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009205
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009206 /* Identity len will be encoded on two bytes */
9207 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009208 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009209 {
9210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9211 }
9212
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009213 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009214 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009215 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009216
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009217 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009218 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009219 conf->psk_len = 0;
9220 }
9221 if( conf->psk_identity != NULL )
9222 {
9223 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009224 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009225 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009226 }
9227
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009228 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9229 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009230 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009231 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009232 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009233 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009234 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009235 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009236 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009237
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009238 conf->psk_len = psk_len;
9239 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009240
Teppo Järvelin91d79382019-10-02 09:09:31 +03009241 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9242 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009243
9244 return( 0 );
9245}
9246
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009247int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9248 const unsigned char *psk, size_t psk_len )
9249{
9250 if( psk == NULL || ssl->handshake == NULL )
9251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9252
9253 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9255
9256 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009257 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009258 mbedtls_platform_zeroize( ssl->handshake->psk,
9259 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009260 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009261 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009262 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009263
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009264 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009265 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009266
9267 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009268 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009269
9270 return( 0 );
9271}
9272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009273void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009275 size_t),
9276 void *p_psk )
9277{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009278 conf->f_psk = f_psk;
9279 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009282
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009283#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009284
9285#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009286int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009287{
9288 int ret;
9289
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009290 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9291 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9292 {
9293 mbedtls_mpi_free( &conf->dhm_P );
9294 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009295 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009297
9298 return( 0 );
9299}
Hanno Becker470a8c42017-10-04 15:28:46 +01009300#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009301
Hanno Beckera90658f2017-10-04 15:29:08 +01009302int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9303 const unsigned char *dhm_P, size_t P_len,
9304 const unsigned char *dhm_G, size_t G_len )
9305{
9306 int ret;
9307
9308 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9309 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9310 {
9311 mbedtls_mpi_free( &conf->dhm_P );
9312 mbedtls_mpi_free( &conf->dhm_G );
9313 return( ret );
9314 }
9315
9316 return( 0 );
9317}
Paul Bakker5121ce52009-01-03 21:22:43 +00009318
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009319int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009320{
9321 int ret;
9322
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009323 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9324 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9325 {
9326 mbedtls_mpi_free( &conf->dhm_P );
9327 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009328 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009329 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009330
9331 return( 0 );
9332}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009333#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009334
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009335#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9336/*
9337 * Set the minimum length for Diffie-Hellman parameters
9338 */
9339void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9340 unsigned int bitlen )
9341{
9342 conf->dhm_min_bitlen = bitlen;
9343}
9344#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9345
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009346#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009347/*
9348 * Set allowed/preferred hashes for handshake signatures
9349 */
9350void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9351 const int *hashes )
9352{
Hanno Becker56595f42019-06-19 16:31:38 +01009353#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009354 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009355#else
9356 ((void) conf);
9357 ((void) hashes);
9358#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009359}
Hanno Becker947194e2017-04-07 13:25:49 +01009360#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009361
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009362#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009363#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009364/*
9365 * Set the allowed elliptic curves
9366 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009367void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009368 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009369{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009370 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009371}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009372#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009373#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009374
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009375#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009377{
Hanno Becker947194e2017-04-07 13:25:49 +01009378 /* Initialize to suppress unnecessary compiler warning */
9379 size_t hostname_len = 0;
9380
9381 /* Check if new hostname is valid before
9382 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009383 if( hostname != NULL )
9384 {
9385 hostname_len = strlen( hostname );
9386
9387 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9389 }
9390
9391 /* Now it's clear that we will overwrite the old hostname,
9392 * so we can free it safely */
9393
9394 if( ssl->hostname != NULL )
9395 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009396 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009397 mbedtls_free( ssl->hostname );
9398 }
9399
9400 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009401
Paul Bakker5121ce52009-01-03 21:22:43 +00009402 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009403 {
9404 ssl->hostname = NULL;
9405 }
9406 else
9407 {
9408 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009409 if( ssl->hostname == NULL )
9410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009411
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009412 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9413 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009414
Hanno Becker947194e2017-04-07 13:25:49 +01009415 ssl->hostname[hostname_len] = '\0';
9416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009417
9418 return( 0 );
9419}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009420#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009421
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009422#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009423void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009425 const unsigned char *, size_t),
9426 void *p_sni )
9427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009428 conf->f_sni = f_sni;
9429 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009434int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009435{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009436 size_t cur_len, tot_len;
9437 const char **p;
9438
9439 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009440 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9441 * MUST NOT be truncated."
9442 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009443 */
9444 tot_len = 0;
9445 for( p = protos; *p != NULL; p++ )
9446 {
9447 cur_len = strlen( *p );
9448 tot_len += cur_len;
9449
9450 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009452 }
9453
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009454 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009455
9456 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009457}
9458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009460{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009461 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009464
Hanno Becker33b9b252019-07-05 11:23:25 +01009465#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9466 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9467void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9468 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009470 conf->max_major_ver = major;
9471 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009472}
Hanno Becker33b9b252019-07-05 11:23:25 +01009473#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9474 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009475
Hanno Becker33b9b252019-07-05 11:23:25 +01009476#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9477 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9478void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9479 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009481 conf->min_major_ver = major;
9482 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009483}
Hanno Becker33b9b252019-07-05 11:23:25 +01009484#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9485 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009488void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009489{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009490 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009491}
9492#endif
9493
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009494#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009495void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9496 char cert_req_ca_list )
9497{
9498 conf->cert_req_ca_list = cert_req_ca_list;
9499}
9500#endif
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009503void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009505 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009506}
9507#endif
9508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009510#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009511void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009512{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009513 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009514}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009515#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9516#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009517void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009518 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009519{
9520 conf->enforce_extended_master_secret = ems_enf;
9521}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009522#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009523#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009524
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009525#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009526void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009527{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009528 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009529}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009530#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009533int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009536 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009539 }
9540
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009541 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009542
9543 return( 0 );
9544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009548void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009550 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009555void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009556{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009557 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009558}
9559#endif
9560
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009561#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009562void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009564 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009565}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009566#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009569void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009571 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009572}
9573
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009574void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009576 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009577}
9578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009579void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009580 const unsigned char period[8] )
9581{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009582 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009587#if defined(MBEDTLS_SSL_CLI_C)
9588void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009589{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009590 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009591}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009592#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009593
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009595void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9596 mbedtls_ssl_ticket_write_t *f_ticket_write,
9597 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9598 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009599{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009600 conf->f_ticket_write = f_ticket_write;
9601 conf->f_ticket_parse = f_ticket_parse;
9602 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009603}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009606
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009607#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9608void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9609 mbedtls_ssl_export_keys_t *f_export_keys,
9610 void *p_export_keys )
9611{
9612 conf->f_export_keys = f_export_keys;
9613 conf->p_export_keys = p_export_keys;
9614}
9615#endif
9616
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009617#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009618void mbedtls_ssl_conf_async_private_cb(
9619 mbedtls_ssl_config *conf,
9620 mbedtls_ssl_async_sign_t *f_async_sign,
9621 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9622 mbedtls_ssl_async_resume_t *f_async_resume,
9623 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009624 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009625{
9626 conf->f_async_sign_start = f_async_sign;
9627 conf->f_async_decrypt_start = f_async_decrypt;
9628 conf->f_async_resume = f_async_resume;
9629 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009630 conf->p_async_config_data = async_config_data;
9631}
9632
Gilles Peskine8f97af72018-04-26 11:46:10 +02009633void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9634{
9635 return( conf->p_async_config_data );
9636}
9637
Gilles Peskine1febfef2018-04-30 11:54:39 +02009638void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009639{
9640 if( ssl->handshake == NULL )
9641 return( NULL );
9642 else
9643 return( ssl->handshake->user_async_ctx );
9644}
9645
Gilles Peskine1febfef2018-04-30 11:54:39 +02009646void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009647 void *ctx )
9648{
9649 if( ssl->handshake != NULL )
9650 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009651}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009652#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009653
Paul Bakker5121ce52009-01-03 21:22:43 +00009654/*
9655 * SSL get accessors
9656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009658{
9659 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9660}
9661
Hanno Becker8b170a02017-10-10 11:51:19 +01009662int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9663{
9664 /*
9665 * Case A: We're currently holding back
9666 * a message for further processing.
9667 */
9668
9669 if( ssl->keep_current_message == 1 )
9670 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009672 return( 1 );
9673 }
9674
9675 /*
9676 * Case B: Further records are pending in the current datagram.
9677 */
9678
9679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009680 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009681 ssl->in_left > ssl->next_record_offset )
9682 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009684 return( 1 );
9685 }
9686#endif /* MBEDTLS_SSL_PROTO_DTLS */
9687
9688 /*
9689 * Case C: A handshake message is being processed.
9690 */
9691
Hanno Becker8b170a02017-10-10 11:51:19 +01009692 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9693 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009695 return( 1 );
9696 }
9697
9698 /*
9699 * Case D: An application data message is being processed
9700 */
9701 if( ssl->in_offt != NULL )
9702 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009704 return( 1 );
9705 }
9706
9707 /*
9708 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009709 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009710 * we implement support for multiple alerts in single records.
9711 */
9712
9713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9714 return( 0 );
9715}
9716
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009717uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009718{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009719 if( ssl->session != NULL )
9720 return( ssl->session->verify_result );
9721
9722 if( ssl->session_negotiate != NULL )
9723 return( ssl->session_negotiate->verify_result );
9724
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009725 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009726}
9727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009729{
Hanno Beckere02758c2019-06-26 15:31:31 +01009730 int suite;
9731
Paul Bakker926c8e42013-03-06 10:23:34 +01009732 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009733 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009734
Hanno Beckere02758c2019-06-26 15:31:31 +01009735 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9736 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009737}
9738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009742 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009743 {
Hanno Becker2881d802019-05-22 14:44:53 +01009744 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009747 return( "DTLSv1.0" );
9748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009750 return( "DTLSv1.2" );
9751
9752 default:
9753 return( "unknown (DTLS)" );
9754 }
9755 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009756 MBEDTLS_SSL_TRANSPORT_ELSE
9757#endif /* MBEDTLS_SSL_PROTO_DTLS */
9758#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009759 {
Hanno Becker2881d802019-05-22 14:44:53 +01009760 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009761 {
9762 case MBEDTLS_SSL_MINOR_VERSION_0:
9763 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009764
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009765 case MBEDTLS_SSL_MINOR_VERSION_1:
9766 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009767
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009768 case MBEDTLS_SSL_MINOR_VERSION_2:
9769 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009770
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009771 case MBEDTLS_SSL_MINOR_VERSION_3:
9772 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009773
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009774 default:
9775 return( "unknown" );
9776 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009777 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009778#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009779}
9780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009782{
Hanno Becker3136ede2018-08-17 15:28:19 +01009783 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009785
Hanno Becker43395762019-05-03 14:46:38 +01009786 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9787
Hanno Becker78640902018-08-13 16:35:15 +01009788 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009789 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#if defined(MBEDTLS_ZLIB_SUPPORT)
9792 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9793 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009794#endif
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009797 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009798#if defined(MBEDTLS_GCM_C) || \
9799 defined(MBEDTLS_CCM_C) || \
9800 defined(MBEDTLS_CHACHAPOLY_C)
9801#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009803#endif
9804#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009806#endif
9807#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009808 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009809#endif
9810 transform_expansion =
9811 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009812 break;
9813
Hanno Beckera9d5c452019-07-25 16:47:12 +01009814#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9815 MBEDTLS_CHACHAPOLY_C */
9816
9817#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9818 case MBEDTLS_MODE_STREAM:
9819 transform_expansion = transform->maclen;
9820 break;
9821#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9822
9823#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009825 {
9826 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009827
9828 block_size = mbedtls_cipher_get_block_size(
9829 &transform->cipher_ctx_enc );
9830
Hanno Becker3136ede2018-08-17 15:28:19 +01009831 /* Expansion due to the addition of the MAC. */
9832 transform_expansion += transform->maclen;
9833
9834 /* Expansion due to the addition of CBC padding;
9835 * Theoretically up to 256 bytes, but we never use
9836 * more than the block size of the underlying cipher. */
9837 transform_expansion += block_size;
9838
9839 /* For TLS 1.1 or higher, an explicit IV is added
9840 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009841#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009842 if( mbedtls_ssl_ver_geq(
9843 mbedtls_ssl_get_minor_ver( ssl ),
9844 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9845 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009846 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009847 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009848#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009849
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009850 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009851 }
9852#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009853
9854 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009857 }
9858
Hanno Beckera5a2b082019-05-15 14:03:01 +01009859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009860 if( transform->out_cid_len != 0 )
9861 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009862#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009863
Hanno Becker43395762019-05-03 14:46:38 +01009864 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009865}
9866
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009867#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9868size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9869{
9870 size_t max_len;
9871
9872 /*
9873 * Assume mfl_code is correct since it was checked when set
9874 */
Angus Grattond8213d02016-05-25 20:56:48 +10009875 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009876
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009877 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009878 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009879 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009880 {
Angus Grattond8213d02016-05-25 20:56:48 +10009881 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009882 }
9883
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009884 /* During a handshake, use the value being negotiated */
9885 if( ssl->session_negotiate != NULL &&
9886 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9887 {
9888 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9889 }
9890
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009891 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009892}
9893#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9894
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009895#if defined(MBEDTLS_SSL_PROTO_DTLS)
9896static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9897{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009898 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009899 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009900 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9901 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009902 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009903
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009904 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9905 return( ssl->mtu );
9906
9907 if( ssl->mtu == 0 )
9908 return( ssl->handshake->mtu );
9909
9910 return( ssl->mtu < ssl->handshake->mtu ?
9911 ssl->mtu : ssl->handshake->mtu );
9912}
9913#endif /* MBEDTLS_SSL_PROTO_DTLS */
9914
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009915int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9916{
9917 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9918
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009919#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9920 !defined(MBEDTLS_SSL_PROTO_DTLS)
9921 (void) ssl;
9922#endif
9923
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009924#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9925 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9926
9927 if( max_len > mfl )
9928 max_len = mfl;
9929#endif
9930
9931#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009932 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009933 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009934 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009935 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9936 const size_t overhead = (size_t) ret;
9937
9938 if( ret < 0 )
9939 return( ret );
9940
9941 if( mtu <= overhead )
9942 {
9943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9944 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9945 }
9946
9947 if( max_len > mtu - overhead )
9948 max_len = mtu - overhead;
9949 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009950#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009951
Hanno Becker0defedb2018-08-10 12:35:02 +01009952#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9953 !defined(MBEDTLS_SSL_PROTO_DTLS)
9954 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009955#endif
9956
9957 return( (int) max_len );
9958}
9959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960#if defined(MBEDTLS_X509_CRT_PARSE_C)
9961const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009962{
9963 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009964 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009965
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009966#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009967 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009968#else
9969 return( NULL );
9970#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009975int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9976 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009977{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009978 if( ssl == NULL ||
9979 dst == NULL ||
9980 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009981 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009984 }
9985
Hanno Becker58fccf22019-02-06 14:30:46 +00009986 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009989
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009990const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9991{
9992 if( ssl == NULL )
9993 return( NULL );
9994
9995 return( ssl->session );
9996}
9997
Paul Bakker5121ce52009-01-03 21:22:43 +00009998/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009999 * Define ticket header determining Mbed TLS version
10000 * and structure of the ticket.
10001 */
10002
Hanno Becker41527622019-05-16 12:50:45 +010010003/*
Hanno Becker26829e92019-05-28 14:30:45 +010010004 * Define bitflag determining compile-time settings influencing
10005 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010006 */
10007
Hanno Becker26829e92019-05-28 14:30:45 +010010008#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010009#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010010#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010011#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010012#endif /* MBEDTLS_HAVE_TIME */
10013
10014#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010015#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010016#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010017#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010018#endif /* MBEDTLS_X509_CRT_PARSE_C */
10019
10020#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010021#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010022#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010023#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010024#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10025
10026#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010027#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010028#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010029#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010030#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10031
10032#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010033#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010034#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010035#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010036#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10037
10038#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010039#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010040#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010041#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010042#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10043
Hanno Becker41527622019-05-16 12:50:45 +010010044#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10045#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10046#else
10047#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10048#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10049
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010050#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10051#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10052#else
10053#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10054#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10055
Hanno Becker88440552019-07-03 14:16:13 +010010056#if defined(MBEDTLS_ZLIB_SUPPORT)
10057#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10058#else
10059#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10060#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10061
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010062#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10063#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10064#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10065#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10066#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10067#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10068#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010069#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010070#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010071
Hanno Becker26829e92019-05-28 14:30:45 +010010072#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010073 ( (uint16_t) ( \
10074 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10075 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10076 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10077 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10078 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10079 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010080 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010081 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010082 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010083
Hanno Becker557fe9f2019-05-16 12:41:07 +010010084static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010085 MBEDTLS_VERSION_MAJOR,
10086 MBEDTLS_VERSION_MINOR,
10087 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010088 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10089 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010090};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010091
10092/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010093 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010094 * (in the presentation language of TLS, RFC 8446 section 3)
10095 *
Hanno Becker26829e92019-05-28 14:30:45 +010010096 * opaque mbedtls_version[3]; // major, minor, patch
10097 * opaque session_format[2]; // version-specific 16-bit field determining
10098 * // the format of the remaining
10099 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010100 *
10101 * Note: When updating the format, remember to keep
10102 * these version+format bytes.
10103 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010104 * // In this version, `session_format` determines
10105 * // the setting of those compile-time
10106 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010107 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010108 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010109 * uint8 ciphersuite[2]; // defined by the standard
10110 * uint8 compression; // 0 or 1
10111 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010112 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010113 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010114 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010115 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10116 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10117 * case disabled: uint8_t peer_cert_digest_type;
10118 * opaque peer_cert_digest<0..2^8-1>;
10119 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010120 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010121 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010122 * uint8 mfl_code; // up to 255 according to standard
10123 * uint8 trunc_hmac; // 0 or 1
10124 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010125 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126 * The order is the same as in the definition of the structure, except
10127 * verify_result is put before peer_cert so that all mandatory fields come
10128 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010129 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010130static int ssl_session_save( const mbedtls_ssl_session *session,
10131 unsigned char omit_header,
10132 unsigned char *buf,
10133 size_t buf_len,
10134 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010135{
10136 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010137 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010138#if defined(MBEDTLS_HAVE_TIME)
10139 uint64_t start;
10140#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010141#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010142#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010143 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010144#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010145#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010146
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010147 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010148 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010149 /*
10150 * Add version identifier
10151 */
10152
10153 used += sizeof( ssl_serialized_session_header );
10154
10155 if( used <= buf_len )
10156 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010157 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010158 sizeof( ssl_serialized_session_header ) );
10159 p += sizeof( ssl_serialized_session_header );
10160 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010161 }
10162
10163 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010164 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010165 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010166#if defined(MBEDTLS_HAVE_TIME)
10167 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010168
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010169 if( used <= buf_len )
10170 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010171 start = (uint64_t) session->start;
10172
10173 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10174 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10175 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10176 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10177 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10178 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10179 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10180 *p++ = (unsigned char)( ( start ) & 0xFF );
10181 }
10182#endif /* MBEDTLS_HAVE_TIME */
10183
10184 /*
10185 * Basic mandatory fields
10186 */
Hanno Becker88440552019-07-03 14:16:13 +010010187 {
10188 size_t const ciphersuite_len = 2;
10189#if defined(MBEDTLS_ZLIB_SUPPORT)
10190 size_t const compression_len = 1;
10191#else
10192 size_t const compression_len = 0;
10193#endif
10194 size_t const id_len_len = 1;
10195 size_t const id_len = 32;
10196 size_t const master_len = 48;
10197 size_t const verif_result_len = 4;
10198
10199 size_t const basic_len =
10200 ciphersuite_len +
10201 compression_len +
10202 id_len_len +
10203 id_len +
10204 master_len +
10205 verif_result_len;
10206
10207 used += basic_len;
10208 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010209
10210 if( used <= buf_len )
10211 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010212 const int ciphersuite =
10213 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010214 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010215
Hanno Becker88440552019-07-03 14:16:13 +010010216#if defined(MBEDTLS_ZLIB_SUPPORT)
10217 *p++ = (unsigned char)(
10218 mbedtls_ssl_session_get_compression( session ) );
10219#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010220
10221 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010222 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10223 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010224 p += 32;
10225
Teppo Järvelin91d79382019-10-02 09:09:31 +030010226 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010227 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010228 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010229 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010230
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010231 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010232 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010233 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010235#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010236 if( session->peer_cert == NULL )
10237 cert_len = 0;
10238 else
10239 cert_len = session->peer_cert->raw.len;
10240
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010241 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010242
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010243 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010244 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010245 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010246
10247 if( session->peer_cert != NULL )
10248 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010249 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010250 p += cert_len;
10251 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010252 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010253
Hanno Becker5882dd02019-06-06 16:25:57 +010010254#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010255 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010256 if( session->peer_cert_digest != NULL )
10257 {
10258 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10259 if( used <= buf_len )
10260 {
10261 *p++ = (unsigned char) session->peer_cert_digest_type;
10262 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010263 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010264 session->peer_cert_digest_len );
10265 p += session->peer_cert_digest_len;
10266 }
10267 }
10268 else
10269 {
10270 used += 2;
10271 if( used <= buf_len )
10272 {
10273 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10274 *p++ = 0;
10275 }
10276 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010277#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010278#endif /* MBEDTLS_X509_CRT_PARSE_C */
10279
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010280 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010281 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010282 */
10283#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010284 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010285
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010286 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010287 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010288 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010289
10290 if( session->ticket != NULL )
10291 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010292 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010293 p += session->ticket_len;
10294 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010295
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010296 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010297 }
10298#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10299
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010300 /*
10301 * Misc extension-related info
10302 */
10303#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10304 used += 1;
10305
10306 if( used <= buf_len )
10307 *p++ = session->mfl_code;
10308#endif
10309
10310#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10311 used += 1;
10312
10313 if( used <= buf_len )
10314 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10315#endif
10316
10317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10318 used += 1;
10319
10320 if( used <= buf_len )
10321 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10322#endif
10323
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010324 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010325 *olen = used;
10326
10327 if( used > buf_len )
10328 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010329
10330 return( 0 );
10331}
10332
10333/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010334 * Public wrapper for ssl_session_save()
10335 */
10336int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10337 unsigned char *buf,
10338 size_t buf_len,
10339 size_t *olen )
10340{
10341 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10342}
10343
10344/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010345 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010346 *
10347 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010348 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010349 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010350static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010351 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010352 const unsigned char *buf,
10353 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010354{
10355 const unsigned char *p = buf;
10356 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010357 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010358#if defined(MBEDTLS_HAVE_TIME)
10359 uint64_t start;
10360#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010361#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010362#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010363 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010364#endif
10365#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010366
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010367 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010368 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010369 /*
10370 * Check version identifier
10371 */
10372
10373 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10375
Teppo Järvelin0efac532019-10-04 13:21:08 +030010376 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010377 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010378 sizeof( ssl_serialized_session_header ) ) != 0 )
10379 {
10380 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10381 }
10382 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010383 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010384
10385 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010386 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010387 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010388#if defined(MBEDTLS_HAVE_TIME)
10389 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10391
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010392 start = ( (uint64_t) p[0] << 56 ) |
10393 ( (uint64_t) p[1] << 48 ) |
10394 ( (uint64_t) p[2] << 40 ) |
10395 ( (uint64_t) p[3] << 32 ) |
10396 ( (uint64_t) p[4] << 24 ) |
10397 ( (uint64_t) p[5] << 16 ) |
10398 ( (uint64_t) p[6] << 8 ) |
10399 ( (uint64_t) p[7] );
10400 p += 8;
10401
10402 session->start = (time_t) start;
10403#endif /* MBEDTLS_HAVE_TIME */
10404
10405 /*
10406 * Basic mandatory fields
10407 */
Hanno Becker88440552019-07-03 14:16:13 +010010408 {
10409 size_t const ciphersuite_len = 2;
10410#if defined(MBEDTLS_ZLIB_SUPPORT)
10411 size_t const compression_len = 1;
10412#else
10413 size_t const compression_len = 0;
10414#endif
10415 size_t const id_len_len = 1;
10416 size_t const id_len = 32;
10417 size_t const master_len = 48;
10418 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010419
Hanno Becker88440552019-07-03 14:16:13 +010010420 size_t const basic_len =
10421 ciphersuite_len +
10422 compression_len +
10423 id_len_len +
10424 id_len +
10425 master_len +
10426 verif_result_len;
10427
10428 if( basic_len > (size_t)( end - p ) )
10429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10430 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010431
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010432 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010433 p += 2;
10434
Hanno Becker73f4cb12019-06-27 13:51:07 +010010435#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010436 session->ciphersuite = ciphersuite;
10437#else
10438 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010439 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010440 {
10441 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10442 }
10443#endif
10444
Hanno Becker88440552019-07-03 14:16:13 +010010445#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010446 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010447#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010448
10449 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010450 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10451 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010452 p += 32;
10453
Teppo Järvelin91d79382019-10-02 09:09:31 +030010454 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010455 p += 48;
10456
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010457 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010458 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010459
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010460 /* Immediately clear invalid pointer values that have been read, in case
10461 * we exit early before we replaced them with valid ones. */
10462#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010463#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010464 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010465#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010466 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010467#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010468#endif
10469#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10470 session->ticket = NULL;
10471#endif
10472
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010473 /*
10474 * Peer certificate
10475 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010476#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010477#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010478 if( 3 > (size_t)( end - p ) )
10479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10480
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010481 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10482
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010483 p += 3;
10484
10485 if( cert_len == 0 )
10486 {
10487 session->peer_cert = NULL;
10488 }
10489 else
10490 {
10491 int ret;
10492
10493 if( cert_len > (size_t)( end - p ) )
10494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10495
10496 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10497
10498 if( session->peer_cert == NULL )
10499 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10500
10501 mbedtls_x509_crt_init( session->peer_cert );
10502
10503 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10504 p, cert_len ) ) != 0 )
10505 {
10506 mbedtls_x509_crt_free( session->peer_cert );
10507 mbedtls_free( session->peer_cert );
10508 session->peer_cert = NULL;
10509 return( ret );
10510 }
10511
10512 p += cert_len;
10513 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010514#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010515 /* Deserialize CRT digest from the end of the ticket. */
10516 if( 2 > (size_t)( end - p ) )
10517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10518
10519 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10520 session->peer_cert_digest_len = (size_t) *p++;
10521
10522 if( session->peer_cert_digest_len != 0 )
10523 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010524 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010525 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010526 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10528 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10530
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010531 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10533
10534 session->peer_cert_digest =
10535 mbedtls_calloc( 1, session->peer_cert_digest_len );
10536 if( session->peer_cert_digest == NULL )
10537 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10538
Teppo Järvelin91d79382019-10-02 09:09:31 +030010539 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010540 session->peer_cert_digest_len );
10541 p += session->peer_cert_digest_len;
10542 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010543#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010544#endif /* MBEDTLS_X509_CRT_PARSE_C */
10545
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010546 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010547 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010548 */
10549#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10550 if( 3 > (size_t)( end - p ) )
10551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10552
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010553 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010554 p += 3;
10555
10556 if( session->ticket_len != 0 )
10557 {
10558 if( session->ticket_len > (size_t)( end - p ) )
10559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10560
10561 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10562 if( session->ticket == NULL )
10563 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10564
Teppo Järvelin91d79382019-10-02 09:09:31 +030010565 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010566 p += session->ticket_len;
10567 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010568
10569 if( 4 > (size_t)( end - p ) )
10570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10571
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010572 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010573 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010574#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10575
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010576 /*
10577 * Misc extension-related info
10578 */
10579#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10580 if( 1 > (size_t)( end - p ) )
10581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10582
10583 session->mfl_code = *p++;
10584#endif
10585
10586#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10587 if( 1 > (size_t)( end - p ) )
10588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10589
10590 session->trunc_hmac = *p++;
10591#endif
10592
10593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10594 if( 1 > (size_t)( end - p ) )
10595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10596
10597 session->encrypt_then_mac = *p++;
10598#endif
10599
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010600 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010601 if( p != end )
10602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10603
10604 return( 0 );
10605}
10606
10607/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010608 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010609 */
10610int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10611 const unsigned char *buf,
10612 size_t len )
10613{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010614 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010615
10616 if( ret != 0 )
10617 mbedtls_ssl_session_free( session );
10618
10619 return( ret );
10620}
10621
10622/*
Paul Bakker1961b702013-01-25 14:49:24 +010010623 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010628
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010629 if( ssl == NULL || ssl->conf == NULL )
10630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010633 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010637 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010639#endif
10640
Hanno Beckerb82350b2019-07-26 07:24:05 +010010641 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010642 return( ret );
10643}
10644
10645/*
10646 * Perform the SSL handshake
10647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010649{
10650 int ret = 0;
10651
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010652 if( ssl == NULL || ssl->conf == NULL )
10653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010660
10661 if( ret != 0 )
10662 break;
10663 }
10664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010666
10667 return( ret );
10668}
10669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670#if defined(MBEDTLS_SSL_RENEGOTIATION)
10671#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010672/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010673 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010676{
10677 int ret;
10678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010680
10681 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10683 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010684
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010685 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010686 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010688 return( ret );
10689 }
10690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010692
10693 return( 0 );
10694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010696
10697/*
10698 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 * - any side: calling mbedtls_ssl_renegotiate(),
10700 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10701 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010702 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010703 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010707{
10708 int ret;
10709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010711
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010712 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10713 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010714
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010715 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10716 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010718 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010720 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010721 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10722 MBEDTLS_SSL_IS_SERVER )
10723 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010724 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010725 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010726 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010727 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010728 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010729 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010730 }
10731#endif
10732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10734 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010739 return( ret );
10740 }
10741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010743
10744 return( 0 );
10745}
10746
10747/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010748 * Renegotiate current connection on client,
10749 * or request renegotiation on server
10750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010754
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010755 if( ssl == NULL || ssl->conf == NULL )
10756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010759 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010760 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010766
10767 /* Did we already try/start sending HelloRequest? */
10768 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010770
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010771 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010776 /*
10777 * On client, either start the renegotiation process or,
10778 * if already in progress, continue the handshake
10779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010784
10785 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010788 return( ret );
10789 }
10790 }
10791 else
10792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010796 return( ret );
10797 }
10798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010800
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010801 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010802}
10803
10804/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010805 * Check record counters and renegotiate if they're above the limit.
10806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010808{
Andres AG2196c7f2016-12-15 17:01:16 +000010809 size_t ep_len = ssl_ep_len( ssl );
10810 int in_ctr_cmp;
10811 int out_ctr_cmp;
10812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10814 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010815 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010816 {
10817 return( 0 );
10818 }
10819
Teppo Järvelin0efac532019-10-04 13:21:08 +030010820 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010821 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010822 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010823 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010824 ssl->conf->renego_period + ep_len, 8 - ep_len );
10825
10826 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010827 {
10828 return( 0 );
10829 }
10830
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010832 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010835
10836/*
10837 * Receive application data decrypted from the SSL layer
10838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010840{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010841 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010842 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010843
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010844 if( ssl == NULL || ssl->conf == NULL )
10845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010853 return( ret );
10854
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010855 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010857 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010858 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010859 return( ret );
10860 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010861 }
10862#endif
10863
Hanno Becker4a810fb2017-05-24 16:27:30 +010010864 /*
10865 * Check if renegotiation is necessary and/or handshake is
10866 * in process. If yes, perform/continue, and fall through
10867 * if an unexpected packet is received while the client
10868 * is waiting for the ServerHello.
10869 *
10870 * (There is no equivalent to the last condition on
10871 * the server-side as it is not treated as within
10872 * a handshake while waiting for the ClientHello
10873 * after a renegotiation request.)
10874 */
10875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010877 ret = ssl_check_ctr_renegotiate( ssl );
10878 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10879 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010882 return( ret );
10883 }
10884#endif
10885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010889 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10890 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010893 return( ret );
10894 }
10895 }
10896
Hanno Beckere41158b2017-10-23 13:30:32 +010010897 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010898 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010899 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010900 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010901 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10902 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010903 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010904 ssl_set_timer( ssl,
10905 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010906 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010907
Hanno Becker327c93b2018-08-15 13:56:18 +010010908 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010909 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010910 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10911 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010912
Hanno Becker4a810fb2017-05-24 16:27:30 +010010913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10914 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010915 }
10916
10917 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010919 {
10920 /*
10921 * OpenSSL sends empty messages to randomize the IV
10922 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010923 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010926 return( 0 );
10927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010929 return( ret );
10930 }
10931 }
10932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010936
Hanno Becker4a810fb2017-05-24 16:27:30 +010010937 /*
10938 * - For client-side, expect SERVER_HELLO_REQUEST.
10939 * - For server-side, expect CLIENT_HELLO.
10940 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10941 */
10942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010944 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10945 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010947 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010950
10951 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010953 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010954 {
10955 continue;
10956 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010957 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010958#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010959#if defined(MBEDTLS_SSL_PROTO_TLS)
10960 {
10961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10962 }
10963#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010964 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010965#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010966
Hanno Becker4a810fb2017-05-24 16:27:30 +010010967#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010968 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10969 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010973
10974 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010976 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010977 {
10978 continue;
10979 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010980 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010981#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010982#if defined(MBEDTLS_SSL_PROTO_TLS)
10983 {
10984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10985 }
10986#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010987 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010988#endif /* MBEDTLS_SSL_SRV_C */
10989
Hanno Becker21df7f92017-10-17 11:03:26 +010010990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010991 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010992 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10993 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010994 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010995 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10996 {
10997 /*
10998 * Accept renegotiation request
10999 */
Paul Bakker48916f92012-09-16 19:57:18 +000011000
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011001 /* DTLS clients need to know renego is server-initiated */
11002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011003 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011004 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11005 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011006 {
11007 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11008 }
11009#endif
11010 ret = ssl_start_renegotiation( ssl );
11011 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11012 ret != 0 )
11013 {
11014 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11015 return( ret );
11016 }
11017 }
11018 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011019#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011020 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011021 /*
11022 * Refuse renegotiation
11023 */
11024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011028 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011029 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011030 /* SSLv3 does not have a "no_renegotiation" warning, so
11031 we send a fatal alert and abort the connection. */
11032 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11033 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11034 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011035 }
11036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11038#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11039 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011040 if( mbedtls_ssl_ver_geq(
11041 mbedtls_ssl_get_minor_ver( ssl ),
11042 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011043 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011044 ret = mbedtls_ssl_send_alert_message( ssl,
11045 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11046 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11047 if( ret != 0 )
11048 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11052 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011056 }
Paul Bakker48916f92012-09-16 19:57:18 +000011057 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011058
Hanno Becker90333da2017-10-10 11:27:13 +010011059 /* At this point, we don't know whether the renegotiation has been
11060 * completed or not. The cases to consider are the following:
11061 * 1) The renegotiation is complete. In this case, no new record
11062 * has been read yet.
11063 * 2) The renegotiation is incomplete because the client received
11064 * an application data record while awaiting the ServerHello.
11065 * 3) The renegotiation is incomplete because the client received
11066 * a non-handshake, non-application data message while awaiting
11067 * the ServerHello.
11068 * In each of these case, looping will be the proper action:
11069 * - For 1), the next iteration will read a new record and check
11070 * if it's application data.
11071 * - For 2), the loop condition isn't satisfied as application data
11072 * is present, hence continue is the same as break
11073 * - For 3), the loop condition is satisfied and read_record
11074 * will re-deliver the message that was held back by the client
11075 * when expecting the ServerHello.
11076 */
11077 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011078 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011079#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011081 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011082 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011083 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011084 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011087 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011088 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011089 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011090 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011091 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011092#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011094 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11095 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011098 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011099 }
11100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11104 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011105 }
11106
11107 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011108
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011109 /* We're going to return something now, cancel timer,
11110 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011112 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011115 /* If we requested renego but received AppData, resend HelloRequest.
11116 * Do it now, after setting in_offt, to avoid taking this branch
11117 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011118#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011119 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11120 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011122 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011123 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011126 return( ret );
11127 }
11128 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011130#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011131 }
11132
11133 n = ( len < ssl->in_msglen )
11134 ? len : ssl->in_msglen;
11135
Teppo Järvelin91d79382019-10-02 09:09:31 +030011136 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011137 ssl->in_msglen -= n;
11138
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011139 // clear incoming data after it's copied to buffer
11140 mbedtls_platform_memset(ssl->in_offt, 0, n);
11141
Paul Bakker5121ce52009-01-03 21:22:43 +000011142 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011143 {
11144 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011145 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011146 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011147 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011148 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011149 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011150 /* more data available */
11151 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011152 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011155
Paul Bakker23986e52011-04-24 08:57:21 +000011156 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011157}
11158
11159/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011160 * Send application data to be encrypted by the SSL layer, taking care of max
11161 * fragment length and buffer size.
11162 *
11163 * According to RFC 5246 Section 6.2.1:
11164 *
11165 * Zero-length fragments of Application data MAY be sent as they are
11166 * potentially useful as a traffic analysis countermeasure.
11167 *
11168 * Therefore, it is possible that the input message length is 0 and the
11169 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011170 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011171static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011172 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011173{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011174 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11175 const size_t max_len = (size_t) ret;
11176
11177 if( ret < 0 )
11178 {
11179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11180 return( ret );
11181 }
11182
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011183 if( len > max_len )
11184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011186 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011189 "maximum fragment length: %d > %d",
11190 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011192 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011193 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011194#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011195#if defined(MBEDTLS_SSL_PROTO_TLS)
11196 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011197 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011198 }
11199#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011200 }
Paul Bakker887bd502011-06-08 13:10:54 +000011201
Paul Bakker5121ce52009-01-03 21:22:43 +000011202 if( ssl->out_left != 0 )
11203 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011204 /*
11205 * The user has previously tried to send the data and
11206 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11207 * written. In this case, we expect the high-level write function
11208 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011213 return( ret );
11214 }
11215 }
Paul Bakker887bd502011-06-08 13:10:54 +000011216 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011217 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011218 /*
11219 * The user is trying to send a message the first time, so we need to
11220 * copy the data into the internal buffers and setup the data structure
11221 * to keep track of partial writes
11222 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011223 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011224 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011225 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011226
Hanno Becker67bc7c32018-08-06 11:33:50 +010011227 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011230 return( ret );
11231 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011232 }
11233
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011234 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011235}
11236
11237/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011238 * Write application data, doing 1/n-1 splitting if necessary.
11239 *
11240 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011241 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011242 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011245static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011246 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011247{
11248 int ret;
11249
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011250 if( ssl->conf->cbc_record_splitting ==
11251 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011252 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011253 mbedtls_ssl_ver_gt(
11254 mbedtls_ssl_get_minor_ver( ssl ),
11255 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011256 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11257 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011258 {
11259 return( ssl_write_real( ssl, buf, len ) );
11260 }
11261
11262 if( ssl->split_done == 0 )
11263 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011264 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011265 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011266 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011267 }
11268
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011269 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11270 return( ret );
11271 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011272
11273 return( ret + 1 );
11274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011275#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011276
11277/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011278 * Write application data (public-facing wrapper)
11279 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011280int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011281{
11282 int ret;
11283
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011285
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011286 if( ssl == NULL || ssl->conf == NULL )
11287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11288
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011289#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011290 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11291 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011292 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011293 return( ret );
11294 }
11295#endif
11296
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011297 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011298 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011299 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011300 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011302 return( ret );
11303 }
11304 }
11305
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011306#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011307 ret = ssl_write_split( ssl, buf, len );
11308#else
11309 ret = ssl_write_real( ssl, buf, len );
11310#endif
11311
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011313
11314 return( ret );
11315}
11316
11317/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011318 * Notify the peer that the connection is being closed
11319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011321{
11322 int ret;
11323
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011324 if( ssl == NULL || ssl->conf == NULL )
11325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011328
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011329 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011330 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011332 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011334 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11335 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11336 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011339 return( ret );
11340 }
11341 }
11342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011344
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011345 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011346}
11347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011349{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011350 if( transform == NULL )
11351 return;
11352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011353#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011354 deflateEnd( &transform->ctx_deflate );
11355 inflateEnd( &transform->ctx_inflate );
11356#endif
11357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011358 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11359 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011360
Hanno Becker92231322018-01-03 15:32:51 +000011361#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011362 mbedtls_md_free( &transform->md_ctx_enc );
11363 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011364#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011365
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011366 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011367}
11368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011369#if defined(MBEDTLS_X509_CRT_PARSE_C)
11370static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011372 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011373
11374 while( cur != NULL )
11375 {
11376 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011377 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011378 cur = next;
11379 }
11380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011381#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011382
Hanno Becker0271f962018-08-16 13:23:47 +010011383#if defined(MBEDTLS_SSL_PROTO_DTLS)
11384
11385static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11386{
11387 unsigned offset;
11388 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11389
11390 if( hs == NULL )
11391 return;
11392
Hanno Becker283f5ef2018-08-24 09:34:47 +010011393 ssl_free_buffered_record( ssl );
11394
Hanno Becker0271f962018-08-16 13:23:47 +010011395 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011396 ssl_buffering_free_slot( ssl, offset );
11397}
11398
11399static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11400 uint8_t slot )
11401{
11402 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11403 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011404
11405 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11406 return;
11407
Hanno Beckere605b192018-08-21 15:59:07 +010011408 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011409 {
Hanno Beckere605b192018-08-21 15:59:07 +010011410 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011411 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011412 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011413 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011414 }
11415}
11416
11417#endif /* MBEDTLS_SSL_PROTO_DTLS */
11418
Gilles Peskine9b562d52018-04-25 20:32:43 +020011419void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011420{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011421 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11422
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011423 if( handshake == NULL )
11424 return;
11425
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011426#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11427 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11428 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011429 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011430 handshake->async_in_progress = 0;
11431 }
11432#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11433
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11436 mbedtls_md5_free( &handshake->fin_md5 );
11437 mbedtls_sha1_free( &handshake->fin_sha1 );
11438#endif
11439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11440#if defined(MBEDTLS_SHA256_C)
11441 mbedtls_sha256_free( &handshake->fin_sha256 );
11442#endif
11443#if defined(MBEDTLS_SHA512_C)
11444 mbedtls_sha512_free( &handshake->fin_sha512 );
11445#endif
11446#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011448#if defined(MBEDTLS_DHM_C)
11449 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011451#if defined(MBEDTLS_ECDH_C)
11452 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011453#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011454#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011455 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011456#if defined(MBEDTLS_SSL_CLI_C)
11457 mbedtls_free( handshake->ecjpake_cache );
11458 handshake->ecjpake_cache = NULL;
11459 handshake->ecjpake_cache_len = 0;
11460#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011461#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011462
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011463#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11464 if( handshake->psk != NULL )
11465 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011466 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011467 mbedtls_free( handshake->psk );
11468 }
11469#endif
11470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011471#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11472 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011473 /*
11474 * Free only the linked list wrapper, not the keys themselves
11475 * since the belong to the SNI callback
11476 */
11477 if( handshake->sni_key_cert != NULL )
11478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011479 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011480
11481 while( cur != NULL )
11482 {
11483 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011484 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011485 cur = next;
11486 }
11487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011488#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011489
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011490#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011491 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011492 if( handshake->ecrs_peer_cert != NULL )
11493 {
11494 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11495 mbedtls_free( handshake->ecrs_peer_cert );
11496 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011497#endif
11498
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011499#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11500 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11501 mbedtls_pk_free( &handshake->peer_pubkey );
11502#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011504#if defined(MBEDTLS_SSL_PROTO_DTLS)
11505 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011506 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011507 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011508#endif
11509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011510 mbedtls_platform_zeroize( handshake,
11511 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011512}
11513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011514void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011515{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011516 if( session == NULL )
11517 return;
11518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011519#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011520 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011521#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011522
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011523#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011524 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011525#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011527 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011528}
11529
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011530#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011531
11532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11533#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11534#else
11535#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11536#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11537
11538#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11539#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11540#else
11541#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11542#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11543
11544#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11545#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11546#else
11547#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11548#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11549
11550#if defined(MBEDTLS_SSL_ALPN)
11551#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11552#else
11553#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11554#endif /* MBEDTLS_SSL_ALPN */
11555
11556#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11557#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11558#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11559#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11560
11561#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11562 ( (uint32_t) ( \
11563 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11564 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11565 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11566 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11567 0u ) )
11568
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011569static unsigned char ssl_serialized_context_header[] = {
11570 MBEDTLS_VERSION_MAJOR,
11571 MBEDTLS_VERSION_MINOR,
11572 MBEDTLS_VERSION_PATCH,
11573 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11574 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011575 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11576 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11577 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011578};
11579
Paul Bakker5121ce52009-01-03 21:22:43 +000011580/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011581 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011582 *
11583 * The format of the serialized data is:
11584 * (in the presentation language of TLS, RFC 8446 section 3)
11585 *
11586 * // header
11587 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011588 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011589 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011590 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011591 * Note: When updating the format, remember to keep these
11592 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011593 *
11594 * // session sub-structure
11595 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11596 * // transform sub-structure
11597 * uint8 random[64]; // ServerHello.random+ClientHello.random
11598 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11599 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11600 * // fields from ssl_context
11601 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11602 * uint64 in_window_top; // DTLS: last validated record seq_num
11603 * uint64 in_window; // DTLS: bitmask for replay protection
11604 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11605 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11606 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11607 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11608 *
11609 * Note that many fields of the ssl_context or sub-structures are not
11610 * serialized, as they fall in one of the following categories:
11611 *
11612 * 1. forced value (eg in_left must be 0)
11613 * 2. pointer to dynamically-allocated memory (eg session, transform)
11614 * 3. value can be re-derived from other data (eg session keys from MS)
11615 * 4. value was temporary (eg content of input buffer)
11616 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011617 */
11618int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11619 unsigned char *buf,
11620 size_t buf_len,
11621 size_t *olen )
11622{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011623 unsigned char *p = buf;
11624 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011625 size_t session_len;
11626 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011627
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011628 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011629 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11630 * this function's documentation.
11631 *
11632 * These are due to assumptions/limitations in the implementation. Some of
11633 * them are likely to stay (no handshake in progress) some might go away
11634 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011635 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011636 /* The initial handshake must be over */
11637 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011639 if( ssl->handshake != NULL )
11640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11641 /* Double-check that sub-structures are indeed ready */
11642 if( ssl->transform == NULL || ssl->session == NULL )
11643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11644 /* There must be no pending incoming or outgoing data */
11645 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11647 if( ssl->out_left != 0 )
11648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11649 /* Protocol must be DLTS, not TLS */
11650 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11652 /* Version must be 1.2 */
11653 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11655 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11657 /* We must be using an AEAD ciphersuite */
11658 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11660 /* Renegotiation must not be enabled */
11661 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011663
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011664 /*
11665 * Version and format identifier
11666 */
11667 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011668
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011669 if( used <= buf_len )
11670 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011671 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011672 sizeof( ssl_serialized_context_header ) );
11673 p += sizeof( ssl_serialized_context_header );
11674 }
11675
11676 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011677 * Session (length + data)
11678 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011679 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011680 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11681 return( ret );
11682
11683 used += 4 + session_len;
11684 if( used <= buf_len )
11685 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011686 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011687
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011688 ret = ssl_session_save( ssl->session, 1,
11689 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011690 if( ret != 0 )
11691 return( ret );
11692
11693 p += session_len;
11694 }
11695
11696 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011697 * Transform
11698 */
11699 used += sizeof( ssl->transform->randbytes );
11700 if( used <= buf_len )
11701 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011702 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011703 sizeof( ssl->transform->randbytes ) );
11704 p += sizeof( ssl->transform->randbytes );
11705 }
11706
11707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11708 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11709 if( used <= buf_len )
11710 {
11711 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011712 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11713 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011714 p += ssl->transform->in_cid_len;
11715
11716 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011717 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11718 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011719 p += ssl->transform->out_cid_len;
11720 }
11721#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11722
11723 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011724 * Saved fields from top-level ssl_context structure
11725 */
11726#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11727 used += 4;
11728 if( used <= buf_len )
11729 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011730 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011731 }
11732#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11733
11734#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11735 used += 16;
11736 if( used <= buf_len )
11737 {
11738 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11739 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11740 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11741 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11742 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11743 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11744 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11745 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11746
11747 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11748 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11749 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11750 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11751 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11752 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11753 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11754 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11755 }
11756#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11757
11758#if defined(MBEDTLS_SSL_PROTO_DTLS)
11759 used += 1;
11760 if( used <= buf_len )
11761 {
11762 *p++ = ssl->disable_datagram_packing;
11763 }
11764#endif /* MBEDTLS_SSL_PROTO_DTLS */
11765
11766 used += 8;
11767 if( used <= buf_len )
11768 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011769 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011770 p += 8;
11771 }
11772
11773#if defined(MBEDTLS_SSL_PROTO_DTLS)
11774 used += 2;
11775 if( used <= buf_len )
11776 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011777 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011778 }
11779#endif /* MBEDTLS_SSL_PROTO_DTLS */
11780
11781#if defined(MBEDTLS_SSL_ALPN)
11782 {
11783 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011784 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011785 : 0;
11786
11787 used += 1 + alpn_len;
11788 if( used <= buf_len )
11789 {
11790 *p++ = alpn_len;
11791
11792 if( ssl->alpn_chosen != NULL )
11793 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011794 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011795 p += alpn_len;
11796 }
11797 }
11798 }
11799#endif /* MBEDTLS_SSL_ALPN */
11800
11801 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011802 * Done
11803 */
11804 *olen = used;
11805
11806 if( used > buf_len )
11807 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011808
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011809 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11810
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011811 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011812}
11813
11814/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011815 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011816 *
11817 * This internal version is wrapped by a public function that cleans up in
11818 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011819 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011820static int ssl_context_load( mbedtls_ssl_context *ssl,
11821 const unsigned char *buf,
11822 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011823{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011824 const unsigned char *p = buf;
11825 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011826 size_t session_len;
11827 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011828
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011829 /*
11830 * The context should have been freshly setup or reset.
11831 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011832 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011833 * renegotiating, or if the user mistakenly loaded a session first.)
11834 */
11835 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11836 ssl->session != NULL )
11837 {
11838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11839 }
11840
11841 /*
11842 * We can't check that the config matches the initial one, but we can at
11843 * least check it matches the requirements for serializing.
11844 */
11845 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011846 mbedtls_ssl_ver_lt(
11847 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11848 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11849 mbedtls_ssl_ver_gt(
11850 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11851 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11852 mbedtls_ssl_ver_lt(
11853 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11854 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11855 mbedtls_ssl_ver_gt(
11856 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11857 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011858 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011859 {
11860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11861 }
11862
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011863 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11864
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011865 /*
11866 * Check version identifier
11867 */
11868 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11870
Teppo Järvelin650343c2019-10-03 15:36:59 +030011871 // use regular memcmp as header is not that critical
11872 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011873 sizeof( ssl_serialized_context_header ) ) != 0 )
11874 {
11875 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11876 }
11877 p += sizeof( ssl_serialized_context_header );
11878
11879 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011880 * Session
11881 */
11882 if( (size_t)( end - p ) < 4 )
11883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11884
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011885 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011886 p += 4;
11887
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011888 /* This has been allocated by ssl_handshake_init(), called by
11889 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11890 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011891 ssl->session_in = ssl->session;
11892 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011893 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011894
11895 if( (size_t)( end - p ) < session_len )
11896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11897
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011898 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011899 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011900 {
11901 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011902 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011903 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011904
11905 p += session_len;
11906
11907 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011908 * Transform
11909 */
11910
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011911 /* This has been allocated by ssl_handshake_init(), called by
11912 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11913 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011914 ssl->transform_in = ssl->transform;
11915 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011916 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011917
11918 /* Read random bytes and populate structure */
11919 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11921
11922 ret = ssl_populate_transform( ssl->transform,
11923 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11924 ssl->session->master,
11925#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11926#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11927 ssl->session->encrypt_then_mac,
11928#endif
11929#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11930 ssl->session->trunc_hmac,
11931#endif
11932#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11933#if defined(MBEDTLS_ZLIB_SUPPORT)
11934 ssl->session->compression,
11935#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011936 p, /* currently pointing to randbytes */
11937 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11938 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11939 ssl );
11940 if( ret != 0 )
11941 return( ret );
11942
11943 p += sizeof( ssl->transform->randbytes );
11944
11945#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11946 /* Read connection IDs and store them */
11947 if( (size_t)( end - p ) < 1 )
11948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11949
11950 ssl->transform->in_cid_len = *p++;
11951
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011952 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11954
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011955 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11956 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011957 p += ssl->transform->in_cid_len;
11958
11959 ssl->transform->out_cid_len = *p++;
11960
11961 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11963
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011964 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11965 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011966 p += ssl->transform->out_cid_len;
11967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11968
11969 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011970 * Saved fields from top-level ssl_context structure
11971 */
11972#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11973 if( (size_t)( end - p ) < 4 )
11974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11975
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011976 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011977 p += 4;
11978#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11979
11980#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11981 if( (size_t)( end - p ) < 16 )
11982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11983
11984 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11985 ( (uint64_t) p[1] << 48 ) |
11986 ( (uint64_t) p[2] << 40 ) |
11987 ( (uint64_t) p[3] << 32 ) |
11988 ( (uint64_t) p[4] << 24 ) |
11989 ( (uint64_t) p[5] << 16 ) |
11990 ( (uint64_t) p[6] << 8 ) |
11991 ( (uint64_t) p[7] );
11992 p += 8;
11993
11994 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11995 ( (uint64_t) p[1] << 48 ) |
11996 ( (uint64_t) p[2] << 40 ) |
11997 ( (uint64_t) p[3] << 32 ) |
11998 ( (uint64_t) p[4] << 24 ) |
11999 ( (uint64_t) p[5] << 16 ) |
12000 ( (uint64_t) p[6] << 8 ) |
12001 ( (uint64_t) p[7] );
12002 p += 8;
12003#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12004
12005#if defined(MBEDTLS_SSL_PROTO_DTLS)
12006 if( (size_t)( end - p ) < 1 )
12007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12008
12009 ssl->disable_datagram_packing = *p++;
12010#endif /* MBEDTLS_SSL_PROTO_DTLS */
12011
12012 if( (size_t)( end - p ) < 8 )
12013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12014
Teppo Järvelin91d79382019-10-02 09:09:31 +030012015 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012016 p += 8;
12017
12018#if defined(MBEDTLS_SSL_PROTO_DTLS)
12019 if( (size_t)( end - p ) < 2 )
12020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012021 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012022 p += 2;
12023#endif /* MBEDTLS_SSL_PROTO_DTLS */
12024
12025#if defined(MBEDTLS_SSL_ALPN)
12026 {
12027 uint8_t alpn_len;
12028 const char **cur;
12029
12030 if( (size_t)( end - p ) < 1 )
12031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12032
12033 alpn_len = *p++;
12034
12035 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12036 {
12037 /* alpn_chosen should point to an item in the configured list */
12038 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12039 {
12040 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012041 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012042 {
12043 ssl->alpn_chosen = *cur;
12044 break;
12045 }
12046 }
12047 }
12048
12049 /* can only happen on conf mismatch */
12050 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12052
12053 p += alpn_len;
12054 }
12055#endif /* MBEDTLS_SSL_ALPN */
12056
12057 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012058 * Forced fields from top-level ssl_context structure
12059 *
12060 * Most of them already set to the correct value by mbedtls_ssl_init() and
12061 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12062 */
12063 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12064
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012065#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012066 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012067#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12068#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012069 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012070#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012071
Hanno Becker83985822019-08-30 10:42:49 +010012072 /* Adjust pointers for header fields of outgoing records to
12073 * the given transform, accounting for explicit IV and CID. */
12074 ssl_update_out_pointers( ssl, ssl->transform );
12075
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012076#if defined(MBEDTLS_SSL_PROTO_DTLS)
12077 ssl->in_epoch = 1;
12078#endif
12079
12080 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12081 * which we don't want - otherwise we'd end up freeing the wrong transform
12082 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12083 if( ssl->handshake != NULL )
12084 {
12085 mbedtls_ssl_handshake_free( ssl );
12086 mbedtls_free( ssl->handshake );
12087 ssl->handshake = NULL;
12088 }
12089
12090 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012091 * Done - should have consumed entire buffer
12092 */
12093 if( p != end )
12094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012095
12096 return( 0 );
12097}
12098
12099/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012100 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012101 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012102int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012103 const unsigned char *buf,
12104 size_t len )
12105{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012106 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012107
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012108 if( ret != 0 )
12109 mbedtls_ssl_free( context );
12110
12111 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012112}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012113#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012114
12115/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012116 * Free an SSL context
12117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012118void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012119{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012120 if( ssl == NULL )
12121 return;
12122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012124
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012125 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012126 {
Angus Grattond8213d02016-05-25 20:56:48 +100012127 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012128 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012129 }
12130
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012131 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012132 {
Angus Grattond8213d02016-05-25 20:56:48 +100012133 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012134 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012135 }
12136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012137#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012138 if( ssl->compress_buf != NULL )
12139 {
Angus Grattond8213d02016-05-25 20:56:48 +100012140 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012141 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012142 }
12143#endif
12144
Paul Bakker48916f92012-09-16 19:57:18 +000012145 if( ssl->transform )
12146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012147 mbedtls_ssl_transform_free( ssl->transform );
12148 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012149 }
12150
12151 if( ssl->handshake )
12152 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012153 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012154 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12155 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157 mbedtls_free( ssl->handshake );
12158 mbedtls_free( ssl->transform_negotiate );
12159 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012160 }
12161
Paul Bakkerc0463502013-02-14 11:19:38 +010012162 if( ssl->session )
12163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012164 mbedtls_ssl_session_free( ssl->session );
12165 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012166 }
12167
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012168#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012169 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012170 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012171 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012172 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012173 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012176#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12177 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12180 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012181 }
12182#endif
12183
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012184#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012185 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012186#endif
12187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012189
Paul Bakker86f04f42013-02-14 11:20:09 +010012190 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012191 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012192}
12193
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012194/*
12195 * Initialze mbedtls_ssl_config
12196 */
12197void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12198{
12199 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012200
12201#if !defined(MBEDTLS_SSL_PROTO_TLS)
12202 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12203#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012204}
12205
Simon Butcherc97b6972015-12-27 23:48:17 +000012206#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012207#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012208static int ssl_preset_default_hashes[] = {
12209#if defined(MBEDTLS_SHA512_C)
12210 MBEDTLS_MD_SHA512,
12211 MBEDTLS_MD_SHA384,
12212#endif
12213#if defined(MBEDTLS_SHA256_C)
12214 MBEDTLS_MD_SHA256,
12215 MBEDTLS_MD_SHA224,
12216#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012217#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012218 MBEDTLS_MD_SHA1,
12219#endif
12220 MBEDTLS_MD_NONE
12221};
Simon Butcherc97b6972015-12-27 23:48:17 +000012222#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012223#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012224
Hanno Becker73f4cb12019-06-27 13:51:07 +010012225#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012226static int ssl_preset_suiteb_ciphersuites[] = {
12227 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12228 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12229 0
12230};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012231#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012232
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012233#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012234#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012235static int ssl_preset_suiteb_hashes[] = {
12236 MBEDTLS_MD_SHA256,
12237 MBEDTLS_MD_SHA384,
12238 MBEDTLS_MD_NONE
12239};
12240#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012241#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012242
Hanno Beckerc1096e72019-06-19 12:30:41 +010012243#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012244static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012245#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012246 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012247#endif
12248#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012249 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012250#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012251 MBEDTLS_ECP_DP_NONE
12252};
12253#endif
12254
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012255/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012256 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012257 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012258int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012259 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012260{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012261#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012262 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012263#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012264
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012265 /* Use the functions here so that they are covered in tests,
12266 * but otherwise access member directly for efficiency */
12267 mbedtls_ssl_conf_endpoint( conf, endpoint );
12268 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012269
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012270 /*
12271 * Things that are common to all presets
12272 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012273#if defined(MBEDTLS_SSL_CLI_C)
12274 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12275 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012276#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012277 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012278#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012279#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12280 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12281#endif
12282 }
12283#endif
12284
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012285#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012286 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012287#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012288
12289#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12290 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12291#endif
12292
12293#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012294#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012295 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012296#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12297#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012298 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012299 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012300#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012301#endif
12302
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012303#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12304 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12305#endif
12306
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012307#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012308 conf->f_cookie_write = ssl_cookie_write_dummy;
12309 conf->f_cookie_check = ssl_cookie_check_dummy;
12310#endif
12311
Hanno Becker7f376f42019-06-12 16:20:48 +010012312#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12313 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012314 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12315#endif
12316
Janos Follath088ce432017-04-10 12:42:31 +010012317#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012318#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012319 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012320#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12321#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012322
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012324#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012325 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012326#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12327#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012328 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012329#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12330#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012331
12332#if defined(MBEDTLS_SSL_RENEGOTIATION)
12333 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012334 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12335 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012336#endif
12337
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012338#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12339 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12340 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012341 const unsigned char dhm_p[] =
12342 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12343 const unsigned char dhm_g[] =
12344 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12345
Hanno Beckera90658f2017-10-04 15:29:08 +010012346 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12347 dhm_p, sizeof( dhm_p ),
12348 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012349 {
12350 return( ret );
12351 }
12352 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012353#endif
12354
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012355 /*
12356 * Preset-specific defaults
12357 */
12358 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012359 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012360 /*
12361 * NSA Suite B
12362 */
12363 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012364#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012365 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012366#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12367#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012368 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012369#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12370#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012371 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012372#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12373#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012374 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012375#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012376
Hanno Becker73f4cb12019-06-27 13:51:07 +010012377#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012378 conf->ciphersuite_list[0] =
12379 conf->ciphersuite_list[1] =
12380 conf->ciphersuite_list[2] =
12381 conf->ciphersuite_list[3] =
12382 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012383#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012384
12385#if defined(MBEDTLS_X509_CRT_PARSE_C)
12386 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012387#endif
12388
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012389#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012390#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012391 conf->sig_hashes = ssl_preset_suiteb_hashes;
12392#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012393#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394
12395#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012396#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012397 conf->curve_list = ssl_preset_suiteb_curves;
12398#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012399#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012400 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012401
12402 /*
12403 * Default
12404 */
12405 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012406#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012407 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12408 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12409 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12410 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012411#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12412#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012413 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12414 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12415 MBEDTLS_SSL_MIN_MINOR_VERSION :
12416 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012418 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012419 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12420#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012421#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12422#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12423 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12424#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12425#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12426 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12427#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012428
Hanno Becker73f4cb12019-06-27 13:51:07 +010012429#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012430 conf->ciphersuite_list[0] =
12431 conf->ciphersuite_list[1] =
12432 conf->ciphersuite_list[2] =
12433 conf->ciphersuite_list[3] =
12434 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012435#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012436
12437#if defined(MBEDTLS_X509_CRT_PARSE_C)
12438 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12439#endif
12440
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012441#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012442#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012443 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012444#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012445#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012446
12447#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012448#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012449 conf->curve_list = mbedtls_ecp_grp_id_list();
12450#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012451#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012452
12453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12454 conf->dhm_min_bitlen = 1024;
12455#endif
12456 }
12457
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012458 return( 0 );
12459}
12460
12461/*
12462 * Free mbedtls_ssl_config
12463 */
12464void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12465{
12466#if defined(MBEDTLS_DHM_C)
12467 mbedtls_mpi_free( &conf->dhm_P );
12468 mbedtls_mpi_free( &conf->dhm_G );
12469#endif
12470
12471#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12472 if( conf->psk != NULL )
12473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012474 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012475 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012476 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012477 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012478 }
12479
12480 if( conf->psk_identity != NULL )
12481 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012482 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012483 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012484 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012485 conf->psk_identity_len = 0;
12486 }
12487#endif
12488
12489#if defined(MBEDTLS_X509_CRT_PARSE_C)
12490 ssl_key_cert_free( conf->key_cert );
12491#endif
12492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012493 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012494}
12495
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012496#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012497 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12498 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012499/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012500 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012502unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012504#if defined(MBEDTLS_RSA_C)
12505 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12506 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012507#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012508#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012509 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12510 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012512 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012513}
12514
Hanno Becker7e5437a2017-04-28 17:15:26 +010012515unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12516{
12517 switch( type ) {
12518 case MBEDTLS_PK_RSA:
12519 return( MBEDTLS_SSL_SIG_RSA );
12520 case MBEDTLS_PK_ECDSA:
12521 case MBEDTLS_PK_ECKEY:
12522 return( MBEDTLS_SSL_SIG_ECDSA );
12523 default:
12524 return( MBEDTLS_SSL_SIG_ANON );
12525 }
12526}
12527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012528mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012529{
12530 switch( sig )
12531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012532#if defined(MBEDTLS_RSA_C)
12533 case MBEDTLS_SSL_SIG_RSA:
12534 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012535#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012536#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012537 case MBEDTLS_SSL_SIG_ECDSA:
12538 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012539#endif
12540 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012541 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012542 }
12543}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012544#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012545
Hanno Becker7e5437a2017-04-28 17:15:26 +010012546#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12547 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12548
12549/* Find an entry in a signature-hash set matching a given hash algorithm. */
12550mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12551 mbedtls_pk_type_t sig_alg )
12552{
12553 switch( sig_alg )
12554 {
12555 case MBEDTLS_PK_RSA:
12556 return( set->rsa );
12557 case MBEDTLS_PK_ECDSA:
12558 return( set->ecdsa );
12559 default:
12560 return( MBEDTLS_MD_NONE );
12561 }
12562}
12563
12564/* Add a signature-hash-pair to a signature-hash set */
12565void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12566 mbedtls_pk_type_t sig_alg,
12567 mbedtls_md_type_t md_alg )
12568{
12569 switch( sig_alg )
12570 {
12571 case MBEDTLS_PK_RSA:
12572 if( set->rsa == MBEDTLS_MD_NONE )
12573 set->rsa = md_alg;
12574 break;
12575
12576 case MBEDTLS_PK_ECDSA:
12577 if( set->ecdsa == MBEDTLS_MD_NONE )
12578 set->ecdsa = md_alg;
12579 break;
12580
12581 default:
12582 break;
12583 }
12584}
12585
12586/* Allow exactly one hash algorithm for each signature. */
12587void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12588 mbedtls_md_type_t md_alg )
12589{
12590 set->rsa = md_alg;
12591 set->ecdsa = md_alg;
12592}
12593
12594#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12595 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12596
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012597/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012598 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012600mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012601{
12602 switch( hash )
12603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012604#if defined(MBEDTLS_MD5_C)
12605 case MBEDTLS_SSL_HASH_MD5:
12606 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012608#if defined(MBEDTLS_SHA1_C)
12609 case MBEDTLS_SSL_HASH_SHA1:
12610 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012611#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012612#if defined(MBEDTLS_SHA256_C)
12613 case MBEDTLS_SSL_HASH_SHA224:
12614 return( MBEDTLS_MD_SHA224 );
12615 case MBEDTLS_SSL_HASH_SHA256:
12616 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012618#if defined(MBEDTLS_SHA512_C)
12619 case MBEDTLS_SSL_HASH_SHA384:
12620 return( MBEDTLS_MD_SHA384 );
12621 case MBEDTLS_SSL_HASH_SHA512:
12622 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012623#endif
12624 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012625 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012626 }
12627}
12628
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012629/*
12630 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12631 */
12632unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12633{
12634 switch( md )
12635 {
12636#if defined(MBEDTLS_MD5_C)
12637 case MBEDTLS_MD_MD5:
12638 return( MBEDTLS_SSL_HASH_MD5 );
12639#endif
12640#if defined(MBEDTLS_SHA1_C)
12641 case MBEDTLS_MD_SHA1:
12642 return( MBEDTLS_SSL_HASH_SHA1 );
12643#endif
12644#if defined(MBEDTLS_SHA256_C)
12645 case MBEDTLS_MD_SHA224:
12646 return( MBEDTLS_SSL_HASH_SHA224 );
12647 case MBEDTLS_MD_SHA256:
12648 return( MBEDTLS_SSL_HASH_SHA256 );
12649#endif
12650#if defined(MBEDTLS_SHA512_C)
12651 case MBEDTLS_MD_SHA384:
12652 return( MBEDTLS_SSL_HASH_SHA384 );
12653 case MBEDTLS_MD_SHA512:
12654 return( MBEDTLS_SSL_HASH_SHA512 );
12655#endif
12656 default:
12657 return( MBEDTLS_SSL_HASH_NONE );
12658 }
12659}
12660
Hanno Beckeree902df2019-08-23 13:47:47 +010012661#if defined(MBEDTLS_USE_TINYCRYPT)
12662/*
12663 * Check if a curve proposed by the peer is in our list.
12664 * Return 0 if we're willing to use it, -1 otherwise.
12665 */
12666int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12667 mbedtls_uecc_group_id grp_id )
12668{
12669 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12670 if( own_ec_id == grp_id )
12671 return( 0 );
12672 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12673
12674 return( -1 );
12675}
12676#endif /* MBEDTLS_USE_TINYCRYPT */
12677
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012678#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012679/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012680 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012681 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012682 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012683int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12684 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012685{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012686 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12687 if( own_ec_id == grp_id )
12688 return( 0 );
12689 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012690
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012691 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012692}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012693#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012694
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012695#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012696/*
12697 * Check if a hash proposed by the peer is in our list.
12698 * Return 0 if we're willing to use it, -1 otherwise.
12699 */
12700int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12701 mbedtls_md_type_t md )
12702{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012703 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12704 if( md_alg == md )
12705 return( 0 );
12706 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012707
12708 return( -1 );
12709}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012710#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012712#if defined(MBEDTLS_X509_CRT_PARSE_C)
12713int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012714 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012715 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012716 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012717{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012718 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012719#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012720 int usage = 0;
12721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012722#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012723 const char *ext_oid;
12724 size_t ext_len;
12725#endif
12726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012727#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12728 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012729 ((void) cert);
12730 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012731 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012732#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012734#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12735 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012736 {
12737 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012738 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012740 case MBEDTLS_KEY_EXCHANGE_RSA:
12741 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012742 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012743 break;
12744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012745 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12746 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12747 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12748 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012749 break;
12750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012751 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12752 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012753 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012754 break;
12755
12756 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012757 case MBEDTLS_KEY_EXCHANGE_NONE:
12758 case MBEDTLS_KEY_EXCHANGE_PSK:
12759 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12760 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012761 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012762 usage = 0;
12763 }
12764 }
12765 else
12766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012767 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12768 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012769 }
12770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012771 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012772 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012773 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012774 ret = -1;
12775 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012776#else
12777 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012778#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012780#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12781 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012783 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12784 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012785 }
12786 else
12787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012788 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12789 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012790 }
12791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012792 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012793 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012794 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012795 ret = -1;
12796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012797#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012798
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012799 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012801#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012802
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012803#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12804 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12805int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12806 unsigned char *output,
12807 unsigned char *data, size_t data_len )
12808{
12809 int ret = 0;
12810 mbedtls_md5_context mbedtls_md5;
12811 mbedtls_sha1_context mbedtls_sha1;
12812
12813 mbedtls_md5_init( &mbedtls_md5 );
12814 mbedtls_sha1_init( &mbedtls_sha1 );
12815
12816 /*
12817 * digitally-signed struct {
12818 * opaque md5_hash[16];
12819 * opaque sha_hash[20];
12820 * };
12821 *
12822 * md5_hash
12823 * MD5(ClientHello.random + ServerHello.random
12824 * + ServerParams);
12825 * sha_hash
12826 * SHA(ClientHello.random + ServerHello.random
12827 * + ServerParams);
12828 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012829 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012830 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012832 goto exit;
12833 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012834 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012835 ssl->handshake->randbytes, 64 ) ) != 0 )
12836 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012838 goto exit;
12839 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012840 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012841 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012843 goto exit;
12844 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012845 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012846 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012848 goto exit;
12849 }
12850
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012854 goto exit;
12855 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012856 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012857 ssl->handshake->randbytes, 64 ) ) != 0 )
12858 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012860 goto exit;
12861 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012862 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012863 data_len ) ) != 0 )
12864 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012866 goto exit;
12867 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012868 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012869 output + 16 ) ) != 0 )
12870 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012872 goto exit;
12873 }
12874
12875exit:
12876 mbedtls_md5_free( &mbedtls_md5 );
12877 mbedtls_sha1_free( &mbedtls_sha1 );
12878
12879 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012880 mbedtls_ssl_pend_fatal_alert( ssl,
12881 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012882
12883 return( ret );
12884
12885}
12886#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12887 MBEDTLS_SSL_PROTO_TLS1_1 */
12888
12889#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12890 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12891int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012892 unsigned char *hash, size_t *hashlen,
12893 unsigned char *data, size_t data_len,
12894 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012895{
12896 int ret = 0;
12897 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012898 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012899 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012900
12901 mbedtls_md_init( &ctx );
12902
12903 /*
12904 * digitally-signed struct {
12905 * opaque client_random[32];
12906 * opaque server_random[32];
12907 * ServerDHParams params;
12908 * };
12909 */
12910 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12911 {
12912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12913 goto exit;
12914 }
12915 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12916 {
12917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12918 goto exit;
12919 }
12920 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12921 {
12922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12923 goto exit;
12924 }
12925 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12926 {
12927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12928 goto exit;
12929 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012930 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012931 {
12932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12933 goto exit;
12934 }
12935
12936exit:
12937 mbedtls_md_free( &ctx );
12938
12939 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012940 mbedtls_ssl_pend_fatal_alert( ssl,
12941 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012942
12943 return( ret );
12944}
12945#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12946 MBEDTLS_SSL_PROTO_TLS1_2 */
12947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012948#endif /* MBEDTLS_SSL_TLS_C */