blob: d396e840a3cb9ea329220edc28bb8db35fc3a343 [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 {
1941 mbedtls_platform_enforce_volatile_reads();
1942 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)
1963 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1964 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 {
2023 mbedtls_platform_enforce_volatile_reads();
2024 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 {
2066 mbedtls_platform_enforce_volatile_reads();
2067 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 {
2097 mbedtls_platform_enforce_volatile_reads();
2098 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 {
2126 mbedtls_platform_enforce_volatile_reads();
2127 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/*
5590 * If applicable, decrypt (and decompress) record content
5591 */
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_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005714 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005716 {
5717 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005720 return( ret );
5721 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005722 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005726 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005729 }
5730#endif
5731
Hanno Beckerf0242852019-07-09 17:30:02 +01005732 /* Check actual (decrypted) record content length against
5733 * configured maximum. */
5734 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5735 {
5736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5737 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5738 }
5739
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 return( 0 );
5741}
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005744
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745/*
5746 * Read a record.
5747 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005748 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5749 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5750 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 */
Hanno Becker1097b342018-08-15 14:09:41 +01005752
5753/* Helper functions for mbedtls_ssl_read_record(). */
5754static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005755static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5756static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005757
Hanno Becker327c93b2018-08-15 13:56:18 +01005758int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005759 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005760{
5761 int ret;
5762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005764
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005765 if( ssl->keep_current_message == 0 )
5766 {
5767 do {
Simon Butcher99000142016-10-13 17:21:01 +01005768
Hanno Becker26994592018-08-15 14:14:59 +01005769 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005770 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005771 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005772
Hanno Beckere74d5562018-08-15 14:26:08 +01005773 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005774 {
Hanno Becker40f50842018-08-15 14:48:01 +01005775#if defined(MBEDTLS_SSL_PROTO_DTLS)
5776 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005777
Hanno Becker40f50842018-08-15 14:48:01 +01005778 /* We only check for buffered messages if the
5779 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005780 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005781 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005782 {
Hanno Becker40f50842018-08-15 14:48:01 +01005783 if( ssl_load_buffered_message( ssl ) == 0 )
5784 have_buffered = 1;
5785 }
5786
5787 if( have_buffered == 0 )
5788#endif /* MBEDTLS_SSL_PROTO_DTLS */
5789 {
5790 ret = ssl_get_next_record( ssl );
5791 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5792 continue;
5793
5794 if( ret != 0 )
5795 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005796 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005797 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005798 return( ret );
5799 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005800 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005801 }
5802
5803 ret = mbedtls_ssl_handle_message_type( ssl );
5804
Hanno Becker40f50842018-08-15 14:48:01 +01005805#if defined(MBEDTLS_SSL_PROTO_DTLS)
5806 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5807 {
5808 /* Buffer future message */
5809 ret = ssl_buffer_message( ssl );
5810 if( ret != 0 )
5811 return( ret );
5812
5813 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5814 }
5815#endif /* MBEDTLS_SSL_PROTO_DTLS */
5816
Hanno Becker90333da2017-10-10 11:27:13 +01005817 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5818 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005819
5820 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005821 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005822 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005823 return( ret );
5824 }
5825
Hanno Becker327c93b2018-08-15 13:56:18 +01005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005827 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005828 {
5829 mbedtls_ssl_update_handshake_status( ssl );
5830 }
Simon Butcher99000142016-10-13 17:21:01 +01005831 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005832 else
Simon Butcher99000142016-10-13 17:21:01 +01005833 {
Hanno Becker02f59072018-08-15 14:00:24 +01005834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005835 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005836 }
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5839
5840 return( 0 );
5841}
5842
Hanno Becker40f50842018-08-15 14:48:01 +01005843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005844static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005845{
Hanno Becker40f50842018-08-15 14:48:01 +01005846 if( ssl->in_left > ssl->next_record_offset )
5847 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005848
Hanno Becker40f50842018-08-15 14:48:01 +01005849 return( 0 );
5850}
5851
5852static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5853{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005854 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005855 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856 int ret = 0;
5857
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 if( hs == NULL )
5859 return( -1 );
5860
Hanno Beckere00ae372018-08-20 09:39:42 +01005861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5862
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5864 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5865 {
5866 /* Check if we have seen a ChangeCipherSpec before.
5867 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005868 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005869 {
5870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5871 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005872 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005873 }
5874
Hanno Becker39b8bc92018-08-28 17:17:13 +01005875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5877 ssl->in_msglen = 1;
5878 ssl->in_msg[0] = 1;
5879
5880 /* As long as they are equal, the exact value doesn't matter. */
5881 ssl->in_left = 0;
5882 ssl->next_record_offset = 0;
5883
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005884 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005885 goto exit;
5886 }
Hanno Becker37f95322018-08-16 13:55:32 +01005887
Hanno Beckerb8f50142018-08-28 10:01:34 +01005888#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005889 /* Debug only */
5890 {
5891 unsigned offset;
5892 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5893 {
5894 hs_buf = &hs->buffering.hs[offset];
5895 if( hs_buf->is_valid == 1 )
5896 {
5897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5898 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005899 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005900 }
5901 }
5902 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005903#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005904
5905 /* Check if we have buffered and/or fully reassembled the
5906 * next handshake message. */
5907 hs_buf = &hs->buffering.hs[0];
5908 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5909 {
5910 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005911 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005912
5913 /* Double-check that we haven't accidentally buffered
5914 * a message that doesn't fit into the input buffer. */
5915 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5916 {
5917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5919 }
5920
5921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5922 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5923 hs_buf->data, msg_len + 12 );
5924
5925 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5926 ssl->in_hslen = msg_len + 12;
5927 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005928 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005929
5930 ret = 0;
5931 goto exit;
5932 }
5933 else
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5936 hs->in_msg_seq ) );
5937 }
5938
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005939 ret = -1;
5940
5941exit:
5942
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5944 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005945}
5946
Hanno Beckera02b0b42018-08-21 17:20:27 +01005947static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5948 size_t desired )
5949{
5950 int offset;
5951 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5953 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005954
Hanno Becker01315ea2018-08-21 17:22:17 +01005955 /* Get rid of future records epoch first, if such exist. */
5956 ssl_free_buffered_record( ssl );
5957
5958 /* Check if we have enough space available now. */
5959 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5960 hs->buffering.total_bytes_buffered ) )
5961 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005963 return( 0 );
5964 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005965
Hanno Becker4f432ad2018-08-28 10:02:32 +01005966 /* We don't have enough space to buffer the next expected handshake
5967 * message. Remove buffers used for future messages to gain space,
5968 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005969 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5970 offset >= 0; offset-- )
5971 {
5972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5973 offset ) );
5974
Hanno Beckerb309b922018-08-23 13:18:05 +01005975 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005976
5977 /* Check if we have enough space available now. */
5978 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5979 hs->buffering.total_bytes_buffered ) )
5980 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005982 return( 0 );
5983 }
5984 }
5985
5986 return( -1 );
5987}
5988
Hanno Becker40f50842018-08-15 14:48:01 +01005989static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5990{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005991 int ret = 0;
5992 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5993
5994 if( hs == NULL )
5995 return( 0 );
5996
5997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5998
5999 switch( ssl->in_msgtype )
6000 {
6001 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006003
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006004 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006005 break;
6006
6007 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006008 {
6009 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006010 unsigned recv_msg_seq = (unsigned)
6011 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006012
Hanno Becker37f95322018-08-16 13:55:32 +01006013 mbedtls_ssl_hs_buffer *hs_buf;
6014 size_t msg_len = ssl->in_hslen - 12;
6015
6016 /* We should never receive an old handshake
6017 * message - double-check nonetheless. */
6018 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6019 {
6020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6021 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6022 }
6023
6024 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6025 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6026 {
6027 /* Silently ignore -- message too far in the future */
6028 MBEDTLS_SSL_DEBUG_MSG( 2,
6029 ( "Ignore future HS message with sequence number %u, "
6030 "buffering window %u - %u",
6031 recv_msg_seq, ssl->handshake->in_msg_seq,
6032 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6033
6034 goto exit;
6035 }
6036
6037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6038 recv_msg_seq, recv_msg_seq_offset ) );
6039
6040 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6041
6042 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006043 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006044 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006045 size_t reassembly_buf_sz;
6046
Hanno Becker37f95322018-08-16 13:55:32 +01006047 hs_buf->is_fragmented =
6048 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6049
6050 /* We copy the message back into the input buffer
6051 * after reassembly, so check that it's not too large.
6052 * This is an implementation-specific limitation
6053 * and not one from the standard, hence it is not
6054 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006055 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006056 {
6057 /* Ignore message */
6058 goto exit;
6059 }
6060
Hanno Beckere0b150f2018-08-21 15:51:03 +01006061 /* Check if we have enough space to buffer the message. */
6062 if( hs->buffering.total_bytes_buffered >
6063 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6064 {
6065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6067 }
6068
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006069 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6070 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006071
6072 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6073 hs->buffering.total_bytes_buffered ) )
6074 {
6075 if( recv_msg_seq_offset > 0 )
6076 {
6077 /* If we can't buffer a future message because
6078 * of space limitations -- ignore. */
6079 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",
6080 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6081 (unsigned) hs->buffering.total_bytes_buffered ) );
6082 goto exit;
6083 }
Hanno Beckere1801392018-08-21 16:51:05 +01006084 else
6085 {
6086 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",
6087 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6088 (unsigned) hs->buffering.total_bytes_buffered ) );
6089 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006090
Hanno Beckera02b0b42018-08-21 17:20:27 +01006091 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006092 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006093 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",
6094 (unsigned) msg_len,
6095 (unsigned) reassembly_buf_sz,
6096 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006097 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006098 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6099 goto exit;
6100 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006101 }
6102
6103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6104 msg_len ) );
6105
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006106 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6107 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006108 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006109 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006110 goto exit;
6111 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006112 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006113
6114 /* Prepare final header: copy msg_type, length and message_seq,
6115 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006116 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006117 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006118 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006119
6120 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006121
6122 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006123 }
6124 else
6125 {
6126 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006127 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 +01006128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6130 /* Ignore */
6131 goto exit;
6132 }
6133 }
6134
Hanno Becker4422bbb2018-08-20 09:40:19 +01006135 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006136 {
6137 size_t frag_len, frag_off;
6138 unsigned char * const msg = hs_buf->data + 12;
6139
6140 /*
6141 * Check and copy current fragment
6142 */
6143
6144 /* Validation of header fields already done in
6145 * mbedtls_ssl_prepare_handshake_record(). */
6146 frag_off = ssl_get_hs_frag_off( ssl );
6147 frag_len = ssl_get_hs_frag_len( ssl );
6148
6149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6150 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006151 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006152
6153 if( hs_buf->is_fragmented )
6154 {
6155 unsigned char * const bitmask = msg + msg_len;
6156 ssl_bitmask_set( bitmask, frag_off, frag_len );
6157 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6158 msg_len ) == 0 );
6159 }
6160 else
6161 {
6162 hs_buf->is_complete = 1;
6163 }
6164
6165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6166 hs_buf->is_complete ? "" : "not yet " ) );
6167 }
6168
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006169 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006170 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006171
6172 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006173 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006174 break;
6175 }
6176
6177exit:
6178
6179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6180 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006181}
6182#endif /* MBEDTLS_SSL_PROTO_DTLS */
6183
Hanno Becker1097b342018-08-15 14:09:41 +01006184static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006185{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006186 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006187 * Consume last content-layer message and potentially
6188 * update in_msglen which keeps track of the contents'
6189 * consumption state.
6190 *
6191 * (1) Handshake messages:
6192 * Remove last handshake message, move content
6193 * and adapt in_msglen.
6194 *
6195 * (2) Alert messages:
6196 * Consume whole record content, in_msglen = 0.
6197 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006198 * (3) Change cipher spec:
6199 * Consume whole record content, in_msglen = 0.
6200 *
6201 * (4) Application data:
6202 * Don't do anything - the record layer provides
6203 * the application data as a stream transport
6204 * and consumes through mbedtls_ssl_read only.
6205 *
6206 */
6207
6208 /* Case (1): Handshake messages */
6209 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006210 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006211 /* Hard assertion to be sure that no application data
6212 * is in flight, as corrupting ssl->in_msglen during
6213 * ssl->in_offt != NULL is fatal. */
6214 if( ssl->in_offt != NULL )
6215 {
6216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6218 }
6219
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006220 /*
6221 * Get next Handshake message in the current record
6222 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006223
Hanno Becker4a810fb2017-05-24 16:27:30 +01006224 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006225 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006226 * current handshake content: If DTLS handshake
6227 * fragmentation is used, that's the fragment
6228 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006229 * size here is faulty and should be changed at
6230 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 * (2) While it doesn't seem to cause problems, one
6232 * has to be very careful not to assume that in_hslen
6233 * is always <= in_msglen in a sensible communication.
6234 * Again, it's wrong for DTLS handshake fragmentation.
6235 * The following check is therefore mandatory, and
6236 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006237 * Additionally, ssl->in_hslen might be arbitrarily out of
6238 * bounds after handling a DTLS message with an unexpected
6239 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006240 */
6241 if( ssl->in_hslen < ssl->in_msglen )
6242 {
6243 ssl->in_msglen -= ssl->in_hslen;
6244 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6245 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006246
Hanno Becker4a810fb2017-05-24 16:27:30 +01006247 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6248 ssl->in_msg, ssl->in_msglen );
6249 }
6250 else
6251 {
6252 ssl->in_msglen = 0;
6253 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006254
Hanno Becker4a810fb2017-05-24 16:27:30 +01006255 ssl->in_hslen = 0;
6256 }
6257 /* Case (4): Application data */
6258 else if( ssl->in_offt != NULL )
6259 {
6260 return( 0 );
6261 }
6262 /* Everything else (CCS & Alerts) */
6263 else
6264 {
6265 ssl->in_msglen = 0;
6266 }
6267
Hanno Becker1097b342018-08-15 14:09:41 +01006268 return( 0 );
6269}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006270
Hanno Beckere74d5562018-08-15 14:26:08 +01006271static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6272{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006273 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006274 return( 1 );
6275
6276 return( 0 );
6277}
6278
Hanno Becker5f066e72018-08-16 14:56:31 +01006279#if defined(MBEDTLS_SSL_PROTO_DTLS)
6280
6281static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6282{
6283 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6284 if( hs == NULL )
6285 return;
6286
Hanno Becker01315ea2018-08-21 17:22:17 +01006287 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006288 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006289 hs->buffering.total_bytes_buffered -=
6290 hs->buffering.future_record.len;
6291
6292 mbedtls_free( hs->buffering.future_record.data );
6293 hs->buffering.future_record.data = NULL;
6294 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006295}
6296
6297static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6298{
6299 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6300 unsigned char * rec;
6301 size_t rec_len;
6302 unsigned rec_epoch;
6303
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006304 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006305 return( 0 );
6306
6307 if( hs == NULL )
6308 return( 0 );
6309
Hanno Becker5f066e72018-08-16 14:56:31 +01006310 rec = hs->buffering.future_record.data;
6311 rec_len = hs->buffering.future_record.len;
6312 rec_epoch = hs->buffering.future_record.epoch;
6313
6314 if( rec == NULL )
6315 return( 0 );
6316
Hanno Becker4cb782d2018-08-20 11:19:05 +01006317 /* Only consider loading future records if the
6318 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006319 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006320 return( 0 );
6321
Hanno Becker5f066e72018-08-16 14:56:31 +01006322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6323
6324 if( rec_epoch != ssl->in_epoch )
6325 {
6326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6327 goto exit;
6328 }
6329
6330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6331
6332 /* Double-check that the record is not too large */
6333 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6334 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6335 {
6336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6338 }
6339
Teppo Järvelin91d79382019-10-02 09:09:31 +03006340 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006341 ssl->in_left = rec_len;
6342 ssl->next_record_offset = 0;
6343
6344 ssl_free_buffered_record( ssl );
6345
6346exit:
6347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6348 return( 0 );
6349}
6350
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006351static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6352 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006353{
6354 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006355
6356 /* Don't buffer future records outside handshakes. */
6357 if( hs == NULL )
6358 return( 0 );
6359
6360 /* Only buffer handshake records (we are only interested
6361 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006362 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006363 return( 0 );
6364
6365 /* Don't buffer more than one future epoch record. */
6366 if( hs->buffering.future_record.data != NULL )
6367 return( 0 );
6368
Hanno Becker01315ea2018-08-21 17:22:17 +01006369 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006370 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006371 hs->buffering.total_bytes_buffered ) )
6372 {
6373 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 +01006374 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006375 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006376 return( 0 );
6377 }
6378
Hanno Becker5f066e72018-08-16 14:56:31 +01006379 /* Buffer record */
6380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6381 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006382 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006383
6384 /* ssl_parse_record_header() only considers records
6385 * of the next epoch as candidates for buffering. */
6386 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006387 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006388
6389 hs->buffering.future_record.data =
6390 mbedtls_calloc( 1, hs->buffering.future_record.len );
6391 if( hs->buffering.future_record.data == NULL )
6392 {
6393 /* If we run out of RAM trying to buffer a
6394 * record from the next epoch, just ignore. */
6395 return( 0 );
6396 }
6397
Teppo Järvelin91d79382019-10-02 09:09:31 +03006398 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006399
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006400 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006401 return( 0 );
6402}
6403
6404#endif /* MBEDTLS_SSL_PROTO_DTLS */
6405
Hanno Beckere74d5562018-08-15 14:26:08 +01006406static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006407{
6408 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006409 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006410
Hanno Becker5f066e72018-08-16 14:56:31 +01006411#if defined(MBEDTLS_SSL_PROTO_DTLS)
6412 /* We might have buffered a future record; if so,
6413 * and if the epoch matches now, load it.
6414 * On success, this call will set ssl->in_left to
6415 * the length of the buffered record, so that
6416 * the calls to ssl_fetch_input() below will
6417 * essentially be no-ops. */
6418 ret = ssl_load_buffered_record( ssl );
6419 if( ret != 0 )
6420 return( ret );
6421#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006422
Hanno Becker8b09b732019-05-08 12:03:28 +01006423 /* Ensure that we have enough space available for the default form
6424 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6425 * with no space for CIDs counted in). */
6426 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6427 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006430 return( ret );
6431 }
6432
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006433 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6434 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006437 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006438 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006439 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6440 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006441 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006442 if( ret != 0 )
6443 return( ret );
6444
6445 /* Fall through to handling of unexpected records */
6446 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6447 }
6448
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006449 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6450 {
Hanno Becker87b56262019-07-10 14:37:41 +01006451#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006452 /* Reset in pointers to default state for TLS/DTLS records,
6453 * assuming no CID and no offset between record content and
6454 * record plaintext. */
6455 ssl_update_in_pointers( ssl );
6456
Hanno Becker69412452019-07-12 08:33:49 +01006457 /* Setup internal message pointers from record structure. */
6458 ssl->in_msgtype = rec.type;
6459#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6460 ssl->in_len = ssl->in_cid + rec.cid_len;
6461#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006462 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006463 ssl->in_msglen = rec.data_len;
6464
Hanno Becker87b56262019-07-10 14:37:41 +01006465 ret = ssl_check_client_reconnect( ssl );
6466 if( ret != 0 )
6467 return( ret );
6468#endif
6469
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006470 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006471 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006472
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6474 "(header)" ) );
6475 }
6476 else
6477 {
6478 /* Skip invalid record and the rest of the datagram */
6479 ssl->next_record_offset = 0;
6480 ssl->in_left = 0;
6481
6482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6483 "(header)" ) );
6484 }
6485
6486 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006487 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006488 }
Hanno Becker87b56262019-07-10 14:37:41 +01006489 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006490#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006491 {
6492 return( ret );
6493 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006494 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006497 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006498 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006499 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006500 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006501 if( ssl->next_record_offset < ssl->in_left )
6502 {
6503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6504 }
6505 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006506 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006507#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006508#if defined(MBEDTLS_SSL_PROTO_TLS)
6509 {
Hanno Beckere0452772019-07-10 17:12:07 +01006510 /*
6511 * Fetch record contents from underlying transport.
6512 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006513 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006514 if( ret != 0 )
6515 {
6516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6517 return( ret );
6518 }
6519
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006520 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006521 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006522#endif /* MBEDTLS_SSL_PROTO_TLS */
6523
6524 /*
6525 * Decrypt record contents.
6526 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006527
Hanno Beckera89610a2019-07-11 13:07:45 +01006528 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006531 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006532 {
6533 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006534 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006535 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006536 /* Except when waiting for Finished as a bad mac here
6537 * probably means something went wrong in the handshake
6538 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6539 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6540 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6541 {
6542#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6543 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6544 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006545 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006546 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6547 }
6548#endif
6549 return( ret );
6550 }
6551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006553 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6554 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006558 }
6559#endif
6560
Hanno Becker4a810fb2017-05-24 16:27:30 +01006561 /* As above, invalid records cause
6562 * dismissal of the whole datagram. */
6563
6564 ssl->next_record_offset = 0;
6565 ssl->in_left = 0;
6566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006568 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006569 }
6570
6571 return( ret );
6572 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006573 MBEDTLS_SSL_TRANSPORT_ELSE
6574#endif /* MBEDTLS_SSL_PROTO_DTLS */
6575#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006576 {
6577 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6579 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006580 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006581 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006583 }
6584#endif
6585 return( ret );
6586 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006587#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006588 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006589
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006590
6591 /* Reset in pointers to default state for TLS/DTLS records,
6592 * assuming no CID and no offset between record content and
6593 * record plaintext. */
6594 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006595#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6596 ssl->in_len = ssl->in_cid + rec.cid_len;
6597#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006598 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006599
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006600 /* The record content type may change during decryption,
6601 * so re-read it. */
6602 ssl->in_msgtype = rec.type;
6603 /* Also update the input buffer, because unfortunately
6604 * the server-side ssl_parse_client_hello() reparses the
6605 * record header when receiving a ClientHello initiating
6606 * a renegotiation. */
6607 ssl->in_hdr[0] = rec.type;
6608 ssl->in_msg = rec.buf + rec.data_offset;
6609 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006610 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006611
Simon Butcher99000142016-10-13 17:21:01 +01006612 return( 0 );
6613}
6614
6615int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6616{
6617 int ret;
6618
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006619 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006620 * Handle particular types of records
6621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006623 {
Simon Butcher99000142016-10-13 17:21:01 +01006624 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6625 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006626 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 }
6629
Hanno Beckere678eaa2018-08-21 14:57:46 +01006630 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006631 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006632 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006633 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6635 ssl->in_msglen ) );
6636 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006637 }
6638
Hanno Beckere678eaa2018-08-21 14:57:46 +01006639 if( ssl->in_msg[0] != 1 )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6642 ssl->in_msg[0] ) );
6643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6644 }
6645
6646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006647 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006648 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6649 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6650 {
6651 if( ssl->handshake == NULL )
6652 {
6653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6654 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6655 }
6656
6657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6658 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6659 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006660#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006661 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006665 if( ssl->in_msglen != 2 )
6666 {
6667 /* Note: Standard allows for more than one 2 byte alert
6668 to be packed in a single message, but Mbed TLS doesn't
6669 currently support this. */
6670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6671 ssl->in_msglen ) );
6672 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6673 }
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006676 ssl->in_msg[0], ssl->in_msg[1] ) );
6677
6678 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006679 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006684 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 }
6687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6689 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6692 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006694
6695#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6696 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6697 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6698 {
Hanno Becker90333da2017-10-10 11:27:13 +01006699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006700 /* Will be handled when trying to parse ServerHello */
6701 return( 0 );
6702 }
6703#endif
6704
6705#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006706 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006707 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6708 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006709 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6710 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6711 {
6712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6713 /* Will be handled in mbedtls_ssl_parse_certificate() */
6714 return( 0 );
6715 }
6716#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6717
6718 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006719 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 }
6721
Hanno Beckerc76c6192017-06-06 10:03:17 +01006722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006723 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006724 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006725 /* Drop unexpected ApplicationData records,
6726 * except at the beginning of renegotiations */
6727 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6728 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6729#if defined(MBEDTLS_SSL_RENEGOTIATION)
6730 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6731 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006732#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006733 )
6734 {
6735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6736 return( MBEDTLS_ERR_SSL_NON_FATAL );
6737 }
6738
6739 if( ssl->handshake != NULL &&
6740 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6741 {
6742 ssl_handshake_wrapup_free_hs_transform( ssl );
6743 }
6744 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006746
Paul Bakker5121ce52009-01-03 21:22:43 +00006747 return( 0 );
6748}
6749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006751{
6752 int ret;
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6755 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6756 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006757 {
6758 return( ret );
6759 }
6760
6761 return( 0 );
6762}
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006765 unsigned char level,
6766 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006767{
6768 int ret;
6769
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006770 if( ssl == NULL || ssl->conf == NULL )
6771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006777 ssl->out_msglen = 2;
6778 ssl->out_msg[0] = level;
6779 ssl->out_msg[1] = message;
6780
Hanno Becker67bc7c32018-08-06 11:33:50 +01006781 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006784 return( ret );
6785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006787
6788 return( 0 );
6789}
6790
Hanno Becker17572472019-02-08 07:19:04 +00006791#if defined(MBEDTLS_X509_CRT_PARSE_C)
6792static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6793{
6794#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6795 if( session->peer_cert != NULL )
6796 {
6797 mbedtls_x509_crt_free( session->peer_cert );
6798 mbedtls_free( session->peer_cert );
6799 session->peer_cert = NULL;
6800 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006801#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006802 if( session->peer_cert_digest != NULL )
6803 {
6804 /* Zeroization is not necessary. */
6805 mbedtls_free( session->peer_cert_digest );
6806 session->peer_cert_digest = NULL;
6807 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6808 session->peer_cert_digest_len = 0;
6809 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006810#else
6811 ((void) session);
6812#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006813}
6814#endif /* MBEDTLS_X509_CRT_PARSE_C */
6815
Paul Bakker5121ce52009-01-03 21:22:43 +00006816/*
6817 * Handshake functions
6818 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006819#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006820/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822{
Hanno Beckerdf645962019-06-26 13:02:22 +01006823 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6824 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Hanno Becker5097cba2019-02-05 13:36:46 +00006828 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006831 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6832 {
6833 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6834 }
6835 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6836 {
6837 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6838 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006839 else
6840 {
6841 ssl->state = MBEDTLS_SSL_INVALID;
6842 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006843 return( 0 );
6844 }
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6847 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006848}
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006851{
Hanno Beckerdf645962019-06-26 13:02:22 +01006852 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6853 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006856
Hanno Becker5097cba2019-02-05 13:36:46 +00006857 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006860 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6861 {
6862 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6863 }
6864 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6865 {
6866 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6867 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006868 else
6869 {
6870 ssl->state = MBEDTLS_SSL_INVALID;
6871 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006872 return( 0 );
6873 }
6874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006877}
Gilles Peskinef9828522017-05-03 12:28:43 +02006878
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006879#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006880/* Some certificate support -> implement write and parse */
6881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006885 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006887 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6888 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006891
Hanno Becker5097cba2019-02-05 13:36:46 +00006892 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006895 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6896 {
6897 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6898 }
6899 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6900 {
6901 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6902 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006903 else
6904 {
6905 ssl->state = MBEDTLS_SSL_INVALID;
6906 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006907 return( 0 );
6908 }
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006911 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6912 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006913 {
6914 if( ssl->client_auth == 0 )
6915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006917 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6918 {
6919 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6920 }
6921 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6922 {
6923 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6924 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006925 else
6926 {
6927 ssl->state = MBEDTLS_SSL_INVALID;
6928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 return( 0 );
6930 }
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006933 /*
6934 * If using SSLv3 and got no cert, send an Alert message
6935 * (otherwise an empty Certificate message will be sent).
6936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006938 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 {
6940 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6942 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6943 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 goto write_msg;
6947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#endif /* MBEDTLS_SSL_CLI_C */
6951#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006952 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6957 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 }
6959 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006960#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006963
6964 /*
6965 * 0 . 0 handshake type
6966 * 1 . 3 handshake length
6967 * 4 . 6 length of all certs
6968 * 7 . 9 length of cert. 1
6969 * 10 . n-1 peer certificate
6970 * n . n+2 length of cert. 2
6971 * n+3 . ... upper level cert, etc.
6972 */
6973 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975
Paul Bakker29087132010-03-21 21:03:34 +00006976 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 {
6978 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006979 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006982 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 }
6985
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006986 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987
Teppo Järvelin91d79382019-10-02 09:09:31 +03006988 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 i += n; crt = crt->next;
6990 }
6991
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006992 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006993
6994 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6996 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006998#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006999write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007000#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007001
Jarno Lamsa2b205162019-11-12 15:36:21 +02007002 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7003 {
7004 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7005 }
7006 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7007 {
7008 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7009 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007010 else
7011 {
7012 ssl->state = MBEDTLS_SSL_INVALID;
7013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007014
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007015 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007016 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007018 return( ret );
7019 }
7020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
Paul Bakkered27a042013-04-18 22:46:23 +02007023 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007024}
7025
Hanno Becker285ff0c2019-01-31 07:44:03 +00007026#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007027
7028#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007029static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7030 unsigned char *crt_buf,
7031 size_t crt_buf_len )
7032{
7033 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7034
7035 if( peer_crt == NULL )
7036 return( -1 );
7037
7038 if( peer_crt->raw.len != crt_buf_len )
7039 return( -1 );
7040
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007041 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007042}
Hanno Becker5882dd02019-06-06 16:25:57 +01007043#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007044static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7045 unsigned char *crt_buf,
7046 size_t crt_buf_len )
7047{
7048 int ret;
7049 unsigned char const * const peer_cert_digest =
7050 ssl->session->peer_cert_digest;
7051 mbedtls_md_type_t const peer_cert_digest_type =
7052 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007053 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007054 mbedtls_md_info_from_type( peer_cert_digest_type );
7055 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7056 size_t digest_len;
7057
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007058 if( peer_cert_digest == NULL ||
7059 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7060 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007061 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007062 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007063
7064 digest_len = mbedtls_md_get_size( digest_info );
7065 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7066 return( -1 );
7067
7068 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7069 if( ret != 0 )
7070 return( -1 );
7071
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007072 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007073}
Hanno Becker5882dd02019-06-06 16:25:57 +01007074#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007075#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007076
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007077/*
7078 * Once the certificate message is read, parse it into a cert chain and
7079 * perform basic checks, but leave actual verification to the caller
7080 */
Hanno Becker35e41772019-02-05 15:37:23 +00007081static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7082 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007084 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007085#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7086 int crt_cnt=0;
7087#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007088 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007089 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007094 mbedtls_ssl_pend_fatal_alert( ssl,
7095 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 }
7098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7100 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007103 mbedtls_ssl_pend_fatal_alert( ssl,
7104 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 }
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007109
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007112 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007113 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007114
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007115 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007119 mbedtls_ssl_pend_fatal_alert( ssl,
7120 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 }
7123
Hanno Becker33c3dc82019-01-30 14:46:46 +00007124 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7125 i += 3;
7126
Hanno Becker33c3dc82019-01-30 14:46:46 +00007127 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 while( i < ssl->in_hslen )
7129 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007130 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007131 if ( i + 3 > ssl->in_hslen ) {
7132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007133 mbedtls_ssl_pend_fatal_alert( ssl,
7134 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7136 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007137 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7138 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007139 if( ssl->in_msg[i] != 0 )
7140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007142 mbedtls_ssl_pend_fatal_alert( ssl,
7143 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007145 }
7146
Hanno Becker33c3dc82019-01-30 14:46:46 +00007147 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007148 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007149 i += 3;
7150
7151 if( n < 128 || i + n > ssl->in_hslen )
7152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007154 mbedtls_ssl_pend_fatal_alert( ssl,
7155 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007157 }
7158
Hanno Becker33c3dc82019-01-30 14:46:46 +00007159 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007160#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7161 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007162 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7163 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007164 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007165 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007166 /* During client-side renegotiation, check that the server's
7167 * end-CRTs hasn't changed compared to the initial handshake,
7168 * mitigating the triple handshake attack. On success, reuse
7169 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7171 if( ssl_check_peer_crt_unchanged( ssl,
7172 &ssl->in_msg[i],
7173 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007174 {
Hanno Becker35e41772019-02-05 15:37:23 +00007175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007176 mbedtls_ssl_pend_fatal_alert( ssl,
7177 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007178 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007179 }
Hanno Becker35e41772019-02-05 15:37:23 +00007180
7181 /* Now we can safely free the original chain. */
7182 ssl_clear_peer_cert( ssl->session );
7183 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007184#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7185
Hanno Becker33c3dc82019-01-30 14:46:46 +00007186 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007187#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007188 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007189#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007190 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007191 * it in-place from the input buffer instead of making a copy. */
7192 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7193#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007194 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007195 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007196 case 0: /*ok*/
7197 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7198 /* Ignore certificate with an unknown algorithm: maybe a
7199 prior certificate was already trusted. */
7200 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007201
Hanno Becker33c3dc82019-01-30 14:46:46 +00007202 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7203 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7204 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007205
Hanno Becker33c3dc82019-01-30 14:46:46 +00007206 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7207 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7208 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007209
Hanno Becker33c3dc82019-01-30 14:46:46 +00007210 default:
7211 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7212 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007213 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007214 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7215 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 }
7217
7218 i += n;
7219 }
7220
Hanno Becker35e41772019-02-05 15:37:23 +00007221 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007222 return( 0 );
7223}
7224
Hanno Beckerb8a08572019-02-05 12:49:06 +00007225#if defined(MBEDTLS_SSL_SRV_C)
7226static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7227{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007228 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007229 return( -1 );
7230
7231#if defined(MBEDTLS_SSL_PROTO_SSL3)
7232 /*
7233 * Check if the client sent an empty certificate
7234 */
Hanno Becker2881d802019-05-22 14:44:53 +01007235 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007236 {
7237 if( ssl->in_msglen == 2 &&
7238 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7239 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7240 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7241 {
7242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7243 return( 0 );
7244 }
7245
7246 return( -1 );
7247 }
7248#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7249
7250#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7251 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7252 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7253 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7254 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007255 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 +00007256 {
7257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7258 return( 0 );
7259 }
7260
Hanno Beckerb8a08572019-02-05 12:49:06 +00007261#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7262 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007263
7264 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007265}
7266#endif /* MBEDTLS_SSL_SRV_C */
7267
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007268/* Check if a certificate message is expected.
7269 * Return either
7270 * - SSL_CERTIFICATE_EXPECTED, or
7271 * - SSL_CERTIFICATE_SKIP
7272 * indicating whether a Certificate message is expected or not.
7273 */
7274#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007275#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007276static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7277 int authmode )
7278{
Hanno Becker473f98f2019-06-26 10:27:32 +01007279 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007280 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007281
7282 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7283 return( SSL_CERTIFICATE_SKIP );
7284
7285#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007286 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007287 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007288 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7289 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7290 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007291 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007292 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007293
7294 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7295 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007296 ssl->session_negotiate->verify_result =
7297 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7298 return( SSL_CERTIFICATE_SKIP );
7299 }
7300 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007301#else
7302 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007303#endif /* MBEDTLS_SSL_SRV_C */
7304
7305 return( SSL_CERTIFICATE_EXPECTED );
7306}
7307
Hanno Becker3cf50612019-02-05 14:36:34 +00007308static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007309 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007310 mbedtls_x509_crt *chain,
7311 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007312{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007313 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7314 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7315 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007316 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007317 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007318 mbedtls_x509_crt *ca_chain;
7319 mbedtls_x509_crl *ca_crl;
7320
7321 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007322 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007323 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007324 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007325
Jarno Lamsae1621d42019-12-19 08:58:56 +02007326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007327#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7328 if( ssl->handshake->sni_ca_chain != NULL )
7329 {
7330 ca_chain = ssl->handshake->sni_ca_chain;
7331 ca_crl = ssl->handshake->sni_ca_crl;
7332 }
7333 else
7334#endif
7335 {
7336 ca_chain = ssl->conf->ca_chain;
7337 ca_crl = ssl->conf->ca_crl;
7338 }
7339
7340 /*
7341 * Main check: verify certificate
7342 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007343 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007344 chain,
7345 ca_chain, ca_crl,
7346 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007347#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007348 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007349#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007350 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007351#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7352 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7353#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7354 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007355
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007356 if( verify_ret == 0 )
7357 {
7358 mbedtls_platform_enforce_volatile_reads();
7359 if( verify_ret == 0 )
7360 {
7361 flow_counter++;
7362 }
7363 else
7364 {
7365 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7366 }
7367 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007368 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007369 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007370 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007371 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007372 }
7373
7374#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007375 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007376 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7377#endif
7378
7379 /*
7380 * Secondary checks: always done, but change 'ret' only if it was 0
7381 */
7382
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007383#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007384 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007385#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007386 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007387#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007388 mbedtls_pk_context *pk;
7389 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7390 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007391 {
7392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007393 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007394 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007395
7396 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007397 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007398 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007399 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007400 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007402 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007403#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007404
7405 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007406 {
7407 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7408
7409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007410 if( verify_ret == 0 )
7411 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007412 flow_counter++;
7413 }
7414 if( ret == 0 )
7415 {
7416 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007417 }
7418 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007419#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007420
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007421 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007422 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007423 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7424 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007425 &ssl->session_negotiate->verify_result );
7426 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007427 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007428 flow_counter++;
7429 }
7430 else
7431 {
7432 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007434 if( verify_ret == 0 )
7435 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007436 }
7437
7438 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7439 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7440 * with details encoded in the verification flags. All other kinds
7441 * of error codes, including those from the user provided f_vrfy
7442 * functions, are treated as fatal and lead to a failure of
7443 * ssl_parse_certificate even if verification was optional. */
7444 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007445 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7446 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007447 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007448 mbedtls_platform_enforce_volatile_reads();
7449 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7450 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7451 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7452 {
7453 verify_ret = 0;
7454 flow_counter++;
7455 }
7456 else
7457 {
7458 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7459 }
7460 } else {
7461 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007462 }
7463
7464 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7465 {
7466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007467 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007468 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007469 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007470 else
7471 {
7472 flow_counter++;
7473 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007474
Hanno Becker8c13ee62019-02-26 16:48:17 +00007475 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007476 {
7477 uint8_t alert;
7478
7479 /* The certificate may have been rejected for several reasons.
7480 Pick one and send the corresponding alert. Which alert to send
7481 may be a subject of debate in some cases. */
7482 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7483 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7484 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7485 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7486 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7487 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7488 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7489 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7490 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7491 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7492 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7493 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7494 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7495 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7496 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7497 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7498 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7499 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7500 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7501 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7502 else
7503 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007504 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007505 }
7506
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007507 if( verify_ret == 0 &&
7508#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7509 flow_counter == 5 )
7510#else
7511 flow_counter == 4 )
7512#endif
7513 {
7514 mbedtls_platform_enforce_volatile_reads();
7515 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 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007523 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7524 }
7525 else
7526 {
7527 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7528 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007529 } else {
7530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007531 }
7532
Hanno Becker3cf50612019-02-05 14:36:34 +00007533#if defined(MBEDTLS_DEBUG_C)
7534 if( ssl->session_negotiate->verify_result != 0 )
7535 {
7536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7537 ssl->session_negotiate->verify_result ) );
7538 }
7539 else
7540 {
7541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7542 }
7543#endif /* MBEDTLS_DEBUG_C */
7544
Hanno Becker8c13ee62019-02-26 16:48:17 +00007545 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007546}
7547
Hanno Becker34106f62019-02-08 14:59:05 +00007548#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007549
7550#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007551static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7552 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007553{
7554 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007555 /* Remember digest of the peer's end-CRT. */
7556 ssl->session_negotiate->peer_cert_digest =
7557 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7558 if( ssl->session_negotiate->peer_cert_digest == NULL )
7559 {
7560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7561 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007562 mbedtls_ssl_pend_fatal_alert( ssl,
7563 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007564
7565 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7566 }
7567
7568 ret = mbedtls_md( mbedtls_md_info_from_type(
7569 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7570 start, len,
7571 ssl->session_negotiate->peer_cert_digest );
7572
7573 ssl->session_negotiate->peer_cert_digest_type =
7574 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7575 ssl->session_negotiate->peer_cert_digest_len =
7576 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7577
7578 return( ret );
7579}
Hanno Becker5882dd02019-06-06 16:25:57 +01007580#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007581
7582static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7583 unsigned char *start, size_t len )
7584{
7585 unsigned char *end = start + len;
7586 int ret;
7587
7588 /* Make a copy of the peer's raw public key. */
7589 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7590 ret = mbedtls_pk_parse_subpubkey( &start, end,
7591 &ssl->handshake->peer_pubkey );
7592 if( ret != 0 )
7593 {
7594 /* We should have parsed the public key before. */
7595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7596 }
7597
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007598 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007599 return( 0 );
7600}
7601#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7602
Hanno Becker3cf50612019-02-05 14:36:34 +00007603int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7604{
7605 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007606 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007607#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7608 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7609 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007610 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007611#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007612 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007613#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007614 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007615 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007616
7617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7618
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007619 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7620 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007621 {
7622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007623 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007624 }
7625
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007626#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7627 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007628 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007629 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007630 chain = ssl->handshake->ecrs_peer_cert;
7631 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007632 goto crt_verify;
7633 }
7634#endif
7635
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007636 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007637 {
7638 /* mbedtls_ssl_read_record may have sent an alert already. We
7639 let it decide whether to alert. */
7640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007641 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007642 }
7643
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007644#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007645 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7646 {
7647 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007648
Hanno Beckerb8a08572019-02-05 12:49:06 +00007649 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007650 ret = 0;
7651 else
7652 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007653
Hanno Becker613d4902019-02-05 13:11:17 +00007654 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007655 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007656#endif /* MBEDTLS_SSL_SRV_C */
7657
Hanno Becker35e41772019-02-05 15:37:23 +00007658 /* Clear existing peer CRT structure in case we tried to
7659 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007660 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007661
7662 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7663 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007664 {
7665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7666 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007667 mbedtls_ssl_pend_fatal_alert( ssl,
7668 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007669
Hanno Beckere4aeb762019-02-05 17:19:52 +00007670 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7671 goto exit;
7672 }
7673 mbedtls_x509_crt_init( chain );
7674
7675 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007676 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007677 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007678
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007679#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7680 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007681 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007682
7683crt_verify:
7684 if( ssl->handshake->ecrs_enabled)
7685 rs_ctx = &ssl->handshake->ecrs_ctx;
7686#endif
7687
Hanno Becker3cf50612019-02-05 14:36:34 +00007688 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007689 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007690 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007691 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Hanno Becker3008d282019-02-05 17:02:28 +00007693#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007695 size_t pk_len;
7696 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007697
Hanno Becker34106f62019-02-08 14:59:05 +00007698 /* We parse the CRT chain without copying, so
7699 * these pointers point into the input buffer,
7700 * and are hence still valid after freeing the
7701 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007702
Hanno Becker5882dd02019-06-06 16:25:57 +01007703#if defined(MBEDTLS_SSL_RENEGOTIATION)
7704 unsigned char *crt_start;
7705 size_t crt_len;
7706
Hanno Becker34106f62019-02-08 14:59:05 +00007707 crt_start = chain->raw.p;
7708 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007709#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007710
Hanno Becker8c13ee62019-02-26 16:48:17 +00007711 pk_start = chain->cache->pk_raw.p;
7712 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007713
7714 /* Free the CRT structures before computing
7715 * digest and copying the peer's public key. */
7716 mbedtls_x509_crt_free( chain );
7717 mbedtls_free( chain );
7718 chain = NULL;
7719
Hanno Becker5882dd02019-06-06 16:25:57 +01007720#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007721 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007722 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007723 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007724#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007725
Hanno Becker34106f62019-02-08 14:59:05 +00007726 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007728 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 }
Hanno Becker34106f62019-02-08 14:59:05 +00007730#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7731 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007732 ssl->session_negotiate->peer_cert = chain;
7733 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007734#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007737
Hanno Becker613d4902019-02-05 13:11:17 +00007738exit:
7739
Hanno Beckere4aeb762019-02-05 17:19:52 +00007740 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007741 {
7742 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7743 {
7744 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7745 }
7746 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7747 {
7748 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7749 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007750 else
7751 {
7752 ssl->state = MBEDTLS_SSL_INVALID;
7753 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007754 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007755
7756#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7757 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7758 {
7759 ssl->handshake->ecrs_peer_cert = chain;
7760 chain = NULL;
7761 }
7762#endif
7763
7764 if( chain != NULL )
7765 {
7766 mbedtls_x509_crt_free( chain );
7767 mbedtls_free( chain );
7768 }
7769
Paul Bakker5121ce52009-01-03 21:22:43 +00007770 return( ret );
7771}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007772#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007775{
7776 int ret;
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007781 ssl->out_msglen = 1;
7782 ssl->out_msg[0] = 1;
7783
Jarno Lamsa2b205162019-11-12 15:36:21 +02007784 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7785 {
7786 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7787 }
7788 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7789 {
7790 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7791 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007792 else
7793 {
7794 ssl->state = MBEDTLS_SSL_INVALID;
7795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007796
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007797 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007798 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007800 return( ret );
7801 }
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
7805 return( 0 );
7806}
7807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007809{
7810 int ret;
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007813
Hanno Becker327c93b2018-08-15 13:56:18 +01007814 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 return( ret );
7818 }
7819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007823 mbedtls_ssl_pend_fatal_alert( ssl,
7824 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 }
7827
Hanno Beckere678eaa2018-08-21 14:57:46 +01007828 /* CCS records are only accepted if they have length 1 and content '1',
7829 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007831 /*
7832 * Switch to our negotiated transform and session parameters for inbound
7833 * data.
7834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007836 ssl->transform_in = ssl->transform_negotiate;
7837 ssl->session_in = ssl->session_negotiate;
7838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007840 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007843 ssl_dtls_replay_reset( ssl );
7844#endif
7845
7846 /* Increment epoch */
7847 if( ++ssl->in_epoch == 0 )
7848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007850 /* This is highly unlikely to happen for legitimate reasons, so
7851 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007853 }
7854 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007855 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007857#if defined(MBEDTLS_SSL_PROTO_TLS)
7858 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007859 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007860 }
7861#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007862
Hanno Beckerf5970a02019-05-08 09:38:41 +01007863 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7866 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007871 mbedtls_ssl_pend_fatal_alert( ssl,
7872 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007874 }
7875 }
7876#endif
7877
Jarno Lamsa2b205162019-11-12 15:36:21 +02007878 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7879 {
7880 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7881 }
7882 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7883 {
7884 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7885 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007886 else
7887 {
7888 ssl->state = MBEDTLS_SSL_INVALID;
7889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007892
7893 return( 0 );
7894}
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7899 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007900 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007901 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7904#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007905 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007908 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007911}
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007916
7917 /*
7918 * Free our handshake params
7919 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007920 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007922 ssl->handshake = NULL;
7923
7924 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007925 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007926 */
7927 if( ssl->transform )
7928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 mbedtls_ssl_transform_free( ssl->transform );
7930 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007931 }
7932 ssl->transform = ssl->transform_negotiate;
7933 ssl->transform_negotiate = NULL;
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007936}
7937
Jarno Lamsae1621d42019-12-19 08:58:56 +02007938int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007939{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007940 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007941
7942#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007943 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007944 ? ssl->handshake->sni_authmode
7945 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7946#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007947 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007948#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007949#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7950 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7951 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_SSL_RENEGOTIATION)
7956 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007959 ssl->renego_records_seen = 0;
7960 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007961#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007962
7963 /*
7964 * Free the previous session and switch in the current one
7965 */
Paul Bakker0a597072012-09-25 21:55:46 +00007966 if( ssl->session )
7967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007969 /* RFC 7366 3.1: keep the EtM state */
7970 ssl->session_negotiate->encrypt_then_mac =
7971 ssl->session->encrypt_then_mac;
7972#endif
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 mbedtls_ssl_session_free( ssl->session );
7975 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007976 }
7977 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007978 ssl->session_negotiate = NULL;
7979
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007980#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007981 /*
7982 * Add cache entry
7983 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007984 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007985 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007986 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007987 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007988 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007990 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007991#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007992
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007993 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
7994 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
7995#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7996 crt_expected == SSL_CERTIFICATE_SKIP )
7997#else
7998 1 )
7999#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008000 {
Jarno Lamsa015aa442019-12-20 12:09:37 +02008001 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008002 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8003 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8004#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8005 crt_expected == SSL_CERTIFICATE_SKIP )
8006#else
8007 1 )
8008#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008009 {
8010 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8011 }
8012 else
8013 {
8014 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8015 goto cleanup;
8016 }
8017 }
8018
Jarno Lamsae1621d42019-12-19 08:58:56 +02008019#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008020 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008021 {
8022 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008023 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008024 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008025 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008026 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008027 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008028 }
8029 else
8030 {
8031 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008032 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008033 }
8034 }
8035#endif
8036
8037 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8038 {
8039 mbedtls_platform_enforce_volatile_reads();
8040 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8041 {
8042 ret = 0;
8043 }
8044 else
8045 {
8046 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008047 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008048 }
8049 }
8050 else
8051 {
8052 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008053 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008054 }
8055
Jarno Lamsa06164052019-12-19 14:40:36 +02008056 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8057 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8058 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8059 {
8060 mbedtls_platform_enforce_volatile_reads();
8061 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8062 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8063 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8064 {
8065 ret = 0;
8066 }
8067 else
8068 {
8069 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8070 goto cleanup;
8071 }
8072 }
8073 else
8074 {
8075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8078 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8079 }
8080
8081cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008083 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008084 ssl->handshake->flight != NULL )
8085 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008086 /* Cancel handshake timer */
8087 ssl_set_timer( ssl, 0 );
8088
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008089 /* Keep last flight around in case we need to resend it:
8090 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008092 }
8093 else
8094#endif
8095 ssl_handshake_wrapup_free_hs_transform( ssl );
8096
Jarno Lamsa2b205162019-11-12 15:36:21 +02008097 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008100 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008101}
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008104{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008105 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008108
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008109 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008110
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008111 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8112 mbedtls_ssl_suite_get_mac(
8113 mbedtls_ssl_ciphersuite_from_id(
8114 mbedtls_ssl_session_get_ciphersuite(
8115 ssl->session_negotiate ) ) ),
8116 ssl, ssl->out_msg + 4,
8117 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008118
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008119 /*
8120 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8121 * may define some other value. Currently (early 2016), no defined
8122 * ciphersuite does this (and this is unlikely to change as activity has
8123 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8124 */
Hanno Becker2881d802019-05-22 14:44:53 +01008125 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008128 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008129 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008130#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008131
Paul Bakker5121ce52009-01-03 21:22:43 +00008132 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8134 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008135
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008136#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008137 /*
8138 * In case of session resuming, invert the client and server
8139 * ChangeCipherSpec messages order.
8140 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008141 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008144 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8145 MBEDTLS_SSL_IS_CLIENT )
8146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008148 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008151 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8152 MBEDTLS_SSL_IS_SERVER )
8153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008155 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008157 }
8158 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008159#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008160 {
8161 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8162 {
8163 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8164 }
8165 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8166 {
8167 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8168 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008169 else
8170 {
8171 ssl->state = MBEDTLS_SSL_INVALID;
8172 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008174
Paul Bakker48916f92012-09-16 19:57:18 +00008175 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008176 * Switch to our negotiated transform and session parameters for outbound
8177 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008182 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008183 {
8184 unsigned char i;
8185
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008186 /* Remember current epoch settings for resending */
8187 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008188 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008189
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008190 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008191 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008192
8193 /* Increment epoch */
8194 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008195 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008196 break;
8197
8198 /* The loop goes to its end iff the counter is wrapping */
8199 if( i == 0 )
8200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8202 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008203 }
8204 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008205 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008207#if defined(MBEDTLS_SSL_PROTO_TLS)
8208 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008209 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008210 }
8211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008212
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008213 ssl->transform_out = ssl->transform_negotiate;
8214 ssl->session_out = ssl->session_negotiate;
8215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8217 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8222 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008223 }
8224 }
8225#endif
8226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008228 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008230#endif
8231
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008232 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008235 return( ret );
8236 }
8237
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008239 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008240 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8241 {
8242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8243 return( ret );
8244 }
8245#endif
8246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008248
8249 return( 0 );
8250}
8251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008253#define SSL_MAX_HASH_LEN 36
8254#else
8255#define SSL_MAX_HASH_LEN 12
8256#endif
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008259{
Paul Bakker23986e52011-04-24 08:57:21 +00008260 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008261 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008262 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008265
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008266 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8267 mbedtls_ssl_suite_get_mac(
8268 mbedtls_ssl_ciphersuite_from_id(
8269 mbedtls_ssl_session_get_ciphersuite(
8270 ssl->session_negotiate ) ) ),
8271 ssl, buf,
8272 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008273
Hanno Becker327c93b2018-08-15 13:56:18 +01008274 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008277 return( ret );
8278 }
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008283 mbedtls_ssl_pend_fatal_alert( ssl,
8284 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008286 }
8287
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008288 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008290 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008291 hash_len = 36;
8292 else
8293#endif
8294 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8297 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008300 mbedtls_ssl_pend_fatal_alert( ssl,
8301 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 }
8304
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008305 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008306 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008309 mbedtls_ssl_pend_fatal_alert( ssl,
8310 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 }
8313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008315 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008316 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008317#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008318
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008319#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008320 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008323 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008327 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008329#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008330 }
8331 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008332#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008333 {
8334 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8335 {
8336 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8337 }
8338 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8339 {
8340 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8341 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008342 else
8343 {
8344 ssl->state = MBEDTLS_SSL_INVALID;
8345 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008349 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008351#endif
8352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008354
8355 return( 0 );
8356}
8357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8363 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8364 mbedtls_md5_init( &handshake->fin_md5 );
8365 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008366 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8367 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8370#if defined(MBEDTLS_SHA256_C)
8371 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008372 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SHA512_C)
8375 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008376 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008379
Hanno Becker7e5437a2017-04-28 17:15:26 +01008380#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8381 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8382 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8383#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_DHM_C)
8386 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388#if defined(MBEDTLS_ECDH_C)
8389 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008390#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008391#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008392 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008393#if defined(MBEDTLS_SSL_CLI_C)
8394 handshake->ecjpake_cache = NULL;
8395 handshake->ecjpake_cache_len = 0;
8396#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008397#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008398
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008399#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008400 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008401#endif
8402
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008403#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8404 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8405#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008406
8407#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8408 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8409 mbedtls_pk_init( &handshake->peer_pubkey );
8410#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008411}
8412
Hanno Becker611a83b2018-01-03 14:27:32 +00008413void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8418 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008419
Hanno Becker92231322018-01-03 15:32:51 +00008420#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 mbedtls_md_init( &transform->md_ctx_enc );
8422 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008423#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008424}
8425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008429}
8430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008432{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008433 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008434 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008436 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008438 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008439 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008440
8441 /*
8442 * Either the pointers are now NULL or cleared properly and can be freed.
8443 * Now allocate missing structures.
8444 */
8445 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008446 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008447 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008448 }
Paul Bakker48916f92012-09-16 19:57:18 +00008449
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008450 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008451 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008452 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008453 }
Paul Bakker48916f92012-09-16 19:57:18 +00008454
Paul Bakker82788fb2014-10-20 13:59:19 +02008455 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008456 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008457 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008458 }
Paul Bakker48916f92012-09-16 19:57:18 +00008459
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008460 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008461 if( ssl->handshake == NULL ||
8462 ssl->transform_negotiate == NULL ||
8463 ssl->session_negotiate == NULL )
8464 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 mbedtls_free( ssl->handshake );
8468 mbedtls_free( ssl->transform_negotiate );
8469 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008470
8471 ssl->handshake = NULL;
8472 ssl->transform_negotiate = NULL;
8473 ssl->session_negotiate = NULL;
8474
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008475 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008476 }
8477
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008478 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008480 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008481 ssl_handshake_params_init( ssl->handshake );
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008484 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008485 {
8486 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008487
Hanno Becker2d9623f2019-06-13 12:07:05 +01008488 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008489 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8490 else
8491 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8492 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008493#endif
8494
Paul Bakker48916f92012-09-16 19:57:18 +00008495 return( 0 );
8496}
8497
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008498#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008499/* Dummy cookie callbacks for defaults */
8500static int ssl_cookie_write_dummy( void *ctx,
8501 unsigned char **p, unsigned char *end,
8502 const unsigned char *cli_id, size_t cli_id_len )
8503{
8504 ((void) ctx);
8505 ((void) p);
8506 ((void) end);
8507 ((void) cli_id);
8508 ((void) cli_id_len);
8509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008511}
8512
8513static int ssl_cookie_check_dummy( void *ctx,
8514 const unsigned char *cookie, size_t cookie_len,
8515 const unsigned char *cli_id, size_t cli_id_len )
8516{
8517 ((void) ctx);
8518 ((void) cookie);
8519 ((void) cookie_len);
8520 ((void) cli_id);
8521 ((void) cli_id_len);
8522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008524}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008525#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008526
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008527/* Once ssl->out_hdr as the address of the beginning of the
8528 * next outgoing record is set, deduce the other pointers.
8529 *
8530 * Note: For TLS, we save the implicit record sequence number
8531 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8532 * and the caller has to make sure there's space for this.
8533 */
8534
8535static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8536 mbedtls_ssl_transform *transform )
8537{
8538#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008539 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008540 {
8541 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008543 ssl->out_cid = ssl->out_ctr + 8;
8544 ssl->out_len = ssl->out_cid;
8545 if( transform != NULL )
8546 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008547#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008548 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008549#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008550 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008551 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008552 MBEDTLS_SSL_TRANSPORT_ELSE
8553#endif /* MBEDTLS_SSL_PROTO_DTLS */
8554#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008555 {
8556 ssl->out_ctr = ssl->out_hdr - 8;
8557 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008558#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008559 ssl->out_cid = ssl->out_len;
8560#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008561 ssl->out_iv = ssl->out_hdr + 5;
8562 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008563#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008564
8565 /* Adjust out_msg to make space for explicit IV, if used. */
8566 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008567 mbedtls_ssl_ver_geq(
8568 mbedtls_ssl_get_minor_ver( ssl ),
8569 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008570 {
8571 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8572 }
8573 else
8574 ssl->out_msg = ssl->out_iv;
8575}
8576
8577/* Once ssl->in_hdr as the address of the beginning of the
8578 * next incoming record is set, deduce the other pointers.
8579 *
8580 * Note: For TLS, we save the implicit record sequence number
8581 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8582 * and the caller has to make sure there's space for this.
8583 */
8584
Hanno Beckerf5970a02019-05-08 09:38:41 +01008585static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008586{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008587 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008588 * of unprotected TLS/DTLS records, with ssl->in_msg
8589 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008590 *
8591 * When decrypting a protected record, ssl->in_msg
8592 * will be shifted to point to the beginning of the
8593 * record plaintext.
8594 */
8595
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008597 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008598 {
Hanno Becker70e79282019-05-03 14:34:53 +01008599 /* This sets the header pointers to match records
8600 * without CID. When we receive a record containing
8601 * a CID, the fields are shifted accordingly in
8602 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008603 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008605 ssl->in_cid = ssl->in_ctr + 8;
8606 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008607#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008608 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008609#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008610 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008611 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008612 MBEDTLS_SSL_TRANSPORT_ELSE
8613#endif /* MBEDTLS_SSL_PROTO_DTLS */
8614#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008615 {
8616 ssl->in_ctr = ssl->in_hdr - 8;
8617 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008618#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008619 ssl->in_cid = ssl->in_len;
8620#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008621 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008622 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008623#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008624}
8625
Paul Bakker5121ce52009-01-03 21:22:43 +00008626/*
8627 * Initialize an SSL context
8628 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008629void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8630{
8631 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8632}
8633
8634/*
8635 * Setup an SSL context
8636 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008637
8638static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8639{
8640 /* Set the incoming and outgoing record pointers. */
8641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008642 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008643 {
8644 ssl->out_hdr = ssl->out_buf;
8645 ssl->in_hdr = ssl->in_buf;
8646 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008647 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008649#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008650 {
8651 ssl->out_hdr = ssl->out_buf + 8;
8652 ssl->in_hdr = ssl->in_buf + 8;
8653 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008654#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008655
8656 /* Derive other internal pointers. */
8657 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008658 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008659}
8660
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008661int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008662 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008663{
Paul Bakker48916f92012-09-16 19:57:18 +00008664 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008665
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008666 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008667
Hanno Beckeref982d52019-07-23 15:56:18 +01008668#if defined(MBEDTLS_USE_TINYCRYPT)
8669 uECC_set_rng( &uecc_rng_wrapper );
8670#endif
8671
Paul Bakker62f2dee2012-09-28 07:31:51 +00008672 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008673 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008674 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008675
8676 /* Set to NULL in case of an error condition */
8677 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008678
Angus Grattond8213d02016-05-25 20:56:48 +10008679 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8680 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008681 {
Angus Grattond8213d02016-05-25 20:56:48 +10008682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008683 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008684 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008685 }
8686
8687 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8688 if( ssl->out_buf == NULL )
8689 {
8690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_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;
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 }
8694
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008695 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008696
Paul Bakker48916f92012-09-16 19:57:18 +00008697 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008698 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008699
Hanno Beckerc8f52992019-07-25 11:15:08 +01008700 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008701
Paul Bakker5121ce52009-01-03 21:22:43 +00008702 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008703
8704error:
8705 mbedtls_free( ssl->in_buf );
8706 mbedtls_free( ssl->out_buf );
8707
8708 ssl->conf = NULL;
8709
8710 ssl->in_buf = NULL;
8711 ssl->out_buf = NULL;
8712
8713 ssl->in_hdr = NULL;
8714 ssl->in_ctr = NULL;
8715 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008716 ssl->in_msg = NULL;
8717
8718 ssl->out_hdr = NULL;
8719 ssl->out_ctr = NULL;
8720 ssl->out_len = NULL;
8721 ssl->out_iv = NULL;
8722 ssl->out_msg = NULL;
8723
k-stachowiak9f7798e2018-07-31 16:52:32 +02008724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008725}
8726
8727/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008728 * Reset an initialized and used SSL context for re-use while retaining
8729 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008730 *
8731 * If partial is non-zero, keep data in the input buffer and client ID.
8732 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008733 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008734static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008735{
Paul Bakker48916f92012-09-16 19:57:18 +00008736 int ret;
8737
Hanno Becker7e772132018-08-10 12:38:21 +01008738#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8739 !defined(MBEDTLS_SSL_SRV_C)
8740 ((void) partial);
8741#endif
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008744
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008745 /* Cancel any possibly running timer */
8746 ssl_set_timer( ssl, 0 );
8747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748#if defined(MBEDTLS_SSL_RENEGOTIATION)
8749 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008750 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008751
8752 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008753 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8754 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008757
Paul Bakker7eb013f2011-10-06 12:37:39 +00008758 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008759 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008760
8761 ssl->in_msgtype = 0;
8762 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008764 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008765 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008768 ssl_dtls_replay_reset( ssl );
8769#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008770
8771 ssl->in_hslen = 0;
8772 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008773
8774 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008775
8776 ssl->out_msgtype = 0;
8777 ssl->out_msglen = 0;
8778 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8780 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008781 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008782#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008783
Hanno Becker19859472018-08-06 09:40:20 +01008784 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8785
Paul Bakker48916f92012-09-16 19:57:18 +00008786 ssl->transform_in = NULL;
8787 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008788
Hanno Becker78640902018-08-13 16:35:15 +01008789 ssl->session_in = NULL;
8790 ssl->session_out = NULL;
8791
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008792 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008793
8794#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008795 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008796#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8797 {
8798 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008799 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008800 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8803 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8806 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008810 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008811 }
8812#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008813
Paul Bakker48916f92012-09-16 19:57:18 +00008814 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 mbedtls_ssl_transform_free( ssl->transform );
8817 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008818 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008819 }
Paul Bakker48916f92012-09-16 19:57:18 +00008820
Paul Bakkerc0463502013-02-14 11:19:38 +01008821 if( ssl->session )
8822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823 mbedtls_ssl_session_free( ssl->session );
8824 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008825 ssl->session = NULL;
8826 }
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008829 ssl->alpn_chosen = NULL;
8830#endif
8831
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008832#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008833#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008834 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008835#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008836 {
8837 mbedtls_free( ssl->cli_id );
8838 ssl->cli_id = NULL;
8839 ssl->cli_id_len = 0;
8840 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008841#endif
8842
Paul Bakker48916f92012-09-16 19:57:18 +00008843 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8844 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008845
8846 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008847}
8848
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008849/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008850 * Reset an initialized and used SSL context for re-use while retaining
8851 * all application-set variables, function pointers and data.
8852 */
8853int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8854{
8855 return( ssl_session_reset_int( ssl, 0 ) );
8856}
8857
8858/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008859 * SSL set accessors
8860 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008861#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008862void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008864 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008865}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008866#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008867
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008868void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008869{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008870 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008871}
8872
Hanno Becker7f376f42019-06-12 16:20:48 +01008873#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8874 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008875void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008876{
Hanno Becker7f376f42019-06-12 16:20:48 +01008877 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008878}
Hanno Becker7f376f42019-06-12 16:20:48 +01008879#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008880
Hanno Beckerde671542019-06-12 16:30:46 +01008881#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8882 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8883void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8884 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008885{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008886 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008887}
Hanno Beckerde671542019-06-12 16:30:46 +01008888#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008891
Hanno Becker1841b0a2018-08-24 11:13:57 +01008892void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8893 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008894{
8895 ssl->disable_datagram_packing = !allow_packing;
8896}
8897
Hanno Becker1f835fa2019-06-13 10:14:59 +01008898#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8899 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008900void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8901 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008902{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008903 conf->hs_timeout_min = min;
8904 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008905}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008906#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8907 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8908void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8909 uint32_t min, uint32_t max )
8910{
8911 ((void) conf);
8912 ((void) min);
8913 ((void) max);
8914}
8915#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8916 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8917
8918#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008919
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008920void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008921{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008922#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8923 conf->authmode = authmode;
8924#else
8925 ((void) conf);
8926 ((void) authmode);
8927#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008928}
8929
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008930#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8931 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008932void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008933 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008934 void *p_vrfy )
8935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008936 conf->f_vrfy = f_vrfy;
8937 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008938}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008939#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008940
Hanno Beckerece325c2019-06-13 15:39:27 +01008941#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008942void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008943 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008944 void *p_rng )
8945{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008946 conf->f_rng = f_rng;
8947 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008948}
Hanno Beckerece325c2019-06-13 15:39:27 +01008949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008950
Hanno Becker14a4a442019-07-02 17:00:34 +01008951#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008952void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008953 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008954 void *p_dbg )
8955{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008956 conf->f_dbg = f_dbg;
8957 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008958}
Hanno Becker14a4a442019-07-02 17:00:34 +01008959#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008960
Hanno Beckera58a8962019-06-13 16:11:15 +01008961#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8962 !defined(MBEDTLS_SSL_CONF_SEND) && \
8963 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008965 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008966 mbedtls_ssl_send_t *f_send,
8967 mbedtls_ssl_recv_t *f_recv,
8968 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008969{
Hanno Beckera58a8962019-06-13 16:11:15 +01008970 ssl->p_bio = p_bio;
8971 ssl->f_send = f_send;
8972 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008973 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008974}
Hanno Beckera58a8962019-06-13 16:11:15 +01008975#else
8976void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8977 void *p_bio )
8978{
8979 ssl->p_bio = p_bio;
8980}
8981#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008982
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008983#if defined(MBEDTLS_SSL_PROTO_DTLS)
8984void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8985{
8986 ssl->mtu = mtu;
8987}
8988#endif
8989
Hanno Becker1f835fa2019-06-13 10:14:59 +01008990#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008991void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008992{
8993 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008994}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008995#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008996
Hanno Becker0ae6b242019-06-13 16:45:36 +01008997#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8998 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008999void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9000 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009001 mbedtls_ssl_set_timer_t *f_set_timer,
9002 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009003{
9004 ssl->p_timer = p_timer;
9005 ssl->f_set_timer = f_set_timer;
9006 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009007 /* Make sure we start with no timer running */
9008 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009009}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009010#else
9011void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9012 void *p_timer )
9013{
9014 ssl->p_timer = p_timer;
9015 /* Make sure we start with no timer running */
9016 ssl_set_timer( ssl, 0 );
9017}
9018#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009019
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009020#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009021void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009022 void *p_cache,
9023 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9024 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009025{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009026 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009027 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009028 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009029}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009030#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009031
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009032#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009034{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009035 int ret;
9036
9037 if( ssl == NULL ||
9038 session == NULL ||
9039 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009040 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009043 }
9044
Hanno Becker58fccf22019-02-06 14:30:46 +00009045 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9046 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009047 return( ret );
9048
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009049 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009050
9051 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009052}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009053#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009054
Hanno Becker73f4cb12019-06-27 13:51:07 +01009055#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009057 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009058{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009059 conf->ciphersuite_list[0] = ciphersuites;
9060 conf->ciphersuite_list[1] = ciphersuites;
9061 conf->ciphersuite_list[2] = ciphersuites;
9062 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009063}
9064
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009065void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009066 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009067 int major, int minor )
9068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009070 return;
9071
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009072 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9073 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9074 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009075 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009076 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009077
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009078 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9079 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009080}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009081#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009084void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009085 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009086{
9087 conf->cert_profile = profile;
9088}
9089
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009090/* Append a new keycert entry to a (possibly empty) list */
9091static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9092 mbedtls_x509_crt *cert,
9093 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009094{
niisato8ee24222018-06-25 19:05:48 +09009095 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009096
niisato8ee24222018-06-25 19:05:48 +09009097 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9098 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009099 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009100
niisato8ee24222018-06-25 19:05:48 +09009101 new_cert->cert = cert;
9102 new_cert->key = key;
9103 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009104
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009105 /* Update head is the list was null, else add to the end */
9106 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009107 {
niisato8ee24222018-06-25 19:05:48 +09009108 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009109 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009110 else
9111 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009112 mbedtls_ssl_key_cert *cur = *head;
9113 while( cur->next != NULL )
9114 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009115 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009116 }
9117
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009118 return( 0 );
9119}
9120
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009122 mbedtls_x509_crt *own_cert,
9123 mbedtls_pk_context *pk_key )
9124{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009125 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009126}
9127
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009129 mbedtls_x509_crt *ca_chain,
9130 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009131{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009132 conf->ca_chain = ca_chain;
9133 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009136
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009137#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9138int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9139 mbedtls_x509_crt *own_cert,
9140 mbedtls_pk_context *pk_key )
9141{
9142 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9143 own_cert, pk_key ) );
9144}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009145
9146void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9147 mbedtls_x509_crt *ca_chain,
9148 mbedtls_x509_crl *ca_crl )
9149{
9150 ssl->handshake->sni_ca_chain = ca_chain;
9151 ssl->handshake->sni_ca_crl = ca_crl;
9152}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009153
9154void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9155 int authmode )
9156{
9157 ssl->handshake->sni_authmode = authmode;
9158}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009159#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9160
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009161#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009162/*
9163 * Set EC J-PAKE password for current handshake
9164 */
9165int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9166 const unsigned char *pw,
9167 size_t pw_len )
9168{
9169 mbedtls_ecjpake_role role;
9170
Janos Follath8eb64132016-06-03 15:40:57 +01009171 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9173
Hanno Becker2d9623f2019-06-13 12:07:05 +01009174 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009175 role = MBEDTLS_ECJPAKE_SERVER;
9176 else
9177 role = MBEDTLS_ECJPAKE_CLIENT;
9178
9179 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9180 role,
9181 MBEDTLS_MD_SHA256,
9182 MBEDTLS_ECP_DP_SECP256R1,
9183 pw, pw_len ) );
9184}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009185#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009188int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009189 const unsigned char *psk, size_t psk_len,
9190 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009191{
Paul Bakker6db455e2013-09-18 17:29:31 +02009192 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009197
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009198 /* Identity len will be encoded on two bytes */
9199 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009200 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009201 {
9202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9203 }
9204
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009205 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009206 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009207 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009208
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009209 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009210 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009211 conf->psk_len = 0;
9212 }
9213 if( conf->psk_identity != NULL )
9214 {
9215 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009216 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009217 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009218 }
9219
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009220 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9221 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009222 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009223 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009224 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009225 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009226 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009228 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009229
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009230 conf->psk_len = psk_len;
9231 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009232
Teppo Järvelin91d79382019-10-02 09:09:31 +03009233 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9234 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009235
9236 return( 0 );
9237}
9238
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009239int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9240 const unsigned char *psk, size_t psk_len )
9241{
9242 if( psk == NULL || ssl->handshake == NULL )
9243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9244
9245 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9247
9248 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009249 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009250 mbedtls_platform_zeroize( ssl->handshake->psk,
9251 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009252 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009253 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009254 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009255
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009256 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009258
9259 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009260 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009261
9262 return( 0 );
9263}
9264
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009265void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009267 size_t),
9268 void *p_psk )
9269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009270 conf->f_psk = f_psk;
9271 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009274
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009275#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009276
9277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009278int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009279{
9280 int ret;
9281
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009282 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9283 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9284 {
9285 mbedtls_mpi_free( &conf->dhm_P );
9286 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009287 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009289
9290 return( 0 );
9291}
Hanno Becker470a8c42017-10-04 15:28:46 +01009292#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009293
Hanno Beckera90658f2017-10-04 15:29:08 +01009294int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9295 const unsigned char *dhm_P, size_t P_len,
9296 const unsigned char *dhm_G, size_t G_len )
9297{
9298 int ret;
9299
9300 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9301 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9302 {
9303 mbedtls_mpi_free( &conf->dhm_P );
9304 mbedtls_mpi_free( &conf->dhm_G );
9305 return( ret );
9306 }
9307
9308 return( 0 );
9309}
Paul Bakker5121ce52009-01-03 21:22:43 +00009310
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009311int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009312{
9313 int ret;
9314
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009315 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9316 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9317 {
9318 mbedtls_mpi_free( &conf->dhm_P );
9319 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009320 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009321 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009322
9323 return( 0 );
9324}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009325#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009326
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009327#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9328/*
9329 * Set the minimum length for Diffie-Hellman parameters
9330 */
9331void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9332 unsigned int bitlen )
9333{
9334 conf->dhm_min_bitlen = bitlen;
9335}
9336#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9337
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009338#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009339/*
9340 * Set allowed/preferred hashes for handshake signatures
9341 */
9342void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9343 const int *hashes )
9344{
Hanno Becker56595f42019-06-19 16:31:38 +01009345#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009346 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009347#else
9348 ((void) conf);
9349 ((void) hashes);
9350#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009351}
Hanno Becker947194e2017-04-07 13:25:49 +01009352#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009353
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009354#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009355#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009356/*
9357 * Set the allowed elliptic curves
9358 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009359void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009360 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009361{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009362 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009363}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009364#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009365#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009366
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009367#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009369{
Hanno Becker947194e2017-04-07 13:25:49 +01009370 /* Initialize to suppress unnecessary compiler warning */
9371 size_t hostname_len = 0;
9372
9373 /* Check if new hostname is valid before
9374 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009375 if( hostname != NULL )
9376 {
9377 hostname_len = strlen( hostname );
9378
9379 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9381 }
9382
9383 /* Now it's clear that we will overwrite the old hostname,
9384 * so we can free it safely */
9385
9386 if( ssl->hostname != NULL )
9387 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009388 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009389 mbedtls_free( ssl->hostname );
9390 }
9391
9392 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009393
Paul Bakker5121ce52009-01-03 21:22:43 +00009394 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009395 {
9396 ssl->hostname = NULL;
9397 }
9398 else
9399 {
9400 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009401 if( ssl->hostname == NULL )
9402 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009403
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009404 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9405 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009406
Hanno Becker947194e2017-04-07 13:25:49 +01009407 ssl->hostname[hostname_len] = '\0';
9408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009409
9410 return( 0 );
9411}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009412#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009413
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009414#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009415void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009417 const unsigned char *, size_t),
9418 void *p_sni )
9419{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009420 conf->f_sni = f_sni;
9421 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009426int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009427{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009428 size_t cur_len, tot_len;
9429 const char **p;
9430
9431 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009432 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9433 * MUST NOT be truncated."
9434 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009435 */
9436 tot_len = 0;
9437 for( p = protos; *p != NULL; p++ )
9438 {
9439 cur_len = strlen( *p );
9440 tot_len += cur_len;
9441
9442 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009444 }
9445
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009446 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009447
9448 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009449}
9450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009452{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009453 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009456
Hanno Becker33b9b252019-07-05 11:23:25 +01009457#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9458 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9459void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9460 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009462 conf->max_major_ver = major;
9463 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009464}
Hanno Becker33b9b252019-07-05 11:23:25 +01009465#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9466 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009467
Hanno Becker33b9b252019-07-05 11:23:25 +01009468#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9469 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9470void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9471 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009473 conf->min_major_ver = major;
9474 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009475}
Hanno Becker33b9b252019-07-05 11:23:25 +01009476#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9477 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009480void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009481{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009482 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009483}
9484#endif
9485
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009486#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009487void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9488 char cert_req_ca_list )
9489{
9490 conf->cert_req_ca_list = cert_req_ca_list;
9491}
9492#endif
9493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009495void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009496{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009497 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009498}
9499#endif
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009502#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009503void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009505 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009506}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009507#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9508#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009509void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009510 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009511{
9512 conf->enforce_extended_master_secret = ems_enf;
9513}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009514#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009515#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009516
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009517#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009518void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009520 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009521}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009522#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009525int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009528 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009531 }
9532
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009533 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009534
9535 return( 0 );
9536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009540void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009542 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009547void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009548{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009549 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009550}
9551#endif
9552
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009553#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009554void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009556 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009557}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009558#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009561void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009563 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009564}
9565
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009566void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009568 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009569}
9570
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009571void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009572 const unsigned char period[8] )
9573{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009574 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009579#if defined(MBEDTLS_SSL_CLI_C)
9580void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009581{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009582 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009583}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009584#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009585
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009586#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009587void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9588 mbedtls_ssl_ticket_write_t *f_ticket_write,
9589 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9590 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009591{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009592 conf->f_ticket_write = f_ticket_write;
9593 conf->f_ticket_parse = f_ticket_parse;
9594 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009595}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009598
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009599#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9600void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9601 mbedtls_ssl_export_keys_t *f_export_keys,
9602 void *p_export_keys )
9603{
9604 conf->f_export_keys = f_export_keys;
9605 conf->p_export_keys = p_export_keys;
9606}
9607#endif
9608
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009609#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009610void mbedtls_ssl_conf_async_private_cb(
9611 mbedtls_ssl_config *conf,
9612 mbedtls_ssl_async_sign_t *f_async_sign,
9613 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9614 mbedtls_ssl_async_resume_t *f_async_resume,
9615 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009616 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009617{
9618 conf->f_async_sign_start = f_async_sign;
9619 conf->f_async_decrypt_start = f_async_decrypt;
9620 conf->f_async_resume = f_async_resume;
9621 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009622 conf->p_async_config_data = async_config_data;
9623}
9624
Gilles Peskine8f97af72018-04-26 11:46:10 +02009625void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9626{
9627 return( conf->p_async_config_data );
9628}
9629
Gilles Peskine1febfef2018-04-30 11:54:39 +02009630void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009631{
9632 if( ssl->handshake == NULL )
9633 return( NULL );
9634 else
9635 return( ssl->handshake->user_async_ctx );
9636}
9637
Gilles Peskine1febfef2018-04-30 11:54:39 +02009638void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009639 void *ctx )
9640{
9641 if( ssl->handshake != NULL )
9642 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009643}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009644#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009645
Paul Bakker5121ce52009-01-03 21:22:43 +00009646/*
9647 * SSL get accessors
9648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009650{
9651 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9652}
9653
Hanno Becker8b170a02017-10-10 11:51:19 +01009654int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9655{
9656 /*
9657 * Case A: We're currently holding back
9658 * a message for further processing.
9659 */
9660
9661 if( ssl->keep_current_message == 1 )
9662 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009664 return( 1 );
9665 }
9666
9667 /*
9668 * Case B: Further records are pending in the current datagram.
9669 */
9670
9671#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009672 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009673 ssl->in_left > ssl->next_record_offset )
9674 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009676 return( 1 );
9677 }
9678#endif /* MBEDTLS_SSL_PROTO_DTLS */
9679
9680 /*
9681 * Case C: A handshake message is being processed.
9682 */
9683
Hanno Becker8b170a02017-10-10 11:51:19 +01009684 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9685 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009687 return( 1 );
9688 }
9689
9690 /*
9691 * Case D: An application data message is being processed
9692 */
9693 if( ssl->in_offt != NULL )
9694 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009696 return( 1 );
9697 }
9698
9699 /*
9700 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009701 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009702 * we implement support for multiple alerts in single records.
9703 */
9704
9705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9706 return( 0 );
9707}
9708
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009709uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009710{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009711 if( ssl->session != NULL )
9712 return( ssl->session->verify_result );
9713
9714 if( ssl->session_negotiate != NULL )
9715 return( ssl->session_negotiate->verify_result );
9716
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009717 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009718}
9719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009721{
Hanno Beckere02758c2019-06-26 15:31:31 +01009722 int suite;
9723
Paul Bakker926c8e42013-03-06 10:23:34 +01009724 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009725 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009726
Hanno Beckere02758c2019-06-26 15:31:31 +01009727 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9728 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009729}
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009735 {
Hanno Becker2881d802019-05-22 14:44:53 +01009736 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009739 return( "DTLSv1.0" );
9740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009742 return( "DTLSv1.2" );
9743
9744 default:
9745 return( "unknown (DTLS)" );
9746 }
9747 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009748 MBEDTLS_SSL_TRANSPORT_ELSE
9749#endif /* MBEDTLS_SSL_PROTO_DTLS */
9750#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009751 {
Hanno Becker2881d802019-05-22 14:44:53 +01009752 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009753 {
9754 case MBEDTLS_SSL_MINOR_VERSION_0:
9755 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009756
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009757 case MBEDTLS_SSL_MINOR_VERSION_1:
9758 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009759
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009760 case MBEDTLS_SSL_MINOR_VERSION_2:
9761 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009762
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009763 case MBEDTLS_SSL_MINOR_VERSION_3:
9764 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009765
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009766 default:
9767 return( "unknown" );
9768 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009769 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009770#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009771}
9772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009774{
Hanno Becker3136ede2018-08-17 15:28:19 +01009775 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009777
Hanno Becker43395762019-05-03 14:46:38 +01009778 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9779
Hanno Becker78640902018-08-13 16:35:15 +01009780 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009781 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_ZLIB_SUPPORT)
9784 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9785 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009786#endif
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009789 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009790#if defined(MBEDTLS_GCM_C) || \
9791 defined(MBEDTLS_CCM_C) || \
9792 defined(MBEDTLS_CHACHAPOLY_C)
9793#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009795#endif
9796#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009798#endif
9799#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009800 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009801#endif
9802 transform_expansion =
9803 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009804 break;
9805
Hanno Beckera9d5c452019-07-25 16:47:12 +01009806#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9807 MBEDTLS_CHACHAPOLY_C */
9808
9809#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9810 case MBEDTLS_MODE_STREAM:
9811 transform_expansion = transform->maclen;
9812 break;
9813#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9814
9815#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009817 {
9818 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009819
9820 block_size = mbedtls_cipher_get_block_size(
9821 &transform->cipher_ctx_enc );
9822
Hanno Becker3136ede2018-08-17 15:28:19 +01009823 /* Expansion due to the addition of the MAC. */
9824 transform_expansion += transform->maclen;
9825
9826 /* Expansion due to the addition of CBC padding;
9827 * Theoretically up to 256 bytes, but we never use
9828 * more than the block size of the underlying cipher. */
9829 transform_expansion += block_size;
9830
9831 /* For TLS 1.1 or higher, an explicit IV is added
9832 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009833#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009834 if( mbedtls_ssl_ver_geq(
9835 mbedtls_ssl_get_minor_ver( ssl ),
9836 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9837 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009838 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009839 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009840#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009841
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009842 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009843 }
9844#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009845
9846 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009849 }
9850
Hanno Beckera5a2b082019-05-15 14:03:01 +01009851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009852 if( transform->out_cid_len != 0 )
9853 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009854#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009855
Hanno Becker43395762019-05-03 14:46:38 +01009856 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009857}
9858
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009859#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9860size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9861{
9862 size_t max_len;
9863
9864 /*
9865 * Assume mfl_code is correct since it was checked when set
9866 */
Angus Grattond8213d02016-05-25 20:56:48 +10009867 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009868
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009869 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009870 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009871 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009872 {
Angus Grattond8213d02016-05-25 20:56:48 +10009873 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009874 }
9875
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009876 /* During a handshake, use the value being negotiated */
9877 if( ssl->session_negotiate != NULL &&
9878 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9879 {
9880 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9881 }
9882
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009883 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009884}
9885#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9886
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009887#if defined(MBEDTLS_SSL_PROTO_DTLS)
9888static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9889{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009890 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009891 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009892 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9893 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009894 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009895
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009896 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9897 return( ssl->mtu );
9898
9899 if( ssl->mtu == 0 )
9900 return( ssl->handshake->mtu );
9901
9902 return( ssl->mtu < ssl->handshake->mtu ?
9903 ssl->mtu : ssl->handshake->mtu );
9904}
9905#endif /* MBEDTLS_SSL_PROTO_DTLS */
9906
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009907int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9908{
9909 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9910
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009911#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9912 !defined(MBEDTLS_SSL_PROTO_DTLS)
9913 (void) ssl;
9914#endif
9915
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009916#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9917 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9918
9919 if( max_len > mfl )
9920 max_len = mfl;
9921#endif
9922
9923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009924 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009925 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009926 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009927 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9928 const size_t overhead = (size_t) ret;
9929
9930 if( ret < 0 )
9931 return( ret );
9932
9933 if( mtu <= overhead )
9934 {
9935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9936 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9937 }
9938
9939 if( max_len > mtu - overhead )
9940 max_len = mtu - overhead;
9941 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009942#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009943
Hanno Becker0defedb2018-08-10 12:35:02 +01009944#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9945 !defined(MBEDTLS_SSL_PROTO_DTLS)
9946 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009947#endif
9948
9949 return( (int) max_len );
9950}
9951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952#if defined(MBEDTLS_X509_CRT_PARSE_C)
9953const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009954{
9955 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009956 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009957
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009958#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009959 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009960#else
9961 return( NULL );
9962#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009963}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009967int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9968 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009969{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009970 if( ssl == NULL ||
9971 dst == NULL ||
9972 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009973 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009976 }
9977
Hanno Becker58fccf22019-02-06 14:30:46 +00009978 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009981
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009982const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9983{
9984 if( ssl == NULL )
9985 return( NULL );
9986
9987 return( ssl->session );
9988}
9989
Paul Bakker5121ce52009-01-03 21:22:43 +00009990/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009991 * Define ticket header determining Mbed TLS version
9992 * and structure of the ticket.
9993 */
9994
Hanno Becker41527622019-05-16 12:50:45 +01009995/*
Hanno Becker26829e92019-05-28 14:30:45 +01009996 * Define bitflag determining compile-time settings influencing
9997 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009998 */
9999
Hanno Becker26829e92019-05-28 14:30:45 +010010000#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010001#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010002#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010003#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010004#endif /* MBEDTLS_HAVE_TIME */
10005
10006#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010007#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010008#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010009#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010010#endif /* MBEDTLS_X509_CRT_PARSE_C */
10011
10012#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010013#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010014#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010015#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010016#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10017
10018#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010019#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010020#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010021#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010022#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10023
10024#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010025#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010026#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010027#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010028#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10029
10030#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010031#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010032#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010033#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010034#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10035
Hanno Becker41527622019-05-16 12:50:45 +010010036#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10037#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10038#else
10039#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10040#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10041
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010042#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10043#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10044#else
10045#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10046#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10047
Hanno Becker88440552019-07-03 14:16:13 +010010048#if defined(MBEDTLS_ZLIB_SUPPORT)
10049#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10050#else
10051#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10052#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10053
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010054#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10055#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10056#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10057#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10058#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10059#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10060#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010061#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010062#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010063
Hanno Becker26829e92019-05-28 14:30:45 +010010064#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010065 ( (uint16_t) ( \
10066 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10067 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10068 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10069 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10070 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10071 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010072 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010073 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010074 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010075
Hanno Becker557fe9f2019-05-16 12:41:07 +010010076static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010077 MBEDTLS_VERSION_MAJOR,
10078 MBEDTLS_VERSION_MINOR,
10079 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010080 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10081 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010082};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010083
10084/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010085 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010086 * (in the presentation language of TLS, RFC 8446 section 3)
10087 *
Hanno Becker26829e92019-05-28 14:30:45 +010010088 * opaque mbedtls_version[3]; // major, minor, patch
10089 * opaque session_format[2]; // version-specific 16-bit field determining
10090 * // the format of the remaining
10091 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010092 *
10093 * Note: When updating the format, remember to keep
10094 * these version+format bytes.
10095 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010096 * // In this version, `session_format` determines
10097 * // the setting of those compile-time
10098 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010099 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010100 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010101 * uint8 ciphersuite[2]; // defined by the standard
10102 * uint8 compression; // 0 or 1
10103 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010104 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010105 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010106 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010107 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10108 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10109 * case disabled: uint8_t peer_cert_digest_type;
10110 * opaque peer_cert_digest<0..2^8-1>;
10111 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010112 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010113 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010114 * uint8 mfl_code; // up to 255 according to standard
10115 * uint8 trunc_hmac; // 0 or 1
10116 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010117 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010118 * The order is the same as in the definition of the structure, except
10119 * verify_result is put before peer_cert so that all mandatory fields come
10120 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010121 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010122static int ssl_session_save( const mbedtls_ssl_session *session,
10123 unsigned char omit_header,
10124 unsigned char *buf,
10125 size_t buf_len,
10126 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010127{
10128 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010129 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010130#if defined(MBEDTLS_HAVE_TIME)
10131 uint64_t start;
10132#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010133#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010134#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010135 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010136#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010137#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010138
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010139 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010140 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010141 /*
10142 * Add version identifier
10143 */
10144
10145 used += sizeof( ssl_serialized_session_header );
10146
10147 if( used <= buf_len )
10148 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010149 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010150 sizeof( ssl_serialized_session_header ) );
10151 p += sizeof( ssl_serialized_session_header );
10152 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010153 }
10154
10155 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010156 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010157 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010158#if defined(MBEDTLS_HAVE_TIME)
10159 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010160
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010161 if( used <= buf_len )
10162 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010163 start = (uint64_t) session->start;
10164
10165 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10166 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10167 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10168 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10169 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10170 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10171 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10172 *p++ = (unsigned char)( ( start ) & 0xFF );
10173 }
10174#endif /* MBEDTLS_HAVE_TIME */
10175
10176 /*
10177 * Basic mandatory fields
10178 */
Hanno Becker88440552019-07-03 14:16:13 +010010179 {
10180 size_t const ciphersuite_len = 2;
10181#if defined(MBEDTLS_ZLIB_SUPPORT)
10182 size_t const compression_len = 1;
10183#else
10184 size_t const compression_len = 0;
10185#endif
10186 size_t const id_len_len = 1;
10187 size_t const id_len = 32;
10188 size_t const master_len = 48;
10189 size_t const verif_result_len = 4;
10190
10191 size_t const basic_len =
10192 ciphersuite_len +
10193 compression_len +
10194 id_len_len +
10195 id_len +
10196 master_len +
10197 verif_result_len;
10198
10199 used += basic_len;
10200 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010201
10202 if( used <= buf_len )
10203 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010204 const int ciphersuite =
10205 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010206 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010207
Hanno Becker88440552019-07-03 14:16:13 +010010208#if defined(MBEDTLS_ZLIB_SUPPORT)
10209 *p++ = (unsigned char)(
10210 mbedtls_ssl_session_get_compression( session ) );
10211#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010212
10213 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010214 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10215 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010216 p += 32;
10217
Teppo Järvelin91d79382019-10-02 09:09:31 +030010218 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010219 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010220 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010221 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010222
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010223 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010224 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010225 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010226#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010227#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010228 if( session->peer_cert == NULL )
10229 cert_len = 0;
10230 else
10231 cert_len = session->peer_cert->raw.len;
10232
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010233 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010234
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010235 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010236 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010237 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010238
10239 if( session->peer_cert != NULL )
10240 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010241 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010242 p += cert_len;
10243 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010244 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010245
Hanno Becker5882dd02019-06-06 16:25:57 +010010246#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010247 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010248 if( session->peer_cert_digest != NULL )
10249 {
10250 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10251 if( used <= buf_len )
10252 {
10253 *p++ = (unsigned char) session->peer_cert_digest_type;
10254 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010255 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010256 session->peer_cert_digest_len );
10257 p += session->peer_cert_digest_len;
10258 }
10259 }
10260 else
10261 {
10262 used += 2;
10263 if( used <= buf_len )
10264 {
10265 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10266 *p++ = 0;
10267 }
10268 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010269#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010270#endif /* MBEDTLS_X509_CRT_PARSE_C */
10271
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010272 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010273 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010274 */
10275#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010276 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010277
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010278 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010279 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010280 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010281
10282 if( session->ticket != NULL )
10283 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010284 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010285 p += session->ticket_len;
10286 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010287
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010288 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010289 }
10290#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10291
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010292 /*
10293 * Misc extension-related info
10294 */
10295#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10296 used += 1;
10297
10298 if( used <= buf_len )
10299 *p++ = session->mfl_code;
10300#endif
10301
10302#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10303 used += 1;
10304
10305 if( used <= buf_len )
10306 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10307#endif
10308
10309#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10310 used += 1;
10311
10312 if( used <= buf_len )
10313 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10314#endif
10315
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010316 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010317 *olen = used;
10318
10319 if( used > buf_len )
10320 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010321
10322 return( 0 );
10323}
10324
10325/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010326 * Public wrapper for ssl_session_save()
10327 */
10328int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10329 unsigned char *buf,
10330 size_t buf_len,
10331 size_t *olen )
10332{
10333 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10334}
10335
10336/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010337 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010338 *
10339 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010340 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010341 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010342static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010343 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010344 const unsigned char *buf,
10345 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010346{
10347 const unsigned char *p = buf;
10348 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010349 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010350#if defined(MBEDTLS_HAVE_TIME)
10351 uint64_t start;
10352#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010353#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010354#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010355 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010356#endif
10357#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010358
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010359 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010360 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010361 /*
10362 * Check version identifier
10363 */
10364
10365 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10366 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10367
Teppo Järvelin0efac532019-10-04 13:21:08 +030010368 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010369 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010370 sizeof( ssl_serialized_session_header ) ) != 0 )
10371 {
10372 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10373 }
10374 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010375 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010376
10377 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010378 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010379 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010380#if defined(MBEDTLS_HAVE_TIME)
10381 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10383
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010384 start = ( (uint64_t) p[0] << 56 ) |
10385 ( (uint64_t) p[1] << 48 ) |
10386 ( (uint64_t) p[2] << 40 ) |
10387 ( (uint64_t) p[3] << 32 ) |
10388 ( (uint64_t) p[4] << 24 ) |
10389 ( (uint64_t) p[5] << 16 ) |
10390 ( (uint64_t) p[6] << 8 ) |
10391 ( (uint64_t) p[7] );
10392 p += 8;
10393
10394 session->start = (time_t) start;
10395#endif /* MBEDTLS_HAVE_TIME */
10396
10397 /*
10398 * Basic mandatory fields
10399 */
Hanno Becker88440552019-07-03 14:16:13 +010010400 {
10401 size_t const ciphersuite_len = 2;
10402#if defined(MBEDTLS_ZLIB_SUPPORT)
10403 size_t const compression_len = 1;
10404#else
10405 size_t const compression_len = 0;
10406#endif
10407 size_t const id_len_len = 1;
10408 size_t const id_len = 32;
10409 size_t const master_len = 48;
10410 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010411
Hanno Becker88440552019-07-03 14:16:13 +010010412 size_t const basic_len =
10413 ciphersuite_len +
10414 compression_len +
10415 id_len_len +
10416 id_len +
10417 master_len +
10418 verif_result_len;
10419
10420 if( basic_len > (size_t)( end - p ) )
10421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10422 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010423
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010424 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010425 p += 2;
10426
Hanno Becker73f4cb12019-06-27 13:51:07 +010010427#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010428 session->ciphersuite = ciphersuite;
10429#else
10430 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010431 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010432 {
10433 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10434 }
10435#endif
10436
Hanno Becker88440552019-07-03 14:16:13 +010010437#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010438 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010439#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010440
10441 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010442 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10443 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010444 p += 32;
10445
Teppo Järvelin91d79382019-10-02 09:09:31 +030010446 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010447 p += 48;
10448
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010449 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010450 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010451
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010452 /* Immediately clear invalid pointer values that have been read, in case
10453 * we exit early before we replaced them with valid ones. */
10454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010455#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010456 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010457#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010458 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010459#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010460#endif
10461#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10462 session->ticket = NULL;
10463#endif
10464
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010465 /*
10466 * Peer certificate
10467 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010468#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010469#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010470 if( 3 > (size_t)( end - p ) )
10471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10472
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010473 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10474
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010475 p += 3;
10476
10477 if( cert_len == 0 )
10478 {
10479 session->peer_cert = NULL;
10480 }
10481 else
10482 {
10483 int ret;
10484
10485 if( cert_len > (size_t)( end - p ) )
10486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10487
10488 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10489
10490 if( session->peer_cert == NULL )
10491 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10492
10493 mbedtls_x509_crt_init( session->peer_cert );
10494
10495 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10496 p, cert_len ) ) != 0 )
10497 {
10498 mbedtls_x509_crt_free( session->peer_cert );
10499 mbedtls_free( session->peer_cert );
10500 session->peer_cert = NULL;
10501 return( ret );
10502 }
10503
10504 p += cert_len;
10505 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010506#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010507 /* Deserialize CRT digest from the end of the ticket. */
10508 if( 2 > (size_t)( end - p ) )
10509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10510
10511 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10512 session->peer_cert_digest_len = (size_t) *p++;
10513
10514 if( session->peer_cert_digest_len != 0 )
10515 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010516 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010517 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010518 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10520 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10522
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010523 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10525
10526 session->peer_cert_digest =
10527 mbedtls_calloc( 1, session->peer_cert_digest_len );
10528 if( session->peer_cert_digest == NULL )
10529 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10530
Teppo Järvelin91d79382019-10-02 09:09:31 +030010531 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010532 session->peer_cert_digest_len );
10533 p += session->peer_cert_digest_len;
10534 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010535#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010536#endif /* MBEDTLS_X509_CRT_PARSE_C */
10537
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010538 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010539 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010540 */
10541#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10542 if( 3 > (size_t)( end - p ) )
10543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10544
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010545 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010546 p += 3;
10547
10548 if( session->ticket_len != 0 )
10549 {
10550 if( session->ticket_len > (size_t)( end - p ) )
10551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10552
10553 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10554 if( session->ticket == NULL )
10555 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10556
Teppo Järvelin91d79382019-10-02 09:09:31 +030010557 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010558 p += session->ticket_len;
10559 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010560
10561 if( 4 > (size_t)( end - p ) )
10562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10563
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010564 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010565 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010566#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10567
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010568 /*
10569 * Misc extension-related info
10570 */
10571#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10572 if( 1 > (size_t)( end - p ) )
10573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10574
10575 session->mfl_code = *p++;
10576#endif
10577
10578#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10579 if( 1 > (size_t)( end - p ) )
10580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10581
10582 session->trunc_hmac = *p++;
10583#endif
10584
10585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10586 if( 1 > (size_t)( end - p ) )
10587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10588
10589 session->encrypt_then_mac = *p++;
10590#endif
10591
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010592 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010593 if( p != end )
10594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10595
10596 return( 0 );
10597}
10598
10599/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010600 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010601 */
10602int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10603 const unsigned char *buf,
10604 size_t len )
10605{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010606 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010607
10608 if( ret != 0 )
10609 mbedtls_ssl_session_free( session );
10610
10611 return( ret );
10612}
10613
10614/*
Paul Bakker1961b702013-01-25 14:49:24 +010010615 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010620
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010621 if( ssl == NULL || ssl->conf == NULL )
10622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010625 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010629 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010631#endif
10632
Hanno Beckerb82350b2019-07-26 07:24:05 +010010633 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010634 return( ret );
10635}
10636
10637/*
10638 * Perform the SSL handshake
10639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010641{
10642 int ret = 0;
10643
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010644 if( ssl == NULL || ssl->conf == NULL )
10645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010652
10653 if( ret != 0 )
10654 break;
10655 }
10656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010658
10659 return( ret );
10660}
10661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662#if defined(MBEDTLS_SSL_RENEGOTIATION)
10663#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010664/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010665 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010668{
10669 int ret;
10670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010672
10673 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10675 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010676
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010677 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010678 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010680 return( ret );
10681 }
10682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010684
10685 return( 0 );
10686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010688
10689/*
10690 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691 * - any side: calling mbedtls_ssl_renegotiate(),
10692 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10693 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010694 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010695 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010699{
10700 int ret;
10701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010703
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010704 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10705 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010706
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010707 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10708 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010710 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010712 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010713 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10714 MBEDTLS_SSL_IS_SERVER )
10715 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010716 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010717 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010718 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010719 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010720 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010721 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010722 }
10723#endif
10724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10726 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010731 return( ret );
10732 }
10733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010735
10736 return( 0 );
10737}
10738
10739/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010740 * Renegotiate current connection on client,
10741 * or request renegotiation on server
10742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010746
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010747 if( ssl == NULL || ssl->conf == NULL )
10748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010751 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010752 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010758
10759 /* Did we already try/start sending HelloRequest? */
10760 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010762
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010763 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010768 /*
10769 * On client, either start the renegotiation process or,
10770 * if already in progress, continue the handshake
10771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010776
10777 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010780 return( ret );
10781 }
10782 }
10783 else
10784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010788 return( ret );
10789 }
10790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010792
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010793 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010794}
10795
10796/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010797 * Check record counters and renegotiate if they're above the limit.
10798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010800{
Andres AG2196c7f2016-12-15 17:01:16 +000010801 size_t ep_len = ssl_ep_len( ssl );
10802 int in_ctr_cmp;
10803 int out_ctr_cmp;
10804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10806 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010807 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010808 {
10809 return( 0 );
10810 }
10811
Teppo Järvelin0efac532019-10-04 13:21:08 +030010812 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010813 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010814 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010815 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010816 ssl->conf->renego_period + ep_len, 8 - ep_len );
10817
10818 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010819 {
10820 return( 0 );
10821 }
10822
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010825}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010827
10828/*
10829 * Receive application data decrypted from the SSL layer
10830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010832{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010833 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010834 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010835
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010836 if( ssl == NULL || ssl->conf == NULL )
10837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010842 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010845 return( ret );
10846
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010847 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010849 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010850 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010851 return( ret );
10852 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010853 }
10854#endif
10855
Hanno Becker4a810fb2017-05-24 16:27:30 +010010856 /*
10857 * Check if renegotiation is necessary and/or handshake is
10858 * in process. If yes, perform/continue, and fall through
10859 * if an unexpected packet is received while the client
10860 * is waiting for the ServerHello.
10861 *
10862 * (There is no equivalent to the last condition on
10863 * the server-side as it is not treated as within
10864 * a handshake while waiting for the ClientHello
10865 * after a renegotiation request.)
10866 */
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010869 ret = ssl_check_ctr_renegotiate( ssl );
10870 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10871 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010874 return( ret );
10875 }
10876#endif
10877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010881 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10882 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010885 return( ret );
10886 }
10887 }
10888
Hanno Beckere41158b2017-10-23 13:30:32 +010010889 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010890 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010891 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010892 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010893 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10894 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010895 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010896 ssl_set_timer( ssl,
10897 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010898 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010899
Hanno Becker327c93b2018-08-15 13:56:18 +010010900 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010901 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010902 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10903 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010904
Hanno Becker4a810fb2017-05-24 16:27:30 +010010905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10906 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010907 }
10908
10909 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010911 {
10912 /*
10913 * OpenSSL sends empty messages to randomize the IV
10914 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010915 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010918 return( 0 );
10919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010921 return( ret );
10922 }
10923 }
10924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010928
Hanno Becker4a810fb2017-05-24 16:27:30 +010010929 /*
10930 * - For client-side, expect SERVER_HELLO_REQUEST.
10931 * - For server-side, expect CLIENT_HELLO.
10932 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10933 */
10934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010936 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10937 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010939 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010942
10943 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010945 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010946 {
10947 continue;
10948 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010949 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010950#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010951#if defined(MBEDTLS_SSL_PROTO_TLS)
10952 {
10953 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10954 }
10955#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010956 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010957#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010958
Hanno Becker4a810fb2017-05-24 16:27:30 +010010959#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010960 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10961 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010965
10966 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010968 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010969 {
10970 continue;
10971 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010972 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010973#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010974#if defined(MBEDTLS_SSL_PROTO_TLS)
10975 {
10976 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10977 }
10978#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010979 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010980#endif /* MBEDTLS_SSL_SRV_C */
10981
Hanno Becker21df7f92017-10-17 11:03:26 +010010982#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010983 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010984 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10985 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010986 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010987 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10988 {
10989 /*
10990 * Accept renegotiation request
10991 */
Paul Bakker48916f92012-09-16 19:57:18 +000010992
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010993 /* DTLS clients need to know renego is server-initiated */
10994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010995 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010996 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10997 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010998 {
10999 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11000 }
11001#endif
11002 ret = ssl_start_renegotiation( ssl );
11003 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11004 ret != 0 )
11005 {
11006 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11007 return( ret );
11008 }
11009 }
11010 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011011#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011012 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011013 /*
11014 * Refuse renegotiation
11015 */
11016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011020 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011021 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011022 /* SSLv3 does not have a "no_renegotiation" warning, so
11023 we send a fatal alert and abort the connection. */
11024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11025 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11026 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011027 }
11028 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11030#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11031 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011032 if( mbedtls_ssl_ver_geq(
11033 mbedtls_ssl_get_minor_ver( ssl ),
11034 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011035 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011036 ret = mbedtls_ssl_send_alert_message( ssl,
11037 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11038 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11039 if( ret != 0 )
11040 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011041 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011042 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11044 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011048 }
Paul Bakker48916f92012-09-16 19:57:18 +000011049 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011050
Hanno Becker90333da2017-10-10 11:27:13 +010011051 /* At this point, we don't know whether the renegotiation has been
11052 * completed or not. The cases to consider are the following:
11053 * 1) The renegotiation is complete. In this case, no new record
11054 * has been read yet.
11055 * 2) The renegotiation is incomplete because the client received
11056 * an application data record while awaiting the ServerHello.
11057 * 3) The renegotiation is incomplete because the client received
11058 * a non-handshake, non-application data message while awaiting
11059 * the ServerHello.
11060 * In each of these case, looping will be the proper action:
11061 * - For 1), the next iteration will read a new record and check
11062 * if it's application data.
11063 * - For 2), the loop condition isn't satisfied as application data
11064 * is present, hence continue is the same as break
11065 * - For 3), the loop condition is satisfied and read_record
11066 * will re-deliver the message that was held back by the client
11067 * when expecting the ServerHello.
11068 */
11069 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011070 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011071#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011073 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011074 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011075 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011076 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011079 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011081 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011082 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011084#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11087 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011090 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011091 }
11092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011093 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11096 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011097 }
11098
11099 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011100
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011101 /* We're going to return something now, cancel timer,
11102 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011104 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011105
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011107 /* If we requested renego but received AppData, resend HelloRequest.
11108 * Do it now, after setting in_offt, to avoid taking this branch
11109 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011111 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11112 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011113 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011114 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011115 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011118 return( ret );
11119 }
11120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011123 }
11124
11125 n = ( len < ssl->in_msglen )
11126 ? len : ssl->in_msglen;
11127
Teppo Järvelin91d79382019-10-02 09:09:31 +030011128 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011129 ssl->in_msglen -= n;
11130
11131 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011132 {
11133 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011134 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011135 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011136 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011137 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011138 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011139 /* more data available */
11140 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011141 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011144
Paul Bakker23986e52011-04-24 08:57:21 +000011145 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011146}
11147
11148/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011149 * Send application data to be encrypted by the SSL layer, taking care of max
11150 * fragment length and buffer size.
11151 *
11152 * According to RFC 5246 Section 6.2.1:
11153 *
11154 * Zero-length fragments of Application data MAY be sent as they are
11155 * potentially useful as a traffic analysis countermeasure.
11156 *
11157 * Therefore, it is possible that the input message length is 0 and the
11158 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011159 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011160static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011161 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011162{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011163 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11164 const size_t max_len = (size_t) ret;
11165
11166 if( ret < 0 )
11167 {
11168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11169 return( ret );
11170 }
11171
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011172 if( len > max_len )
11173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011175 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011178 "maximum fragment length: %d > %d",
11179 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011181 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011182 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011183#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011184#if defined(MBEDTLS_SSL_PROTO_TLS)
11185 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011186 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011187 }
11188#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011189 }
Paul Bakker887bd502011-06-08 13:10:54 +000011190
Paul Bakker5121ce52009-01-03 21:22:43 +000011191 if( ssl->out_left != 0 )
11192 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011193 /*
11194 * The user has previously tried to send the data and
11195 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11196 * written. In this case, we expect the high-level write function
11197 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011202 return( ret );
11203 }
11204 }
Paul Bakker887bd502011-06-08 13:10:54 +000011205 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011206 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011207 /*
11208 * The user is trying to send a message the first time, so we need to
11209 * copy the data into the internal buffers and setup the data structure
11210 * to keep track of partial writes
11211 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011212 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011213 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011214 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011215
Hanno Becker67bc7c32018-08-06 11:33:50 +010011216 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011219 return( ret );
11220 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011221 }
11222
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011223 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011224}
11225
11226/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011227 * Write application data, doing 1/n-1 splitting if necessary.
11228 *
11229 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011230 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011231 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011233#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011234static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011235 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011236{
11237 int ret;
11238
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011239 if( ssl->conf->cbc_record_splitting ==
11240 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011241 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011242 mbedtls_ssl_ver_gt(
11243 mbedtls_ssl_get_minor_ver( ssl ),
11244 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11246 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011247 {
11248 return( ssl_write_real( ssl, buf, len ) );
11249 }
11250
11251 if( ssl->split_done == 0 )
11252 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011253 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011254 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011255 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011256 }
11257
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011258 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11259 return( ret );
11260 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011261
11262 return( ret + 1 );
11263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011264#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011265
11266/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011267 * Write application data (public-facing wrapper)
11268 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011269int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011270{
11271 int ret;
11272
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011274
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011275 if( ssl == NULL || ssl->conf == NULL )
11276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11277
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011278#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011279 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11280 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011281 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011282 return( ret );
11283 }
11284#endif
11285
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011286 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011287 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011288 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011289 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011291 return( ret );
11292 }
11293 }
11294
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011295#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011296 ret = ssl_write_split( ssl, buf, len );
11297#else
11298 ret = ssl_write_real( ssl, buf, len );
11299#endif
11300
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011302
11303 return( ret );
11304}
11305
11306/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011307 * Notify the peer that the connection is being closed
11308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011309int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011310{
11311 int ret;
11312
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011313 if( ssl == NULL || ssl->conf == NULL )
11314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011317
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011318 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011319 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011321 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011323 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11324 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11325 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011328 return( ret );
11329 }
11330 }
11331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011333
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011334 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011335}
11336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011337void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011338{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011339 if( transform == NULL )
11340 return;
11341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011342#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011343 deflateEnd( &transform->ctx_deflate );
11344 inflateEnd( &transform->ctx_inflate );
11345#endif
11346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11348 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011349
Hanno Becker92231322018-01-03 15:32:51 +000011350#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011351 mbedtls_md_free( &transform->md_ctx_enc );
11352 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011353#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011354
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011355 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011356}
11357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011358#if defined(MBEDTLS_X509_CRT_PARSE_C)
11359static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011362
11363 while( cur != NULL )
11364 {
11365 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011366 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011367 cur = next;
11368 }
11369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011370#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011371
Hanno Becker0271f962018-08-16 13:23:47 +010011372#if defined(MBEDTLS_SSL_PROTO_DTLS)
11373
11374static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11375{
11376 unsigned offset;
11377 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11378
11379 if( hs == NULL )
11380 return;
11381
Hanno Becker283f5ef2018-08-24 09:34:47 +010011382 ssl_free_buffered_record( ssl );
11383
Hanno Becker0271f962018-08-16 13:23:47 +010011384 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011385 ssl_buffering_free_slot( ssl, offset );
11386}
11387
11388static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11389 uint8_t slot )
11390{
11391 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11392 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011393
11394 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11395 return;
11396
Hanno Beckere605b192018-08-21 15:59:07 +010011397 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011398 {
Hanno Beckere605b192018-08-21 15:59:07 +010011399 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011400 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011401 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011402 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011403 }
11404}
11405
11406#endif /* MBEDTLS_SSL_PROTO_DTLS */
11407
Gilles Peskine9b562d52018-04-25 20:32:43 +020011408void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011409{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011410 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11411
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011412 if( handshake == NULL )
11413 return;
11414
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011415#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11416 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11417 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011418 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011419 handshake->async_in_progress = 0;
11420 }
11421#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11422
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011423#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11424 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11425 mbedtls_md5_free( &handshake->fin_md5 );
11426 mbedtls_sha1_free( &handshake->fin_sha1 );
11427#endif
11428#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11429#if defined(MBEDTLS_SHA256_C)
11430 mbedtls_sha256_free( &handshake->fin_sha256 );
11431#endif
11432#if defined(MBEDTLS_SHA512_C)
11433 mbedtls_sha512_free( &handshake->fin_sha512 );
11434#endif
11435#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011437#if defined(MBEDTLS_DHM_C)
11438 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011440#if defined(MBEDTLS_ECDH_C)
11441 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011442#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011443#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011444 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011445#if defined(MBEDTLS_SSL_CLI_C)
11446 mbedtls_free( handshake->ecjpake_cache );
11447 handshake->ecjpake_cache = NULL;
11448 handshake->ecjpake_cache_len = 0;
11449#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011450#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011451
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011452#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11453 if( handshake->psk != NULL )
11454 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011455 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011456 mbedtls_free( handshake->psk );
11457 }
11458#endif
11459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011460#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11461 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011462 /*
11463 * Free only the linked list wrapper, not the keys themselves
11464 * since the belong to the SNI callback
11465 */
11466 if( handshake->sni_key_cert != NULL )
11467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011468 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011469
11470 while( cur != NULL )
11471 {
11472 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011473 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011474 cur = next;
11475 }
11476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011477#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011478
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011479#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011480 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011481 if( handshake->ecrs_peer_cert != NULL )
11482 {
11483 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11484 mbedtls_free( handshake->ecrs_peer_cert );
11485 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011486#endif
11487
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011488#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11489 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11490 mbedtls_pk_free( &handshake->peer_pubkey );
11491#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011493#if defined(MBEDTLS_SSL_PROTO_DTLS)
11494 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011495 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011496 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011497#endif
11498
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011499 mbedtls_platform_zeroize( handshake,
11500 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011501}
11502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011503void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011504{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011505 if( session == NULL )
11506 return;
11507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011508#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011509 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011510#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011511
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011512#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011513 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011514#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011516 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011517}
11518
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011519#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011520
11521#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11522#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11523#else
11524#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11525#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11526
11527#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11528#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11529#else
11530#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11531#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11532
11533#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11534#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11535#else
11536#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11537#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11538
11539#if defined(MBEDTLS_SSL_ALPN)
11540#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11541#else
11542#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11543#endif /* MBEDTLS_SSL_ALPN */
11544
11545#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11546#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11547#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11548#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11549
11550#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11551 ( (uint32_t) ( \
11552 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11553 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11554 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11555 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11556 0u ) )
11557
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011558static unsigned char ssl_serialized_context_header[] = {
11559 MBEDTLS_VERSION_MAJOR,
11560 MBEDTLS_VERSION_MINOR,
11561 MBEDTLS_VERSION_PATCH,
11562 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11563 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011564 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11565 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11566 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011567};
11568
Paul Bakker5121ce52009-01-03 21:22:43 +000011569/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011570 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011571 *
11572 * The format of the serialized data is:
11573 * (in the presentation language of TLS, RFC 8446 section 3)
11574 *
11575 * // header
11576 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011577 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011578 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011579 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011580 * Note: When updating the format, remember to keep these
11581 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011582 *
11583 * // session sub-structure
11584 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11585 * // transform sub-structure
11586 * uint8 random[64]; // ServerHello.random+ClientHello.random
11587 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11588 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11589 * // fields from ssl_context
11590 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11591 * uint64 in_window_top; // DTLS: last validated record seq_num
11592 * uint64 in_window; // DTLS: bitmask for replay protection
11593 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11594 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11595 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11596 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11597 *
11598 * Note that many fields of the ssl_context or sub-structures are not
11599 * serialized, as they fall in one of the following categories:
11600 *
11601 * 1. forced value (eg in_left must be 0)
11602 * 2. pointer to dynamically-allocated memory (eg session, transform)
11603 * 3. value can be re-derived from other data (eg session keys from MS)
11604 * 4. value was temporary (eg content of input buffer)
11605 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011606 */
11607int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11608 unsigned char *buf,
11609 size_t buf_len,
11610 size_t *olen )
11611{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011612 unsigned char *p = buf;
11613 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011614 size_t session_len;
11615 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011616
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011617 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011618 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11619 * this function's documentation.
11620 *
11621 * These are due to assumptions/limitations in the implementation. Some of
11622 * them are likely to stay (no handshake in progress) some might go away
11623 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011624 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011625 /* The initial handshake must be over */
11626 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011628 if( ssl->handshake != NULL )
11629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11630 /* Double-check that sub-structures are indeed ready */
11631 if( ssl->transform == NULL || ssl->session == NULL )
11632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11633 /* There must be no pending incoming or outgoing data */
11634 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11636 if( ssl->out_left != 0 )
11637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11638 /* Protocol must be DLTS, not TLS */
11639 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11641 /* Version must be 1.2 */
11642 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11644 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11646 /* We must be using an AEAD ciphersuite */
11647 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11649 /* Renegotiation must not be enabled */
11650 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011652
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011653 /*
11654 * Version and format identifier
11655 */
11656 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011657
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011658 if( used <= buf_len )
11659 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011660 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011661 sizeof( ssl_serialized_context_header ) );
11662 p += sizeof( ssl_serialized_context_header );
11663 }
11664
11665 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011666 * Session (length + data)
11667 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011668 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011669 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11670 return( ret );
11671
11672 used += 4 + session_len;
11673 if( used <= buf_len )
11674 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011675 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011676
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011677 ret = ssl_session_save( ssl->session, 1,
11678 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011679 if( ret != 0 )
11680 return( ret );
11681
11682 p += session_len;
11683 }
11684
11685 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011686 * Transform
11687 */
11688 used += sizeof( ssl->transform->randbytes );
11689 if( used <= buf_len )
11690 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011691 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011692 sizeof( ssl->transform->randbytes ) );
11693 p += sizeof( ssl->transform->randbytes );
11694 }
11695
11696#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11697 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11698 if( used <= buf_len )
11699 {
11700 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011701 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11702 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011703 p += ssl->transform->in_cid_len;
11704
11705 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011706 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11707 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011708 p += ssl->transform->out_cid_len;
11709 }
11710#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11711
11712 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011713 * Saved fields from top-level ssl_context structure
11714 */
11715#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11716 used += 4;
11717 if( used <= buf_len )
11718 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011719 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011720 }
11721#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11722
11723#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11724 used += 16;
11725 if( used <= buf_len )
11726 {
11727 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11728 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11729 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11730 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11731 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11732 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11733 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11734 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11735
11736 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11737 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11738 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11739 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11740 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11741 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11742 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11743 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11744 }
11745#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11746
11747#if defined(MBEDTLS_SSL_PROTO_DTLS)
11748 used += 1;
11749 if( used <= buf_len )
11750 {
11751 *p++ = ssl->disable_datagram_packing;
11752 }
11753#endif /* MBEDTLS_SSL_PROTO_DTLS */
11754
11755 used += 8;
11756 if( used <= buf_len )
11757 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011758 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011759 p += 8;
11760 }
11761
11762#if defined(MBEDTLS_SSL_PROTO_DTLS)
11763 used += 2;
11764 if( used <= buf_len )
11765 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011766 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011767 }
11768#endif /* MBEDTLS_SSL_PROTO_DTLS */
11769
11770#if defined(MBEDTLS_SSL_ALPN)
11771 {
11772 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011773 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011774 : 0;
11775
11776 used += 1 + alpn_len;
11777 if( used <= buf_len )
11778 {
11779 *p++ = alpn_len;
11780
11781 if( ssl->alpn_chosen != NULL )
11782 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011783 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011784 p += alpn_len;
11785 }
11786 }
11787 }
11788#endif /* MBEDTLS_SSL_ALPN */
11789
11790 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011791 * Done
11792 */
11793 *olen = used;
11794
11795 if( used > buf_len )
11796 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011797
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011798 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11799
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011800 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011801}
11802
11803/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011804 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011805 *
11806 * This internal version is wrapped by a public function that cleans up in
11807 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011808 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011809static int ssl_context_load( mbedtls_ssl_context *ssl,
11810 const unsigned char *buf,
11811 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011812{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011813 const unsigned char *p = buf;
11814 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011815 size_t session_len;
11816 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011817
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011818 /*
11819 * The context should have been freshly setup or reset.
11820 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011821 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011822 * renegotiating, or if the user mistakenly loaded a session first.)
11823 */
11824 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11825 ssl->session != NULL )
11826 {
11827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11828 }
11829
11830 /*
11831 * We can't check that the config matches the initial one, but we can at
11832 * least check it matches the requirements for serializing.
11833 */
11834 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011835 mbedtls_ssl_ver_lt(
11836 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11837 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11838 mbedtls_ssl_ver_gt(
11839 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11840 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11841 mbedtls_ssl_ver_lt(
11842 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11843 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11844 mbedtls_ssl_ver_gt(
11845 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11846 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011847 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011848 {
11849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11850 }
11851
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011852 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11853
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011854 /*
11855 * Check version identifier
11856 */
11857 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11859
Teppo Järvelin650343c2019-10-03 15:36:59 +030011860 // use regular memcmp as header is not that critical
11861 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011862 sizeof( ssl_serialized_context_header ) ) != 0 )
11863 {
11864 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11865 }
11866 p += sizeof( ssl_serialized_context_header );
11867
11868 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011869 * Session
11870 */
11871 if( (size_t)( end - p ) < 4 )
11872 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11873
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011874 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011875 p += 4;
11876
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011877 /* This has been allocated by ssl_handshake_init(), called by
11878 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11879 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011880 ssl->session_in = ssl->session;
11881 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011882 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011883
11884 if( (size_t)( end - p ) < session_len )
11885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11886
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011887 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011888 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011889 {
11890 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011891 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011892 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011893
11894 p += session_len;
11895
11896 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011897 * Transform
11898 */
11899
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011900 /* This has been allocated by ssl_handshake_init(), called by
11901 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11902 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011903 ssl->transform_in = ssl->transform;
11904 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011905 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011906
11907 /* Read random bytes and populate structure */
11908 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11910
11911 ret = ssl_populate_transform( ssl->transform,
11912 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11913 ssl->session->master,
11914#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11916 ssl->session->encrypt_then_mac,
11917#endif
11918#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11919 ssl->session->trunc_hmac,
11920#endif
11921#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11922#if defined(MBEDTLS_ZLIB_SUPPORT)
11923 ssl->session->compression,
11924#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011925 p, /* currently pointing to randbytes */
11926 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11927 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11928 ssl );
11929 if( ret != 0 )
11930 return( ret );
11931
11932 p += sizeof( ssl->transform->randbytes );
11933
11934#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11935 /* Read connection IDs and store them */
11936 if( (size_t)( end - p ) < 1 )
11937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11938
11939 ssl->transform->in_cid_len = *p++;
11940
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011941 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11943
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011944 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11945 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011946 p += ssl->transform->in_cid_len;
11947
11948 ssl->transform->out_cid_len = *p++;
11949
11950 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11952
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011953 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11954 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011955 p += ssl->transform->out_cid_len;
11956#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11957
11958 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011959 * Saved fields from top-level ssl_context structure
11960 */
11961#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11962 if( (size_t)( end - p ) < 4 )
11963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11964
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011965 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011966 p += 4;
11967#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11968
11969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11970 if( (size_t)( end - p ) < 16 )
11971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11972
11973 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11974 ( (uint64_t) p[1] << 48 ) |
11975 ( (uint64_t) p[2] << 40 ) |
11976 ( (uint64_t) p[3] << 32 ) |
11977 ( (uint64_t) p[4] << 24 ) |
11978 ( (uint64_t) p[5] << 16 ) |
11979 ( (uint64_t) p[6] << 8 ) |
11980 ( (uint64_t) p[7] );
11981 p += 8;
11982
11983 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11984 ( (uint64_t) p[1] << 48 ) |
11985 ( (uint64_t) p[2] << 40 ) |
11986 ( (uint64_t) p[3] << 32 ) |
11987 ( (uint64_t) p[4] << 24 ) |
11988 ( (uint64_t) p[5] << 16 ) |
11989 ( (uint64_t) p[6] << 8 ) |
11990 ( (uint64_t) p[7] );
11991 p += 8;
11992#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11993
11994#if defined(MBEDTLS_SSL_PROTO_DTLS)
11995 if( (size_t)( end - p ) < 1 )
11996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11997
11998 ssl->disable_datagram_packing = *p++;
11999#endif /* MBEDTLS_SSL_PROTO_DTLS */
12000
12001 if( (size_t)( end - p ) < 8 )
12002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12003
Teppo Järvelin91d79382019-10-02 09:09:31 +030012004 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012005 p += 8;
12006
12007#if defined(MBEDTLS_SSL_PROTO_DTLS)
12008 if( (size_t)( end - p ) < 2 )
12009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012010 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012011 p += 2;
12012#endif /* MBEDTLS_SSL_PROTO_DTLS */
12013
12014#if defined(MBEDTLS_SSL_ALPN)
12015 {
12016 uint8_t alpn_len;
12017 const char **cur;
12018
12019 if( (size_t)( end - p ) < 1 )
12020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12021
12022 alpn_len = *p++;
12023
12024 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12025 {
12026 /* alpn_chosen should point to an item in the configured list */
12027 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12028 {
12029 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012030 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012031 {
12032 ssl->alpn_chosen = *cur;
12033 break;
12034 }
12035 }
12036 }
12037
12038 /* can only happen on conf mismatch */
12039 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12041
12042 p += alpn_len;
12043 }
12044#endif /* MBEDTLS_SSL_ALPN */
12045
12046 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012047 * Forced fields from top-level ssl_context structure
12048 *
12049 * Most of them already set to the correct value by mbedtls_ssl_init() and
12050 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12051 */
12052 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12053
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012054#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012055 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012056#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12057#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012058 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012059#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012060
Hanno Becker83985822019-08-30 10:42:49 +010012061 /* Adjust pointers for header fields of outgoing records to
12062 * the given transform, accounting for explicit IV and CID. */
12063 ssl_update_out_pointers( ssl, ssl->transform );
12064
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012065#if defined(MBEDTLS_SSL_PROTO_DTLS)
12066 ssl->in_epoch = 1;
12067#endif
12068
12069 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12070 * which we don't want - otherwise we'd end up freeing the wrong transform
12071 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12072 if( ssl->handshake != NULL )
12073 {
12074 mbedtls_ssl_handshake_free( ssl );
12075 mbedtls_free( ssl->handshake );
12076 ssl->handshake = NULL;
12077 }
12078
12079 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012080 * Done - should have consumed entire buffer
12081 */
12082 if( p != end )
12083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012084
12085 return( 0 );
12086}
12087
12088/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012089 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012090 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012091int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012092 const unsigned char *buf,
12093 size_t len )
12094{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012095 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012096
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012097 if( ret != 0 )
12098 mbedtls_ssl_free( context );
12099
12100 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012101}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012102#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012103
12104/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012105 * Free an SSL context
12106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012107void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012108{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012109 if( ssl == NULL )
12110 return;
12111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012113
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012114 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012115 {
Angus Grattond8213d02016-05-25 20:56:48 +100012116 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012117 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012118 }
12119
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012120 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012121 {
Angus Grattond8213d02016-05-25 20:56:48 +100012122 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012123 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012124 }
12125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012126#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012127 if( ssl->compress_buf != NULL )
12128 {
Angus Grattond8213d02016-05-25 20:56:48 +100012129 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012130 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012131 }
12132#endif
12133
Paul Bakker48916f92012-09-16 19:57:18 +000012134 if( ssl->transform )
12135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136 mbedtls_ssl_transform_free( ssl->transform );
12137 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012138 }
12139
12140 if( ssl->handshake )
12141 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012142 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012143 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12144 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012146 mbedtls_free( ssl->handshake );
12147 mbedtls_free( ssl->transform_negotiate );
12148 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012149 }
12150
Paul Bakkerc0463502013-02-14 11:19:38 +010012151 if( ssl->session )
12152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012153 mbedtls_ssl_session_free( ssl->session );
12154 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012155 }
12156
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012157#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012158 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012159 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012160 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012161 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012162 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012165#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12166 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12169 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012170 }
12171#endif
12172
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012173#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012174 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012175#endif
12176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012178
Paul Bakker86f04f42013-02-14 11:20:09 +010012179 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012180 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012181}
12182
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012183/*
12184 * Initialze mbedtls_ssl_config
12185 */
12186void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12187{
12188 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012189
12190#if !defined(MBEDTLS_SSL_PROTO_TLS)
12191 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12192#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012193}
12194
Simon Butcherc97b6972015-12-27 23:48:17 +000012195#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012196#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012197static int ssl_preset_default_hashes[] = {
12198#if defined(MBEDTLS_SHA512_C)
12199 MBEDTLS_MD_SHA512,
12200 MBEDTLS_MD_SHA384,
12201#endif
12202#if defined(MBEDTLS_SHA256_C)
12203 MBEDTLS_MD_SHA256,
12204 MBEDTLS_MD_SHA224,
12205#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012206#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012207 MBEDTLS_MD_SHA1,
12208#endif
12209 MBEDTLS_MD_NONE
12210};
Simon Butcherc97b6972015-12-27 23:48:17 +000012211#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012212#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012213
Hanno Becker73f4cb12019-06-27 13:51:07 +010012214#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012215static int ssl_preset_suiteb_ciphersuites[] = {
12216 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12217 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12218 0
12219};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012220#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012221
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012222#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012223#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012224static int ssl_preset_suiteb_hashes[] = {
12225 MBEDTLS_MD_SHA256,
12226 MBEDTLS_MD_SHA384,
12227 MBEDTLS_MD_NONE
12228};
12229#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012230#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012231
Hanno Beckerc1096e72019-06-19 12:30:41 +010012232#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012233static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012234#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012235 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012236#endif
12237#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012238 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012239#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012240 MBEDTLS_ECP_DP_NONE
12241};
12242#endif
12243
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012244/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012245 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012246 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012247int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012248 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012249{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012250#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012251 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012252#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012253
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012254 /* Use the functions here so that they are covered in tests,
12255 * but otherwise access member directly for efficiency */
12256 mbedtls_ssl_conf_endpoint( conf, endpoint );
12257 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012258
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012259 /*
12260 * Things that are common to all presets
12261 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012262#if defined(MBEDTLS_SSL_CLI_C)
12263 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12264 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012265#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012266 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012267#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012268#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12269 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12270#endif
12271 }
12272#endif
12273
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012274#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012275 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012276#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012277
12278#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12279 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12280#endif
12281
12282#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012283#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012284 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012285#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12286#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012287 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012288 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012289#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012290#endif
12291
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012292#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12293 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12294#endif
12295
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012296#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012297 conf->f_cookie_write = ssl_cookie_write_dummy;
12298 conf->f_cookie_check = ssl_cookie_check_dummy;
12299#endif
12300
Hanno Becker7f376f42019-06-12 16:20:48 +010012301#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12302 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012303 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12304#endif
12305
Janos Follath088ce432017-04-10 12:42:31 +010012306#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012307#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012308 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012309#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12310#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012311
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012313#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012314 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012315#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12316#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012317 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012318#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12319#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012320
12321#if defined(MBEDTLS_SSL_RENEGOTIATION)
12322 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012323 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12324 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012325#endif
12326
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012327#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12328 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12329 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012330 const unsigned char dhm_p[] =
12331 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12332 const unsigned char dhm_g[] =
12333 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12334
Hanno Beckera90658f2017-10-04 15:29:08 +010012335 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12336 dhm_p, sizeof( dhm_p ),
12337 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012338 {
12339 return( ret );
12340 }
12341 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012342#endif
12343
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012344 /*
12345 * Preset-specific defaults
12346 */
12347 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012348 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012349 /*
12350 * NSA Suite B
12351 */
12352 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012353#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012354 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012355#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12356#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012357 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012358#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12359#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012360 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012361#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12362#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012363 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012364#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012365
Hanno Becker73f4cb12019-06-27 13:51:07 +010012366#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012367 conf->ciphersuite_list[0] =
12368 conf->ciphersuite_list[1] =
12369 conf->ciphersuite_list[2] =
12370 conf->ciphersuite_list[3] =
12371 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012372#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012373
12374#if defined(MBEDTLS_X509_CRT_PARSE_C)
12375 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012376#endif
12377
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012378#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012379#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012380 conf->sig_hashes = ssl_preset_suiteb_hashes;
12381#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012382#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012383
12384#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012385#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012386 conf->curve_list = ssl_preset_suiteb_curves;
12387#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012388#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012389 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012390
12391 /*
12392 * Default
12393 */
12394 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012395#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012396 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12397 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12398 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12399 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012400#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12401#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012402 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12403 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12404 MBEDTLS_SSL_MIN_MINOR_VERSION :
12405 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012407 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012408 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12409#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012410#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12411#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12412 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12413#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12414#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12415 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12416#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012417
Hanno Becker73f4cb12019-06-27 13:51:07 +010012418#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012419 conf->ciphersuite_list[0] =
12420 conf->ciphersuite_list[1] =
12421 conf->ciphersuite_list[2] =
12422 conf->ciphersuite_list[3] =
12423 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012424#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012425
12426#if defined(MBEDTLS_X509_CRT_PARSE_C)
12427 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12428#endif
12429
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012430#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012431#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012432 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012433#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012434#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012435
12436#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012437#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012438 conf->curve_list = mbedtls_ecp_grp_id_list();
12439#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012440#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012441
12442#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12443 conf->dhm_min_bitlen = 1024;
12444#endif
12445 }
12446
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012447 return( 0 );
12448}
12449
12450/*
12451 * Free mbedtls_ssl_config
12452 */
12453void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12454{
12455#if defined(MBEDTLS_DHM_C)
12456 mbedtls_mpi_free( &conf->dhm_P );
12457 mbedtls_mpi_free( &conf->dhm_G );
12458#endif
12459
12460#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12461 if( conf->psk != NULL )
12462 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012463 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012464 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012465 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012466 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012467 }
12468
12469 if( conf->psk_identity != NULL )
12470 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012471 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012472 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012473 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012474 conf->psk_identity_len = 0;
12475 }
12476#endif
12477
12478#if defined(MBEDTLS_X509_CRT_PARSE_C)
12479 ssl_key_cert_free( conf->key_cert );
12480#endif
12481
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012482 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012483}
12484
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012485#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012486 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12487 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012488/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012489 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012491unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012493#if defined(MBEDTLS_RSA_C)
12494 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12495 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012496#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012497#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012498 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12499 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012501 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012502}
12503
Hanno Becker7e5437a2017-04-28 17:15:26 +010012504unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12505{
12506 switch( type ) {
12507 case MBEDTLS_PK_RSA:
12508 return( MBEDTLS_SSL_SIG_RSA );
12509 case MBEDTLS_PK_ECDSA:
12510 case MBEDTLS_PK_ECKEY:
12511 return( MBEDTLS_SSL_SIG_ECDSA );
12512 default:
12513 return( MBEDTLS_SSL_SIG_ANON );
12514 }
12515}
12516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012517mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012518{
12519 switch( sig )
12520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012521#if defined(MBEDTLS_RSA_C)
12522 case MBEDTLS_SSL_SIG_RSA:
12523 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012524#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012525#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012526 case MBEDTLS_SSL_SIG_ECDSA:
12527 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012528#endif
12529 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012530 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012531 }
12532}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012533#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012534
Hanno Becker7e5437a2017-04-28 17:15:26 +010012535#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12536 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12537
12538/* Find an entry in a signature-hash set matching a given hash algorithm. */
12539mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12540 mbedtls_pk_type_t sig_alg )
12541{
12542 switch( sig_alg )
12543 {
12544 case MBEDTLS_PK_RSA:
12545 return( set->rsa );
12546 case MBEDTLS_PK_ECDSA:
12547 return( set->ecdsa );
12548 default:
12549 return( MBEDTLS_MD_NONE );
12550 }
12551}
12552
12553/* Add a signature-hash-pair to a signature-hash set */
12554void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12555 mbedtls_pk_type_t sig_alg,
12556 mbedtls_md_type_t md_alg )
12557{
12558 switch( sig_alg )
12559 {
12560 case MBEDTLS_PK_RSA:
12561 if( set->rsa == MBEDTLS_MD_NONE )
12562 set->rsa = md_alg;
12563 break;
12564
12565 case MBEDTLS_PK_ECDSA:
12566 if( set->ecdsa == MBEDTLS_MD_NONE )
12567 set->ecdsa = md_alg;
12568 break;
12569
12570 default:
12571 break;
12572 }
12573}
12574
12575/* Allow exactly one hash algorithm for each signature. */
12576void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12577 mbedtls_md_type_t md_alg )
12578{
12579 set->rsa = md_alg;
12580 set->ecdsa = md_alg;
12581}
12582
12583#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12584 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12585
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012586/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012587 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012589mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012590{
12591 switch( hash )
12592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012593#if defined(MBEDTLS_MD5_C)
12594 case MBEDTLS_SSL_HASH_MD5:
12595 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012597#if defined(MBEDTLS_SHA1_C)
12598 case MBEDTLS_SSL_HASH_SHA1:
12599 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012601#if defined(MBEDTLS_SHA256_C)
12602 case MBEDTLS_SSL_HASH_SHA224:
12603 return( MBEDTLS_MD_SHA224 );
12604 case MBEDTLS_SSL_HASH_SHA256:
12605 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012607#if defined(MBEDTLS_SHA512_C)
12608 case MBEDTLS_SSL_HASH_SHA384:
12609 return( MBEDTLS_MD_SHA384 );
12610 case MBEDTLS_SSL_HASH_SHA512:
12611 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012612#endif
12613 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012614 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012615 }
12616}
12617
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012618/*
12619 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12620 */
12621unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12622{
12623 switch( md )
12624 {
12625#if defined(MBEDTLS_MD5_C)
12626 case MBEDTLS_MD_MD5:
12627 return( MBEDTLS_SSL_HASH_MD5 );
12628#endif
12629#if defined(MBEDTLS_SHA1_C)
12630 case MBEDTLS_MD_SHA1:
12631 return( MBEDTLS_SSL_HASH_SHA1 );
12632#endif
12633#if defined(MBEDTLS_SHA256_C)
12634 case MBEDTLS_MD_SHA224:
12635 return( MBEDTLS_SSL_HASH_SHA224 );
12636 case MBEDTLS_MD_SHA256:
12637 return( MBEDTLS_SSL_HASH_SHA256 );
12638#endif
12639#if defined(MBEDTLS_SHA512_C)
12640 case MBEDTLS_MD_SHA384:
12641 return( MBEDTLS_SSL_HASH_SHA384 );
12642 case MBEDTLS_MD_SHA512:
12643 return( MBEDTLS_SSL_HASH_SHA512 );
12644#endif
12645 default:
12646 return( MBEDTLS_SSL_HASH_NONE );
12647 }
12648}
12649
Hanno Beckeree902df2019-08-23 13:47:47 +010012650#if defined(MBEDTLS_USE_TINYCRYPT)
12651/*
12652 * Check if a curve proposed by the peer is in our list.
12653 * Return 0 if we're willing to use it, -1 otherwise.
12654 */
12655int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12656 mbedtls_uecc_group_id grp_id )
12657{
12658 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12659 if( own_ec_id == grp_id )
12660 return( 0 );
12661 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12662
12663 return( -1 );
12664}
12665#endif /* MBEDTLS_USE_TINYCRYPT */
12666
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012667#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012668/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012669 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012670 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012671 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012672int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12673 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012674{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012675 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12676 if( own_ec_id == grp_id )
12677 return( 0 );
12678 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012679
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012680 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012681}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012682#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012683
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012684#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012685/*
12686 * Check if a hash proposed by the peer is in our list.
12687 * Return 0 if we're willing to use it, -1 otherwise.
12688 */
12689int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12690 mbedtls_md_type_t md )
12691{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012692 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12693 if( md_alg == md )
12694 return( 0 );
12695 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012696
12697 return( -1 );
12698}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012699#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012701#if defined(MBEDTLS_X509_CRT_PARSE_C)
12702int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012703 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012704 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012705 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012706{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012707 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012708#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012709 int usage = 0;
12710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012711#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012712 const char *ext_oid;
12713 size_t ext_len;
12714#endif
12715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012716#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12717 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012718 ((void) cert);
12719 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012720 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012721#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012723#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12724 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012725 {
12726 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012727 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012729 case MBEDTLS_KEY_EXCHANGE_RSA:
12730 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012731 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012732 break;
12733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012734 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12735 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12736 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12737 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012738 break;
12739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012740 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12741 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012742 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012743 break;
12744
12745 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012746 case MBEDTLS_KEY_EXCHANGE_NONE:
12747 case MBEDTLS_KEY_EXCHANGE_PSK:
12748 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12749 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012750 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012751 usage = 0;
12752 }
12753 }
12754 else
12755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012756 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12757 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012758 }
12759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012760 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012761 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012762 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012763 ret = -1;
12764 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012765#else
12766 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012767#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012769#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12770 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012772 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12773 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012774 }
12775 else
12776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012777 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12778 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012779 }
12780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012781 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012782 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012783 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012784 ret = -1;
12785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012786#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012787
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012788 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012791
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012792#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12793 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12794int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12795 unsigned char *output,
12796 unsigned char *data, size_t data_len )
12797{
12798 int ret = 0;
12799 mbedtls_md5_context mbedtls_md5;
12800 mbedtls_sha1_context mbedtls_sha1;
12801
12802 mbedtls_md5_init( &mbedtls_md5 );
12803 mbedtls_sha1_init( &mbedtls_sha1 );
12804
12805 /*
12806 * digitally-signed struct {
12807 * opaque md5_hash[16];
12808 * opaque sha_hash[20];
12809 * };
12810 *
12811 * md5_hash
12812 * MD5(ClientHello.random + ServerHello.random
12813 * + ServerParams);
12814 * sha_hash
12815 * SHA(ClientHello.random + ServerHello.random
12816 * + ServerParams);
12817 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012818 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012819 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012821 goto exit;
12822 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012823 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012824 ssl->handshake->randbytes, 64 ) ) != 0 )
12825 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012827 goto exit;
12828 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012829 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 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_update_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_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012835 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012837 goto exit;
12838 }
12839
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012840 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012841 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_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_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012846 ssl->handshake->randbytes, 64 ) ) != 0 )
12847 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012849 goto exit;
12850 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 data_len ) ) != 0 )
12853 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012855 goto exit;
12856 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012857 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012858 output + 16 ) ) != 0 )
12859 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012861 goto exit;
12862 }
12863
12864exit:
12865 mbedtls_md5_free( &mbedtls_md5 );
12866 mbedtls_sha1_free( &mbedtls_sha1 );
12867
12868 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012869 mbedtls_ssl_pend_fatal_alert( ssl,
12870 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012871
12872 return( ret );
12873
12874}
12875#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12876 MBEDTLS_SSL_PROTO_TLS1_1 */
12877
12878#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12879 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12880int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012881 unsigned char *hash, size_t *hashlen,
12882 unsigned char *data, size_t data_len,
12883 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012884{
12885 int ret = 0;
12886 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012887 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012888 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012889
12890 mbedtls_md_init( &ctx );
12891
12892 /*
12893 * digitally-signed struct {
12894 * opaque client_random[32];
12895 * opaque server_random[32];
12896 * ServerDHParams params;
12897 * };
12898 */
12899 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12900 {
12901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12902 goto exit;
12903 }
12904 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12905 {
12906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12907 goto exit;
12908 }
12909 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12910 {
12911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12912 goto exit;
12913 }
12914 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12915 {
12916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12917 goto exit;
12918 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012919 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012920 {
12921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12922 goto exit;
12923 }
12924
12925exit:
12926 mbedtls_md_free( &ctx );
12927
12928 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012929 mbedtls_ssl_pend_fatal_alert( ssl,
12930 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012931
12932 return( ret );
12933}
12934#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12935 MBEDTLS_SSL_PROTO_TLS1_2 */
12936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012937#endif /* MBEDTLS_SSL_TLS_C */