blob: e8a230d3edee78b21d0c910d12a517f5f269460c [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"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
Teppo Järvelin91d79382019-10-02 09:09:31 +030092 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010093
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300302 /* Not using more secure mbedtls_platform_memcpy as cid is public */
303 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100304 /* Truncation is not an issue here because
305 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
306 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100307
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100308 return( 0 );
309}
310
311int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
312 int *enabled,
313 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
314 size_t *peer_cid_len )
315{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100316 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100317
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200318 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100319 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
320 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100322 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100323
Hanno Beckercb063f52019-05-03 12:54:52 +0100324 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
325 * were used, but client and server requested the empty CID.
326 * This is indistinguishable from not using the CID extension
327 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100328 if( ssl->transform_in->in_cid_len == 0 &&
329 ssl->transform_in->out_cid_len == 0 )
330 {
331 return( 0 );
332 }
333
Hanno Becker633d6042019-05-22 16:50:35 +0100334 if( peer_cid_len != NULL )
335 {
336 *peer_cid_len = ssl->transform_in->out_cid_len;
337 if( peer_cid != NULL )
338 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300339 /* Not using more secure mbedtls_platform_memcpy as cid is public */
340 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100341 ssl->transform_in->out_cid_len );
342 }
343 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100344
345 *enabled = MBEDTLS_SSL_CID_ENABLED;
346
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100347 return( 0 );
348}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100349#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100350
Hanno Beckerd5847772018-08-28 10:09:23 +0100351/* Forward declarations for functions related to message buffering. */
352static void ssl_buffering_free( mbedtls_ssl_context *ssl );
353static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
354 uint8_t slot );
355static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
357static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
358static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100359static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
360 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100361static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100362
Hanno Beckera67dee22018-08-22 10:05:20 +0100363static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100364static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365{
Hanno Becker11682cc2018-08-22 14:41:02 +0100366 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100367
368 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100369 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100370
371 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
372}
373
Hanno Becker67bc7c32018-08-06 11:33:50 +0100374static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
375{
Hanno Becker11682cc2018-08-22 14:41:02 +0100376 size_t const bytes_written = ssl->out_left;
377 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100378
379 /* Double-check that the write-index hasn't gone
380 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100381 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100382 {
383 /* Should never happen... */
384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
385 }
386
387 return( (int) ( mtu - bytes_written ) );
388}
389
390static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
391{
392 int ret;
393 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400394 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100395
396#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
397 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
398
399 if( max_len > mfl )
400 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100401
402 /* By the standard (RFC 6066 Sect. 4), the MFL extension
403 * only limits the maximum record payload size, so in theory
404 * we would be allowed to pack multiple records of payload size
405 * MFL into a single datagram. However, this would mean that there's
406 * no way to explicitly communicate MTU restrictions to the peer.
407 *
408 * The following reduction of max_len makes sure that we never
409 * write datagrams larger than MFL + Record Expansion Overhead.
410 */
411 if( max_len <= ssl->out_left )
412 return( 0 );
413
414 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100415#endif
416
417 ret = ssl_get_remaining_space_in_datagram( ssl );
418 if( ret < 0 )
419 return( ret );
420 remaining = (size_t) ret;
421
422 ret = mbedtls_ssl_get_record_expansion( ssl );
423 if( ret < 0 )
424 return( ret );
425 expansion = (size_t) ret;
426
427 if( remaining <= expansion )
428 return( 0 );
429
430 remaining -= expansion;
431 if( remaining >= max_len )
432 remaining = max_len;
433
434 return( (int) remaining );
435}
436
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200437/*
438 * Double the retransmit timeout value, within the allowed range,
439 * returning -1 if the maximum value has already been reached.
440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200442{
443 uint32_t new_timeout;
444
Hanno Becker1f835fa2019-06-13 10:14:59 +0100445 if( ssl->handshake->retransmit_timeout >=
446 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
447 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100449 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200450
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200451 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
452 * in the following way: after the initial transmission and a first
453 * retransmission, back off to a temporary estimated MTU of 508 bytes.
454 * This value is guaranteed to be deliverable (if not guaranteed to be
455 * delivered) of any compliant IPv4 (and IPv6) network, and should work
456 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100457 if( ssl->handshake->retransmit_timeout !=
458 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200460 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
462 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200463
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200464 new_timeout = 2 * ssl->handshake->retransmit_timeout;
465
466 /* Avoid arithmetic overflow and range overflow */
467 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 {
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 }
472
473 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200475 ssl->handshake->retransmit_timeout ) );
476
477 return( 0 );
478}
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200481{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100482 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200484 ssl->handshake->retransmit_timeout ) );
485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200489/*
490 * Convert max_fragment_length codes to length.
491 * RFC 6066 says:
492 * enum{
493 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
494 * } MaxFragmentLength;
495 * and we add 0 -> extension unused
496 */
Angus Grattond8213d02016-05-25 20:56:48 +1000497static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200498{
Angus Grattond8213d02016-05-25 20:56:48 +1000499 switch( mfl )
500 {
501 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300502 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000503 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
504 return 512;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
506 return 1024;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
508 return 2048;
509 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
510 return 4096;
511 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300512 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000513 }
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200516
Hanno Becker58fccf22019-02-06 14:30:46 +0000517int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
518 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300521 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000524
525#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200526 if( src->peer_cert != NULL )
527 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200528 int ret;
529
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200530 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200531 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200532 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200537 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200540 dst->peer_cert = NULL;
541 return( ret );
542 }
543 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100544#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 if( src->peer_cert_digest != NULL )
546 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000548 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000549 if( dst->peer_cert_digest == NULL )
550 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
551
Teppo Järvelin91d79382019-10-02 09:09:31 +0300552 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000553 src->peer_cert_digest_len );
554 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000555 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100557#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200561#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200562 if( src->ticket != NULL )
563 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200564 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200565 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200566 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200567
Teppo Järvelin91d79382019-10-02 09:09:31 +0300568 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200570#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200571
572 return( 0 );
573}
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
576int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200577 const unsigned char *key_enc, const unsigned char *key_dec,
578 size_t keylen,
579 const unsigned char *iv_enc, const unsigned char *iv_dec,
580 size_t ivlen,
581 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200582 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
584int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
586int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
587int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
588#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000589
Paul Bakker5121ce52009-01-03 21:22:43 +0000590/*
591 * Key material generation
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100594MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200595 const char *label,
596 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000597 unsigned char *dstbuf, size_t dlen )
598{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100599 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000600 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200601 mbedtls_md5_context md5;
602 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000603 unsigned char padding[16];
604 unsigned char sha1sum[20];
605 ((void)label);
606
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200607 mbedtls_md5_init( &md5 );
608 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200609
Paul Bakker5f70b252012-09-13 14:23:06 +0000610 /*
611 * SSLv3:
612 * block =
613 * MD5( secret + SHA1( 'A' + secret + random ) ) +
614 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
615 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
616 * ...
617 */
618 for( i = 0; i < dlen / 16; i++ )
619 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200620 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000621
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 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, secret, slen ) ) != 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, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100630 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100631 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000632
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 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, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100639 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100640 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000641 }
642
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100643exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200644 mbedtls_md5_free( &md5 );
645 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000646
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500647 mbedtls_platform_zeroize( padding, sizeof( padding ) );
648 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100650 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100655MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200656 const char *label,
657 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000658 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659{
Paul Bakker23986e52011-04-24 08:57:21 +0000660 size_t nb, hs;
661 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200662 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 unsigned char tmp[128];
664 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100665 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667 int ret;
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 hs = ( slen + 1 ) / 2;
675 S1 = secret;
676 S2 = secret + slen - hs;
677
678 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300679 mbedtls_platform_memcpy( tmp + 20, label, nb );
680 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 nb += rlen;
682
683 /*
684 * First compute P_md5(secret,label+random)[0..dlen]
685 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100686 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
687 MBEDTLS_MD_INVALID_HANDLE )
688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100690 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100693 return( ret );
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
696 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
697 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
699 for( i = 0; i < dlen; i += 16 )
700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_md_hmac_reset ( &md_ctx );
702 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
703 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_md_hmac_reset ( &md_ctx );
706 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
707 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
709 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
710
711 for( j = 0; j < k; j++ )
712 dstbuf[i + j] = h_i[j];
713 }
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100716
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 /*
718 * XOR out with P_sha1(secret,label+random)[0..dlen]
719 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100720 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
721 MBEDTLS_MD_INVALID_HANDLE )
722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100724 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100727 return( ret );
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
730 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
731 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 for( i = 0; i < dlen; i += 20 )
734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_hmac_reset ( &md_ctx );
736 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
737 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_hmac_reset ( &md_ctx );
740 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
741 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
744
745 for( j = 0; j < k; j++ )
746 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
747 }
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100750
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500751 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
752 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
754 return( 0 );
755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100759#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
760MBEDTLS_ALWAYS_INLINE static inline
761#else
762static
763#endif
764int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 const unsigned char *secret, size_t slen,
766 const char *label,
767 const unsigned char *random, size_t rlen,
768 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000769{
770 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000772 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100774 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100776 int ret;
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100780 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
781 MBEDTLS_MD_INVALID_HANDLE )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100784 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787
788 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000790
791 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300792 mbedtls_platform_memcpy( tmp + md_len, label, nb );
793 mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000794 nb += rlen;
795
796 /*
797 * Compute P_<hash>(secret, label + random)[0..dlen]
798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800 return( ret );
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
803 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
804 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100805
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100806 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_md_hmac_reset ( &md_ctx );
809 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
810 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_md_hmac_reset ( &md_ctx );
813 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
814 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100816 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000817
818 for( j = 0; j < k; j++ )
819 dstbuf[i + j] = h_i[j];
820 }
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100823
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500824 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
825 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826
827 return( 0 );
828}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100831MBEDTLS_NO_INLINE static int tls_prf_sha256(
832 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100833 const char *label,
834 const unsigned char *random, size_t rlen,
835 unsigned char *dstbuf, size_t dlen )
836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100838 label, random, rlen, dstbuf, dlen ) );
839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100843MBEDTLS_NO_INLINE static int tls_prf_sha384(
844 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200845 const char *label,
846 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000847 unsigned char *dstbuf, size_t dlen )
848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100850 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_SHA512_C */
853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000854
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100855/*
856 * Call the appropriate PRF function
857 */
Hanno Becker2793f742019-08-16 14:28:43 +0100858MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100859 mbedtls_md_type_t hash,
860 const unsigned char *secret, size_t slen,
861 const char *label,
862 const unsigned char *random, size_t rlen,
863 unsigned char *dstbuf, size_t dlen )
864{
865#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
866 (void) hash;
867#endif
868
869#if defined(MBEDTLS_SSL_PROTO_SSL3)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
871 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
872 else
873#endif
874#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100875 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100876 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
877 else
878#endif
879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
880#if defined(MBEDTLS_SHA512_C)
881 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
882 hash == MBEDTLS_MD_SHA384 )
883 {
884 return( tls_prf_sha384( secret, slen, label, random, rlen,
885 dstbuf, dlen ) );
886 }
887 else
888#endif
889#if defined(MBEDTLS_SHA256_C)
890 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
891 {
892 return( tls_prf_sha256( secret, slen, label, random, rlen,
893 dstbuf, dlen ) );
894 }
895#endif
896#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
897
898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
899}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200900
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100901#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100902MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100903 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
904{
905 const char *sender;
906 mbedtls_md5_context md5;
907 mbedtls_sha1_context sha1;
908
909 unsigned char padbuf[48];
910 unsigned char md5sum[16];
911 unsigned char sha1sum[20];
912
913 mbedtls_ssl_session *session = ssl->session_negotiate;
914 if( !session )
915 session = ssl->session;
916
917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
918
919 mbedtls_md5_init( &md5 );
920 mbedtls_sha1_init( &sha1 );
921
922 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
923 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
924
925 /*
926 * SSLv3:
927 * hash =
928 * MD5( master + pad2 +
929 * MD5( handshake + sender + master + pad1 ) )
930 * + SHA1( master + pad2 +
931 * SHA1( handshake + sender + master + pad1 ) )
932 */
933
934#if !defined(MBEDTLS_MD5_ALT)
935 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
936 md5.state, sizeof( md5.state ) );
937#endif
938
939#if !defined(MBEDTLS_SHA1_ALT)
940 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
941 sha1.state, sizeof( sha1.state ) );
942#endif
943
944 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
945 : "SRVR";
946
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200947 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100948
949 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
950 mbedtls_md5_update_ret( &md5, session->master, 48 );
951 mbedtls_md5_update_ret( &md5, padbuf, 48 );
952 mbedtls_md5_finish_ret( &md5, md5sum );
953
954 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
955 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
956 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
957 mbedtls_sha1_finish_ret( &sha1, sha1sum );
958
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200959 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100960
961 mbedtls_md5_starts_ret( &md5 );
962 mbedtls_md5_update_ret( &md5, session->master, 48 );
963 mbedtls_md5_update_ret( &md5, padbuf, 48 );
964 mbedtls_md5_update_ret( &md5, md5sum, 16 );
965 mbedtls_md5_finish_ret( &md5, buf );
966
967 mbedtls_sha1_starts_ret( &sha1 );
968 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
969 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
970 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
971 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
972
973 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
974
975 mbedtls_md5_free( &md5 );
976 mbedtls_sha1_free( &sha1 );
977
978 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
979 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
980 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
981
982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
983}
984#endif /* MBEDTLS_SSL_PROTO_SSL3 */
985
986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100987MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100988 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
989{
990 int len = 12;
991 const char *sender;
992 mbedtls_md5_context md5;
993 mbedtls_sha1_context sha1;
994 unsigned char padbuf[36];
995
996 mbedtls_ssl_session *session = ssl->session_negotiate;
997 if( !session )
998 session = ssl->session;
999
1000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1001
1002 mbedtls_md5_init( &md5 );
1003 mbedtls_sha1_init( &sha1 );
1004
1005 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1006 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1007
1008 /*
1009 * TLSv1:
1010 * hash = PRF( master, finished_label,
1011 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1012 */
1013
1014#if !defined(MBEDTLS_MD5_ALT)
1015 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1016 md5.state, sizeof( md5.state ) );
1017#endif
1018
1019#if !defined(MBEDTLS_SHA1_ALT)
1020 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1021 sha1.state, sizeof( sha1.state ) );
1022#endif
1023
1024 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1025 ? "client finished"
1026 : "server finished";
1027
1028 mbedtls_md5_finish_ret( &md5, padbuf );
1029 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1030
1031 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1032 mbedtls_ssl_suite_get_mac(
1033 mbedtls_ssl_ciphersuite_from_id(
1034 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1035 session->master, 48, sender,
1036 padbuf, 36, buf, len );
1037
1038 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1039
1040 mbedtls_md5_free( &md5 );
1041 mbedtls_sha1_free( &sha1 );
1042
1043 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1044
1045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1046}
1047#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1048
1049#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1050#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001051MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001052 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1053{
1054 int len = 12;
1055 const char *sender;
1056 mbedtls_sha256_context sha256;
1057 unsigned char padbuf[32];
1058
1059 mbedtls_ssl_session *session = ssl->session_negotiate;
1060 if( !session )
1061 session = ssl->session;
1062
1063 mbedtls_sha256_init( &sha256 );
1064
1065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1066
1067 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1068
1069 /*
1070 * TLSv1.2:
1071 * hash = PRF( master, finished_label,
1072 * Hash( handshake ) )[0.11]
1073 */
1074
1075#if !defined(MBEDTLS_SHA256_ALT)
1076 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1077 sha256.state, sizeof( sha256.state ) );
1078#endif
1079
1080 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1081 ? "client finished"
1082 : "server finished";
1083
1084 mbedtls_sha256_finish_ret( &sha256, padbuf );
1085
1086 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1087 mbedtls_ssl_suite_get_mac(
1088 mbedtls_ssl_ciphersuite_from_id(
1089 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1090 session->master, 48, sender,
1091 padbuf, 32, buf, len );
1092
1093 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1094
1095 mbedtls_sha256_free( &sha256 );
1096
1097 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1098
1099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1100}
1101#endif /* MBEDTLS_SHA256_C */
1102
1103#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001104MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001105 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1106{
1107 int len = 12;
1108 const char *sender;
1109 mbedtls_sha512_context sha512;
1110 unsigned char padbuf[48];
1111
1112 mbedtls_ssl_session *session = ssl->session_negotiate;
1113 if( !session )
1114 session = ssl->session;
1115
1116 mbedtls_sha512_init( &sha512 );
1117
1118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1119
1120 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1121
1122 /*
1123 * TLSv1.2:
1124 * hash = PRF( master, finished_label,
1125 * Hash( handshake ) )[0.11]
1126 */
1127
1128#if !defined(MBEDTLS_SHA512_ALT)
1129 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1130 sha512.state, sizeof( sha512.state ) );
1131#endif
1132
1133 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1134 ? "client finished"
1135 : "server finished";
1136
1137 mbedtls_sha512_finish_ret( &sha512, padbuf );
1138
1139 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1140 mbedtls_ssl_suite_get_mac(
1141 mbedtls_ssl_ciphersuite_from_id(
1142 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1143 session->master, 48, sender,
1144 padbuf, 48, buf, len );
1145
1146 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1147
1148 mbedtls_sha512_free( &sha512 );
1149
1150 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1151
1152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1153}
1154#endif /* MBEDTLS_SHA512_C */
1155#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1156
Hanno Becker2793f742019-08-16 14:28:43 +01001157MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1158 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001159 mbedtls_md_type_t hash,
1160 mbedtls_ssl_context *ssl,
1161 unsigned char *buf,
1162 int from )
1163{
1164#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1165 (void) hash;
1166#endif
1167
1168#if defined(MBEDTLS_SSL_PROTO_SSL3)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1170 ssl_calc_finished_ssl( ssl, buf, from );
1171 else
1172#endif
1173#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001174 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001175 ssl_calc_finished_tls( ssl, buf, from );
1176 else
1177#endif
1178#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1179#if defined(MBEDTLS_SHA512_C)
1180 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1181 hash == MBEDTLS_MD_SHA384 )
1182 {
1183 ssl_calc_finished_tls_sha384( ssl, buf, from );
1184 }
1185 else
1186#endif
1187#if defined(MBEDTLS_SHA256_C)
1188 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1189 ssl_calc_finished_tls_sha256( ssl, buf, from );
1190 else
1191#endif
1192#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1194
1195 return( 0 );
1196}
1197
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001198/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001199 * Populate a transform structure with session keys and all the other
1200 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001201 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001202 * Parameters:
1203 * - [in/out]: transform: structure to populate
1204 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001205 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001206 * - [in] ciphersuite
1207 * - [in] master
1208 * - [in] encrypt_then_mac
1209 * - [in] trunc_hmac
1210 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001211 * - [in] tls_prf: pointer to PRF to use for key derivation
1212 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001213 * - [in] minor_ver: SSL/TLS minor version
1214 * - [in] endpoint: client or server
1215 * - [in] ssl: optionally used for:
1216 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1217 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1218 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001219 */
Hanno Becker298a4702019-08-16 10:21:32 +01001220/* Force compilers to inline this function if it's used only
1221 * from one place, because at least ARMC5 doesn't do that
1222 * automatically. */
1223#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1224MBEDTLS_ALWAYS_INLINE static inline
1225#else
1226static
1227#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1228int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001229 int ciphersuite,
1230 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001231#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001232#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1233 int encrypt_then_mac,
1234#endif
1235#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1236 int trunc_hmac,
1237#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001238#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001239#if defined(MBEDTLS_ZLIB_SUPPORT)
1240 int compression,
1241#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001242 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001243 int minor_ver,
1244 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001245 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001247 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 unsigned char keyblk[256];
1249 unsigned char *key1;
1250 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001251 unsigned char *mac_enc;
1252 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001253 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001254 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001255 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001256 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001258 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001259
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001260#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1261 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1262 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001263 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001264 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001265#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001266
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001267 /*
1268 * Some data just needs copying into the structure
1269 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001270#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1271 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001272 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001273#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001274
1275#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001276 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001277#else
1278 ((void) minor_ver);
1279#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001281#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001282 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001283#endif
1284
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001285 /*
1286 * Get various info structures
1287 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001288 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001289 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001290 {
1291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001292 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1294 }
1295
Hanno Becker473f98f2019-06-26 10:27:32 +01001296 cipher_info = mbedtls_cipher_info_from_type(
1297 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001298 if( cipher_info == NULL )
1299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001301 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001303 }
1304
Hanno Becker473f98f2019-06-26 10:27:32 +01001305 md_info = mbedtls_md_info_from_type(
1306 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001307 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001310 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001312 }
1313
Hanno Beckera5a2b082019-05-15 14:03:01 +01001314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001315 /* Copy own and peer's CID if the use of the CID
1316 * extension has been negotiated. */
1317 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1318 {
1319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001320
Hanno Becker4932f9f2019-05-03 15:23:51 +01001321 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001322 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1323 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001324 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001325 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001326
1327 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001328 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1329 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001330 ssl->handshake->peer_cid_len );
1331 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1332 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001333 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001334#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001337 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001338 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001339 ret = ssl_prf( minor_ver,
1340 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1341 master, 48, "key expansion", randbytes, 64,
1342 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001343 if( ret != 0 )
1344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001346 return( ret );
1347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001350 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001351 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001352 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 /*
1356 * Determine the appropriate key, IV and MAC length.
1357 */
Paul Bakker68884e32013-01-07 18:20:04 +01001358
Hanno Beckere7f2df02017-12-27 08:17:40 +00001359 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001360
Hanno Beckerf1229442018-01-03 15:32:31 +00001361#if defined(MBEDTLS_GCM_C) || \
1362 defined(MBEDTLS_CCM_C) || \
1363 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 cipher_info->mode == MBEDTLS_MODE_CCM ||
1366 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001368 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001369 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001370 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1371 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001372
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001373 /* All modes haves 96-bit IVs;
1374 * GCM and CCM has 4 implicit and 8 explicit bytes
1375 * ChachaPoly has all 12 bytes implicit
1376 */
Paul Bakker68884e32013-01-07 18:20:04 +01001377 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001378 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1379 transform->fixed_ivlen = 12;
1380 else
1381 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
1383 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001384#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1385#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1386 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1387 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001388 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1391 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001394 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001397 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001398 mac_key_len = mbedtls_md_get_size( md_info );
1399 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001402 /*
1403 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1404 * (rfc 6066 page 13 or rfc 2104 section 4),
1405 * so we only need to adjust the length here.
1406 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001407 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001410
1411#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1412 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001413 * HMAC implementation which also truncates the key
1414 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001415 mac_key_len = transform->maclen;
1416#endif
1417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001419
1420 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001421 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001423 else
1424#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1425 {
1426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
Hanno Beckera9d5c452019-07-25 16:47:12 +01001430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001431 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001432 (unsigned) transform->ivlen,
1433 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
1435 /*
1436 * Finally setup the cipher contexts, IVs and MAC secrets.
1437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001439 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001441 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Paul Bakker68884e32013-01-07 18:20:04 +01001444 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001445 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001447 /*
1448 * This is not used in TLS v1.1.
1449 */
Paul Bakker48916f92012-09-16 19:57:18 +00001450 iv_copy_len = ( transform->fixed_ivlen ) ?
1451 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001452 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1453 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001454 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 }
1456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_SSL_CLI_C */
1458#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001459 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001461 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001462 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Hanno Becker81c7b182017-11-09 18:39:33 +00001464 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001465 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467 /*
1468 * This is not used in TLS v1.1.
1469 */
Paul Bakker48916f92012-09-16 19:57:18 +00001470 iv_copy_len = ( transform->fixed_ivlen ) ?
1471 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001472 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1473 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001474 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001476 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001482
Hanno Becker92231322018-01-03 15:32:51 +00001483#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001485 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001486 {
Hanno Becker92231322018-01-03 15:32:51 +00001487 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001491 }
1492
Teppo Järvelin91d79382019-10-02 09:09:31 +03001493 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1494 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001495 }
1496 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1498#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1499 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001500 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001501 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001502 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1503 For AEAD-based ciphersuites, there is nothing to do here. */
1504 if( mac_key_len != 0 )
1505 {
1506 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1507 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1508 }
Paul Bakker68884e32013-01-07 18:20:04 +01001509 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510 else
1511#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001515 }
Hanno Becker92231322018-01-03 15:32:51 +00001516#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1519 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 {
1521 int ret = 0;
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001524
Hanno Beckere7f2df02017-12-27 08:17:40 +00001525 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001526 transform->iv_enc, transform->iv_dec,
1527 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001528 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001529 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001533 }
1534 }
Hanno Becker92231322018-01-03 15:32:51 +00001535#else
1536 ((void) mac_dec);
1537 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001539
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001540#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1541 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001542 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001543 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001544 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001545 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001546 iv_copy_len );
1547 }
1548#endif
1549
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001550 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001551 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 return( ret );
1555 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001557 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001558 cipher_info ) ) != 0 )
1559 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 return( ret );
1562 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001565 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 return( ret );
1570 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001573 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 return( ret );
1578 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_CIPHER_MODE_CBC)
1581 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1584 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001587 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001588 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1591 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 return( ret );
1595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001599 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001601 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001603 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001607 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1608 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001610 if( deflateInit( &transform->ctx_deflate,
1611 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001612 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1615 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616 }
1617 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 return( 0 );
1621}
1622
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001623#if defined(MBEDTLS_SSL_PROTO_SSL3)
1624static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1625 unsigned char hash[36],
1626 size_t *hlen )
1627{
1628 mbedtls_md5_context md5;
1629 mbedtls_sha1_context sha1;
1630 unsigned char pad_1[48];
1631 unsigned char pad_2[48];
1632
1633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1634
1635 mbedtls_md5_init( &md5 );
1636 mbedtls_sha1_init( &sha1 );
1637
1638 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1639 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1640
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001641 mbedtls_platform_memset( pad_1, 0x36, 48 );
1642 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001643
1644 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1645 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1646 mbedtls_md5_finish_ret( &md5, hash );
1647
1648 mbedtls_md5_starts_ret( &md5 );
1649 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1650 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1651 mbedtls_md5_update_ret( &md5, hash, 16 );
1652 mbedtls_md5_finish_ret( &md5, hash );
1653
1654 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1655 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1656 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1657
1658 mbedtls_sha1_starts_ret( &sha1 );
1659 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1660 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1661 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1662 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1663
1664 *hlen = 36;
1665
1666 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1668
1669 mbedtls_md5_free( &md5 );
1670 mbedtls_sha1_free( &sha1 );
1671
1672 return;
1673}
1674#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1675
1676#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1677static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1678 unsigned char hash[36],
1679 size_t *hlen )
1680{
1681 mbedtls_md5_context md5;
1682 mbedtls_sha1_context sha1;
1683
1684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1685
1686 mbedtls_md5_init( &md5 );
1687 mbedtls_sha1_init( &sha1 );
1688
1689 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1690 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1691
1692 mbedtls_md5_finish_ret( &md5, hash );
1693 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1694
1695 *hlen = 36;
1696
1697 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1699
1700 mbedtls_md5_free( &md5 );
1701 mbedtls_sha1_free( &sha1 );
1702
1703 return;
1704}
1705#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1706
1707#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1708#if defined(MBEDTLS_SHA256_C)
1709static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1710 unsigned char hash[32],
1711 size_t *hlen )
1712{
1713 mbedtls_sha256_context sha256;
1714
1715 mbedtls_sha256_init( &sha256 );
1716
1717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1718
1719 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1720 mbedtls_sha256_finish_ret( &sha256, hash );
1721
1722 *hlen = 32;
1723
1724 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1726
1727 mbedtls_sha256_free( &sha256 );
1728
1729 return;
1730}
1731#endif /* MBEDTLS_SHA256_C */
1732
1733#if defined(MBEDTLS_SHA512_C)
1734static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1735 unsigned char hash[48],
1736 size_t *hlen )
1737{
1738 mbedtls_sha512_context sha512;
1739
1740 mbedtls_sha512_init( &sha512 );
1741
1742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1743
1744 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1745 mbedtls_sha512_finish_ret( &sha512, hash );
1746
1747 *hlen = 48;
1748
1749 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1751
1752 mbedtls_sha512_free( &sha512 );
1753
1754 return;
1755}
1756#endif /* MBEDTLS_SHA512_C */
1757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1758
Hanno Becker2f41b242019-08-15 17:29:43 +01001759int mbedtls_ssl_calc_verify( int minor_ver,
1760 mbedtls_md_type_t hash,
1761 mbedtls_ssl_context const *ssl,
1762 unsigned char *dst,
1763 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001765#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1766 (void) hash;
1767#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001768
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001769#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001770 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 else
1773#endif
1774#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001775 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001776 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 else
1778#endif
1779#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1780#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001781 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1782 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001783 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001784 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001785 }
1786 else
1787#endif
1788#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001789 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001790 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001791 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001792 }
1793 else
1794#endif
1795#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1796 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001797 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1798 }
1799
1800 return( 0 );
1801}
1802
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001804 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805 *
1806 * Parameters:
1807 * [in/out] handshake
1808 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1809 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001810 * [out] master
1811 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001812 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001813static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001814 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001815 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816{
1817 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001818
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001819/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1820/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1821/* (void) ssl; */
1822/* #endif */
1823
1824 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1825 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001826
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001827#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001828 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001829 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1831 return( 0 );
1832 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001833#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001834
1835 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1836 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001837
1838#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001839 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1840 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001841 {
1842 unsigned char session_hash[48];
1843 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001844
Hanno Becker2f41b242019-08-15 17:29:43 +01001845 mbedtls_ssl_calc_verify(
1846 mbedtls_ssl_get_minor_ver( ssl ),
1847 mbedtls_ssl_suite_get_mac( ciphersuite ),
1848 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001849
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001850 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1851 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001852
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001853 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1854 mbedtls_ssl_suite_get_mac( ciphersuite ),
1855 handshake->premaster, handshake->pmslen,
1856 "extended master secret",
1857 session_hash, hash_len,
1858 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001859 }
1860 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001861#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001862 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001863 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1864 mbedtls_ssl_suite_get_mac( ciphersuite ),
1865 handshake->premaster, handshake->pmslen,
1866 "master secret",
1867 handshake->randbytes, 64,
1868 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001869 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001870 if( ret != 0 )
1871 {
1872 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1873 return( ret );
1874 }
1875
1876 mbedtls_platform_zeroize( handshake->premaster,
1877 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001878
1879 return( 0 );
1880}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001881
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001882int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1883{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001884 volatile int ret;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001885
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001887 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001888 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001889 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001890 ssl->session_negotiate->master,
1891 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001892 if( ret != 0 )
1893 {
1894 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1895 return( ret );
1896 }
1897
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001898 /* Swap the client and server random values:
1899 * - MS derivation wanted client+server (RFC 5246 8.1)
1900 * - key derivation wants server+client (RFC 5246 6.3) */
1901 {
1902 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001903 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1904 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1905 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001906 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1907 }
1908
1909 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001910 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1912 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001913#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001914#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001915 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001916#endif
1917#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001918 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001919#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001920#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001921#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001922 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001923#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001925 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001926 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1927 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001928 if( ret == 0 )
1929 {
1930 mbedtls_platform_enforce_volatile_reads();
1931 if( ret == 0 )
1932 {
1933 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1934 }
1935 else
1936 {
1937 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1938 }
1939 }
1940 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001941 {
1942 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1943 return( ret );
1944 }
1945
1946 /* We no longer need Server/ClientHello.random values */
1947 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1948 sizeof( ssl->handshake->randbytes ) );
1949
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001950 /* Allocate compression buffer */
1951#if defined(MBEDTLS_ZLIB_SUPPORT)
1952 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1953 ssl->compress_buf == NULL )
1954 {
1955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1956 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1957 if( ssl->compress_buf == NULL )
1958 {
1959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001960 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001961 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1962 }
1963 }
1964#endif
1965
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1967
1968 return( 0 );
1969}
1970
Hanno Becker09d23642019-07-22 17:18:18 +01001971int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1972{
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001973 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker09d23642019-07-22 17:18:18 +01001974
1975 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1976 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001977#if defined(MBEDTLS_USE_TINYCRYPT)
1978 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001979 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001980 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001981 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1982 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1983 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1984 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1985 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001986 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001987 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1988 ssl->handshake->ecdh_privkey,
1989 ssl->handshake->premaster );
1990 if( ret == UECC_FAULT_DETECTED )
1991 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1992 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001993 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001994 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01001995
1996 ssl->handshake->pmslen = NUM_ECC_BYTES;
1997 }
1998 else
1999#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002000#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2001 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2002 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2003 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002004 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002005 ssl->handshake->premaster,
2006 MBEDTLS_PREMASTER_SIZE,
2007 &ssl->handshake->pmslen,
2008 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002009 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2010 if( ret == 0 )
2011 {
2012 mbedtls_platform_enforce_volatile_reads();
2013 if( ret == 0 )
2014 {
2015 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2016 }
2017 else
2018 {
2019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2020 return( ret );
2021 }
2022 }
2023 else
Hanno Becker09d23642019-07-22 17:18:18 +01002024 {
2025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2026 return( ret );
2027 }
2028
2029 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2030 }
2031 else
2032#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002033#if defined(MBEDTLS_ECDH_C) && \
2034 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2035 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2036 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2037 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002038 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2039 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2040 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2041 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2042 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2043 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2044 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2045 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2046 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002047 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002048 &ssl->handshake->pmslen,
2049 ssl->handshake->premaster,
2050 MBEDTLS_MPI_MAX_SIZE,
2051 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002052 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2053 if( ret == 0 )
2054 {
2055 mbedtls_platform_enforce_volatile_reads();
2056 if( ret == 0 )
2057 {
2058 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2059 }
2060 else
2061 {
2062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2063 return( ret );
2064 }
2065 }
2066 else
Hanno Becker09d23642019-07-22 17:18:18 +01002067 {
2068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2069 return( ret );
2070 }
2071
2072 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2073 }
2074 else
2075#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2076 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2077 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2078 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2079#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2080 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2081 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002082 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2083 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2084 if( ret == 0 )
2085 {
2086 mbedtls_platform_enforce_volatile_reads();
2087 if( ret == 0 )
2088 {
2089 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2090 }
2091 else
2092 {
2093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2094 return( ret );
2095 }
2096 }
2097 else
Hanno Becker09d23642019-07-22 17:18:18 +01002098 {
2099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2100 return( ret );
2101 }
2102 }
2103 else
2104#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2105#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2106 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2107 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2108 {
2109 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2110 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2111 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002112 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002113 if( ret == 0 )
2114 {
2115 mbedtls_platform_enforce_volatile_reads();
2116 if( ret == 0 )
2117 {
2118 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2119 }
2120 else
2121 {
2122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2123 return( ret );
2124 }
2125 }
2126 else
Hanno Becker09d23642019-07-22 17:18:18 +01002127 {
2128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2129 return( ret );
2130 }
2131 }
2132 else
2133#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2134#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2135 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2136 == MBEDTLS_KEY_EXCHANGE_RSA )
2137 {
2138 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002139 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002140 * ssl_rsa_generate_partial_pms(). Only the
2141 * PMS length needs to be set. */
2142 ssl->handshake->pmslen = 48;
2143 }
2144 else
2145#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2146 {
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2149 }
2150
2151 return( 0 );
2152}
2153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2155int 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 +02002156{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 unsigned char *p = ssl->handshake->premaster;
2158 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002159 const unsigned char *psk = ssl->conf->psk;
2160 size_t psk_len = ssl->conf->psk_len;
2161
2162 /* If the psk callback was called, use its result */
2163 if( ssl->handshake->psk != NULL )
2164 {
2165 psk = ssl->handshake->psk;
2166 psk_len = ssl->handshake->psk_len;
2167 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002168
2169 /*
2170 * PMS = struct {
2171 * opaque other_secret<0..2^16-1>;
2172 * opaque psk<0..2^16-1>;
2173 * };
2174 * with "other_secret" depending on the particular key exchange
2175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2177 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002178 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002179 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002181
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002182 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002183
2184 if( end < p || (size_t)( end - p ) < psk_len )
2185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2186
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002187 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002189 }
2190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2192#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2193 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002194 {
2195 /*
2196 * other_secret already set by the ClientKeyExchange message,
2197 * and is 48 bytes long
2198 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002199 if( end - p < 2 )
2200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2201
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002202 *p++ = 0;
2203 *p++ = 48;
2204 p += 48;
2205 }
2206 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2208#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2209 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002210 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002211 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002212 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002213
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002214 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002216 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002217 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002218 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 return( ret );
2222 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002223 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002224 p += len;
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002227 }
2228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2230#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2231 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002232 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002233 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 size_t zlen;
2235
Hanno Becker982da7e2019-09-02 09:47:39 +01002236#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002237 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2238 ssl->handshake->ecdh_privkey,
2239 p + 2 );
2240 if( ret == UECC_FAULT_DETECTED )
2241 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2242 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002243 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002244
2245 zlen = NUM_ECC_BYTES;
2246#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002248 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002249 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002250 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 return( ret );
2254 }
2255
Hanno Becker982da7e2019-09-02 09:47:39 +01002256 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2257 MBEDTLS_DEBUG_ECDH_Z );
2258#endif /* MBEDTLS_USE_TINYCRYPT */
2259
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002260 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002261 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002262 }
2263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002268 }
2269
2270 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002271 if( end - p < 2 )
2272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002273
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002274 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002275
2276 if( end < p || (size_t)( end - p ) < psk_len )
2277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2278
Teppo Järvelin91d79382019-10-02 09:09:31 +03002279 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002280 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002281
2282 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2283
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002284 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2285
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002286 return( 0 );
2287}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002291/*
2292 * SSLv3.0 MAC functions
2293 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002294#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002295static void ssl_mac( mbedtls_md_context_t *md_ctx,
2296 const unsigned char *secret,
2297 const unsigned char *buf, size_t len,
2298 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002299 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002300{
2301 unsigned char header[11];
2302 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002303 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2305 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002306
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002307 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002309 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002310 else
Paul Bakker68884e32013-01-07 18:20:04 +01002311 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Teppo Järvelin91d79382019-10-02 09:09:31 +03002313 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002314 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002315 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002317 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 mbedtls_md_starts( md_ctx );
2319 mbedtls_md_update( md_ctx, secret, md_size );
2320 mbedtls_md_update( md_ctx, padding, padlen );
2321 mbedtls_md_update( md_ctx, header, 11 );
2322 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002323 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002325 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 mbedtls_md_starts( md_ctx );
2327 mbedtls_md_update( md_ctx, secret, md_size );
2328 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002329 mbedtls_md_update( md_ctx, out, md_size );
2330 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002333
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002334/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002335 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002336#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002337 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2338 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2339 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2340/* This function makes sure every byte in the memory region is accessed
2341 * (in ascending addresses order) */
2342static void ssl_read_memory( unsigned char *p, size_t len )
2343{
2344 unsigned char acc = 0;
2345 volatile unsigned char force;
2346
2347 for( ; len != 0; p++, len-- )
2348 acc ^= *p;
2349
2350 force = acc;
2351 (void) force;
2352}
2353#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2354
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002355/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002357 */
Hanno Becker3307b532017-12-27 21:37:21 +00002358
Hanno Beckera5a2b082019-05-15 14:03:01 +01002359#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002360/* This functions transforms a DTLS plaintext fragment and a record content
2361 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002362 *
2363 * struct {
2364 * opaque content[DTLSPlaintext.length];
2365 * ContentType real_type;
2366 * uint8 zeros[length_of_padding];
2367 * } DTLSInnerPlaintext;
2368 *
2369 * Input:
2370 * - `content`: The beginning of the buffer holding the
2371 * plaintext to be wrapped.
2372 * - `*content_size`: The length of the plaintext in Bytes.
2373 * - `max_len`: The number of Bytes available starting from
2374 * `content`. This must be `>= *content_size`.
2375 * - `rec_type`: The desired record content type.
2376 *
2377 * Output:
2378 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2379 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2380 *
2381 * Returns:
2382 * - `0` on success.
2383 * - A negative error code if `max_len` didn't offer enough space
2384 * for the expansion.
2385 */
2386static int ssl_cid_build_inner_plaintext( unsigned char *content,
2387 size_t *content_size,
2388 size_t remaining,
2389 uint8_t rec_type )
2390{
2391 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002392 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2393 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2394 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002395
2396 /* Write real content type */
2397 if( remaining == 0 )
2398 return( -1 );
2399 content[ len ] = rec_type;
2400 len++;
2401 remaining--;
2402
2403 if( remaining < pad )
2404 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002405 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002406 len += pad;
2407 remaining -= pad;
2408
2409 *content_size = len;
2410 return( 0 );
2411}
2412
Hanno Becker7dc25772019-05-20 15:08:01 +01002413/* This function parses a DTLSInnerPlaintext structure.
2414 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002415static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2416 size_t *content_size,
2417 uint8_t *rec_type )
2418{
2419 size_t remaining = *content_size;
2420
2421 /* Determine length of padding by skipping zeroes from the back. */
2422 do
2423 {
2424 if( remaining == 0 )
2425 return( -1 );
2426 remaining--;
2427 } while( content[ remaining ] == 0 );
2428
2429 *content_size = remaining;
2430 *rec_type = content[ remaining ];
2431
2432 return( 0 );
2433}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002434#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002435
Hanno Becker99abf512019-05-20 14:50:53 +01002436/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002437 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002438static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002439 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002440 mbedtls_record *rec )
2441{
Hanno Becker99abf512019-05-20 14:50:53 +01002442 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002443 *
2444 * additional_data = seq_num + TLSCompressed.type +
2445 * TLSCompressed.version + TLSCompressed.length;
2446 *
Hanno Becker99abf512019-05-20 14:50:53 +01002447 * For the CID extension, this is extended as follows
2448 * (quoting draft-ietf-tls-dtls-connection-id-05,
2449 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002450 *
2451 * additional_data = seq_num + DTLSPlaintext.type +
2452 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002453 * cid +
2454 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002455 * length_of_DTLSInnerPlaintext;
2456 */
2457
Teppo Järvelin91d79382019-10-02 09:09:31 +03002458 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002459 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002460 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002461
Hanno Beckera5a2b082019-05-15 14:03:01 +01002462#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002463 if( rec->cid_len != 0 )
2464 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002465 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002466 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002467 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2468 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002469 *add_data_len = 13 + 1 + rec->cid_len;
2470 }
2471 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002472#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002473 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002474 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 *add_data_len = 13;
2476 }
Hanno Becker3307b532017-12-27 21:37:21 +00002477}
2478
Hanno Becker611a83b2018-01-03 14:27:32 +00002479int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2480 mbedtls_ssl_transform *transform,
2481 mbedtls_record *rec,
2482 int (*f_rng)(void *, unsigned char *, size_t),
2483 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002486 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002487 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002488 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002489 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002490 size_t post_avail;
2491
2492 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002493#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002494 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002495 ((void) ssl);
2496#endif
2497
2498 /* The PRNG is used for dynamic IV generation that's used
2499 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2500#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2501 ( defined(MBEDTLS_AES_C) || \
2502 defined(MBEDTLS_ARIA_C) || \
2503 defined(MBEDTLS_CAMELLIA_C) ) && \
2504 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2505 ((void) f_rng);
2506 ((void) p_rng);
2507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Hanno Becker3307b532017-12-27 21:37:21 +00002511 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002512 {
Hanno Becker3307b532017-12-27 21:37:21 +00002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2515 }
Hanno Becker505089d2019-05-01 09:45:57 +01002516 if( rec == NULL
2517 || rec->buf == NULL
2518 || rec->buf_len < rec->data_offset
2519 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002520#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002521 || rec->cid_len != 0
2522#endif
2523 )
Hanno Becker3307b532017-12-27 21:37:21 +00002524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002527 }
2528
Hanno Becker3307b532017-12-27 21:37:21 +00002529 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002530 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002532 data, rec->data_len );
2533
2534 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2535
2536 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2537 {
2538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2539 (unsigned) rec->data_len,
2540 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2542 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002543
Hanno Beckera5a2b082019-05-15 14:03:01 +01002544#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002545 /*
2546 * Add CID information
2547 */
2548 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002549 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2550 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002551 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002552
2553 if( rec->cid_len != 0 )
2554 {
2555 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002556 * Wrap plaintext into DTLSInnerPlaintext structure.
2557 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002558 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002559 * Note that this changes `rec->data_len`, and hence
2560 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002561 */
2562 if( ssl_cid_build_inner_plaintext( data,
2563 &rec->data_len,
2564 post_avail,
2565 rec->type ) != 0 )
2566 {
2567 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2568 }
2569
2570 rec->type = MBEDTLS_SSL_MSG_CID;
2571 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002572#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002573
Hanno Becker92c930f2019-04-29 17:31:37 +01002574 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2575
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002577 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002579#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 if( mode == MBEDTLS_MODE_STREAM ||
2581 ( mode == MBEDTLS_MODE_CBC
2582#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002583 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002584#endif
2585 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 {
Hanno Becker3307b532017-12-27 21:37:21 +00002587 if( post_avail < transform->maclen )
2588 {
2589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2590 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2591 }
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002594 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2595 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002596 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002597 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002598 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2599 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002600 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002601 }
2602 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2605 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002606 if( mbedtls_ssl_ver_geq(
2607 mbedtls_ssl_transform_get_minor_ver( transform ),
2608 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002609 {
Hanno Becker992b6872017-11-09 18:57:39 +00002610 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2611
Hanno Beckere83efe62019-04-29 13:52:53 +01002612 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002613
Hanno Becker3307b532017-12-27 21:37:21 +00002614 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002615 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002616 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2617 data, rec->data_len );
2618 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2619 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2620
Teppo Järvelin91d79382019-10-02 09:09:31 +03002621 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002622 }
2623 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002624#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002628 }
2629
Hanno Becker3307b532017-12-27 21:37:21 +00002630 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2631 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002632
Hanno Becker3307b532017-12-27 21:37:21 +00002633 rec->data_len += transform->maclen;
2634 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002635 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002636 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002637#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 /*
2640 * Encrypt
2641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2643 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002645 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002646 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002648 "including %d bytes of padding",
2649 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Hanno Becker3307b532017-12-27 21:37:21 +00002651 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2652 transform->iv_enc, transform->ivlen,
2653 data, rec->data_len,
2654 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002657 return( ret );
2658 }
2659
Hanno Becker3307b532017-12-27 21:37:21 +00002660 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
Paul Bakker68884e32013-01-07 18:20:04 +01002666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002668
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002669#if defined(MBEDTLS_GCM_C) || \
2670 defined(MBEDTLS_CCM_C) || \
2671 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002673 mode == MBEDTLS_MODE_CCM ||
2674 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002675 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002676 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002677 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002678 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002679
Hanno Becker3307b532017-12-27 21:37:21 +00002680 /* Check that there's space for both the authentication tag
2681 * and the explicit IV before and after the record content. */
2682 if( post_avail < transform->taglen ||
2683 rec->data_offset < explicit_iv_len )
2684 {
2685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2686 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2687 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688
Paul Bakker68884e32013-01-07 18:20:04 +01002689 /*
2690 * Generate IV
2691 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002692 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2693 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002694 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002695 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2696 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002697 explicit_iv_len );
2698 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002699 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002700 }
2701 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2702 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002703 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002704 unsigned char i;
2705
Teppo Järvelin91d79382019-10-02 09:09:31 +03002706 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002707
2708 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002709 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002710 }
2711 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002712 {
2713 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002716 }
2717
Hanno Beckere83efe62019-04-29 13:52:53 +01002718 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002719
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2721 iv, transform->ivlen );
2722 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002723 data - explicit_iv_len, explicit_iv_len );
2724 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002725 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002727 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002728 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002729
Paul Bakker68884e32013-01-07 18:20:04 +01002730 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002731 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002732 */
Hanno Becker3307b532017-12-27 21:37:21 +00002733
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002734 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002735 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002736 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002737 data, rec->data_len, /* source */
2738 data, &rec->data_len, /* destination */
2739 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002742 return( ret );
2743 }
2744
Hanno Becker3307b532017-12-27 21:37:21 +00002745 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2746 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002747
Hanno Becker3307b532017-12-27 21:37:21 +00002748 rec->data_len += transform->taglen + explicit_iv_len;
2749 rec->data_offset -= explicit_iv_len;
2750 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002751 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2755#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002756 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002759 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002760 size_t padlen, i;
2761 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002762
Hanno Becker3307b532017-12-27 21:37:21 +00002763 /* Currently we're always using minimal padding
2764 * (up to 255 bytes would be allowed). */
2765 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2766 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 padlen = 0;
2768
Hanno Becker3307b532017-12-27 21:37:21 +00002769 /* Check there's enough space in the buffer for the padding. */
2770 if( post_avail < padlen + 1 )
2771 {
2772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2773 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2774 }
2775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002777 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Hanno Becker3307b532017-12-27 21:37:21 +00002779 rec->data_len += padlen + 1;
2780 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002783 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2785 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002786 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002787 if( mbedtls_ssl_ver_geq(
2788 mbedtls_ssl_transform_get_minor_ver( transform ),
2789 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002790 {
Hanno Becker3307b532017-12-27 21:37:21 +00002791 if( f_rng == NULL )
2792 {
2793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2795 }
2796
2797 if( rec->data_offset < transform->ivlen )
2798 {
2799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2800 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2801 }
2802
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 /*
2804 * Generate IV
2805 */
Hanno Becker3307b532017-12-27 21:37:21 +00002806 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002807 if( ret != 0 )
2808 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002809
Teppo Järvelin91d79382019-10-02 09:09:31 +03002810 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002811 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002818 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002819 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Hanno Becker3307b532017-12-27 21:37:21 +00002821 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2822 transform->iv_enc,
2823 transform->ivlen,
2824 data, rec->data_len,
2825 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002828 return( ret );
2829 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002830
Hanno Becker3307b532017-12-27 21:37:21 +00002831 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002835 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002838 if( mbedtls_ssl_ver_lt(
2839 mbedtls_ssl_transform_get_minor_ver( transform ),
2840 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002841 {
2842 /*
2843 * Save IV in SSL3 and TLS1
2844 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002845 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002846 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 }
Hanno Becker3307b532017-12-27 21:37:21 +00002848 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002849#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002850 {
2851 data -= transform->ivlen;
2852 rec->data_offset -= transform->ivlen;
2853 rec->data_len += transform->ivlen;
2854 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002857 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002858 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002859 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2860
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002861 /*
2862 * MAC(MAC_write_key, seq_num +
2863 * TLSCipherText.type +
2864 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002865 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002866 * IV + // except for TLS 1.0
2867 * ENC(content + padding + padding_length));
2868 */
Hanno Becker3307b532017-12-27 21:37:21 +00002869
2870 if( post_avail < transform->maclen)
2871 {
2872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2873 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2874 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875
Hanno Beckere83efe62019-04-29 13:52:53 +01002876 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002879 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002880 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002881
Hanno Becker3307b532017-12-27 21:37:21 +00002882 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002883 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002884 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2885 data, rec->data_len );
2886 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2887 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002888
Teppo Järvelin91d79382019-10-02 09:09:31 +03002889 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002890
Hanno Becker3307b532017-12-27 21:37:21 +00002891 rec->data_len += transform->maclen;
2892 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002893 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002894 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002899 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002905 /* Make extra sure authentication was performed, exactly once */
2906 if( auth_done != 1 )
2907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002910 }
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
2914 return( 0 );
2915}
2916
Hanno Becker40478be2019-07-12 08:23:59 +01002917int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002918 mbedtls_ssl_transform *transform,
2919 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002920{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002921 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002923 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002924#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002925 size_t padlen = 0, correct = 1;
2926#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002927 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002928 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002929 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002930
Hanno Becker611a83b2018-01-03 14:27:32 +00002931#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002932 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002933 ((void) ssl);
2934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002937 if( rec == NULL ||
2938 rec->buf == NULL ||
2939 rec->buf_len < rec->data_offset ||
2940 rec->buf_len - rec->data_offset < rec->data_len )
2941 {
2942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002944 }
2945
Hanno Becker4c6876b2017-12-27 21:28:58 +00002946 data = rec->buf + rec->data_offset;
2947 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Hanno Beckera5a2b082019-05-15 14:03:01 +01002949#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002950 /*
2951 * Match record's CID with incoming CID.
2952 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002953 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002954 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002955 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002956 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002957 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002958#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2961 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002962 {
2963 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002964 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2965 transform->iv_dec,
2966 transform->ivlen,
2967 data, rec->data_len,
2968 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002971 return( ret );
2972 }
2973
Hanno Becker4c6876b2017-12-27 21:28:58 +00002974 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
Paul Bakker68884e32013-01-07 18:20:04 +01002980 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002982#if defined(MBEDTLS_GCM_C) || \
2983 defined(MBEDTLS_CCM_C) || \
2984 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002986 mode == MBEDTLS_MODE_CCM ||
2987 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002988 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002989 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002990 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002992 /*
2993 * Prepare IV from explicit and implicit data.
2994 */
2995
2996 /* Check that there's enough space for the explicit IV
2997 * (at the beginning of the record) and the MAC (at the
2998 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002999 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003002 "+ taglen (%d)", rec->data_len,
3003 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003005 }
Paul Bakker68884e32013-01-07 18:20:04 +01003006
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003007#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003008 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3009 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003010 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003011
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003012 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003013 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003014 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003015 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003016 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003017 else
3018#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3019#if defined(MBEDTLS_CHACHAPOLY_C)
3020 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003021 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003022 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003023 unsigned char i;
3024
Teppo Järvelin91d79382019-10-02 09:09:31 +03003025 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003026
3027 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003028 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003029 }
3030 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003031#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003032 {
3033 /* Reminder if we ever add an AEAD mode with a different size */
3034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3036 }
3037
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003038 /* Group changes to data, data_len, and add_data, because
3039 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003040 data += explicit_iv_len;
3041 rec->data_offset += explicit_iv_len;
3042 rec->data_len -= explicit_iv_len + transform->taglen;
3043
Hanno Beckere83efe62019-04-29 13:52:53 +01003044 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003045 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003046 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003048 /* Because of the check above, we know that there are
3049 * explicit_iv_len Bytes preceeding data, and taglen
3050 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003051 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003052 * mbedtls_cipher_auth_decrypt() below. */
3053
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003054 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003055 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003056 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003057
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003058 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003059 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003060 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003061 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3062 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003063 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003064 data, rec->data_len,
3065 data, &olen,
3066 data + rec->data_len,
3067 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3072 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003073
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003074 return( ret );
3075 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003076 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003077
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003078 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003079 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003083 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3087#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003088 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003091 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003092
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 /*
Paul Bakker45829992013-01-03 14:52:21 +01003094 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003097 if( mbedtls_ssl_ver_geq(
3098 mbedtls_ssl_transform_get_minor_ver( transform ),
3099 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003100 {
3101 /* The ciphertext is prefixed with the CBC IV. */
3102 minlen += transform->ivlen;
3103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003104#endif
Paul Bakker45829992013-01-03 14:52:21 +01003105
Hanno Becker4c6876b2017-12-27 21:28:58 +00003106 /* Size considerations:
3107 *
3108 * - The CBC cipher text must not be empty and hence
3109 * at least of size transform->ivlen.
3110 *
3111 * Together with the potential IV-prefix, this explains
3112 * the first of the two checks below.
3113 *
3114 * - The record must contain a MAC, either in plain or
3115 * encrypted, depending on whether Encrypt-then-MAC
3116 * is used or not.
3117 * - If it is, the message contains the IV-prefix,
3118 * the CBC ciphertext, and the MAC.
3119 * - If it is not, the padded plaintext, and hence
3120 * the CBC ciphertext, has at least length maclen + 1
3121 * because there is at least the padding length byte.
3122 *
3123 * As the CBC ciphertext is not empty, both cases give the
3124 * lower bound minlen + maclen + 1 on the record size, which
3125 * we test for in the second check below.
3126 */
3127 if( rec->data_len < minlen + transform->ivlen ||
3128 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003131 "+ 1 ) ( + expl IV )", rec->data_len,
3132 transform->ivlen,
3133 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003135 }
3136
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003137 /*
3138 * Authenticate before decrypt if enabled
3139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003141 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003142 {
Hanno Becker992b6872017-11-09 18:57:39 +00003143 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003146
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003147 /* Update data_len in tandem with add_data.
3148 *
3149 * The subtraction is safe because of the previous check
3150 * data_len >= minlen + maclen + 1.
3151 *
3152 * Afterwards, we know that data + data_len is followed by at
3153 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003154 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003155 *
3156 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003157 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003158 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003159
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003160 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003161 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3162 add_data_len );
3163 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3164 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003165 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3166 data, rec->data_len );
3167 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3168 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003169
Hanno Becker4c6876b2017-12-27 21:28:58 +00003170 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3171 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003172 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003173 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003174
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003175 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003176 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003177 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003181 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003182 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185
3186 /*
3187 * Check length sanity
3188 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003189
3190 /* We know from above that data_len > minlen >= 0,
3191 * so the following check in particular implies that
3192 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003193 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003196 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003198 }
3199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003201 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003202 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003203 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003204 if( mbedtls_ssl_ver_geq(
3205 mbedtls_ssl_transform_get_minor_ver( transform ),
3206 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003207 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003208 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003209 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003210
Hanno Becker4c6876b2017-12-27 21:28:58 +00003211 data += transform->ivlen;
3212 rec->data_offset += transform->ivlen;
3213 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003214 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003217 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3218
Hanno Becker4c6876b2017-12-27 21:28:58 +00003219 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3220 transform->iv_dec, transform->ivlen,
3221 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003224 return( ret );
3225 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003226
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003227 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003228 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003232 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003235 if( mbedtls_ssl_ver_lt(
3236 mbedtls_ssl_transform_get_minor_ver( transform ),
3237 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003238 {
3239 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003240 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3241 * records is equivalent to CBC decryption of the concatenation
3242 * of the records; in other words, IVs are maintained across
3243 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003244 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003245 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003246 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
Hanno Becker4c6876b2017-12-27 21:28:58 +00003250 /* Safe since data_len >= minlen + maclen + 1, so after having
3251 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003252 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3253 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003254 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003255
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 if( auth_done == 1 )
3257 {
3258 correct *= ( rec->data_len >= padlen + 1 );
3259 padlen *= ( rec->data_len >= padlen + 1 );
3260 }
3261 else
Paul Bakker45829992013-01-03 14:52:21 +01003262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003264 if( rec->data_len < transform->maclen + padlen + 1 )
3265 {
3266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3267 rec->data_len,
3268 transform->maclen,
3269 padlen + 1 ) );
3270 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003271#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003272
3273 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3274 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
Hanno Becker4c6876b2017-12-27 21:28:58 +00003277 padlen++;
3278
3279 /* Regardless of the validity of the padding,
3280 * we have data_len >= padlen here. */
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003283 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3284 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_DEBUG_ALL)
3289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003290 "should be no more than %d",
3291 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003292#endif
Paul Bakker45829992013-01-03 14:52:21 +01003293 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 }
3295 }
3296 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3298#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3299 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003300 if( mbedtls_ssl_ver_gt(
3301 mbedtls_ssl_transform_get_minor_ver( transform ),
3302 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003304 /* The padding check involves a series of up to 256
3305 * consecutive memory reads at the end of the record
3306 * plaintext buffer. In order to hide the length and
3307 * validity of the padding, always perform exactly
3308 * `min(256,plaintext_len)` reads (but take into account
3309 * only the last `padlen` bytes for the padding check). */
3310 size_t pad_count = 0;
3311 size_t real_count = 0;
3312 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003313
Hanno Becker4c6876b2017-12-27 21:28:58 +00003314 /* Index of first padding byte; it has been ensured above
3315 * that the subtraction is safe. */
3316 size_t const padding_idx = rec->data_len - padlen;
3317 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3318 size_t const start_idx = rec->data_len - num_checks;
3319 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003320
Hanno Becker4c6876b2017-12-27 21:28:58 +00003321 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003322 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 real_count |= ( idx >= padding_idx );
3324 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003325 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003326 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003329 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003331#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003332 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003334 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3336 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003340 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003341
Hanno Becker4c6876b2017-12-27 21:28:58 +00003342 /* If the padding was found to be invalid, padlen == 0
3343 * and the subtraction is safe. If the padding was found valid,
3344 * padlen hasn't been changed and the previous assertion
3345 * data_len >= padlen still holds. */
3346 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003348 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003350 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003356#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003358 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
3361 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003362 * Authenticate if not done yet.
3363 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003365#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003366 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003367 {
Hanno Becker992b6872017-11-09 18:57:39 +00003368 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003369
Hanno Becker4c6876b2017-12-27 21:28:58 +00003370 /* If the initial value of padlen was such that
3371 * data_len < maclen + padlen + 1, then padlen
3372 * got reset to 1, and the initial check
3373 * data_len >= minlen + maclen + 1
3374 * guarantees that at this point we still
3375 * have at least data_len >= maclen.
3376 *
3377 * If the initial value of padlen was such that
3378 * data_len >= maclen + padlen + 1, then we have
3379 * subtracted either padlen + 1 (if the padding was correct)
3380 * or 0 (if the padding was incorrect) since then,
3381 * hence data_len >= maclen in any case.
3382 */
3383 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003384 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003387 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3388 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003389 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003390 ssl_mac( &transform->md_ctx_dec,
3391 transform->mac_dec,
3392 data, rec->data_len,
3393 rec->ctr, rec->type,
3394 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003395 }
3396 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3398#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3399 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003400 if( mbedtls_ssl_ver_gt(
3401 mbedtls_ssl_transform_get_minor_ver( transform ),
3402 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003403 {
3404 /*
3405 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003406 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003407 *
3408 * Known timing attacks:
3409 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3410 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003411 * To compensate for different timings for the MAC calculation
3412 * depending on how much padding was removed (which is determined
3413 * by padlen), process extra_run more blocks through the hash
3414 * function.
3415 *
3416 * The formula in the paper is
3417 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3418 * where L1 is the size of the header plus the decrypted message
3419 * plus CBC padding and L2 is the size of the header plus the
3420 * decrypted message. This is for an underlying hash function
3421 * with 64-byte blocks.
3422 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3423 * correctly. We round down instead of up, so -56 is the correct
3424 * value for our calculations instead of -55.
3425 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003426 * Repeat the formula rather than defining a block_size variable.
3427 * This avoids requiring division by a variable at runtime
3428 * (which would be marginally less efficient and would require
3429 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003430 */
3431 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003432 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003433
3434 /*
3435 * The next two sizes are the minimum and maximum values of
3436 * in_msglen over all padlen values.
3437 *
3438 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003439 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003440 *
3441 * Note that max_len + maclen is never more than the buffer
3442 * length, as we previously did in_msglen -= maclen too.
3443 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003444 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003445 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3446
Hanno Becker4c6876b2017-12-27 21:28:58 +00003447 memset( tmp, 0, sizeof( tmp ) );
3448
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003449 switch( mbedtls_md_get_type(
3450 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003451 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003452#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3453 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003454 case MBEDTLS_MD_MD5:
3455 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003456 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003457 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003458 extra_run =
3459 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3460 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003461 break;
3462#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003463#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003464 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003466 extra_run =
3467 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3468 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003469 break;
3470#endif
3471 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003473 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3474 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003475
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003476 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003477
Hanno Beckere83efe62019-04-29 13:52:53 +01003478 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3479 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003480 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3481 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003482 /* Make sure we access everything even when padlen > 0. This
3483 * makes the synchronisation requirements for just-in-time
3484 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003485 ssl_read_memory( data + rec->data_len, padlen );
3486 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003487
3488 /* Call mbedtls_md_process at least once due to cache attacks
3489 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003490 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003491 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003492
Hanno Becker4c6876b2017-12-27 21:28:58 +00003493 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003494
3495 /* Make sure we access all the memory that could contain the MAC,
3496 * before we check it in the next code block. This makes the
3497 * synchronisation requirements for just-in-time Prime+Probe
3498 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003499 ssl_read_memory( data + min_len,
3500 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003501 }
3502 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3504 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3507 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003510#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003511 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3512 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003515 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003516 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518#if defined(MBEDTLS_SSL_DEBUG_ALL)
3519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003520#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003521 correct = 0;
3522 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003523 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003524 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003525
3526 /*
3527 * Finally check the correct flag
3528 */
3529 if( correct == 0 )
3530 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003531#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003532
3533 /* Make extra sure authentication was performed, exactly once */
3534 if( auth_done != 1 )
3535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Hanno Beckera5a2b082019-05-15 14:03:01 +01003540#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003541 if( rec->cid_len != 0 )
3542 {
3543 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3544 &rec->type );
3545 if( ret != 0 )
3546 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3547 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003548#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
3552 return( 0 );
3553}
3554
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003555#undef MAC_NONE
3556#undef MAC_PLAINTEXT
3557#undef MAC_CIPHERTEXT
3558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560/*
3561 * Compression/decompression functions
3562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003564{
3565 int ret;
3566 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003567 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003569 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003573 if( len_pre == 0 )
3574 return( 0 );
3575
Teppo Järvelin91d79382019-10-02 09:09:31 +03003576 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579 ssl->out_msglen ) );
3580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582 ssl->out_msg, ssl->out_msglen );
3583
Paul Bakker48916f92012-09-16 19:57:18 +00003584 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3585 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3586 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003587 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588
Paul Bakker48916f92012-09-16 19:57:18 +00003589 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590 if( ret != Z_OK )
3591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3593 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003594 }
3595
Angus Grattond8213d02016-05-25 20:56:48 +10003596 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003597 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600 ssl->out_msglen ) );
3601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003603 ssl->out_msg, ssl->out_msglen );
3604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003606
3607 return( 0 );
3608}
3609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003611{
3612 int ret;
3613 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003614 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003615 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003616 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003619
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003620 if( len_pre == 0 )
3621 return( 0 );
3622
Teppo Järvelin91d79382019-10-02 09:09:31 +03003623 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626 ssl->in_msglen ) );
3627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003629 ssl->in_msg, ssl->in_msglen );
3630
Paul Bakker48916f92012-09-16 19:57:18 +00003631 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3632 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3633 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003634 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003635 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003636
Paul Bakker48916f92012-09-16 19:57:18 +00003637 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638 if( ret != Z_OK )
3639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3641 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003642 }
3643
Angus Grattond8213d02016-05-25 20:56:48 +10003644 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003645 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648 ssl->in_msglen ) );
3649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003651 ssl->in_msg, ssl->in_msglen );
3652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003654
3655 return( 0 );
3656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3660static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#if defined(MBEDTLS_SSL_PROTO_DTLS)
3663static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003664{
3665 /* If renegotiation is not enforced, retransmit until we would reach max
3666 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003667 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003668 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003669 uint32_t ratio =
3670 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3671 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003672 unsigned char doublings = 1;
3673
3674 while( ratio != 0 )
3675 {
3676 ++doublings;
3677 ratio >>= 1;
3678 }
3679
3680 if( ++ssl->renego_records_seen > doublings )
3681 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003683 return( 0 );
3684 }
3685 }
3686
3687 return( ssl_write_hello_request( ssl ) );
3688}
3689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003691
Paul Bakker5121ce52009-01-03 21:22:43 +00003692/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003693 * Fill the input message buffer by appending data to it.
3694 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003695 *
3696 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3697 * available (from this read and/or a previous one). Otherwise, an error code
3698 * is returned (possibly EOF or WANT_READ).
3699 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003700 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3701 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3702 * since we always read a whole datagram at once.
3703 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003704 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003705 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708{
Paul Bakker23986e52011-04-24 08:57:21 +00003709 int ret;
3710 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Hanno Beckera58a8962019-06-13 16:11:15 +01003714 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3715 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003718 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003720 }
3721
Angus Grattond8213d02016-05-25 20:56:48 +10003722 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003726 }
3727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003731 uint32_t timeout;
3732
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003733 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003734 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3735 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3738 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3740 }
3741
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003742 /*
3743 * The point is, we need to always read a full datagram at once, so we
3744 * sometimes read more then requested, and handle the additional data.
3745 * It could be the rest of the current record (while fetching the
3746 * header) and/or some other records in the same datagram.
3747 */
3748
3749 /*
3750 * Move to the next record in the already read datagram if applicable
3751 */
3752 if( ssl->next_record_offset != 0 )
3753 {
3754 if( ssl->in_left < ssl->next_record_offset )
3755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003758 }
3759
3760 ssl->in_left -= ssl->next_record_offset;
3761
3762 if( ssl->in_left != 0 )
3763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003765 ssl->next_record_offset ) );
3766 memmove( ssl->in_hdr,
3767 ssl->in_hdr + ssl->next_record_offset,
3768 ssl->in_left );
3769 }
3770
3771 ssl->next_record_offset = 0;
3772 }
3773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003776
3777 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003778 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003779 */
3780 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003783 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003784 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785
3786 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003787 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003788 * are not at the beginning of a new record, the caller did something
3789 * wrong.
3790 */
3791 if( ssl->in_left != 0 )
3792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003795 }
3796
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003797 /*
3798 * Don't even try to read if time's out already.
3799 * This avoids by-passing the timer when repeatedly receiving messages
3800 * that will end up being dropped.
3801 */
3802 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003805 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003806 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003807 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003808 {
Angus Grattond8213d02016-05-25 20:56:48 +10003809 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003812 timeout = ssl->handshake->retransmit_timeout;
3813 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003814 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003817
Hanno Beckera58a8962019-06-13 16:11:15 +01003818 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3819 {
3820 ret = mbedtls_ssl_get_recv_timeout( ssl )
3821 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3822 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003823 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003824 {
3825 ret = mbedtls_ssl_get_recv( ssl )
3826 ( ssl->p_bio, ssl->in_hdr, len );
3827 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
3831 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003833 }
3834
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003835 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003838 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003841 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003842 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003845 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003846 }
3847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 return( ret );
3852 }
3853
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003854 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003857 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3858 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003860 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003861 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003864 return( ret );
3865 }
3866
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003867 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003868 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003870 }
3871
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 if( ret < 0 )
3873 return( ret );
3874
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003875 ssl->in_left = ret;
3876 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003877 MBEDTLS_SSL_TRANSPORT_ELSE
3878#endif /* MBEDTLS_SSL_PROTO_DTLS */
3879#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003882 ssl->in_left, nb_want ) );
3883
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003884 while( ssl->in_left < nb_want )
3885 {
3886 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003887
3888 if( ssl_check_timer( ssl ) != 0 )
3889 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3890 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003891 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003892 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003893 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003894 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3895 ssl->in_hdr + ssl->in_left, len,
3896 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003897 }
3898 else
3899 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003900 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003901 ssl->in_hdr + ssl->in_left, len );
3902 }
3903 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003906 ssl->in_left, nb_want ) );
3907 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003908
3909 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003911
3912 if( ret < 0 )
3913 return( ret );
3914
mohammad160352aecb92018-03-28 23:41:40 -07003915 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003916 {
Darryl Green11999bb2018-03-13 15:22:58 +00003917 MBEDTLS_SSL_DEBUG_MSG( 1,
3918 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003919 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3921 }
3922
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003923 ssl->in_left += ret;
3924 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003926#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003929
3930 return( 0 );
3931}
3932
3933/*
3934 * Flush any data not yet written
3935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003937{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003938 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003939 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Hanno Beckera58a8962019-06-13 16:11:15 +01003943 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003946 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003948 }
3949
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003950 /* Avoid incrementing counter if data is flushed */
3951 if( ssl->out_left == 0 )
3952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003954 return( 0 );
3955 }
3956
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 while( ssl->out_left > 0 )
3958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003960 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Hanno Becker2b1e3542018-08-06 11:19:13 +01003962 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003963 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
3967 if( ret <= 0 )
3968 return( ret );
3969
mohammad160352aecb92018-03-28 23:41:40 -07003970 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003971 {
Darryl Green11999bb2018-03-13 15:22:58 +00003972 MBEDTLS_SSL_DEBUG_MSG( 1,
3973 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003974 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3976 }
3977
Paul Bakker5121ce52009-01-03 21:22:43 +00003978 ssl->out_left -= ret;
3979 }
3980
Hanno Becker2b1e3542018-08-06 11:19:13 +01003981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003982 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003983 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003984 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003985 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003986 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003987#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003988#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003989 {
3990 ssl->out_hdr = ssl->out_buf + 8;
3991 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003992#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003993 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
3997 return( 0 );
3998}
3999
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004000/*
4001 * Functions to handle the DTLS retransmission state machine
4002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004004/*
4005 * Append current handshake message to current outgoing flight
4006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4011 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4012 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004013
4014 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004015 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004016 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004019 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020 }
4021
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004022 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004023 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004026 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027 }
4028
4029 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004030 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004031 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004032 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 msg->next = NULL;
4034
4035 /* Append to the current flight */
4036 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004037 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004038 else
4039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041 while( cur->next != NULL )
4042 cur = cur->next;
4043 cur->next = msg;
4044 }
4045
Hanno Becker3b235902018-08-06 09:54:53 +01004046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047 return( 0 );
4048}
4049
4050/*
4051 * Free the current flight of handshake messages
4052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 mbedtls_ssl_flight_item *cur = flight;
4056 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057
4058 while( cur != NULL )
4059 {
4060 next = cur->next;
4061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 mbedtls_free( cur->p );
4063 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064
4065 cur = next;
4066 }
4067}
4068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4070static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004071#endif
4072
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004074 * Swap transform_out and out_ctr with the alternative ones
4075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079 unsigned char tmp_out_ctr[8];
4080
4081 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004084 return;
4085 }
4086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004089 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090 tmp_transform = ssl->transform_out;
4091 ssl->transform_out = ssl->handshake->alt_transform_out;
4092 ssl->handshake->alt_transform_out = tmp_transform;
4093
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004094 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004095 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4096 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4097 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004098
4099 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004100 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4103 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4108 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004109 }
4110 }
4111#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004112}
4113
4114/*
4115 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004116 */
4117int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4118{
4119 int ret = 0;
4120
4121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4122
4123 ret = mbedtls_ssl_flight_transmit( ssl );
4124
4125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4126
4127 return( ret );
4128}
4129
4130/*
4131 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004132 *
4133 * Need to remember the current message in case flush_output returns
4134 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004135 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004136 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004137int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004138{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004139 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004143 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004145
4146 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004147 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004148 ssl_swap_epochs( ssl );
4149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004151 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152
4153 while( ssl->handshake->cur_msg != NULL )
4154 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004155 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004156 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157
Hanno Beckere1dcb032018-08-17 16:47:58 +01004158 int const is_finished =
4159 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4160 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4161
Hanno Becker04da1892018-08-14 13:22:10 +01004162 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4163 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4164
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004165 /* Swap epochs before sending Finished: we can't do it after
4166 * sending ChangeCipherSpec, in case write returns WANT_READ.
4167 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004168 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004169 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004171 ssl_swap_epochs( ssl );
4172 }
4173
Hanno Becker67bc7c32018-08-06 11:33:50 +01004174 ret = ssl_get_remaining_payload_in_datagram( ssl );
4175 if( ret < 0 )
4176 return( ret );
4177 max_frag_len = (size_t) ret;
4178
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004179 /* CCS is copied as is, while HS messages may need fragmentation */
4180 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4181 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004182 if( max_frag_len == 0 )
4183 {
4184 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4185 return( ret );
4186
4187 continue;
4188 }
4189
Teppo Järvelin91d79382019-10-02 09:09:31 +03004190 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004191 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004192 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004193
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004194 /* Update position inside current message */
4195 ssl->handshake->cur_msg_p += cur->len;
4196 }
4197 else
4198 {
4199 const unsigned char * const p = ssl->handshake->cur_msg_p;
4200 const size_t hs_len = cur->len - 12;
4201 const size_t frag_off = p - ( cur->p + 12 );
4202 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004203 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004204
Hanno Beckere1dcb032018-08-17 16:47:58 +01004205 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004206 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004207 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004208 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004209
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4211 return( ret );
4212
4213 continue;
4214 }
4215 max_hs_frag_len = max_frag_len - 12;
4216
4217 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4218 max_hs_frag_len : rem_len;
4219
4220 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004223 (unsigned) cur_hs_frag_len,
4224 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004225 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004226
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004227 /* Messages are stored with handshake headers as if not fragmented,
4228 * copy beginning of headers then fill fragmentation fields.
4229 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004230 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004231
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004232 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4233 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4234 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004235
4236 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4237
Hanno Becker3f7b9732018-08-28 09:53:25 +01004238 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004239 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004240 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004241 ssl->out_msgtype = cur->type;
4242
4243 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004245 }
4246
4247 /* If done with the current message move to the next one if any */
4248 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4249 {
4250 if( cur->next != NULL )
4251 {
4252 ssl->handshake->cur_msg = cur->next;
4253 ssl->handshake->cur_msg_p = cur->next->p + 12;
4254 }
4255 else
4256 {
4257 ssl->handshake->cur_msg = NULL;
4258 ssl->handshake->cur_msg_p = NULL;
4259 }
4260 }
4261
4262 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004263 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 return( ret );
4267 }
4268 }
4269
Hanno Becker67bc7c32018-08-06 11:33:50 +01004270 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4271 return( ret );
4272
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004273 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4275 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004276 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004279 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4280 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004281
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004283
4284 return( 0 );
4285}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004286
4287/*
4288 * To be called when the last message of an incoming flight is received.
4289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004291{
4292 /* We won't need to resend that one any more */
4293 ssl_flight_free( ssl->handshake->flight );
4294 ssl->handshake->flight = NULL;
4295 ssl->handshake->cur_msg = NULL;
4296
4297 /* The next incoming flight will start with this msg_seq */
4298 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4299
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004300 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004301 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004302
Hanno Becker0271f962018-08-16 13:23:47 +01004303 /* Clear future message buffering structure. */
4304 ssl_buffering_free( ssl );
4305
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004306 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004307 ssl_set_timer( ssl, 0 );
4308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4310 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004313 }
4314 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004316}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004317
4318/*
4319 * To be called when the last message of an outgoing flight is send.
4320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004322{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004323 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004324 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4327 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004330 }
4331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004335
Paul Bakker5121ce52009-01-03 21:22:43 +00004336/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004337 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004339
4340/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004341 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004342 *
4343 * - fill in handshake headers
4344 * - update handshake checksum
4345 * - DTLS: save message for resending
4346 * - then pass to the record layer
4347 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004348 * DTLS: except for HelloRequest, messages are only queued, and will only be
4349 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004350 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004351 * Inputs:
4352 * - ssl->out_msglen: 4 + actual handshake message len
4353 * (4 is the size of handshake headers for TLS)
4354 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4355 * - ssl->out_msg + 4: the handshake message body
4356 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004357 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004358 * - ssl->out_msglen: the length of the record contents
4359 * (including handshake headers but excluding record headers)
4360 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004361 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004362int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004363{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004364 int ret;
4365 const size_t hs_len = ssl->out_msglen - 4;
4366 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004367
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4369
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004370 /*
4371 * Sanity checks
4372 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004373 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004374 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4375 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004376 /* In SSLv3, the client might send a NoCertificate alert. */
4377#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004378 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004379 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004380 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4381 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004382#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4383 {
4384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4386 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004388
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004389 /* Whenever we send anything different from a
4390 * HelloRequest we should be in a handshake - double check. */
4391 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4392 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004393 ssl->handshake == NULL )
4394 {
4395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004400 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004401 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004403 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004406 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004407#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004408
Hanno Beckerb50a2532018-08-06 11:52:54 +01004409 /* Double-check that we did not exceed the bounds
4410 * of the outgoing record buffer.
4411 * This should never fail as the various message
4412 * writing functions must obey the bounds of the
4413 * outgoing record buffer, but better be safe.
4414 *
4415 * Note: We deliberately do not check for the MTU or MFL here.
4416 */
4417 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4418 {
4419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4420 "size %u, maximum %u",
4421 (unsigned) ssl->out_msglen,
4422 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4424 }
4425
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004426 /*
4427 * Fill handshake headers
4428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004431 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004433 /*
4434 * DTLS has additional fields in the Handshake layer,
4435 * between the length field and the actual payload:
4436 * uint16 message_seq;
4437 * uint24 fragment_offset;
4438 * uint24 fragment_length;
4439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004441 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004442 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004443 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004444 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004445 {
4446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4447 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004448 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004449 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004450 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4451 }
4452
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004453 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004454 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004455
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004456 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004457 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004458 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004459 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4460 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004461 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004462 }
4463 else
4464 {
4465 ssl->out_msg[4] = 0;
4466 ssl->out_msg[5] = 0;
4467 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004468
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004469 /* Handshake hashes are computed without fragmentation,
4470 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004471 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004472 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004475
Hanno Becker0207e532018-08-28 10:28:28 +01004476 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004477 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004478 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 }
4480
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004481 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004483 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004484 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4485 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004486 {
4487 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004490 return( ret );
4491 }
4492 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004493 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004494#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004495 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004496 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004497 {
4498 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4499 return( ret );
4500 }
4501 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004502
4503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4504
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004505 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004506}
4507
4508/*
4509 * Record layer functions
4510 */
4511
4512/*
4513 * Write current record.
4514 *
4515 * Uses:
4516 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4517 * - ssl->out_msglen: length of the record content (excl headers)
4518 * - ssl->out_msg: record content
4519 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004520int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004521{
4522 int ret, done = 0;
4523 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004524 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004525
4526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004529 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004531 {
4532 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004535 return( ret );
4536 }
4537
4538 len = ssl->out_msglen;
4539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4543 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 ret = mbedtls_ssl_hw_record_write( ssl );
4548 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4551 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004552 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004553
4554 if( ret == 0 )
4555 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004558 if( !done )
4559 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004560 unsigned i;
4561 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004562 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004563
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004564 /* Skip writing the record content type to after the encryption,
4565 * as it may change when using the CID extension. */
4566
Hanno Becker2881d802019-05-22 14:44:53 +01004567 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4568 mbedtls_ssl_get_minor_ver( ssl ),
4569 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004570
Teppo Järvelin91d79382019-10-02 09:09:31 +03004571 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004572 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004573
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004575 {
Hanno Becker3307b532017-12-27 21:37:21 +00004576 mbedtls_record rec;
4577
4578 rec.buf = ssl->out_iv;
4579 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4580 ( ssl->out_iv - ssl->out_buf );
4581 rec.data_len = ssl->out_msglen;
4582 rec.data_offset = ssl->out_msg - rec.buf;
4583
Teppo Järvelin91d79382019-10-02 09:09:31 +03004584 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004585 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4586 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004587 ssl->conf->transport, rec.ver );
4588 rec.type = ssl->out_msgtype;
4589
Hanno Beckera5a2b082019-05-15 14:03:01 +01004590#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004591 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004592 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004593#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004594
Hanno Becker611a83b2018-01-03 14:27:32 +00004595 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004596 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004597 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004600 return( ret );
4601 }
4602
Hanno Becker3307b532017-12-27 21:37:21 +00004603 if( rec.data_offset != 0 )
4604 {
4605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4607 }
4608
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004609 /* Update the record content type and CID. */
4610 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004611#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004612 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4613 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004614#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004615 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004616 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004617 encrypted_fi = 1;
4618 }
4619
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004620 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004621 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4622 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004623 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004624 }
4625
Hanno Becker43395762019-05-03 14:46:38 +01004626 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004627
4628#if defined(MBEDTLS_SSL_PROTO_DTLS)
4629 /* In case of DTLS, double-check that we don't exceed
4630 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004631 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004632 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004633 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004634 if( ret < 0 )
4635 return( ret );
4636
4637 if( protected_record_size > (size_t) ret )
4638 {
4639 /* Should never happen */
4640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4641 }
4642 }
4643#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004644
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004645 /* Now write the potentially updated record content type. */
4646 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004649 "version = [%d:%d], msglen = %d",
4650 ssl->out_hdr[0], ssl->out_hdr[1],
4651 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004654 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004655
4656 ssl->out_left += protected_record_size;
4657 ssl->out_hdr += protected_record_size;
4658 ssl_update_out_pointers( ssl, ssl->transform_out );
4659
Hanno Becker04484622018-08-06 09:49:38 +01004660 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4661 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4662 break;
4663
4664 /* The loop goes to its end iff the counter is wrapping */
4665 if( i == ssl_ep_len( ssl ) )
4666 {
4667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4668 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 }
4671
Hanno Becker67bc7c32018-08-06 11:33:50 +01004672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004673 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004674 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004675 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004676 size_t remaining;
4677 ret = ssl_get_remaining_payload_in_datagram( ssl );
4678 if( ret < 0 )
4679 {
4680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4681 ret );
4682 return( ret );
4683 }
4684
4685 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004686 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004687 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004688 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004689 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004690 else
4691 {
Hanno Becker513815a2018-08-20 11:56:09 +01004692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004693 }
4694 }
4695#endif /* MBEDTLS_SSL_PROTO_DTLS */
4696
4697 if( ( flush == SSL_FORCE_FLUSH ) &&
4698 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 return( ret );
4702 }
4703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705
4706 return( 0 );
4707}
4708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004710
4711static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4712{
4713 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004714 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4715 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004716 {
4717 return( 1 );
4718 }
4719 return( 0 );
4720}
Hanno Becker44650b72018-08-16 12:51:11 +01004721
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004722static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004723{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004724 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004725}
4726
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004727static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004728{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004729 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004730}
4731
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004732static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004733{
4734 uint32_t msg_len, frag_off, frag_len;
4735
4736 msg_len = ssl_get_hs_total_len( ssl );
4737 frag_off = ssl_get_hs_frag_off( ssl );
4738 frag_len = ssl_get_hs_frag_len( ssl );
4739
4740 if( frag_off > msg_len )
4741 return( -1 );
4742
4743 if( frag_len > msg_len - frag_off )
4744 return( -1 );
4745
4746 if( frag_len + 12 > ssl->in_msglen )
4747 return( -1 );
4748
4749 return( 0 );
4750}
4751
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004752/*
4753 * Mark bits in bitmask (used for DTLS HS reassembly)
4754 */
4755static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4756{
4757 unsigned int start_bits, end_bits;
4758
4759 start_bits = 8 - ( offset % 8 );
4760 if( start_bits != 8 )
4761 {
4762 size_t first_byte_idx = offset / 8;
4763
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004764 /* Special case */
4765 if( len <= start_bits )
4766 {
4767 for( ; len != 0; len-- )
4768 mask[first_byte_idx] |= 1 << ( start_bits - len );
4769
4770 /* Avoid potential issues with offset or len becoming invalid */
4771 return;
4772 }
4773
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004774 offset += start_bits; /* Now offset % 8 == 0 */
4775 len -= start_bits;
4776
4777 for( ; start_bits != 0; start_bits-- )
4778 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4779 }
4780
4781 end_bits = len % 8;
4782 if( end_bits != 0 )
4783 {
4784 size_t last_byte_idx = ( offset + len ) / 8;
4785
4786 len -= end_bits; /* Now len % 8 == 0 */
4787
4788 for( ; end_bits != 0; end_bits-- )
4789 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4790 }
4791
4792 memset( mask + offset / 8, 0xFF, len / 8 );
4793}
4794
4795/*
4796 * Check that bitmask is full
4797 */
4798static int ssl_bitmask_check( unsigned char *mask, size_t len )
4799{
4800 size_t i;
4801
4802 for( i = 0; i < len / 8; i++ )
4803 if( mask[i] != 0xFF )
4804 return( -1 );
4805
4806 for( i = 0; i < len % 8; i++ )
4807 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4808 return( -1 );
4809
4810 return( 0 );
4811}
4812
Hanno Becker56e205e2018-08-16 09:06:12 +01004813/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004814static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004815 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004816{
Hanno Becker56e205e2018-08-16 09:06:12 +01004817 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004818
Hanno Becker56e205e2018-08-16 09:06:12 +01004819 alloc_len = 12; /* Handshake header */
4820 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004821
Hanno Beckerd07df862018-08-16 09:14:58 +01004822 if( add_bitmap )
4823 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004824
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004825 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004826}
Hanno Becker56e205e2018-08-16 09:06:12 +01004827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004829
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004830static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004831{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004832 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004833}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004834
Simon Butcher99000142016-10-13 17:21:01 +01004835int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004840 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004842 }
4843
Hanno Becker12555c62018-08-16 12:47:53 +01004844 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004848 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004851 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004852 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004853 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004854 unsigned int recv_msg_seq = (unsigned int)
4855 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004856
Hanno Becker44650b72018-08-16 12:51:11 +01004857 if( ssl_check_hs_header( ssl ) != 0 )
4858 {
4859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4861 }
4862
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004863 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004864 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4865 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4866 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4867 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004868 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004869 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4872 recv_msg_seq,
4873 ssl->handshake->in_msg_seq ) );
4874 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4875 }
4876
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004877 /* Retransmit only on last message from previous flight, to avoid
4878 * too many retransmissions.
4879 * Besides, No sane server ever retransmits HelloVerifyRequest */
4880 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004884 "message_seq = %d, start_of_flight = %d",
4885 recv_msg_seq,
4886 ssl->handshake->in_flight_start_seq ) );
4887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004891 return( ret );
4892 }
4893 }
4894 else
4895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004897 "message_seq = %d, expected = %d",
4898 recv_msg_seq,
4899 ssl->handshake->in_msg_seq ) );
4900 }
4901
Hanno Becker90333da2017-10-10 11:27:13 +01004902 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004903 }
4904 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004905
Hanno Becker6d97ef52018-08-16 13:09:04 +01004906 /* Message reassembly is handled alongside buffering of future
4907 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004908 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004909 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004910 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004913 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004914 }
4915 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004916 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004918#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004919 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004920 /* With TLS we don't handle fragmentation (for now) */
4921 if( ssl->in_msglen < ssl->in_hslen )
4922 {
4923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4924 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4925 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004926 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004927#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004928
Simon Butcher99000142016-10-13 17:21:01 +01004929 return( 0 );
4930}
4931
4932void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4933{
Hanno Becker0271f962018-08-16 13:23:47 +01004934 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004935
Hanno Becker0271f962018-08-16 13:23:47 +01004936 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004937 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004938
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004939 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004941 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004942 ssl->handshake != NULL )
4943 {
Hanno Becker0271f962018-08-16 13:23:47 +01004944 unsigned offset;
4945 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004946
Hanno Becker0271f962018-08-16 13:23:47 +01004947 /* Increment handshake sequence number */
4948 hs->in_msg_seq++;
4949
4950 /*
4951 * Clear up handshake buffering and reassembly structure.
4952 */
4953
4954 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004955 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004956
4957 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004958 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4959 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004960 offset++, hs_buf++ )
4961 {
4962 *hs_buf = *(hs_buf + 1);
4963 }
4964
4965 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004966 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004967 }
4968#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004969}
4970
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004971/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004972 * DTLS anti-replay: RFC 6347 4.1.2.6
4973 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004974 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4975 * Bit n is set iff record number in_window_top - n has been seen.
4976 *
4977 * Usually, in_window_top is the last record number seen and the lsb of
4978 * in_window is set. The only exception is the initial state (record number 0
4979 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4982static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983{
4984 ssl->in_window_top = 0;
4985 ssl->in_window = 0;
4986}
4987
4988static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4989{
4990 return( ( (uint64_t) buf[0] << 40 ) |
4991 ( (uint64_t) buf[1] << 32 ) |
4992 ( (uint64_t) buf[2] << 24 ) |
4993 ( (uint64_t) buf[3] << 16 ) |
4994 ( (uint64_t) buf[4] << 8 ) |
4995 ( (uint64_t) buf[5] ) );
4996}
4997
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02004998static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
4999{
5000 int ret;
5001 unsigned char *original_in_ctr;
5002
5003 // save original in_ctr
5004 original_in_ctr = ssl->in_ctr;
5005
5006 // use counter from record
5007 ssl->in_ctr = record_in_ctr;
5008
5009 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5010
5011 // restore the counter
5012 ssl->in_ctr = original_in_ctr;
5013
5014 return ret;
5015}
5016
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005017/*
5018 * Return 0 if sequence number is acceptable, -1 otherwise
5019 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005020int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005021{
5022 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5023 uint64_t bit;
5024
Hanno Becker7f376f42019-06-12 16:20:48 +01005025 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5026 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5027 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005028 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005029 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005030
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005031 if( rec_seqnum > ssl->in_window_top )
5032 return( 0 );
5033
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005034 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005035
5036 if( bit >= 64 )
5037 return( -1 );
5038
5039 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5040 return( -1 );
5041
5042 return( 0 );
5043}
5044
5045/*
5046 * Update replay window on new validated record
5047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005049{
5050 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5051
Hanno Becker7f376f42019-06-12 16:20:48 +01005052 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5053 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5054 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005055 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005056 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005057
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005058 if( rec_seqnum > ssl->in_window_top )
5059 {
5060 /* Update window_top and the contents of the window */
5061 uint64_t shift = rec_seqnum - ssl->in_window_top;
5062
5063 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005064 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005065 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005066 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005067 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005068 ssl->in_window |= 1;
5069 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005070
5071 ssl->in_window_top = rec_seqnum;
5072 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005073 else
5074 {
5075 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005076 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005077
5078 if( bit < 64 ) /* Always true, but be extra sure */
5079 ssl->in_window |= (uint64_t) 1 << bit;
5080 }
5081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005083
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005084#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005085/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005086static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5087
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005088/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005089 * Without any SSL context, check if a datagram looks like a ClientHello with
5090 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005091 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005092 *
5093 * - if cookie is valid, return 0
5094 * - if ClientHello looks superficially valid but cookie is not,
5095 * fill obuf and set olen, then
5096 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5097 * - otherwise return a specific error code
5098 */
5099static int ssl_check_dtls_clihlo_cookie(
5100 mbedtls_ssl_cookie_write_t *f_cookie_write,
5101 mbedtls_ssl_cookie_check_t *f_cookie_check,
5102 void *p_cookie,
5103 const unsigned char *cli_id, size_t cli_id_len,
5104 const unsigned char *in, size_t in_len,
5105 unsigned char *obuf, size_t buf_len, size_t *olen )
5106{
5107 size_t sid_len, cookie_len;
5108 unsigned char *p;
5109
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 /*
5111 * Structure of ClientHello with record and handshake headers,
5112 * and expected values. We don't need to check a lot, more checks will be
5113 * done when actually parsing the ClientHello - skipping those checks
5114 * avoids code duplication and does not make cookie forging any easier.
5115 *
5116 * 0-0 ContentType type; copied, must be handshake
5117 * 1-2 ProtocolVersion version; copied
5118 * 3-4 uint16 epoch; copied, must be 0
5119 * 5-10 uint48 sequence_number; copied
5120 * 11-12 uint16 length; (ignored)
5121 *
5122 * 13-13 HandshakeType msg_type; (ignored)
5123 * 14-16 uint24 length; (ignored)
5124 * 17-18 uint16 message_seq; copied
5125 * 19-21 uint24 fragment_offset; copied, must be 0
5126 * 22-24 uint24 fragment_length; (ignored)
5127 *
5128 * 25-26 ProtocolVersion client_version; (ignored)
5129 * 27-58 Random random; (ignored)
5130 * 59-xx SessionID session_id; 1 byte len + sid_len content
5131 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5132 * ...
5133 *
5134 * Minimum length is 61 bytes.
5135 */
5136 if( in_len < 61 ||
5137 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5138 in[3] != 0 || in[4] != 0 ||
5139 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5140 {
5141 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5142 }
5143
5144 sid_len = in[59];
5145 if( sid_len > in_len - 61 )
5146 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5147
5148 cookie_len = in[60 + sid_len];
5149 if( cookie_len > in_len - 60 )
5150 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5151
5152 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5153 cli_id, cli_id_len ) == 0 )
5154 {
5155 /* Valid cookie */
5156 return( 0 );
5157 }
5158
5159 /*
5160 * If we get here, we've got an invalid cookie, let's prepare HVR.
5161 *
5162 * 0-0 ContentType type; copied
5163 * 1-2 ProtocolVersion version; copied
5164 * 3-4 uint16 epoch; copied
5165 * 5-10 uint48 sequence_number; copied
5166 * 11-12 uint16 length; olen - 13
5167 *
5168 * 13-13 HandshakeType msg_type; hello_verify_request
5169 * 14-16 uint24 length; olen - 25
5170 * 17-18 uint16 message_seq; copied
5171 * 19-21 uint24 fragment_offset; copied
5172 * 22-24 uint24 fragment_length; olen - 25
5173 *
5174 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5175 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5176 *
5177 * Minimum length is 28.
5178 */
5179 if( buf_len < 28 )
5180 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5181
5182 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005183 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005184 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5185 obuf[25] = 0xfe;
5186 obuf[26] = 0xff;
5187
5188 /* Generate and write actual cookie */
5189 p = obuf + 28;
5190 if( f_cookie_write( p_cookie,
5191 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5192 {
5193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5194 }
5195
5196 *olen = p - obuf;
5197
5198 /* Go back and fill length fields */
5199 obuf[27] = (unsigned char)( *olen - 28 );
5200
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005201 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005202 obuf[22] = obuf[14];
5203 obuf[23] = obuf[15];
5204 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005205
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005206 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005207
5208 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5209}
5210
5211/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005212 * Handle possible client reconnect with the same UDP quadruplet
5213 * (RFC 6347 Section 4.2.8).
5214 *
5215 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5216 * that looks like a ClientHello.
5217 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005218 * - if the input looks like a ClientHello without cookies,
5219 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005220 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005221 * - if the input looks like a ClientHello with a valid cookie,
5222 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005223 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005224 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005225 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005227 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5228 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005229 */
5230static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5231{
5232 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005233 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005234
Hanno Becker87b56262019-07-10 14:37:41 +01005235 if( ssl->conf->f_cookie_write == NULL ||
5236 ssl->conf->f_cookie_check == NULL )
5237 {
5238 /* If we can't use cookies to verify reachability of the peer,
5239 * drop the record. */
5240 return( 0 );
5241 }
5242
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005243 ret = ssl_check_dtls_clihlo_cookie(
5244 ssl->conf->f_cookie_write,
5245 ssl->conf->f_cookie_check,
5246 ssl->conf->p_cookie,
5247 ssl->cli_id, ssl->cli_id_len,
5248 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005249 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005250
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005251 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5252
5253 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005254 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005255 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005256 * If the error is permanent we'll catch it later,
5257 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005258 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005259 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005260 }
5261
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005262 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005263 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005264 /* Got a valid cookie, partially reset context */
5265 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5266 {
5267 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5268 return( ret );
5269 }
5270
5271 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005272 }
5273
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274 return( ret );
5275}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005276#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005277
Hanno Becker46483f12019-05-03 13:25:54 +01005278static int ssl_check_record_type( uint8_t record_type )
5279{
5280 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5281 record_type != MBEDTLS_SSL_MSG_ALERT &&
5282 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5283 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5284 {
5285 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5286 }
5287
5288 return( 0 );
5289}
5290
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005291/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005292 * ContentType type;
5293 * ProtocolVersion version;
5294 * uint16 epoch; // DTLS only
5295 * uint48 sequence_number; // DTLS only
5296 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005297 *
5298 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005299 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005300 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5301 *
5302 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005303 * 1. proceed with the record if this function returns 0
5304 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5305 * 3. return CLIENT_RECONNECT if this function return that value
5306 * 4. drop the whole datagram if this function returns anything else.
5307 * Point 2 is needed when the peer is resending, and we have already received
5308 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005309 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005310static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005311 unsigned char *buf,
5312 size_t len,
5313 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005314{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005315 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005316
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005317 size_t const rec_hdr_type_offset = 0;
5318 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005319
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005320 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5321 rec_hdr_type_len;
5322 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005323
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005324 size_t const rec_hdr_ctr_len = 8;
5325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005326 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005327 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5328 rec_hdr_version_len;
5329
Hanno Beckera5a2b082019-05-15 14:03:01 +01005330#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005331 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5332 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005333 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005334#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5335#endif /* MBEDTLS_SSL_PROTO_DTLS */
5336
5337 size_t rec_hdr_len_offset; /* To be determined */
5338 size_t const rec_hdr_len_len = 2;
5339
5340 /*
5341 * Check minimum lengths for record header.
5342 */
5343
5344#if defined(MBEDTLS_SSL_PROTO_DTLS)
5345 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5346 {
5347 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5348 }
5349 MBEDTLS_SSL_TRANSPORT_ELSE
5350#endif /* MBEDTLS_SSL_PROTO_DTLS */
5351#if defined(MBEDTLS_SSL_PROTO_TLS)
5352 {
5353 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5354 }
5355#endif /* MBEDTLS_SSL_PROTO_DTLS */
5356
5357 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5358 {
5359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5360 (unsigned) len,
5361 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5362 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5363 }
5364
5365 /*
5366 * Parse and validate record content type
5367 */
5368
5369 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005370
5371 /* Check record content type */
5372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5373 rec->cid_len = 0;
5374
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005375 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005376 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5377 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005378 {
5379 /* Shift pointers to account for record header including CID
5380 * struct {
5381 * ContentType special_type = tls12_cid;
5382 * ProtocolVersion version;
5383 * uint16 epoch;
5384 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005385 * opaque cid[cid_length]; // Additional field compared to
5386 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005387 * uint16 length;
5388 * opaque enc_content[DTLSCiphertext.length];
5389 * } DTLSCiphertext;
5390 */
5391
5392 /* So far, we only support static CID lengths
5393 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005394 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5395 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005396
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005397 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005398 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5400 (unsigned) len,
5401 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005402 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005403 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005404
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005405 /* configured CID len is guaranteed at most 255, see
5406 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5407 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005408 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5409 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005410 }
5411 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005412#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005413 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005414 if( ssl_check_record_type( rec->type ) )
5415 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5417 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005418 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5419 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005420 }
5421
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005422 /*
5423 * Parse and validate record version
5424 */
5425
Hanno Becker8061c6e2019-07-26 08:07:03 +01005426 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5427 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005428 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5429 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005430 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005431
Hanno Becker2881d802019-05-22 14:44:53 +01005432 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005436 }
5437
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005438 if( mbedtls_ssl_ver_gt( minor_ver,
5439 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 }
5444
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005445 /*
5446 * Parse/Copy record sequence number.
5447 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005448
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005449#if defined(MBEDTLS_SSL_PROTO_DTLS)
5450 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5451 {
5452 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005453 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5454 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005455 rec_hdr_ctr_len );
5456 }
5457 MBEDTLS_SSL_TRANSPORT_ELSE
5458#endif /* MBEDTLS_SSL_PROTO_DTLS */
5459#if defined(MBEDTLS_SSL_PROTO_TLS)
5460 {
5461 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005462 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5463 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005464 }
5465#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005466
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005467 /*
5468 * Parse record length.
5469 */
5470
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005471 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005472 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005473 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5474
Hanno Becker8b09b732019-05-08 12:03:28 +01005475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005476 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005477 rec->type,
5478 major_ver, minor_ver, rec->data_len ) );
5479
5480 rec->buf = buf;
5481 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005482
Hanno Beckerec014082019-07-26 08:20:27 +01005483 if( rec->data_len == 0 )
5484 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5485
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005486 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005487 * DTLS-related tests.
5488 * Check epoch before checking length constraint because
5489 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5490 * message gets duplicated before the corresponding Finished message,
5491 * the second ChangeCipherSpec should be discarded because it belongs
5492 * to an old epoch, but not because its length is shorter than
5493 * the minimum record length for packets using the new record transform.
5494 * Note that these two kinds of failures are handled differently,
5495 * as an unexpected record is silently skipped but an invalid
5496 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005497 */
5498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005499 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005500 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005501 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005502
Hanno Beckere0452772019-07-10 17:12:07 +01005503 /* Check that the datagram is large enough to contain a record
5504 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005505 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005506 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5508 (unsigned) len,
5509 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005510 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5511 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005512
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005513 /* Records from other, non-matching epochs are silently discarded.
5514 * (The case of same-port Client reconnects must be considered in
5515 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005516 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005517 {
5518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5519 "expected %d, received %d",
5520 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005521
5522 /* Records from the next epoch are considered for buffering
5523 * (concretely: early Finished messages). */
5524 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5525 {
5526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5527 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5528 }
5529
Hanno Becker87b56262019-07-10 14:37:41 +01005530 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005531 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005532#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005533 /* For records from the correct epoch, check whether their
5534 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005535 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5536 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005537 {
5538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5539 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5540 }
5541#endif
5542 }
5543#endif /* MBEDTLS_SSL_PROTO_DTLS */
5544
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005545 return( 0 );
5546}
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
Hanno Becker87b56262019-07-10 14:37:41 +01005548
5549#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5550static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5551{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005552 unsigned int rec_epoch = (unsigned int)
5553 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005554
5555 /*
5556 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5557 * access the first byte of record content (handshake type), as we
5558 * have an active transform (possibly iv_len != 0), so use the
5559 * fact that the record header len is 13 instead.
5560 */
5561 if( rec_epoch == 0 &&
5562 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5563 MBEDTLS_SSL_IS_SERVER &&
5564 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5565 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5566 ssl->in_left > 13 &&
5567 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5568 {
5569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5570 "from the same port" ) );
5571 return( ssl_handle_possible_reconnect( ssl ) );
5572 }
5573
5574 return( 0 );
5575}
5576#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5577
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005578/*
5579 * If applicable, decrypt (and decompress) record content
5580 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005581static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5582 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005583{
5584 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005587 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5590 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 ret = mbedtls_ssl_hw_record_read( ssl );
5595 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5598 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005599 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005600
5601 if( ret == 0 )
5602 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005605 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005607 unsigned char const old_msg_type = rec->type;
5608
Hanno Becker611a83b2018-01-03 14:27:32 +00005609 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005610 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005613
Hanno Beckera5a2b082019-05-15 14:03:01 +01005614#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005615 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005616 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5617 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005618 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005620 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005621 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005622#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005623
Paul Bakker5121ce52009-01-03 21:22:43 +00005624 return( ret );
5625 }
5626
Hanno Becker106f3da2019-07-12 09:35:58 +01005627 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005628 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005629 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005630 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005632
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005634 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005635
Hanno Beckera5a2b082019-05-15 14:03:01 +01005636#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005637 /* We have already checked the record content type
5638 * in ssl_parse_record_header(), failing or silently
5639 * dropping the record in the case of an unknown type.
5640 *
5641 * Since with the use of CIDs, the record content type
5642 * might change during decryption, re-check the record
5643 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005644 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005645 {
5646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5647 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5648 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005649#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005650
Hanno Becker106f3da2019-07-12 09:35:58 +01005651 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005652 {
5653#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005654 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005655 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005656 {
5657 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5660 }
5661#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5662
5663 ssl->nb_zero++;
5664
5665 /*
5666 * Three or more empty messages may be a DoS attack
5667 * (excessive CPU consumption).
5668 */
5669 if( ssl->nb_zero > 3 )
5670 {
5671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005672 "messages, possible DoS attack" ) );
5673 /* Treat the records as if they were not properly authenticated,
5674 * thereby failing the connection if we see more than allowed
5675 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005676 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5677 }
5678 }
5679 else
5680 ssl->nb_zero = 0;
5681
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005682 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5683#if defined(MBEDTLS_SSL_PROTO_TLS)
5684 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005685 {
5686 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005687 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005688 if( ++ssl->in_ctr[i - 1] != 0 )
5689 break;
5690
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005691 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005692 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005693 {
5694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5695 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5696 }
5697 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005698#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005699
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 }
5701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005703 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005705 {
5706 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005709 return( ret );
5710 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005715 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005718 }
5719#endif
5720
Hanno Beckerf0242852019-07-09 17:30:02 +01005721 /* Check actual (decrypted) record content length against
5722 * configured maximum. */
5723 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5724 {
5725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5726 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5727 }
5728
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005729 return( 0 );
5730}
5731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005733
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005734/*
5735 * Read a record.
5736 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005737 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5738 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5739 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005740 */
Hanno Becker1097b342018-08-15 14:09:41 +01005741
5742/* Helper functions for mbedtls_ssl_read_record(). */
5743static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005744static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5745static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005746
Hanno Becker327c93b2018-08-15 13:56:18 +01005747int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005748 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749{
5750 int ret;
5751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005753
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005754 if( ssl->keep_current_message == 0 )
5755 {
5756 do {
Simon Butcher99000142016-10-13 17:21:01 +01005757
Hanno Becker26994592018-08-15 14:14:59 +01005758 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005759 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005760 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005761
Hanno Beckere74d5562018-08-15 14:26:08 +01005762 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005763 {
Hanno Becker40f50842018-08-15 14:48:01 +01005764#if defined(MBEDTLS_SSL_PROTO_DTLS)
5765 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005766
Hanno Becker40f50842018-08-15 14:48:01 +01005767 /* We only check for buffered messages if the
5768 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005769 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005770 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005771 {
Hanno Becker40f50842018-08-15 14:48:01 +01005772 if( ssl_load_buffered_message( ssl ) == 0 )
5773 have_buffered = 1;
5774 }
5775
5776 if( have_buffered == 0 )
5777#endif /* MBEDTLS_SSL_PROTO_DTLS */
5778 {
5779 ret = ssl_get_next_record( ssl );
5780 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5781 continue;
5782
5783 if( ret != 0 )
5784 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005785 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005786 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005787 return( ret );
5788 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005789 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005790 }
5791
5792 ret = mbedtls_ssl_handle_message_type( ssl );
5793
Hanno Becker40f50842018-08-15 14:48:01 +01005794#if defined(MBEDTLS_SSL_PROTO_DTLS)
5795 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5796 {
5797 /* Buffer future message */
5798 ret = ssl_buffer_message( ssl );
5799 if( ret != 0 )
5800 return( ret );
5801
5802 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5803 }
5804#endif /* MBEDTLS_SSL_PROTO_DTLS */
5805
Hanno Becker90333da2017-10-10 11:27:13 +01005806 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5807 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005808
5809 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005810 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005811 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005812 return( ret );
5813 }
5814
Hanno Becker327c93b2018-08-15 13:56:18 +01005815 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005816 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005817 {
5818 mbedtls_ssl_update_handshake_status( ssl );
5819 }
Simon Butcher99000142016-10-13 17:21:01 +01005820 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005821 else
Simon Butcher99000142016-10-13 17:21:01 +01005822 {
Hanno Becker02f59072018-08-15 14:00:24 +01005823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005824 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005825 }
5826
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5828
5829 return( 0 );
5830}
5831
Hanno Becker40f50842018-08-15 14:48:01 +01005832#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005833static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005834{
Hanno Becker40f50842018-08-15 14:48:01 +01005835 if( ssl->in_left > ssl->next_record_offset )
5836 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005837
Hanno Becker40f50842018-08-15 14:48:01 +01005838 return( 0 );
5839}
5840
5841static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5842{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005844 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005845 int ret = 0;
5846
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 if( hs == NULL )
5848 return( -1 );
5849
Hanno Beckere00ae372018-08-20 09:39:42 +01005850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5851
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005852 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5853 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5854 {
5855 /* Check if we have seen a ChangeCipherSpec before.
5856 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005857 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 {
5859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5860 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005861 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005862 }
5863
Hanno Becker39b8bc92018-08-28 17:17:13 +01005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5866 ssl->in_msglen = 1;
5867 ssl->in_msg[0] = 1;
5868
5869 /* As long as they are equal, the exact value doesn't matter. */
5870 ssl->in_left = 0;
5871 ssl->next_record_offset = 0;
5872
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005873 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005874 goto exit;
5875 }
Hanno Becker37f95322018-08-16 13:55:32 +01005876
Hanno Beckerb8f50142018-08-28 10:01:34 +01005877#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005878 /* Debug only */
5879 {
5880 unsigned offset;
5881 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5882 {
5883 hs_buf = &hs->buffering.hs[offset];
5884 if( hs_buf->is_valid == 1 )
5885 {
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5887 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005888 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005889 }
5890 }
5891 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005892#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005893
5894 /* Check if we have buffered and/or fully reassembled the
5895 * next handshake message. */
5896 hs_buf = &hs->buffering.hs[0];
5897 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5898 {
5899 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005900 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005901
5902 /* Double-check that we haven't accidentally buffered
5903 * a message that doesn't fit into the input buffer. */
5904 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5905 {
5906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5908 }
5909
5910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5911 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5912 hs_buf->data, msg_len + 12 );
5913
5914 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5915 ssl->in_hslen = msg_len + 12;
5916 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005917 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005918
5919 ret = 0;
5920 goto exit;
5921 }
5922 else
5923 {
5924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5925 hs->in_msg_seq ) );
5926 }
5927
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005928 ret = -1;
5929
5930exit:
5931
5932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5933 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005934}
5935
Hanno Beckera02b0b42018-08-21 17:20:27 +01005936static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5937 size_t desired )
5938{
5939 int offset;
5940 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5942 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005943
Hanno Becker01315ea2018-08-21 17:22:17 +01005944 /* Get rid of future records epoch first, if such exist. */
5945 ssl_free_buffered_record( ssl );
5946
5947 /* Check if we have enough space available now. */
5948 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5949 hs->buffering.total_bytes_buffered ) )
5950 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005952 return( 0 );
5953 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005954
Hanno Becker4f432ad2018-08-28 10:02:32 +01005955 /* We don't have enough space to buffer the next expected handshake
5956 * message. Remove buffers used for future messages to gain space,
5957 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005958 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5959 offset >= 0; offset-- )
5960 {
5961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5962 offset ) );
5963
Hanno Beckerb309b922018-08-23 13:18:05 +01005964 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005965
5966 /* Check if we have enough space available now. */
5967 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5968 hs->buffering.total_bytes_buffered ) )
5969 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005971 return( 0 );
5972 }
5973 }
5974
5975 return( -1 );
5976}
5977
Hanno Becker40f50842018-08-15 14:48:01 +01005978static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5979{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005980 int ret = 0;
5981 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5982
5983 if( hs == NULL )
5984 return( 0 );
5985
5986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5987
5988 switch( ssl->in_msgtype )
5989 {
5990 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005992
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005993 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005994 break;
5995
5996 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005997 {
5998 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005999 unsigned recv_msg_seq = (unsigned)
6000 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006001
Hanno Becker37f95322018-08-16 13:55:32 +01006002 mbedtls_ssl_hs_buffer *hs_buf;
6003 size_t msg_len = ssl->in_hslen - 12;
6004
6005 /* We should never receive an old handshake
6006 * message - double-check nonetheless. */
6007 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6008 {
6009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6011 }
6012
6013 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6014 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6015 {
6016 /* Silently ignore -- message too far in the future */
6017 MBEDTLS_SSL_DEBUG_MSG( 2,
6018 ( "Ignore future HS message with sequence number %u, "
6019 "buffering window %u - %u",
6020 recv_msg_seq, ssl->handshake->in_msg_seq,
6021 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6022
6023 goto exit;
6024 }
6025
6026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6027 recv_msg_seq, recv_msg_seq_offset ) );
6028
6029 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6030
6031 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006032 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006033 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006034 size_t reassembly_buf_sz;
6035
Hanno Becker37f95322018-08-16 13:55:32 +01006036 hs_buf->is_fragmented =
6037 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6038
6039 /* We copy the message back into the input buffer
6040 * after reassembly, so check that it's not too large.
6041 * This is an implementation-specific limitation
6042 * and not one from the standard, hence it is not
6043 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006044 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006045 {
6046 /* Ignore message */
6047 goto exit;
6048 }
6049
Hanno Beckere0b150f2018-08-21 15:51:03 +01006050 /* Check if we have enough space to buffer the message. */
6051 if( hs->buffering.total_bytes_buffered >
6052 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6053 {
6054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6056 }
6057
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006058 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6059 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006060
6061 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6062 hs->buffering.total_bytes_buffered ) )
6063 {
6064 if( recv_msg_seq_offset > 0 )
6065 {
6066 /* If we can't buffer a future message because
6067 * of space limitations -- ignore. */
6068 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",
6069 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6070 (unsigned) hs->buffering.total_bytes_buffered ) );
6071 goto exit;
6072 }
Hanno Beckere1801392018-08-21 16:51:05 +01006073 else
6074 {
6075 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",
6076 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6077 (unsigned) hs->buffering.total_bytes_buffered ) );
6078 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006079
Hanno Beckera02b0b42018-08-21 17:20:27 +01006080 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006081 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006082 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",
6083 (unsigned) msg_len,
6084 (unsigned) reassembly_buf_sz,
6085 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006086 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006087 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6088 goto exit;
6089 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006090 }
6091
6092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6093 msg_len ) );
6094
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006095 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6096 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006097 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006098 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006099 goto exit;
6100 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006101 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006102
6103 /* Prepare final header: copy msg_type, length and message_seq,
6104 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006105 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006106 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006107 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006108
6109 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006110
6111 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006112 }
6113 else
6114 {
6115 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006116 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 +01006117 {
6118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6119 /* Ignore */
6120 goto exit;
6121 }
6122 }
6123
Hanno Becker4422bbb2018-08-20 09:40:19 +01006124 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006125 {
6126 size_t frag_len, frag_off;
6127 unsigned char * const msg = hs_buf->data + 12;
6128
6129 /*
6130 * Check and copy current fragment
6131 */
6132
6133 /* Validation of header fields already done in
6134 * mbedtls_ssl_prepare_handshake_record(). */
6135 frag_off = ssl_get_hs_frag_off( ssl );
6136 frag_len = ssl_get_hs_frag_len( ssl );
6137
6138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6139 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006140 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006141
6142 if( hs_buf->is_fragmented )
6143 {
6144 unsigned char * const bitmask = msg + msg_len;
6145 ssl_bitmask_set( bitmask, frag_off, frag_len );
6146 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6147 msg_len ) == 0 );
6148 }
6149 else
6150 {
6151 hs_buf->is_complete = 1;
6152 }
6153
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6155 hs_buf->is_complete ? "" : "not yet " ) );
6156 }
6157
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006158 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006159 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006160
6161 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006162 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006163 break;
6164 }
6165
6166exit:
6167
6168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6169 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006170}
6171#endif /* MBEDTLS_SSL_PROTO_DTLS */
6172
Hanno Becker1097b342018-08-15 14:09:41 +01006173static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006174{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006175 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006176 * Consume last content-layer message and potentially
6177 * update in_msglen which keeps track of the contents'
6178 * consumption state.
6179 *
6180 * (1) Handshake messages:
6181 * Remove last handshake message, move content
6182 * and adapt in_msglen.
6183 *
6184 * (2) Alert messages:
6185 * Consume whole record content, in_msglen = 0.
6186 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006187 * (3) Change cipher spec:
6188 * Consume whole record content, in_msglen = 0.
6189 *
6190 * (4) Application data:
6191 * Don't do anything - the record layer provides
6192 * the application data as a stream transport
6193 * and consumes through mbedtls_ssl_read only.
6194 *
6195 */
6196
6197 /* Case (1): Handshake messages */
6198 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006199 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006200 /* Hard assertion to be sure that no application data
6201 * is in flight, as corrupting ssl->in_msglen during
6202 * ssl->in_offt != NULL is fatal. */
6203 if( ssl->in_offt != NULL )
6204 {
6205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6207 }
6208
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006209 /*
6210 * Get next Handshake message in the current record
6211 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006212
Hanno Becker4a810fb2017-05-24 16:27:30 +01006213 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006214 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006215 * current handshake content: If DTLS handshake
6216 * fragmentation is used, that's the fragment
6217 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006218 * size here is faulty and should be changed at
6219 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006220 * (2) While it doesn't seem to cause problems, one
6221 * has to be very careful not to assume that in_hslen
6222 * is always <= in_msglen in a sensible communication.
6223 * Again, it's wrong for DTLS handshake fragmentation.
6224 * The following check is therefore mandatory, and
6225 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006226 * Additionally, ssl->in_hslen might be arbitrarily out of
6227 * bounds after handling a DTLS message with an unexpected
6228 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006229 */
6230 if( ssl->in_hslen < ssl->in_msglen )
6231 {
6232 ssl->in_msglen -= ssl->in_hslen;
6233 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6234 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006235
Hanno Becker4a810fb2017-05-24 16:27:30 +01006236 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6237 ssl->in_msg, ssl->in_msglen );
6238 }
6239 else
6240 {
6241 ssl->in_msglen = 0;
6242 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006243
Hanno Becker4a810fb2017-05-24 16:27:30 +01006244 ssl->in_hslen = 0;
6245 }
6246 /* Case (4): Application data */
6247 else if( ssl->in_offt != NULL )
6248 {
6249 return( 0 );
6250 }
6251 /* Everything else (CCS & Alerts) */
6252 else
6253 {
6254 ssl->in_msglen = 0;
6255 }
6256
Hanno Becker1097b342018-08-15 14:09:41 +01006257 return( 0 );
6258}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006259
Hanno Beckere74d5562018-08-15 14:26:08 +01006260static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6261{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006262 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006263 return( 1 );
6264
6265 return( 0 );
6266}
6267
Hanno Becker5f066e72018-08-16 14:56:31 +01006268#if defined(MBEDTLS_SSL_PROTO_DTLS)
6269
6270static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6271{
6272 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6273 if( hs == NULL )
6274 return;
6275
Hanno Becker01315ea2018-08-21 17:22:17 +01006276 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006277 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006278 hs->buffering.total_bytes_buffered -=
6279 hs->buffering.future_record.len;
6280
6281 mbedtls_free( hs->buffering.future_record.data );
6282 hs->buffering.future_record.data = NULL;
6283 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006284}
6285
6286static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6287{
6288 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6289 unsigned char * rec;
6290 size_t rec_len;
6291 unsigned rec_epoch;
6292
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006293 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006294 return( 0 );
6295
6296 if( hs == NULL )
6297 return( 0 );
6298
Hanno Becker5f066e72018-08-16 14:56:31 +01006299 rec = hs->buffering.future_record.data;
6300 rec_len = hs->buffering.future_record.len;
6301 rec_epoch = hs->buffering.future_record.epoch;
6302
6303 if( rec == NULL )
6304 return( 0 );
6305
Hanno Becker4cb782d2018-08-20 11:19:05 +01006306 /* Only consider loading future records if the
6307 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006308 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006309 return( 0 );
6310
Hanno Becker5f066e72018-08-16 14:56:31 +01006311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6312
6313 if( rec_epoch != ssl->in_epoch )
6314 {
6315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6316 goto exit;
6317 }
6318
6319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6320
6321 /* Double-check that the record is not too large */
6322 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6323 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6324 {
6325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6327 }
6328
Teppo Järvelin91d79382019-10-02 09:09:31 +03006329 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006330 ssl->in_left = rec_len;
6331 ssl->next_record_offset = 0;
6332
6333 ssl_free_buffered_record( ssl );
6334
6335exit:
6336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6337 return( 0 );
6338}
6339
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006340static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6341 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006342{
6343 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006344
6345 /* Don't buffer future records outside handshakes. */
6346 if( hs == NULL )
6347 return( 0 );
6348
6349 /* Only buffer handshake records (we are only interested
6350 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006351 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006352 return( 0 );
6353
6354 /* Don't buffer more than one future epoch record. */
6355 if( hs->buffering.future_record.data != NULL )
6356 return( 0 );
6357
Hanno Becker01315ea2018-08-21 17:22:17 +01006358 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006359 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006360 hs->buffering.total_bytes_buffered ) )
6361 {
6362 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 +01006363 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006364 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006365 return( 0 );
6366 }
6367
Hanno Becker5f066e72018-08-16 14:56:31 +01006368 /* Buffer record */
6369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6370 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006371 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006372
6373 /* ssl_parse_record_header() only considers records
6374 * of the next epoch as candidates for buffering. */
6375 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006376 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006377
6378 hs->buffering.future_record.data =
6379 mbedtls_calloc( 1, hs->buffering.future_record.len );
6380 if( hs->buffering.future_record.data == NULL )
6381 {
6382 /* If we run out of RAM trying to buffer a
6383 * record from the next epoch, just ignore. */
6384 return( 0 );
6385 }
6386
Teppo Järvelin91d79382019-10-02 09:09:31 +03006387 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006388
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006389 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006390 return( 0 );
6391}
6392
6393#endif /* MBEDTLS_SSL_PROTO_DTLS */
6394
Hanno Beckere74d5562018-08-15 14:26:08 +01006395static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006396{
6397 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006398 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006399
Hanno Becker5f066e72018-08-16 14:56:31 +01006400#if defined(MBEDTLS_SSL_PROTO_DTLS)
6401 /* We might have buffered a future record; if so,
6402 * and if the epoch matches now, load it.
6403 * On success, this call will set ssl->in_left to
6404 * the length of the buffered record, so that
6405 * the calls to ssl_fetch_input() below will
6406 * essentially be no-ops. */
6407 ret = ssl_load_buffered_record( ssl );
6408 if( ret != 0 )
6409 return( ret );
6410#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006411
Hanno Becker8b09b732019-05-08 12:03:28 +01006412 /* Ensure that we have enough space available for the default form
6413 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6414 * with no space for CIDs counted in). */
6415 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6416 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006419 return( ret );
6420 }
6421
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006422 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6423 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006426 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006427 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006428 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6429 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006430 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006431 if( ret != 0 )
6432 return( ret );
6433
6434 /* Fall through to handling of unexpected records */
6435 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6436 }
6437
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006438 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6439 {
Hanno Becker87b56262019-07-10 14:37:41 +01006440#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006441 /* Reset in pointers to default state for TLS/DTLS records,
6442 * assuming no CID and no offset between record content and
6443 * record plaintext. */
6444 ssl_update_in_pointers( ssl );
6445
Hanno Becker69412452019-07-12 08:33:49 +01006446 /* Setup internal message pointers from record structure. */
6447 ssl->in_msgtype = rec.type;
6448#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6449 ssl->in_len = ssl->in_cid + rec.cid_len;
6450#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006451 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006452 ssl->in_msglen = rec.data_len;
6453
Hanno Becker87b56262019-07-10 14:37:41 +01006454 ret = ssl_check_client_reconnect( ssl );
6455 if( ret != 0 )
6456 return( ret );
6457#endif
6458
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006459 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006460 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006461
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6463 "(header)" ) );
6464 }
6465 else
6466 {
6467 /* Skip invalid record and the rest of the datagram */
6468 ssl->next_record_offset = 0;
6469 ssl->in_left = 0;
6470
6471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6472 "(header)" ) );
6473 }
6474
6475 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006476 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006477 }
Hanno Becker87b56262019-07-10 14:37:41 +01006478 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006479#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006480 {
6481 return( ret );
6482 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006483 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006486 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006487 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006488 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006489 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006490 if( ssl->next_record_offset < ssl->in_left )
6491 {
6492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6493 }
6494 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006495 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006496#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006497#if defined(MBEDTLS_SSL_PROTO_TLS)
6498 {
Hanno Beckere0452772019-07-10 17:12:07 +01006499 /*
6500 * Fetch record contents from underlying transport.
6501 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006502 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006503 if( ret != 0 )
6504 {
6505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6506 return( ret );
6507 }
6508
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006509 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006510 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006511#endif /* MBEDTLS_SSL_PROTO_TLS */
6512
6513 /*
6514 * Decrypt record contents.
6515 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006516
Hanno Beckera89610a2019-07-11 13:07:45 +01006517 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006520 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006521 {
6522 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006523 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006524 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006525 /* Except when waiting for Finished as a bad mac here
6526 * probably means something went wrong in the handshake
6527 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6528 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6529 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6530 {
6531#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6532 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6533 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006534 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006535 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6536 }
6537#endif
6538 return( ret );
6539 }
6540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006542 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6543 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6546 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006547 }
6548#endif
6549
Hanno Becker4a810fb2017-05-24 16:27:30 +01006550 /* As above, invalid records cause
6551 * dismissal of the whole datagram. */
6552
6553 ssl->next_record_offset = 0;
6554 ssl->in_left = 0;
6555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006557 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006558 }
6559
6560 return( ret );
6561 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006562 MBEDTLS_SSL_TRANSPORT_ELSE
6563#endif /* MBEDTLS_SSL_PROTO_DTLS */
6564#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006565 {
6566 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6568 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006569 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006570 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006572 }
6573#endif
6574 return( ret );
6575 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006576#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006577 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006578
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006579
6580 /* Reset in pointers to default state for TLS/DTLS records,
6581 * assuming no CID and no offset between record content and
6582 * record plaintext. */
6583 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006584#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6585 ssl->in_len = ssl->in_cid + rec.cid_len;
6586#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006587 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006588
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006589 /* The record content type may change during decryption,
6590 * so re-read it. */
6591 ssl->in_msgtype = rec.type;
6592 /* Also update the input buffer, because unfortunately
6593 * the server-side ssl_parse_client_hello() reparses the
6594 * record header when receiving a ClientHello initiating
6595 * a renegotiation. */
6596 ssl->in_hdr[0] = rec.type;
6597 ssl->in_msg = rec.buf + rec.data_offset;
6598 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006599 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006600
Simon Butcher99000142016-10-13 17:21:01 +01006601 return( 0 );
6602}
6603
6604int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6605{
6606 int ret;
6607
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006608 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006609 * Handle particular types of records
6610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 {
Simon Butcher99000142016-10-13 17:21:01 +01006613 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6614 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006615 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 }
6618
Hanno Beckere678eaa2018-08-21 14:57:46 +01006619 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006620 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006621 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006622 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6624 ssl->in_msglen ) );
6625 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006626 }
6627
Hanno Beckere678eaa2018-08-21 14:57:46 +01006628 if( ssl->in_msg[0] != 1 )
6629 {
6630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6631 ssl->in_msg[0] ) );
6632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6633 }
6634
6635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006636 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006637 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6638 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6639 {
6640 if( ssl->handshake == NULL )
6641 {
6642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6643 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6644 }
6645
6646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6647 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6648 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006649#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006650 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006654 if( ssl->in_msglen != 2 )
6655 {
6656 /* Note: Standard allows for more than one 2 byte alert
6657 to be packed in a single message, but Mbed TLS doesn't
6658 currently support this. */
6659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6660 ssl->in_msglen ) );
6661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6662 }
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 ssl->in_msg[0], ssl->in_msg[1] ) );
6666
6667 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006668 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006673 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 }
6676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6678 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6681 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006682 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006683
6684#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6685 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6686 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6687 {
Hanno Becker90333da2017-10-10 11:27:13 +01006688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006689 /* Will be handled when trying to parse ServerHello */
6690 return( 0 );
6691 }
6692#endif
6693
6694#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006695 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006696 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6697 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006698 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6699 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6700 {
6701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6702 /* Will be handled in mbedtls_ssl_parse_certificate() */
6703 return( 0 );
6704 }
6705#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6706
6707 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006708 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 }
6710
Hanno Beckerc76c6192017-06-06 10:03:17 +01006711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006712 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006713 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006714 /* Drop unexpected ApplicationData records,
6715 * except at the beginning of renegotiations */
6716 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6717 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6718#if defined(MBEDTLS_SSL_RENEGOTIATION)
6719 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6720 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006721#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006722 )
6723 {
6724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6725 return( MBEDTLS_ERR_SSL_NON_FATAL );
6726 }
6727
6728 if( ssl->handshake != NULL &&
6729 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6730 {
6731 ssl_handshake_wrapup_free_hs_transform( ssl );
6732 }
6733 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006734#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006735
Paul Bakker5121ce52009-01-03 21:22:43 +00006736 return( 0 );
6737}
6738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006740{
6741 int ret;
6742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6744 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6745 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006746 {
6747 return( ret );
6748 }
6749
6750 return( 0 );
6751}
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006754 unsigned char level,
6755 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006756{
6757 int ret;
6758
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006759 if( ssl == NULL || ssl->conf == NULL )
6760 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006766 ssl->out_msglen = 2;
6767 ssl->out_msg[0] = level;
6768 ssl->out_msg[1] = message;
6769
Hanno Becker67bc7c32018-08-06 11:33:50 +01006770 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006773 return( ret );
6774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006776
6777 return( 0 );
6778}
6779
Hanno Becker17572472019-02-08 07:19:04 +00006780#if defined(MBEDTLS_X509_CRT_PARSE_C)
6781static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6782{
6783#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6784 if( session->peer_cert != NULL )
6785 {
6786 mbedtls_x509_crt_free( session->peer_cert );
6787 mbedtls_free( session->peer_cert );
6788 session->peer_cert = NULL;
6789 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006790#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006791 if( session->peer_cert_digest != NULL )
6792 {
6793 /* Zeroization is not necessary. */
6794 mbedtls_free( session->peer_cert_digest );
6795 session->peer_cert_digest = NULL;
6796 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6797 session->peer_cert_digest_len = 0;
6798 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006799#else
6800 ((void) session);
6801#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006802}
6803#endif /* MBEDTLS_X509_CRT_PARSE_C */
6804
Paul Bakker5121ce52009-01-03 21:22:43 +00006805/*
6806 * Handshake functions
6807 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006808#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006809/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811{
Hanno Beckerdf645962019-06-26 13:02:22 +01006812 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6813 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Hanno Becker5097cba2019-02-05 13:36:46 +00006817 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006820 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6821 {
6822 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6823 }
6824 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6825 {
6826 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6827 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006828 else
6829 {
6830 ssl->state = MBEDTLS_SSL_INVALID;
6831 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006832 return( 0 );
6833 }
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006837}
6838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006840{
Hanno Beckerdf645962019-06-26 13:02:22 +01006841 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6842 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006845
Hanno Becker5097cba2019-02-05 13:36:46 +00006846 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006849 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6850 {
6851 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6852 }
6853 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6854 {
6855 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6856 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006857 else
6858 {
6859 ssl->state = MBEDTLS_SSL_INVALID;
6860 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006861 return( 0 );
6862 }
6863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006866}
Gilles Peskinef9828522017-05-03 12:28:43 +02006867
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006868#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006869/* Some certificate support -> implement write and parse */
6870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006874 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006876 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6877 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880
Hanno Becker5097cba2019-02-05 13:36:46 +00006881 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006884 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6885 {
6886 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6887 }
6888 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6889 {
6890 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6891 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006892 else
6893 {
6894 ssl->state = MBEDTLS_SSL_INVALID;
6895 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006896 return( 0 );
6897 }
6898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006900 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6901 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006902 {
6903 if( ssl->client_auth == 0 )
6904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006906 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6907 {
6908 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6909 }
6910 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6911 {
6912 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6913 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006914 else
6915 {
6916 ssl->state = MBEDTLS_SSL_INVALID;
6917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 return( 0 );
6919 }
6920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 /*
6923 * If using SSLv3 and got no cert, send an Alert message
6924 * (otherwise an empty Certificate message will be sent).
6925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006927 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006928 {
6929 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6931 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6932 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 goto write_msg;
6936 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SSL_CLI_C */
6940#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006941 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6946 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 }
6948 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
6953 /*
6954 * 0 . 0 handshake type
6955 * 1 . 3 handshake length
6956 * 4 . 6 length of all certs
6957 * 7 . 9 length of cert. 1
6958 * 10 . n-1 peer certificate
6959 * n . n+2 length of cert. 2
6960 * n+3 . ... upper level cert, etc.
6961 */
6962 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006964
Paul Bakker29087132010-03-21 21:03:34 +00006965 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 {
6967 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006968 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006971 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 }
6974
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006975 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976
Teppo Järvelin91d79382019-10-02 09:09:31 +03006977 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978 i += n; crt = crt->next;
6979 }
6980
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006981 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982
6983 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6985 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006986
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006987#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006988write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
Jarno Lamsa2b205162019-11-12 15:36:21 +02006991 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6992 {
6993 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6994 }
6995 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6996 {
6997 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6998 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006999 else
7000 {
7001 ssl->state = MBEDTLS_SSL_INVALID;
7002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007003
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007004 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007005 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 return( ret );
7008 }
7009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
Paul Bakkered27a042013-04-18 22:46:23 +02007012 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013}
7014
Hanno Becker285ff0c2019-01-31 07:44:03 +00007015#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007016
7017#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7019 unsigned char *crt_buf,
7020 size_t crt_buf_len )
7021{
7022 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7023
7024 if( peer_crt == NULL )
7025 return( -1 );
7026
7027 if( peer_crt->raw.len != crt_buf_len )
7028 return( -1 );
7029
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007030 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007031}
Hanno Becker5882dd02019-06-06 16:25:57 +01007032#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007033static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7034 unsigned char *crt_buf,
7035 size_t crt_buf_len )
7036{
7037 int ret;
7038 unsigned char const * const peer_cert_digest =
7039 ssl->session->peer_cert_digest;
7040 mbedtls_md_type_t const peer_cert_digest_type =
7041 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007042 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007043 mbedtls_md_info_from_type( peer_cert_digest_type );
7044 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7045 size_t digest_len;
7046
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007047 if( peer_cert_digest == NULL ||
7048 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7049 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007050 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007051 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007052
7053 digest_len = mbedtls_md_get_size( digest_info );
7054 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7055 return( -1 );
7056
7057 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7058 if( ret != 0 )
7059 return( -1 );
7060
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007061 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007062}
Hanno Becker5882dd02019-06-06 16:25:57 +01007063#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007064#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007065
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007066/*
7067 * Once the certificate message is read, parse it into a cert chain and
7068 * perform basic checks, but leave actual verification to the caller
7069 */
Hanno Becker35e41772019-02-05 15:37:23 +00007070static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7071 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007072{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007073 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007074#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7075 int crt_cnt=0;
7076#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007077 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007078 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007083 mbedtls_ssl_pend_fatal_alert( ssl,
7084 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086 }
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7089 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007092 mbedtls_ssl_pend_fatal_alert( ssl,
7093 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007095 }
7096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007098
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007102 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007104 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007108 mbedtls_ssl_pend_fatal_alert( ssl,
7109 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 }
7112
Hanno Becker33c3dc82019-01-30 14:46:46 +00007113 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7114 i += 3;
7115
Hanno Becker33c3dc82019-01-30 14:46:46 +00007116 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 while( i < ssl->in_hslen )
7118 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007119 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007120 if ( i + 3 > ssl->in_hslen ) {
7121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007122 mbedtls_ssl_pend_fatal_alert( ssl,
7123 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007124 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7125 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007126 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7127 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 if( ssl->in_msg[i] != 0 )
7129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007131 mbedtls_ssl_pend_fatal_alert( ssl,
7132 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 }
7135
Hanno Becker33c3dc82019-01-30 14:46:46 +00007136 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007137 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007138 i += 3;
7139
7140 if( n < 128 || i + n > ssl->in_hslen )
7141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007143 mbedtls_ssl_pend_fatal_alert( ssl,
7144 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007146 }
7147
Hanno Becker33c3dc82019-01-30 14:46:46 +00007148 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007149#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7150 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007151 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7152 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007153 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007154 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007155 /* During client-side renegotiation, check that the server's
7156 * end-CRTs hasn't changed compared to the initial handshake,
7157 * mitigating the triple handshake attack. On success, reuse
7158 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7160 if( ssl_check_peer_crt_unchanged( ssl,
7161 &ssl->in_msg[i],
7162 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007163 {
Hanno Becker35e41772019-02-05 15:37:23 +00007164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007165 mbedtls_ssl_pend_fatal_alert( ssl,
7166 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007167 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007168 }
Hanno Becker35e41772019-02-05 15:37:23 +00007169
7170 /* Now we can safely free the original chain. */
7171 ssl_clear_peer_cert( ssl->session );
7172 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007173#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7174
Hanno Becker33c3dc82019-01-30 14:46:46 +00007175 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007176#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007177 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007178#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007179 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007180 * it in-place from the input buffer instead of making a copy. */
7181 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7182#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007183 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007184 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007185 case 0: /*ok*/
7186 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7187 /* Ignore certificate with an unknown algorithm: maybe a
7188 prior certificate was already trusted. */
7189 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007190
Hanno Becker33c3dc82019-01-30 14:46:46 +00007191 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7192 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7193 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007194
Hanno Becker33c3dc82019-01-30 14:46:46 +00007195 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7196 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7197 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007198
Hanno Becker33c3dc82019-01-30 14:46:46 +00007199 default:
7200 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7201 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007202 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007203 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7204 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007205 }
7206
7207 i += n;
7208 }
7209
Hanno Becker35e41772019-02-05 15:37:23 +00007210 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007211 return( 0 );
7212}
7213
Hanno Beckerb8a08572019-02-05 12:49:06 +00007214#if defined(MBEDTLS_SSL_SRV_C)
7215static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7216{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007217 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007218 return( -1 );
7219
7220#if defined(MBEDTLS_SSL_PROTO_SSL3)
7221 /*
7222 * Check if the client sent an empty certificate
7223 */
Hanno Becker2881d802019-05-22 14:44:53 +01007224 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007225 {
7226 if( ssl->in_msglen == 2 &&
7227 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7228 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7229 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7230 {
7231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7232 return( 0 );
7233 }
7234
7235 return( -1 );
7236 }
7237#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7238
7239#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7240 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7241 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7242 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7243 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007244 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 +00007245 {
7246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7247 return( 0 );
7248 }
7249
Hanno Beckerb8a08572019-02-05 12:49:06 +00007250#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7251 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007252
7253 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007254}
7255#endif /* MBEDTLS_SSL_SRV_C */
7256
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007257/* Check if a certificate message is expected.
7258 * Return either
7259 * - SSL_CERTIFICATE_EXPECTED, or
7260 * - SSL_CERTIFICATE_SKIP
7261 * indicating whether a Certificate message is expected or not.
7262 */
7263#define SSL_CERTIFICATE_EXPECTED 0
7264#define SSL_CERTIFICATE_SKIP 1
7265static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7266 int authmode )
7267{
Hanno Becker473f98f2019-06-26 10:27:32 +01007268 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007269 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007270
7271 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7272 return( SSL_CERTIFICATE_SKIP );
7273
7274#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007275 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007276 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007277 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7278 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7279 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007280 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007281 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007282
7283 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7284 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007285 ssl->session_negotiate->verify_result =
7286 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7287 return( SSL_CERTIFICATE_SKIP );
7288 }
7289 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007290#else
7291 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007292#endif /* MBEDTLS_SSL_SRV_C */
7293
7294 return( SSL_CERTIFICATE_EXPECTED );
7295}
7296
Hanno Becker3cf50612019-02-05 14:36:34 +00007297static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007298 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007299 mbedtls_x509_crt *chain,
7300 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007301{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007302 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7303 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7304 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007305 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007306 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007307 mbedtls_x509_crt *ca_chain;
7308 mbedtls_x509_crl *ca_crl;
7309
7310 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007311 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007312 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007313 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007314
Jarno Lamsae1621d42019-12-19 08:58:56 +02007315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007316#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7317 if( ssl->handshake->sni_ca_chain != NULL )
7318 {
7319 ca_chain = ssl->handshake->sni_ca_chain;
7320 ca_crl = ssl->handshake->sni_ca_crl;
7321 }
7322 else
7323#endif
7324 {
7325 ca_chain = ssl->conf->ca_chain;
7326 ca_crl = ssl->conf->ca_crl;
7327 }
7328
7329 /*
7330 * Main check: verify certificate
7331 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007332 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007333 chain,
7334 ca_chain, ca_crl,
7335 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007336#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007337 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007338#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007339 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007340#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7341 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7342#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7343 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007344
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007345 if( verify_ret == 0 )
7346 {
7347 mbedtls_platform_enforce_volatile_reads();
7348 if( verify_ret == 0 )
7349 {
7350 flow_counter++;
7351 }
7352 else
7353 {
7354 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7355 }
7356 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007357 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007358 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007359 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007360 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007361 }
7362
7363#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007364 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007365 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7366#endif
7367
7368 /*
7369 * Secondary checks: always done, but change 'ret' only if it was 0
7370 */
7371
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007372#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007373 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007374#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007375 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007376#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007377 mbedtls_pk_context *pk;
7378 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7379 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007380 {
7381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007382 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007383 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007384
7385 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007386 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007387 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007388 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007389 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007390
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007391 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007392#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007393
7394 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007395 {
7396 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7397
7398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007399 if( verify_ret == 0 )
7400 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007401 flow_counter++;
7402 }
7403 if( ret == 0 )
7404 {
7405 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007406 }
7407 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007408#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007409
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007410 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007411 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007412 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7413 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007414 &ssl->session_negotiate->verify_result );
7415 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007416 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007417 flow_counter++;
7418 }
7419 else
7420 {
7421 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007423 if( verify_ret == 0 )
7424 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007425 }
7426
7427 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7428 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7429 * with details encoded in the verification flags. All other kinds
7430 * of error codes, including those from the user provided f_vrfy
7431 * functions, are treated as fatal and lead to a failure of
7432 * ssl_parse_certificate even if verification was optional. */
7433 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007434 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7435 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007436 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007437 mbedtls_platform_enforce_volatile_reads();
7438 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7439 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7440 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7441 {
7442 verify_ret = 0;
7443 flow_counter++;
7444 }
7445 else
7446 {
7447 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7448 }
7449 } else {
7450 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007451 }
7452
7453 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7454 {
7455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007456 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007457 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007458 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007459 else
7460 {
7461 flow_counter++;
7462 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007463
Hanno Becker8c13ee62019-02-26 16:48:17 +00007464 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007465 {
7466 uint8_t alert;
7467
7468 /* The certificate may have been rejected for several reasons.
7469 Pick one and send the corresponding alert. Which alert to send
7470 may be a subject of debate in some cases. */
7471 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7472 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7473 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7474 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7475 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7476 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7477 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7478 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7479 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7480 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7481 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7482 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7483 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7484 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7485 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7486 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7487 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7488 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7489 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7490 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7491 else
7492 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007493 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007494 }
7495
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007496 if( verify_ret == 0 &&
7497#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7498 flow_counter == 5 )
7499#else
7500 flow_counter == 4 )
7501#endif
7502 {
7503 mbedtls_platform_enforce_volatile_reads();
7504 if( verify_ret == 0 &&
7505#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7506 flow_counter == 5 )
7507#else
7508 flow_counter == 4 )
7509#endif
7510 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007512 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7513 }
7514 else
7515 {
7516 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7517 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007518 } else {
7519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007520 }
7521
Hanno Becker3cf50612019-02-05 14:36:34 +00007522#if defined(MBEDTLS_DEBUG_C)
7523 if( ssl->session_negotiate->verify_result != 0 )
7524 {
7525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7526 ssl->session_negotiate->verify_result ) );
7527 }
7528 else
7529 {
7530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7531 }
7532#endif /* MBEDTLS_DEBUG_C */
7533
Hanno Becker8c13ee62019-02-26 16:48:17 +00007534 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007535}
7536
Hanno Becker34106f62019-02-08 14:59:05 +00007537#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007538
7539#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007540static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7541 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007542{
7543 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007544 /* Remember digest of the peer's end-CRT. */
7545 ssl->session_negotiate->peer_cert_digest =
7546 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7547 if( ssl->session_negotiate->peer_cert_digest == NULL )
7548 {
7549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7550 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007551 mbedtls_ssl_pend_fatal_alert( ssl,
7552 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007553
7554 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7555 }
7556
7557 ret = mbedtls_md( mbedtls_md_info_from_type(
7558 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7559 start, len,
7560 ssl->session_negotiate->peer_cert_digest );
7561
7562 ssl->session_negotiate->peer_cert_digest_type =
7563 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7564 ssl->session_negotiate->peer_cert_digest_len =
7565 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7566
7567 return( ret );
7568}
Hanno Becker5882dd02019-06-06 16:25:57 +01007569#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007570
7571static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7572 unsigned char *start, size_t len )
7573{
7574 unsigned char *end = start + len;
7575 int ret;
7576
7577 /* Make a copy of the peer's raw public key. */
7578 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7579 ret = mbedtls_pk_parse_subpubkey( &start, end,
7580 &ssl->handshake->peer_pubkey );
7581 if( ret != 0 )
7582 {
7583 /* We should have parsed the public key before. */
7584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7585 }
7586
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007587 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007588 return( 0 );
7589}
7590#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7591
Hanno Becker3cf50612019-02-05 14:36:34 +00007592int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7593{
7594 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007595 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007596#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7597 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7598 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007599 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007600#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007601 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007602#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007603 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007604 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007605
7606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7607
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007608 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7609 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007610 {
7611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007612 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Becker613d4902019-02-05 13:11:17 +00007613 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007614 }
7615
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007616#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7617 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007618 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007619 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007620 chain = ssl->handshake->ecrs_peer_cert;
7621 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007622 goto crt_verify;
7623 }
7624#endif
7625
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007626 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007627 {
7628 /* mbedtls_ssl_read_record may have sent an alert already. We
7629 let it decide whether to alert. */
7630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007631 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007632 }
7633
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007634#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007635 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7636 {
7637 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007638
Hanno Beckerb8a08572019-02-05 12:49:06 +00007639 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007640 ret = 0;
7641 else
7642 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007643
Hanno Becker613d4902019-02-05 13:11:17 +00007644 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007645 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007646#endif /* MBEDTLS_SSL_SRV_C */
7647
Hanno Becker35e41772019-02-05 15:37:23 +00007648 /* Clear existing peer CRT structure in case we tried to
7649 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007650 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007651
7652 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7653 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007654 {
7655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7656 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007657 mbedtls_ssl_pend_fatal_alert( ssl,
7658 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007659
Hanno Beckere4aeb762019-02-05 17:19:52 +00007660 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7661 goto exit;
7662 }
7663 mbedtls_x509_crt_init( chain );
7664
7665 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007666 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007667 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007668
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007669#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7670 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007671 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007672
7673crt_verify:
7674 if( ssl->handshake->ecrs_enabled)
7675 rs_ctx = &ssl->handshake->ecrs_ctx;
7676#endif
7677
Hanno Becker3cf50612019-02-05 14:36:34 +00007678 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007679 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007680 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007681 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
Hanno Becker3008d282019-02-05 17:02:28 +00007683#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007684 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007685 size_t pk_len;
7686 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007687
Hanno Becker34106f62019-02-08 14:59:05 +00007688 /* We parse the CRT chain without copying, so
7689 * these pointers point into the input buffer,
7690 * and are hence still valid after freeing the
7691 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Hanno Becker5882dd02019-06-06 16:25:57 +01007693#if defined(MBEDTLS_SSL_RENEGOTIATION)
7694 unsigned char *crt_start;
7695 size_t crt_len;
7696
Hanno Becker34106f62019-02-08 14:59:05 +00007697 crt_start = chain->raw.p;
7698 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007699#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007700
Hanno Becker8c13ee62019-02-26 16:48:17 +00007701 pk_start = chain->cache->pk_raw.p;
7702 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007703
7704 /* Free the CRT structures before computing
7705 * digest and copying the peer's public key. */
7706 mbedtls_x509_crt_free( chain );
7707 mbedtls_free( chain );
7708 chain = NULL;
7709
Hanno Becker5882dd02019-06-06 16:25:57 +01007710#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007711 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007712 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007713 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007714#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007715
Hanno Becker34106f62019-02-08 14:59:05 +00007716 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007718 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719 }
Hanno Becker34106f62019-02-08 14:59:05 +00007720#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7721 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007722 ssl->session_negotiate->peer_cert = chain;
7723 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007724#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007727
Hanno Becker613d4902019-02-05 13:11:17 +00007728exit:
7729
Hanno Beckere4aeb762019-02-05 17:19:52 +00007730 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007731 {
7732 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7733 {
7734 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7735 }
7736 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7737 {
7738 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7739 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007740 else
7741 {
7742 ssl->state = MBEDTLS_SSL_INVALID;
7743 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007744 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007745
7746#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7747 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7748 {
7749 ssl->handshake->ecrs_peer_cert = chain;
7750 chain = NULL;
7751 }
7752#endif
7753
7754 if( chain != NULL )
7755 {
7756 mbedtls_x509_crt_free( chain );
7757 mbedtls_free( chain );
7758 }
7759
Paul Bakker5121ce52009-01-03 21:22:43 +00007760 return( ret );
7761}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007762#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007765{
7766 int ret;
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007771 ssl->out_msglen = 1;
7772 ssl->out_msg[0] = 1;
7773
Jarno Lamsa2b205162019-11-12 15:36:21 +02007774 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7775 {
7776 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7777 }
7778 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7779 {
7780 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7781 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007782 else
7783 {
7784 ssl->state = MBEDTLS_SSL_INVALID;
7785 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007786
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007787 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007790 return( ret );
7791 }
7792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007794
7795 return( 0 );
7796}
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007799{
7800 int ret;
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
Hanno Becker327c93b2018-08-15 13:56:18 +01007804 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 return( ret );
7808 }
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007813 mbedtls_ssl_pend_fatal_alert( ssl,
7814 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 }
7817
Hanno Beckere678eaa2018-08-21 14:57:46 +01007818 /* CCS records are only accepted if they have length 1 and content '1',
7819 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007821 /*
7822 * Switch to our negotiated transform and session parameters for inbound
7823 * data.
7824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007826 ssl->transform_in = ssl->transform_negotiate;
7827 ssl->session_in = ssl->session_negotiate;
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007830 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007833 ssl_dtls_replay_reset( ssl );
7834#endif
7835
7836 /* Increment epoch */
7837 if( ++ssl->in_epoch == 0 )
7838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007840 /* This is highly unlikely to happen for legitimate reasons, so
7841 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007843 }
7844 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007845 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007847#if defined(MBEDTLS_SSL_PROTO_TLS)
7848 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007849 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007850 }
7851#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007852
Hanno Beckerf5970a02019-05-08 09:38:41 +01007853 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7856 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007861 mbedtls_ssl_pend_fatal_alert( ssl,
7862 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007864 }
7865 }
7866#endif
7867
Jarno Lamsa2b205162019-11-12 15:36:21 +02007868 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7869 {
7870 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7871 }
7872 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7873 {
7874 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7875 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007876 else
7877 {
7878 ssl->state = MBEDTLS_SSL_INVALID;
7879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007882
7883 return( 0 );
7884}
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7889 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007890 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007891 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7894#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007895 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007896#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007898 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007901}
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007906
7907 /*
7908 * Free our handshake params
7909 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007910 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007912 ssl->handshake = NULL;
7913
7914 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007915 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007916 */
7917 if( ssl->transform )
7918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 mbedtls_ssl_transform_free( ssl->transform );
7920 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007921 }
7922 ssl->transform = ssl->transform_negotiate;
7923 ssl->transform_negotiate = NULL;
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007926}
7927
Jarno Lamsae1621d42019-12-19 08:58:56 +02007928int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007929{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007930 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007931
7932#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7933 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7934 ? ssl->handshake->sni_authmode
7935 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7936#else
7937 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
7938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_RENEGOTIATION)
7942 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007945 ssl->renego_records_seen = 0;
7946 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007947#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948
7949 /*
7950 * Free the previous session and switch in the current one
7951 */
Paul Bakker0a597072012-09-25 21:55:46 +00007952 if( ssl->session )
7953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007955 /* RFC 7366 3.1: keep the EtM state */
7956 ssl->session_negotiate->encrypt_then_mac =
7957 ssl->session->encrypt_then_mac;
7958#endif
7959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 mbedtls_ssl_session_free( ssl->session );
7961 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007962 }
7963 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007964 ssl->session_negotiate = NULL;
7965
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007966#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007967 /*
7968 * Add cache entry
7969 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007970 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007971 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007972 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007973 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007974 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007976 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007977#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007978
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007979 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7980 {
7981 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7982 {
7983 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7984 }
7985 else
7986 {
7987 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
7988 goto cleanup;
7989 }
7990 }
7991
Jarno Lamsae1621d42019-12-19 08:58:56 +02007992#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007993 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007994 {
7995 mbedtls_platform_enforce_volatile_reads();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02007996 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007997 {
Jarno Lamsa06164052019-12-19 14:40:36 +02007998 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02007999 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008000 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008001 }
8002 else
8003 {
8004 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008005 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008006 }
8007 }
8008#endif
8009
8010 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8011 {
8012 mbedtls_platform_enforce_volatile_reads();
8013 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8014 {
8015 ret = 0;
8016 }
8017 else
8018 {
8019 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008020 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008021 }
8022 }
8023 else
8024 {
8025 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008026 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008027 }
8028
Jarno Lamsa06164052019-12-19 14:40:36 +02008029 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8030 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8031 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8032 {
8033 mbedtls_platform_enforce_volatile_reads();
8034 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8035 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8036 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8037 {
8038 ret = 0;
8039 }
8040 else
8041 {
8042 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8043 goto cleanup;
8044 }
8045 }
8046 else
8047 {
8048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8051 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8052 }
8053
8054cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008056 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008057 ssl->handshake->flight != NULL )
8058 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008059 /* Cancel handshake timer */
8060 ssl_set_timer( ssl, 0 );
8061
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008062 /* Keep last flight around in case we need to resend it:
8063 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008065 }
8066 else
8067#endif
8068 ssl_handshake_wrapup_free_hs_transform( ssl );
8069
Jarno Lamsa2b205162019-11-12 15:36:21 +02008070 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008073 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008074}
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008077{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008078 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008081
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008082 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008083
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008084 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8085 mbedtls_ssl_suite_get_mac(
8086 mbedtls_ssl_ciphersuite_from_id(
8087 mbedtls_ssl_session_get_ciphersuite(
8088 ssl->session_negotiate ) ) ),
8089 ssl, ssl->out_msg + 4,
8090 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008091
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008092 /*
8093 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8094 * may define some other value. Currently (early 2016), no defined
8095 * ciphersuite does this (and this is unlikely to change as activity has
8096 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8097 */
Hanno Becker2881d802019-05-22 14:44:53 +01008098 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008101 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008102 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008103#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008104
Paul Bakker5121ce52009-01-03 21:22:43 +00008105 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8107 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008108
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008109#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008110 /*
8111 * In case of session resuming, invert the client and server
8112 * ChangeCipherSpec messages order.
8113 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008114 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008117 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8118 MBEDTLS_SSL_IS_CLIENT )
8119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008121 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008124 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8125 MBEDTLS_SSL_IS_SERVER )
8126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008128 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008130 }
8131 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008132#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008133 {
8134 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8135 {
8136 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8137 }
8138 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8139 {
8140 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8141 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008142 else
8143 {
8144 ssl->state = MBEDTLS_SSL_INVALID;
8145 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008147
Paul Bakker48916f92012-09-16 19:57:18 +00008148 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008149 * Switch to our negotiated transform and session parameters for outbound
8150 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008155 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008156 {
8157 unsigned char i;
8158
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008159 /* Remember current epoch settings for resending */
8160 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008161 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008162
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008163 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008164 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008165
8166 /* Increment epoch */
8167 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008168 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008169 break;
8170
8171 /* The loop goes to its end iff the counter is wrapping */
8172 if( i == 0 )
8173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8175 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008176 }
8177 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008178 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008180#if defined(MBEDTLS_SSL_PROTO_TLS)
8181 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008182 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008183 }
8184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008185
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008186 ssl->transform_out = ssl->transform_negotiate;
8187 ssl->session_out = ssl->session_negotiate;
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8190 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8195 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008196 }
8197 }
8198#endif
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008201 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008203#endif
8204
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008205 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008206 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008208 return( ret );
8209 }
8210
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008212 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008213 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8214 {
8215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8216 return( ret );
8217 }
8218#endif
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
8222 return( 0 );
8223}
8224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008226#define SSL_MAX_HASH_LEN 36
8227#else
8228#define SSL_MAX_HASH_LEN 12
8229#endif
8230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008232{
Paul Bakker23986e52011-04-24 08:57:21 +00008233 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008234 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008235 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008239 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8240 mbedtls_ssl_suite_get_mac(
8241 mbedtls_ssl_ciphersuite_from_id(
8242 mbedtls_ssl_session_get_ciphersuite(
8243 ssl->session_negotiate ) ) ),
8244 ssl, buf,
8245 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008246
Hanno Becker327c93b2018-08-15 13:56:18 +01008247 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008250 return( ret );
8251 }
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008256 mbedtls_ssl_pend_fatal_alert( ssl,
8257 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008259 }
8260
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008261 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008263 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008264 hash_len = 36;
8265 else
8266#endif
8267 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8270 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008273 mbedtls_ssl_pend_fatal_alert( ssl,
8274 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008276 }
8277
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008278 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008279 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008282 mbedtls_ssl_pend_fatal_alert( ssl,
8283 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008285 }
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008288 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008289 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008290#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008291
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008292#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008293 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008296 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008300 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008303 }
8304 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008305#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008306 {
8307 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8308 {
8309 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8310 }
8311 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8312 {
8313 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8314 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008315 else
8316 {
8317 ssl->state = MBEDTLS_SSL_INVALID;
8318 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008322 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008324#endif
8325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008327
8328 return( 0 );
8329}
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8336 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8337 mbedtls_md5_init( &handshake->fin_md5 );
8338 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008339 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8340 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8343#if defined(MBEDTLS_SHA256_C)
8344 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008345 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SHA512_C)
8348 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008349 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008352
Hanno Becker7e5437a2017-04-28 17:15:26 +01008353#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8354 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8355 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8356#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_DHM_C)
8359 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_ECDH_C)
8362 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008363#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008364#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008365 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008366#if defined(MBEDTLS_SSL_CLI_C)
8367 handshake->ecjpake_cache = NULL;
8368 handshake->ecjpake_cache_len = 0;
8369#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008370#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008371
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008372#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008373 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008374#endif
8375
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008376#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8377 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8378#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008379
8380#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8381 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8382 mbedtls_pk_init( &handshake->peer_pubkey );
8383#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008384}
8385
Hanno Becker611a83b2018-01-03 14:27:32 +00008386void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8391 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008392
Hanno Becker92231322018-01-03 15:32:51 +00008393#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394 mbedtls_md_init( &transform->md_ctx_enc );
8395 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008396#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008397}
8398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008402}
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008405{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008406 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008407 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008409 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008411 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008412 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008413
8414 /*
8415 * Either the pointers are now NULL or cleared properly and can be freed.
8416 * Now allocate missing structures.
8417 */
8418 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008419 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008420 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008421 }
Paul Bakker48916f92012-09-16 19:57:18 +00008422
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008423 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008424 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008425 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008426 }
Paul Bakker48916f92012-09-16 19:57:18 +00008427
Paul Bakker82788fb2014-10-20 13:59:19 +02008428 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008429 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008430 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008431 }
Paul Bakker48916f92012-09-16 19:57:18 +00008432
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008433 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008434 if( ssl->handshake == NULL ||
8435 ssl->transform_negotiate == NULL ||
8436 ssl->session_negotiate == NULL )
8437 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 mbedtls_free( ssl->handshake );
8441 mbedtls_free( ssl->transform_negotiate );
8442 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008443
8444 ssl->handshake = NULL;
8445 ssl->transform_negotiate = NULL;
8446 ssl->session_negotiate = NULL;
8447
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008448 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008449 }
8450
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008451 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008453 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008454 ssl_handshake_params_init( ssl->handshake );
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008457 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008458 {
8459 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008460
Hanno Becker2d9623f2019-06-13 12:07:05 +01008461 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008462 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8463 else
8464 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8465 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008466#endif
8467
Paul Bakker48916f92012-09-16 19:57:18 +00008468 return( 0 );
8469}
8470
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008471#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008472/* Dummy cookie callbacks for defaults */
8473static int ssl_cookie_write_dummy( void *ctx,
8474 unsigned char **p, unsigned char *end,
8475 const unsigned char *cli_id, size_t cli_id_len )
8476{
8477 ((void) ctx);
8478 ((void) p);
8479 ((void) end);
8480 ((void) cli_id);
8481 ((void) cli_id_len);
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008484}
8485
8486static int ssl_cookie_check_dummy( void *ctx,
8487 const unsigned char *cookie, size_t cookie_len,
8488 const unsigned char *cli_id, size_t cli_id_len )
8489{
8490 ((void) ctx);
8491 ((void) cookie);
8492 ((void) cookie_len);
8493 ((void) cli_id);
8494 ((void) cli_id_len);
8495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008497}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008498#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008499
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008500/* Once ssl->out_hdr as the address of the beginning of the
8501 * next outgoing record is set, deduce the other pointers.
8502 *
8503 * Note: For TLS, we save the implicit record sequence number
8504 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8505 * and the caller has to make sure there's space for this.
8506 */
8507
8508static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8509 mbedtls_ssl_transform *transform )
8510{
8511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008513 {
8514 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008515#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008516 ssl->out_cid = ssl->out_ctr + 8;
8517 ssl->out_len = ssl->out_cid;
8518 if( transform != NULL )
8519 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008520#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008521 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008522#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008523 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008524 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008525 MBEDTLS_SSL_TRANSPORT_ELSE
8526#endif /* MBEDTLS_SSL_PROTO_DTLS */
8527#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008528 {
8529 ssl->out_ctr = ssl->out_hdr - 8;
8530 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008532 ssl->out_cid = ssl->out_len;
8533#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008534 ssl->out_iv = ssl->out_hdr + 5;
8535 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008536#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008537
8538 /* Adjust out_msg to make space for explicit IV, if used. */
8539 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008540 mbedtls_ssl_ver_geq(
8541 mbedtls_ssl_get_minor_ver( ssl ),
8542 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008543 {
8544 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8545 }
8546 else
8547 ssl->out_msg = ssl->out_iv;
8548}
8549
8550/* Once ssl->in_hdr as the address of the beginning of the
8551 * next incoming record is set, deduce the other pointers.
8552 *
8553 * Note: For TLS, we save the implicit record sequence number
8554 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8555 * and the caller has to make sure there's space for this.
8556 */
8557
Hanno Beckerf5970a02019-05-08 09:38:41 +01008558static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008559{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008560 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008561 * of unprotected TLS/DTLS records, with ssl->in_msg
8562 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008563 *
8564 * When decrypting a protected record, ssl->in_msg
8565 * will be shifted to point to the beginning of the
8566 * record plaintext.
8567 */
8568
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008570 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008571 {
Hanno Becker70e79282019-05-03 14:34:53 +01008572 /* This sets the header pointers to match records
8573 * without CID. When we receive a record containing
8574 * a CID, the fields are shifted accordingly in
8575 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008576 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008577#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008578 ssl->in_cid = ssl->in_ctr + 8;
8579 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008580#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008581 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008582#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008583 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008584 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008585 MBEDTLS_SSL_TRANSPORT_ELSE
8586#endif /* MBEDTLS_SSL_PROTO_DTLS */
8587#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008588 {
8589 ssl->in_ctr = ssl->in_hdr - 8;
8590 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008592 ssl->in_cid = ssl->in_len;
8593#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008594 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008595 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008596#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008597}
8598
Paul Bakker5121ce52009-01-03 21:22:43 +00008599/*
8600 * Initialize an SSL context
8601 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008602void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8603{
8604 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8605}
8606
8607/*
8608 * Setup an SSL context
8609 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008610
8611static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8612{
8613 /* Set the incoming and outgoing record pointers. */
8614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008615 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008616 {
8617 ssl->out_hdr = ssl->out_buf;
8618 ssl->in_hdr = ssl->in_buf;
8619 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008620 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008622#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008623 {
8624 ssl->out_hdr = ssl->out_buf + 8;
8625 ssl->in_hdr = ssl->in_buf + 8;
8626 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008627#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008628
8629 /* Derive other internal pointers. */
8630 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008631 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008632}
8633
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008634int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008635 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008636{
Paul Bakker48916f92012-09-16 19:57:18 +00008637 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008638
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008639 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008640
Hanno Beckeref982d52019-07-23 15:56:18 +01008641#if defined(MBEDTLS_USE_TINYCRYPT)
8642 uECC_set_rng( &uecc_rng_wrapper );
8643#endif
8644
Paul Bakker62f2dee2012-09-28 07:31:51 +00008645 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008646 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008647 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008648
8649 /* Set to NULL in case of an error condition */
8650 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008651
Angus Grattond8213d02016-05-25 20:56:48 +10008652 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8653 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008654 {
Angus Grattond8213d02016-05-25 20:56:48 +10008655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008656 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008657 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008658 }
8659
8660 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8661 if( ssl->out_buf == NULL )
8662 {
8663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008664 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008665 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008666 }
8667
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008668 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008669
Paul Bakker48916f92012-09-16 19:57:18 +00008670 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008671 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008672
Hanno Beckerc8f52992019-07-25 11:15:08 +01008673 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008674
Paul Bakker5121ce52009-01-03 21:22:43 +00008675 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008676
8677error:
8678 mbedtls_free( ssl->in_buf );
8679 mbedtls_free( ssl->out_buf );
8680
8681 ssl->conf = NULL;
8682
8683 ssl->in_buf = NULL;
8684 ssl->out_buf = NULL;
8685
8686 ssl->in_hdr = NULL;
8687 ssl->in_ctr = NULL;
8688 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008689 ssl->in_msg = NULL;
8690
8691 ssl->out_hdr = NULL;
8692 ssl->out_ctr = NULL;
8693 ssl->out_len = NULL;
8694 ssl->out_iv = NULL;
8695 ssl->out_msg = NULL;
8696
k-stachowiak9f7798e2018-07-31 16:52:32 +02008697 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008698}
8699
8700/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008701 * Reset an initialized and used SSL context for re-use while retaining
8702 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008703 *
8704 * If partial is non-zero, keep data in the input buffer and client ID.
8705 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008706 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008707static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008708{
Paul Bakker48916f92012-09-16 19:57:18 +00008709 int ret;
8710
Hanno Becker7e772132018-08-10 12:38:21 +01008711#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8712 !defined(MBEDTLS_SSL_SRV_C)
8713 ((void) partial);
8714#endif
8715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008717
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008718 /* Cancel any possibly running timer */
8719 ssl_set_timer( ssl, 0 );
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_SSL_RENEGOTIATION)
8722 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008723 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008724
8725 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008726 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8727 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008730
Paul Bakker7eb013f2011-10-06 12:37:39 +00008731 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008732 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008733
8734 ssl->in_msgtype = 0;
8735 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008737 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008738 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008741 ssl_dtls_replay_reset( ssl );
8742#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008743
8744 ssl->in_hslen = 0;
8745 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008746
8747 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008748
8749 ssl->out_msgtype = 0;
8750 ssl->out_msglen = 0;
8751 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8753 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008754 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008755#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008756
Hanno Becker19859472018-08-06 09:40:20 +01008757 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8758
Paul Bakker48916f92012-09-16 19:57:18 +00008759 ssl->transform_in = NULL;
8760 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008761
Hanno Becker78640902018-08-13 16:35:15 +01008762 ssl->session_in = NULL;
8763 ssl->session_out = NULL;
8764
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008765 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008766
8767#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008768 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008769#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8770 {
8771 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008772 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008773 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8776 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8779 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8782 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008783 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008784 }
8785#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008786
Paul Bakker48916f92012-09-16 19:57:18 +00008787 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789 mbedtls_ssl_transform_free( ssl->transform );
8790 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008791 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008792 }
Paul Bakker48916f92012-09-16 19:57:18 +00008793
Paul Bakkerc0463502013-02-14 11:19:38 +01008794 if( ssl->session )
8795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 mbedtls_ssl_session_free( ssl->session );
8797 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008798 ssl->session = NULL;
8799 }
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008802 ssl->alpn_chosen = NULL;
8803#endif
8804
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008805#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008806#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008807 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008808#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008809 {
8810 mbedtls_free( ssl->cli_id );
8811 ssl->cli_id = NULL;
8812 ssl->cli_id_len = 0;
8813 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008814#endif
8815
Paul Bakker48916f92012-09-16 19:57:18 +00008816 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8817 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008818
8819 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008820}
8821
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008822/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008823 * Reset an initialized and used SSL context for re-use while retaining
8824 * all application-set variables, function pointers and data.
8825 */
8826int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8827{
8828 return( ssl_session_reset_int( ssl, 0 ) );
8829}
8830
8831/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008832 * SSL set accessors
8833 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008834#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008835void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008836{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008837 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008838}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008839#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008840
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008841void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008842{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008843 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008844}
8845
Hanno Becker7f376f42019-06-12 16:20:48 +01008846#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8847 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008848void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008849{
Hanno Becker7f376f42019-06-12 16:20:48 +01008850 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008851}
Hanno Becker7f376f42019-06-12 16:20:48 +01008852#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008853
Hanno Beckerde671542019-06-12 16:30:46 +01008854#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8855 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8856void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8857 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008858{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008859 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008860}
Hanno Beckerde671542019-06-12 16:30:46 +01008861#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008864
Hanno Becker1841b0a2018-08-24 11:13:57 +01008865void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8866 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008867{
8868 ssl->disable_datagram_packing = !allow_packing;
8869}
8870
Hanno Becker1f835fa2019-06-13 10:14:59 +01008871#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8872 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008873void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8874 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008876 conf->hs_timeout_min = min;
8877 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008878}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008879#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8880 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8881void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8882 uint32_t min, uint32_t max )
8883{
8884 ((void) conf);
8885 ((void) min);
8886 ((void) max);
8887}
8888#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8889 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8890
8891#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008892
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008893void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008894{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008895#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8896 conf->authmode = authmode;
8897#else
8898 ((void) conf);
8899 ((void) authmode);
8900#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008901}
8902
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008903#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8904 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008905void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008906 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008907 void *p_vrfy )
8908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008909 conf->f_vrfy = f_vrfy;
8910 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008911}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008912#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008913
Hanno Beckerece325c2019-06-13 15:39:27 +01008914#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008915void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008916 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008917 void *p_rng )
8918{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008919 conf->f_rng = f_rng;
8920 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008921}
Hanno Beckerece325c2019-06-13 15:39:27 +01008922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008923
Hanno Becker14a4a442019-07-02 17:00:34 +01008924#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008925void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008926 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008927 void *p_dbg )
8928{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008929 conf->f_dbg = f_dbg;
8930 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008931}
Hanno Becker14a4a442019-07-02 17:00:34 +01008932#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008933
Hanno Beckera58a8962019-06-13 16:11:15 +01008934#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8935 !defined(MBEDTLS_SSL_CONF_SEND) && \
8936 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008938 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008939 mbedtls_ssl_send_t *f_send,
8940 mbedtls_ssl_recv_t *f_recv,
8941 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008942{
Hanno Beckera58a8962019-06-13 16:11:15 +01008943 ssl->p_bio = p_bio;
8944 ssl->f_send = f_send;
8945 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008946 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008947}
Hanno Beckera58a8962019-06-13 16:11:15 +01008948#else
8949void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8950 void *p_bio )
8951{
8952 ssl->p_bio = p_bio;
8953}
8954#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008955
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008956#if defined(MBEDTLS_SSL_PROTO_DTLS)
8957void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8958{
8959 ssl->mtu = mtu;
8960}
8961#endif
8962
Hanno Becker1f835fa2019-06-13 10:14:59 +01008963#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008964void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008965{
8966 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008967}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008968#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008969
Hanno Becker0ae6b242019-06-13 16:45:36 +01008970#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8971 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008972void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8973 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008974 mbedtls_ssl_set_timer_t *f_set_timer,
8975 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008976{
8977 ssl->p_timer = p_timer;
8978 ssl->f_set_timer = f_set_timer;
8979 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008980 /* Make sure we start with no timer running */
8981 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008982}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008983#else
8984void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8985 void *p_timer )
8986{
8987 ssl->p_timer = p_timer;
8988 /* Make sure we start with no timer running */
8989 ssl_set_timer( ssl, 0 );
8990}
8991#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008992
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008993#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008994void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008995 void *p_cache,
8996 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8997 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008998{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008999 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009000 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009001 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009002}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009003#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009004
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009005#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009007{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009008 int ret;
9009
9010 if( ssl == NULL ||
9011 session == NULL ||
9012 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009013 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009016 }
9017
Hanno Becker58fccf22019-02-06 14:30:46 +00009018 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9019 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009020 return( ret );
9021
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009022 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009023
9024 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009025}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009026#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009027
Hanno Becker73f4cb12019-06-27 13:51:07 +01009028#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009029void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009030 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009031{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009032 conf->ciphersuite_list[0] = ciphersuites;
9033 conf->ciphersuite_list[1] = ciphersuites;
9034 conf->ciphersuite_list[2] = ciphersuites;
9035 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009036}
9037
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009038void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009039 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009040 int major, int minor )
9041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009043 return;
9044
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009045 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9046 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9047 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009048 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009049 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009050
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009051 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9052 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009053}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009054#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009057void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009058 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009059{
9060 conf->cert_profile = profile;
9061}
9062
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009063/* Append a new keycert entry to a (possibly empty) list */
9064static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9065 mbedtls_x509_crt *cert,
9066 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009067{
niisato8ee24222018-06-25 19:05:48 +09009068 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009069
niisato8ee24222018-06-25 19:05:48 +09009070 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9071 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009073
niisato8ee24222018-06-25 19:05:48 +09009074 new_cert->cert = cert;
9075 new_cert->key = key;
9076 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009077
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009078 /* Update head is the list was null, else add to the end */
9079 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009080 {
niisato8ee24222018-06-25 19:05:48 +09009081 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009082 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009083 else
9084 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009085 mbedtls_ssl_key_cert *cur = *head;
9086 while( cur->next != NULL )
9087 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009088 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009089 }
9090
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009091 return( 0 );
9092}
9093
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009094int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009095 mbedtls_x509_crt *own_cert,
9096 mbedtls_pk_context *pk_key )
9097{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009098 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009099}
9100
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009101void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009102 mbedtls_x509_crt *ca_chain,
9103 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009104{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009105 conf->ca_chain = ca_chain;
9106 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009109
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009110#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9111int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9112 mbedtls_x509_crt *own_cert,
9113 mbedtls_pk_context *pk_key )
9114{
9115 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9116 own_cert, pk_key ) );
9117}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009118
9119void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9120 mbedtls_x509_crt *ca_chain,
9121 mbedtls_x509_crl *ca_crl )
9122{
9123 ssl->handshake->sni_ca_chain = ca_chain;
9124 ssl->handshake->sni_ca_crl = ca_crl;
9125}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009126
9127void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9128 int authmode )
9129{
9130 ssl->handshake->sni_authmode = authmode;
9131}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009132#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9133
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009134#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009135/*
9136 * Set EC J-PAKE password for current handshake
9137 */
9138int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9139 const unsigned char *pw,
9140 size_t pw_len )
9141{
9142 mbedtls_ecjpake_role role;
9143
Janos Follath8eb64132016-06-03 15:40:57 +01009144 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9146
Hanno Becker2d9623f2019-06-13 12:07:05 +01009147 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009148 role = MBEDTLS_ECJPAKE_SERVER;
9149 else
9150 role = MBEDTLS_ECJPAKE_CLIENT;
9151
9152 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9153 role,
9154 MBEDTLS_MD_SHA256,
9155 MBEDTLS_ECP_DP_SECP256R1,
9156 pw, pw_len ) );
9157}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009158#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009161int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009162 const unsigned char *psk, size_t psk_len,
9163 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009164{
Paul Bakker6db455e2013-09-18 17:29:31 +02009165 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009170
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009171 /* Identity len will be encoded on two bytes */
9172 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009173 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009174 {
9175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9176 }
9177
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009178 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009180 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009181
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009182 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009183 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009184 conf->psk_len = 0;
9185 }
9186 if( conf->psk_identity != NULL )
9187 {
9188 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009189 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009190 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009191 }
9192
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009193 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9194 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009195 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009196 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009197 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009198 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009199 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009200 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009201 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009202
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009203 conf->psk_len = psk_len;
9204 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009205
Teppo Järvelin91d79382019-10-02 09:09:31 +03009206 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9207 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009208
9209 return( 0 );
9210}
9211
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009212int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9213 const unsigned char *psk, size_t psk_len )
9214{
9215 if( psk == NULL || ssl->handshake == NULL )
9216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9217
9218 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9220
9221 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009222 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009223 mbedtls_platform_zeroize( ssl->handshake->psk,
9224 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009225 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009226 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009227 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009228
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009229 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009230 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009231
9232 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009233 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009234
9235 return( 0 );
9236}
9237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009238void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009240 size_t),
9241 void *p_psk )
9242{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009243 conf->f_psk = f_psk;
9244 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009247
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009248#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009249
9250#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009251int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009252{
9253 int ret;
9254
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009255 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9256 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9257 {
9258 mbedtls_mpi_free( &conf->dhm_P );
9259 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009260 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009262
9263 return( 0 );
9264}
Hanno Becker470a8c42017-10-04 15:28:46 +01009265#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009266
Hanno Beckera90658f2017-10-04 15:29:08 +01009267int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9268 const unsigned char *dhm_P, size_t P_len,
9269 const unsigned char *dhm_G, size_t G_len )
9270{
9271 int ret;
9272
9273 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9274 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9275 {
9276 mbedtls_mpi_free( &conf->dhm_P );
9277 mbedtls_mpi_free( &conf->dhm_G );
9278 return( ret );
9279 }
9280
9281 return( 0 );
9282}
Paul Bakker5121ce52009-01-03 21:22:43 +00009283
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009284int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009285{
9286 int ret;
9287
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009288 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9289 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9290 {
9291 mbedtls_mpi_free( &conf->dhm_P );
9292 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009293 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009294 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009295
9296 return( 0 );
9297}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009298#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009299
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009300#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9301/*
9302 * Set the minimum length for Diffie-Hellman parameters
9303 */
9304void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9305 unsigned int bitlen )
9306{
9307 conf->dhm_min_bitlen = bitlen;
9308}
9309#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9310
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009311#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009312/*
9313 * Set allowed/preferred hashes for handshake signatures
9314 */
9315void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9316 const int *hashes )
9317{
Hanno Becker56595f42019-06-19 16:31:38 +01009318#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009319 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009320#else
9321 ((void) conf);
9322 ((void) hashes);
9323#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009324}
Hanno Becker947194e2017-04-07 13:25:49 +01009325#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009326
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009327#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009328#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009329/*
9330 * Set the allowed elliptic curves
9331 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009332void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009333 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009335 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009336}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009337#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009338#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009339
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009340#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009342{
Hanno Becker947194e2017-04-07 13:25:49 +01009343 /* Initialize to suppress unnecessary compiler warning */
9344 size_t hostname_len = 0;
9345
9346 /* Check if new hostname is valid before
9347 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009348 if( hostname != NULL )
9349 {
9350 hostname_len = strlen( hostname );
9351
9352 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9354 }
9355
9356 /* Now it's clear that we will overwrite the old hostname,
9357 * so we can free it safely */
9358
9359 if( ssl->hostname != NULL )
9360 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009361 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009362 mbedtls_free( ssl->hostname );
9363 }
9364
9365 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009366
Paul Bakker5121ce52009-01-03 21:22:43 +00009367 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009368 {
9369 ssl->hostname = NULL;
9370 }
9371 else
9372 {
9373 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009374 if( ssl->hostname == NULL )
9375 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009376
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009377 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9378 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009379
Hanno Becker947194e2017-04-07 13:25:49 +01009380 ssl->hostname[hostname_len] = '\0';
9381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009382
9383 return( 0 );
9384}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009385#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009386
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009387#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009388void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009390 const unsigned char *, size_t),
9391 void *p_sni )
9392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009393 conf->f_sni = f_sni;
9394 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009399int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009400{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009401 size_t cur_len, tot_len;
9402 const char **p;
9403
9404 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009405 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9406 * MUST NOT be truncated."
9407 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009408 */
9409 tot_len = 0;
9410 for( p = protos; *p != NULL; p++ )
9411 {
9412 cur_len = strlen( *p );
9413 tot_len += cur_len;
9414
9415 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009417 }
9418
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009419 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009420
9421 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009422}
9423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009425{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009426 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009429
Hanno Becker33b9b252019-07-05 11:23:25 +01009430#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9431 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9432void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9433 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009435 conf->max_major_ver = major;
9436 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009437}
Hanno Becker33b9b252019-07-05 11:23:25 +01009438#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9439 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009440
Hanno Becker33b9b252019-07-05 11:23:25 +01009441#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9442 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9443void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9444 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009446 conf->min_major_ver = major;
9447 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009448}
Hanno Becker33b9b252019-07-05 11:23:25 +01009449#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9450 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009453void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009454{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009455 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009456}
9457#endif
9458
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009459#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009460void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9461 char cert_req_ca_list )
9462{
9463 conf->cert_req_ca_list = cert_req_ca_list;
9464}
9465#endif
9466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009468void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009470 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009471}
9472#endif
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009475#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009476void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009477{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009478 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009479}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009480#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9481#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009482void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009483 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009484{
9485 conf->enforce_extended_master_secret = ems_enf;
9486}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009487#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009488#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009489
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009490#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009491void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009492{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009493 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009494}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009495#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009498int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009501 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009504 }
9505
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009506 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009507
9508 return( 0 );
9509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009513void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009515 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009520void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009521{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009522 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009523}
9524#endif
9525
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009526#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009527void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009529 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009530}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009531#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009534void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009536 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009537}
9538
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009539void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009541 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009542}
9543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009544void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009545 const unsigned char period[8] )
9546{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009547 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009552#if defined(MBEDTLS_SSL_CLI_C)
9553void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009554{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009555 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009556}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009557#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009559#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009560void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9561 mbedtls_ssl_ticket_write_t *f_ticket_write,
9562 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9563 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009564{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009565 conf->f_ticket_write = f_ticket_write;
9566 conf->f_ticket_parse = f_ticket_parse;
9567 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009568}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009571
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009572#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9573void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9574 mbedtls_ssl_export_keys_t *f_export_keys,
9575 void *p_export_keys )
9576{
9577 conf->f_export_keys = f_export_keys;
9578 conf->p_export_keys = p_export_keys;
9579}
9580#endif
9581
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009582#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009583void mbedtls_ssl_conf_async_private_cb(
9584 mbedtls_ssl_config *conf,
9585 mbedtls_ssl_async_sign_t *f_async_sign,
9586 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9587 mbedtls_ssl_async_resume_t *f_async_resume,
9588 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009589 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009590{
9591 conf->f_async_sign_start = f_async_sign;
9592 conf->f_async_decrypt_start = f_async_decrypt;
9593 conf->f_async_resume = f_async_resume;
9594 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009595 conf->p_async_config_data = async_config_data;
9596}
9597
Gilles Peskine8f97af72018-04-26 11:46:10 +02009598void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9599{
9600 return( conf->p_async_config_data );
9601}
9602
Gilles Peskine1febfef2018-04-30 11:54:39 +02009603void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009604{
9605 if( ssl->handshake == NULL )
9606 return( NULL );
9607 else
9608 return( ssl->handshake->user_async_ctx );
9609}
9610
Gilles Peskine1febfef2018-04-30 11:54:39 +02009611void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009612 void *ctx )
9613{
9614 if( ssl->handshake != NULL )
9615 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009616}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009617#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009618
Paul Bakker5121ce52009-01-03 21:22:43 +00009619/*
9620 * SSL get accessors
9621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009623{
9624 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9625}
9626
Hanno Becker8b170a02017-10-10 11:51:19 +01009627int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9628{
9629 /*
9630 * Case A: We're currently holding back
9631 * a message for further processing.
9632 */
9633
9634 if( ssl->keep_current_message == 1 )
9635 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009637 return( 1 );
9638 }
9639
9640 /*
9641 * Case B: Further records are pending in the current datagram.
9642 */
9643
9644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009645 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009646 ssl->in_left > ssl->next_record_offset )
9647 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009649 return( 1 );
9650 }
9651#endif /* MBEDTLS_SSL_PROTO_DTLS */
9652
9653 /*
9654 * Case C: A handshake message is being processed.
9655 */
9656
Hanno Becker8b170a02017-10-10 11:51:19 +01009657 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9658 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009660 return( 1 );
9661 }
9662
9663 /*
9664 * Case D: An application data message is being processed
9665 */
9666 if( ssl->in_offt != NULL )
9667 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009669 return( 1 );
9670 }
9671
9672 /*
9673 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009674 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009675 * we implement support for multiple alerts in single records.
9676 */
9677
9678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9679 return( 0 );
9680}
9681
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009682uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009683{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009684 if( ssl->session != NULL )
9685 return( ssl->session->verify_result );
9686
9687 if( ssl->session_negotiate != NULL )
9688 return( ssl->session_negotiate->verify_result );
9689
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009690 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009691}
9692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009694{
Hanno Beckere02758c2019-06-26 15:31:31 +01009695 int suite;
9696
Paul Bakker926c8e42013-03-06 10:23:34 +01009697 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009698 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009699
Hanno Beckere02758c2019-06-26 15:31:31 +01009700 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9701 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009702}
9703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009707 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009708 {
Hanno Becker2881d802019-05-22 14:44:53 +01009709 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009712 return( "DTLSv1.0" );
9713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009715 return( "DTLSv1.2" );
9716
9717 default:
9718 return( "unknown (DTLS)" );
9719 }
9720 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009721 MBEDTLS_SSL_TRANSPORT_ELSE
9722#endif /* MBEDTLS_SSL_PROTO_DTLS */
9723#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009724 {
Hanno Becker2881d802019-05-22 14:44:53 +01009725 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009726 {
9727 case MBEDTLS_SSL_MINOR_VERSION_0:
9728 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009729
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009730 case MBEDTLS_SSL_MINOR_VERSION_1:
9731 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009732
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009733 case MBEDTLS_SSL_MINOR_VERSION_2:
9734 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009735
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009736 case MBEDTLS_SSL_MINOR_VERSION_3:
9737 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009738
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009739 default:
9740 return( "unknown" );
9741 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009742 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009743#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009744}
9745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009747{
Hanno Becker3136ede2018-08-17 15:28:19 +01009748 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009750
Hanno Becker43395762019-05-03 14:46:38 +01009751 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9752
Hanno Becker78640902018-08-13 16:35:15 +01009753 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009754 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_ZLIB_SUPPORT)
9757 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9758 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009759#endif
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009762 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009763#if defined(MBEDTLS_GCM_C) || \
9764 defined(MBEDTLS_CCM_C) || \
9765 defined(MBEDTLS_CHACHAPOLY_C)
9766#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009768#endif
9769#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009771#endif
9772#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009773 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009774#endif
9775 transform_expansion =
9776 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009777 break;
9778
Hanno Beckera9d5c452019-07-25 16:47:12 +01009779#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9780 MBEDTLS_CHACHAPOLY_C */
9781
9782#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9783 case MBEDTLS_MODE_STREAM:
9784 transform_expansion = transform->maclen;
9785 break;
9786#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9787
9788#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009790 {
9791 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009792
9793 block_size = mbedtls_cipher_get_block_size(
9794 &transform->cipher_ctx_enc );
9795
Hanno Becker3136ede2018-08-17 15:28:19 +01009796 /* Expansion due to the addition of the MAC. */
9797 transform_expansion += transform->maclen;
9798
9799 /* Expansion due to the addition of CBC padding;
9800 * Theoretically up to 256 bytes, but we never use
9801 * more than the block size of the underlying cipher. */
9802 transform_expansion += block_size;
9803
9804 /* For TLS 1.1 or higher, an explicit IV is added
9805 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009806#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009807 if( mbedtls_ssl_ver_geq(
9808 mbedtls_ssl_get_minor_ver( ssl ),
9809 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9810 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009811 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009812 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009813#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009814
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009815 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009816 }
9817#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009818
9819 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009822 }
9823
Hanno Beckera5a2b082019-05-15 14:03:01 +01009824#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009825 if( transform->out_cid_len != 0 )
9826 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009827#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009828
Hanno Becker43395762019-05-03 14:46:38 +01009829 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009830}
9831
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009832#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9833size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9834{
9835 size_t max_len;
9836
9837 /*
9838 * Assume mfl_code is correct since it was checked when set
9839 */
Angus Grattond8213d02016-05-25 20:56:48 +10009840 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009841
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009842 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009843 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009844 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009845 {
Angus Grattond8213d02016-05-25 20:56:48 +10009846 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009847 }
9848
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009849 /* During a handshake, use the value being negotiated */
9850 if( ssl->session_negotiate != NULL &&
9851 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9852 {
9853 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9854 }
9855
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009856 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009857}
9858#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9859
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009860#if defined(MBEDTLS_SSL_PROTO_DTLS)
9861static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9862{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009863 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009864 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009865 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9866 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009867 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009868
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009869 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9870 return( ssl->mtu );
9871
9872 if( ssl->mtu == 0 )
9873 return( ssl->handshake->mtu );
9874
9875 return( ssl->mtu < ssl->handshake->mtu ?
9876 ssl->mtu : ssl->handshake->mtu );
9877}
9878#endif /* MBEDTLS_SSL_PROTO_DTLS */
9879
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009880int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9881{
9882 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9883
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009884#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9885 !defined(MBEDTLS_SSL_PROTO_DTLS)
9886 (void) ssl;
9887#endif
9888
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009889#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9890 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9891
9892 if( max_len > mfl )
9893 max_len = mfl;
9894#endif
9895
9896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009897 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009898 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009899 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009900 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9901 const size_t overhead = (size_t) ret;
9902
9903 if( ret < 0 )
9904 return( ret );
9905
9906 if( mtu <= overhead )
9907 {
9908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9910 }
9911
9912 if( max_len > mtu - overhead )
9913 max_len = mtu - overhead;
9914 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009915#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009916
Hanno Becker0defedb2018-08-10 12:35:02 +01009917#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9918 !defined(MBEDTLS_SSL_PROTO_DTLS)
9919 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009920#endif
9921
9922 return( (int) max_len );
9923}
9924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925#if defined(MBEDTLS_X509_CRT_PARSE_C)
9926const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009927{
9928 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009929 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009930
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009931#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009932 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009933#else
9934 return( NULL );
9935#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009940int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9941 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009942{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009943 if( ssl == NULL ||
9944 dst == NULL ||
9945 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009946 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009949 }
9950
Hanno Becker58fccf22019-02-06 14:30:46 +00009951 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009954
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009955const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9956{
9957 if( ssl == NULL )
9958 return( NULL );
9959
9960 return( ssl->session );
9961}
9962
Paul Bakker5121ce52009-01-03 21:22:43 +00009963/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009964 * Define ticket header determining Mbed TLS version
9965 * and structure of the ticket.
9966 */
9967
Hanno Becker41527622019-05-16 12:50:45 +01009968/*
Hanno Becker26829e92019-05-28 14:30:45 +01009969 * Define bitflag determining compile-time settings influencing
9970 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009971 */
9972
Hanno Becker26829e92019-05-28 14:30:45 +01009973#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009974#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009975#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009976#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009977#endif /* MBEDTLS_HAVE_TIME */
9978
9979#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009980#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009981#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009982#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009983#endif /* MBEDTLS_X509_CRT_PARSE_C */
9984
9985#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009986#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009987#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009988#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009989#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9990
9991#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009992#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009993#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009994#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009995#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9996
9997#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009998#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009999#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010000#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010001#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10002
10003#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010004#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010005#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010006#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010007#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10008
Hanno Becker41527622019-05-16 12:50:45 +010010009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10010#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10011#else
10012#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10013#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10014
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010015#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10016#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10017#else
10018#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10019#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10020
Hanno Becker88440552019-07-03 14:16:13 +010010021#if defined(MBEDTLS_ZLIB_SUPPORT)
10022#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10023#else
10024#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10025#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10026
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010027#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10028#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10029#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10030#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10031#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10032#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10033#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010034#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010035#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010036
Hanno Becker26829e92019-05-28 14:30:45 +010010037#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010038 ( (uint16_t) ( \
10039 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10040 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10041 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10042 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10043 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10044 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010045 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010046 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010047 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010048
Hanno Becker557fe9f2019-05-16 12:41:07 +010010049static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010050 MBEDTLS_VERSION_MAJOR,
10051 MBEDTLS_VERSION_MINOR,
10052 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010053 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10054 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010055};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010056
10057/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010058 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010059 * (in the presentation language of TLS, RFC 8446 section 3)
10060 *
Hanno Becker26829e92019-05-28 14:30:45 +010010061 * opaque mbedtls_version[3]; // major, minor, patch
10062 * opaque session_format[2]; // version-specific 16-bit field determining
10063 * // the format of the remaining
10064 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010065 *
10066 * Note: When updating the format, remember to keep
10067 * these version+format bytes.
10068 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010069 * // In this version, `session_format` determines
10070 * // the setting of those compile-time
10071 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010072 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010073 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010074 * uint8 ciphersuite[2]; // defined by the standard
10075 * uint8 compression; // 0 or 1
10076 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010077 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010078 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010079 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010080 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10081 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10082 * case disabled: uint8_t peer_cert_digest_type;
10083 * opaque peer_cert_digest<0..2^8-1>;
10084 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010085 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010086 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010087 * uint8 mfl_code; // up to 255 according to standard
10088 * uint8 trunc_hmac; // 0 or 1
10089 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010090 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010091 * The order is the same as in the definition of the structure, except
10092 * verify_result is put before peer_cert so that all mandatory fields come
10093 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010094 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010095static int ssl_session_save( const mbedtls_ssl_session *session,
10096 unsigned char omit_header,
10097 unsigned char *buf,
10098 size_t buf_len,
10099 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010100{
10101 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010102 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010103#if defined(MBEDTLS_HAVE_TIME)
10104 uint64_t start;
10105#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010106#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010107#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010108 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010109#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010110#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010111
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010112 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010113 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010114 /*
10115 * Add version identifier
10116 */
10117
10118 used += sizeof( ssl_serialized_session_header );
10119
10120 if( used <= buf_len )
10121 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010122 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010123 sizeof( ssl_serialized_session_header ) );
10124 p += sizeof( ssl_serialized_session_header );
10125 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010126 }
10127
10128 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010129 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010130 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010131#if defined(MBEDTLS_HAVE_TIME)
10132 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010133
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010134 if( used <= buf_len )
10135 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010136 start = (uint64_t) session->start;
10137
10138 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10139 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10140 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10141 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10142 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10143 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10144 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10145 *p++ = (unsigned char)( ( start ) & 0xFF );
10146 }
10147#endif /* MBEDTLS_HAVE_TIME */
10148
10149 /*
10150 * Basic mandatory fields
10151 */
Hanno Becker88440552019-07-03 14:16:13 +010010152 {
10153 size_t const ciphersuite_len = 2;
10154#if defined(MBEDTLS_ZLIB_SUPPORT)
10155 size_t const compression_len = 1;
10156#else
10157 size_t const compression_len = 0;
10158#endif
10159 size_t const id_len_len = 1;
10160 size_t const id_len = 32;
10161 size_t const master_len = 48;
10162 size_t const verif_result_len = 4;
10163
10164 size_t const basic_len =
10165 ciphersuite_len +
10166 compression_len +
10167 id_len_len +
10168 id_len +
10169 master_len +
10170 verif_result_len;
10171
10172 used += basic_len;
10173 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010174
10175 if( used <= buf_len )
10176 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010177 const int ciphersuite =
10178 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010179 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010180
Hanno Becker88440552019-07-03 14:16:13 +010010181#if defined(MBEDTLS_ZLIB_SUPPORT)
10182 *p++ = (unsigned char)(
10183 mbedtls_ssl_session_get_compression( session ) );
10184#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010185
10186 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010187 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10188 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010189 p += 32;
10190
Teppo Järvelin91d79382019-10-02 09:09:31 +030010191 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010192 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010193 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010194 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010195
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010196 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010197 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010198 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010199#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010200#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010201 if( session->peer_cert == NULL )
10202 cert_len = 0;
10203 else
10204 cert_len = session->peer_cert->raw.len;
10205
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010206 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010207
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010208 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010209 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010210 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010211
10212 if( session->peer_cert != NULL )
10213 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010214 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010215 p += cert_len;
10216 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010217 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010218
Hanno Becker5882dd02019-06-06 16:25:57 +010010219#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010220 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010221 if( session->peer_cert_digest != NULL )
10222 {
10223 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10224 if( used <= buf_len )
10225 {
10226 *p++ = (unsigned char) session->peer_cert_digest_type;
10227 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010228 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010229 session->peer_cert_digest_len );
10230 p += session->peer_cert_digest_len;
10231 }
10232 }
10233 else
10234 {
10235 used += 2;
10236 if( used <= buf_len )
10237 {
10238 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10239 *p++ = 0;
10240 }
10241 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010242#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010243#endif /* MBEDTLS_X509_CRT_PARSE_C */
10244
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010245 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010246 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010247 */
10248#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010249 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010250
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010251 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010252 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010253 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010254
10255 if( session->ticket != NULL )
10256 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010257 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010258 p += session->ticket_len;
10259 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010260
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010261 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010262 }
10263#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10264
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010265 /*
10266 * Misc extension-related info
10267 */
10268#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10269 used += 1;
10270
10271 if( used <= buf_len )
10272 *p++ = session->mfl_code;
10273#endif
10274
10275#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10276 used += 1;
10277
10278 if( used <= buf_len )
10279 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10280#endif
10281
10282#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10283 used += 1;
10284
10285 if( used <= buf_len )
10286 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10287#endif
10288
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010289 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010290 *olen = used;
10291
10292 if( used > buf_len )
10293 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010294
10295 return( 0 );
10296}
10297
10298/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010299 * Public wrapper for ssl_session_save()
10300 */
10301int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10302 unsigned char *buf,
10303 size_t buf_len,
10304 size_t *olen )
10305{
10306 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10307}
10308
10309/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010310 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010311 *
10312 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010313 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010314 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010315static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010316 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010317 const unsigned char *buf,
10318 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010319{
10320 const unsigned char *p = buf;
10321 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010322 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010323#if defined(MBEDTLS_HAVE_TIME)
10324 uint64_t start;
10325#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010326#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010327#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010328 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010329#endif
10330#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010331
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010332 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010333 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010334 /*
10335 * Check version identifier
10336 */
10337
10338 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10340
Teppo Järvelin0efac532019-10-04 13:21:08 +030010341 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010342 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010343 sizeof( ssl_serialized_session_header ) ) != 0 )
10344 {
10345 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10346 }
10347 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010348 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010349
10350 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010351 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010352 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010353#if defined(MBEDTLS_HAVE_TIME)
10354 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10356
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010357 start = ( (uint64_t) p[0] << 56 ) |
10358 ( (uint64_t) p[1] << 48 ) |
10359 ( (uint64_t) p[2] << 40 ) |
10360 ( (uint64_t) p[3] << 32 ) |
10361 ( (uint64_t) p[4] << 24 ) |
10362 ( (uint64_t) p[5] << 16 ) |
10363 ( (uint64_t) p[6] << 8 ) |
10364 ( (uint64_t) p[7] );
10365 p += 8;
10366
10367 session->start = (time_t) start;
10368#endif /* MBEDTLS_HAVE_TIME */
10369
10370 /*
10371 * Basic mandatory fields
10372 */
Hanno Becker88440552019-07-03 14:16:13 +010010373 {
10374 size_t const ciphersuite_len = 2;
10375#if defined(MBEDTLS_ZLIB_SUPPORT)
10376 size_t const compression_len = 1;
10377#else
10378 size_t const compression_len = 0;
10379#endif
10380 size_t const id_len_len = 1;
10381 size_t const id_len = 32;
10382 size_t const master_len = 48;
10383 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010384
Hanno Becker88440552019-07-03 14:16:13 +010010385 size_t const basic_len =
10386 ciphersuite_len +
10387 compression_len +
10388 id_len_len +
10389 id_len +
10390 master_len +
10391 verif_result_len;
10392
10393 if( basic_len > (size_t)( end - p ) )
10394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10395 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010396
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010397 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010398 p += 2;
10399
Hanno Becker73f4cb12019-06-27 13:51:07 +010010400#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010401 session->ciphersuite = ciphersuite;
10402#else
10403 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010404 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010405 {
10406 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10407 }
10408#endif
10409
Hanno Becker88440552019-07-03 14:16:13 +010010410#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010411 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010412#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010413
10414 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010415 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10416 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010417 p += 32;
10418
Teppo Järvelin91d79382019-10-02 09:09:31 +030010419 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010420 p += 48;
10421
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010422 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010423 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010424
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010425 /* Immediately clear invalid pointer values that have been read, in case
10426 * we exit early before we replaced them with valid ones. */
10427#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010428#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010429 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010430#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010431 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010432#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010433#endif
10434#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10435 session->ticket = NULL;
10436#endif
10437
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010438 /*
10439 * Peer certificate
10440 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010442#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010443 if( 3 > (size_t)( end - p ) )
10444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10445
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010446 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10447
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010448 p += 3;
10449
10450 if( cert_len == 0 )
10451 {
10452 session->peer_cert = NULL;
10453 }
10454 else
10455 {
10456 int ret;
10457
10458 if( cert_len > (size_t)( end - p ) )
10459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10460
10461 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10462
10463 if( session->peer_cert == NULL )
10464 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10465
10466 mbedtls_x509_crt_init( session->peer_cert );
10467
10468 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10469 p, cert_len ) ) != 0 )
10470 {
10471 mbedtls_x509_crt_free( session->peer_cert );
10472 mbedtls_free( session->peer_cert );
10473 session->peer_cert = NULL;
10474 return( ret );
10475 }
10476
10477 p += cert_len;
10478 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010479#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010480 /* Deserialize CRT digest from the end of the ticket. */
10481 if( 2 > (size_t)( end - p ) )
10482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10483
10484 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10485 session->peer_cert_digest_len = (size_t) *p++;
10486
10487 if( session->peer_cert_digest_len != 0 )
10488 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010489 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010490 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010491 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10493 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10495
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010496 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10498
10499 session->peer_cert_digest =
10500 mbedtls_calloc( 1, session->peer_cert_digest_len );
10501 if( session->peer_cert_digest == NULL )
10502 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10503
Teppo Järvelin91d79382019-10-02 09:09:31 +030010504 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010505 session->peer_cert_digest_len );
10506 p += session->peer_cert_digest_len;
10507 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010508#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010509#endif /* MBEDTLS_X509_CRT_PARSE_C */
10510
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010511 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010512 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010513 */
10514#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10515 if( 3 > (size_t)( end - p ) )
10516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10517
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010518 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010519 p += 3;
10520
10521 if( session->ticket_len != 0 )
10522 {
10523 if( session->ticket_len > (size_t)( end - p ) )
10524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10525
10526 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10527 if( session->ticket == NULL )
10528 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10529
Teppo Järvelin91d79382019-10-02 09:09:31 +030010530 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010531 p += session->ticket_len;
10532 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010533
10534 if( 4 > (size_t)( end - p ) )
10535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10536
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010537 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010538 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010539#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10540
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010541 /*
10542 * Misc extension-related info
10543 */
10544#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10545 if( 1 > (size_t)( end - p ) )
10546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10547
10548 session->mfl_code = *p++;
10549#endif
10550
10551#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10552 if( 1 > (size_t)( end - p ) )
10553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10554
10555 session->trunc_hmac = *p++;
10556#endif
10557
10558#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10559 if( 1 > (size_t)( end - p ) )
10560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10561
10562 session->encrypt_then_mac = *p++;
10563#endif
10564
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010565 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010566 if( p != end )
10567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10568
10569 return( 0 );
10570}
10571
10572/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010573 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010574 */
10575int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10576 const unsigned char *buf,
10577 size_t len )
10578{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010579 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010580
10581 if( ret != 0 )
10582 mbedtls_ssl_session_free( session );
10583
10584 return( ret );
10585}
10586
10587/*
Paul Bakker1961b702013-01-25 14:49:24 +010010588 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010593
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010594 if( ssl == NULL || ssl->conf == NULL )
10595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010598 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010602 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010604#endif
10605
Hanno Beckerb82350b2019-07-26 07:24:05 +010010606 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010607 return( ret );
10608}
10609
10610/*
10611 * Perform the SSL handshake
10612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010614{
10615 int ret = 0;
10616
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010617 if( ssl == NULL || ssl->conf == NULL )
10618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010625
10626 if( ret != 0 )
10627 break;
10628 }
10629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010631
10632 return( ret );
10633}
10634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#if defined(MBEDTLS_SSL_RENEGOTIATION)
10636#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010637/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010638 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010641{
10642 int ret;
10643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010645
10646 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10648 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010649
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010650 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010651 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010653 return( ret );
10654 }
10655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010657
10658 return( 0 );
10659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010661
10662/*
10663 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 * - any side: calling mbedtls_ssl_renegotiate(),
10665 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10666 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010667 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010668 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010672{
10673 int ret;
10674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010676
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010677 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10678 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010679
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010680 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10681 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010683 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010685 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010686 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10687 MBEDTLS_SSL_IS_SERVER )
10688 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010689 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010690 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010691 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010692 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010693 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010694 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010695 }
10696#endif
10697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10699 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010704 return( ret );
10705 }
10706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010708
10709 return( 0 );
10710}
10711
10712/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010713 * Renegotiate current connection on client,
10714 * or request renegotiation on server
10715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010719
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010720 if( ssl == NULL || ssl->conf == NULL )
10721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010724 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010725 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10728 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010731
10732 /* Did we already try/start sending HelloRequest? */
10733 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010735
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010736 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010737 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010741 /*
10742 * On client, either start the renegotiation process or,
10743 * if already in progress, continue the handshake
10744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010749
10750 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010753 return( ret );
10754 }
10755 }
10756 else
10757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010761 return( ret );
10762 }
10763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010765
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010766 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010767}
10768
10769/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010770 * Check record counters and renegotiate if they're above the limit.
10771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010773{
Andres AG2196c7f2016-12-15 17:01:16 +000010774 size_t ep_len = ssl_ep_len( ssl );
10775 int in_ctr_cmp;
10776 int out_ctr_cmp;
10777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10779 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010780 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010781 {
10782 return( 0 );
10783 }
10784
Teppo Järvelin0efac532019-10-04 13:21:08 +030010785 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010786 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010787 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010788 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010789 ssl->conf->renego_period + ep_len, 8 - ep_len );
10790
10791 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010792 {
10793 return( 0 );
10794 }
10795
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010798}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010800
10801/*
10802 * Receive application data decrypted from the SSL layer
10803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010805{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010806 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010807 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010808
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010809 if( ssl == NULL || ssl->conf == NULL )
10810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010815 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010817 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010818 return( ret );
10819
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010820 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010822 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010823 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010824 return( ret );
10825 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010826 }
10827#endif
10828
Hanno Becker4a810fb2017-05-24 16:27:30 +010010829 /*
10830 * Check if renegotiation is necessary and/or handshake is
10831 * in process. If yes, perform/continue, and fall through
10832 * if an unexpected packet is received while the client
10833 * is waiting for the ServerHello.
10834 *
10835 * (There is no equivalent to the last condition on
10836 * the server-side as it is not treated as within
10837 * a handshake while waiting for the ClientHello
10838 * after a renegotiation request.)
10839 */
10840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010842 ret = ssl_check_ctr_renegotiate( ssl );
10843 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10844 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010847 return( ret );
10848 }
10849#endif
10850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010854 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10855 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010858 return( ret );
10859 }
10860 }
10861
Hanno Beckere41158b2017-10-23 13:30:32 +010010862 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010863 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010864 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010865 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010866 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10867 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010868 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010869 ssl_set_timer( ssl,
10870 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010871 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010872
Hanno Becker327c93b2018-08-15 13:56:18 +010010873 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010874 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010875 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10876 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010877
Hanno Becker4a810fb2017-05-24 16:27:30 +010010878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10879 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010880 }
10881
10882 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010883 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010884 {
10885 /*
10886 * OpenSSL sends empty messages to randomize the IV
10887 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010888 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010891 return( 0 );
10892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010894 return( ret );
10895 }
10896 }
10897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010901
Hanno Becker4a810fb2017-05-24 16:27:30 +010010902 /*
10903 * - For client-side, expect SERVER_HELLO_REQUEST.
10904 * - For server-side, expect CLIENT_HELLO.
10905 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10906 */
10907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010909 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10910 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010912 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010915
10916 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010919 {
10920 continue;
10921 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010922 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010923#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010924#if defined(MBEDTLS_SSL_PROTO_TLS)
10925 {
10926 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10927 }
10928#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010929 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010930#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010931
Hanno Becker4a810fb2017-05-24 16:27:30 +010010932#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010933 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10934 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010938
10939 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010941 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010942 {
10943 continue;
10944 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010945 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010946#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010947#if defined(MBEDTLS_SSL_PROTO_TLS)
10948 {
10949 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10950 }
10951#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010952 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010953#endif /* MBEDTLS_SSL_SRV_C */
10954
Hanno Becker21df7f92017-10-17 11:03:26 +010010955#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010956 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010957 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10958 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010959 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010960 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10961 {
10962 /*
10963 * Accept renegotiation request
10964 */
Paul Bakker48916f92012-09-16 19:57:18 +000010965
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010966 /* DTLS clients need to know renego is server-initiated */
10967#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 Becker2d9623f2019-06-13 12:07:05 +010010969 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10970 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010971 {
10972 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10973 }
10974#endif
10975 ret = ssl_start_renegotiation( ssl );
10976 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10977 ret != 0 )
10978 {
10979 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10980 return( ret );
10981 }
10982 }
10983 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010984#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010985 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010986 /*
10987 * Refuse renegotiation
10988 */
10989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010993 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010994 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010995 /* SSLv3 does not have a "no_renegotiation" warning, so
10996 we send a fatal alert and abort the connection. */
10997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10998 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10999 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011000 }
11001 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11003#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11004 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011005 if( mbedtls_ssl_ver_geq(
11006 mbedtls_ssl_get_minor_ver( ssl ),
11007 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011008 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011009 ret = mbedtls_ssl_send_alert_message( ssl,
11010 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11011 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11012 if( ret != 0 )
11013 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011014 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11017 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11020 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011021 }
Paul Bakker48916f92012-09-16 19:57:18 +000011022 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011023
Hanno Becker90333da2017-10-10 11:27:13 +010011024 /* At this point, we don't know whether the renegotiation has been
11025 * completed or not. The cases to consider are the following:
11026 * 1) The renegotiation is complete. In this case, no new record
11027 * has been read yet.
11028 * 2) The renegotiation is incomplete because the client received
11029 * an application data record while awaiting the ServerHello.
11030 * 3) The renegotiation is incomplete because the client received
11031 * a non-handshake, non-application data message while awaiting
11032 * the ServerHello.
11033 * In each of these case, looping will be the proper action:
11034 * - For 1), the next iteration will read a new record and check
11035 * if it's application data.
11036 * - For 2), the loop condition isn't satisfied as application data
11037 * is present, hence continue is the same as break
11038 * - For 3), the loop condition is satisfied and read_record
11039 * will re-deliver the message that was held back by the client
11040 * when expecting the ServerHello.
11041 */
11042 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011043 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011044#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011046 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011047 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011048 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011049 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011052 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011054 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011055 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11060 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011063 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011064 }
11065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11069 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011070 }
11071
11072 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011073
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011074 /* We're going to return something now, cancel timer,
11075 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011077 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011078
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011080 /* If we requested renego but received AppData, resend HelloRequest.
11081 * Do it now, after setting in_offt, to avoid taking this branch
11082 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011084 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11085 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011087 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011088 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011090 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011091 return( ret );
11092 }
11093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011094#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011095#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011096 }
11097
11098 n = ( len < ssl->in_msglen )
11099 ? len : ssl->in_msglen;
11100
Teppo Järvelin91d79382019-10-02 09:09:31 +030011101 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011102 ssl->in_msglen -= n;
11103
11104 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011105 {
11106 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011107 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011108 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011109 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011110 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011111 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011112 /* more data available */
11113 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011114 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011117
Paul Bakker23986e52011-04-24 08:57:21 +000011118 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011119}
11120
11121/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011122 * Send application data to be encrypted by the SSL layer, taking care of max
11123 * fragment length and buffer size.
11124 *
11125 * According to RFC 5246 Section 6.2.1:
11126 *
11127 * Zero-length fragments of Application data MAY be sent as they are
11128 * potentially useful as a traffic analysis countermeasure.
11129 *
11130 * Therefore, it is possible that the input message length is 0 and the
11131 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011132 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011133static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011134 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011135{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011136 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11137 const size_t max_len = (size_t) ret;
11138
11139 if( ret < 0 )
11140 {
11141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11142 return( ret );
11143 }
11144
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011145 if( len > max_len )
11146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011148 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011151 "maximum fragment length: %d > %d",
11152 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011154 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011155 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011156#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011157#if defined(MBEDTLS_SSL_PROTO_TLS)
11158 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011159 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011160 }
11161#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011162 }
Paul Bakker887bd502011-06-08 13:10:54 +000011163
Paul Bakker5121ce52009-01-03 21:22:43 +000011164 if( ssl->out_left != 0 )
11165 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011166 /*
11167 * The user has previously tried to send the data and
11168 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11169 * written. In this case, we expect the high-level write function
11170 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011175 return( ret );
11176 }
11177 }
Paul Bakker887bd502011-06-08 13:10:54 +000011178 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011179 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011180 /*
11181 * The user is trying to send a message the first time, so we need to
11182 * copy the data into the internal buffers and setup the data structure
11183 * to keep track of partial writes
11184 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011185 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011187 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011188
Hanno Becker67bc7c32018-08-06 11:33:50 +010011189 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011192 return( ret );
11193 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011194 }
11195
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011196 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011197}
11198
11199/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011200 * Write application data, doing 1/n-1 splitting if necessary.
11201 *
11202 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011203 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011204 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011207static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011208 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011209{
11210 int ret;
11211
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011212 if( ssl->conf->cbc_record_splitting ==
11213 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011214 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011215 mbedtls_ssl_ver_gt(
11216 mbedtls_ssl_get_minor_ver( ssl ),
11217 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011218 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11219 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011220 {
11221 return( ssl_write_real( ssl, buf, len ) );
11222 }
11223
11224 if( ssl->split_done == 0 )
11225 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011226 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011227 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011228 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011229 }
11230
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011231 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11232 return( ret );
11233 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011234
11235 return( ret + 1 );
11236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011237#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011238
11239/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011240 * Write application data (public-facing wrapper)
11241 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011242int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011243{
11244 int ret;
11245
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011248 if( ssl == NULL || ssl->conf == NULL )
11249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11250
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011252 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11253 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011254 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011255 return( ret );
11256 }
11257#endif
11258
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011259 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011260 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011261 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011262 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011264 return( ret );
11265 }
11266 }
11267
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011268#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011269 ret = ssl_write_split( ssl, buf, len );
11270#else
11271 ret = ssl_write_real( ssl, buf, len );
11272#endif
11273
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011275
11276 return( ret );
11277}
11278
11279/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011280 * Notify the peer that the connection is being closed
11281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011282int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011283{
11284 int ret;
11285
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011286 if( ssl == NULL || ssl->conf == NULL )
11287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011290
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011291 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011292 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011294 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011296 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11297 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11298 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011301 return( ret );
11302 }
11303 }
11304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011306
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011307 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011308}
11309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011311{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011312 if( transform == NULL )
11313 return;
11314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011315#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011316 deflateEnd( &transform->ctx_deflate );
11317 inflateEnd( &transform->ctx_inflate );
11318#endif
11319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11321 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011322
Hanno Becker92231322018-01-03 15:32:51 +000011323#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011324 mbedtls_md_free( &transform->md_ctx_enc );
11325 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011326#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011327
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011328 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011329}
11330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011331#if defined(MBEDTLS_X509_CRT_PARSE_C)
11332static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011334 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011335
11336 while( cur != NULL )
11337 {
11338 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011339 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011340 cur = next;
11341 }
11342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011344
Hanno Becker0271f962018-08-16 13:23:47 +010011345#if defined(MBEDTLS_SSL_PROTO_DTLS)
11346
11347static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11348{
11349 unsigned offset;
11350 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11351
11352 if( hs == NULL )
11353 return;
11354
Hanno Becker283f5ef2018-08-24 09:34:47 +010011355 ssl_free_buffered_record( ssl );
11356
Hanno Becker0271f962018-08-16 13:23:47 +010011357 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011358 ssl_buffering_free_slot( ssl, offset );
11359}
11360
11361static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11362 uint8_t slot )
11363{
11364 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11365 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011366
11367 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11368 return;
11369
Hanno Beckere605b192018-08-21 15:59:07 +010011370 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011371 {
Hanno Beckere605b192018-08-21 15:59:07 +010011372 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011373 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011374 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011375 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011376 }
11377}
11378
11379#endif /* MBEDTLS_SSL_PROTO_DTLS */
11380
Gilles Peskine9b562d52018-04-25 20:32:43 +020011381void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011382{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011383 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11384
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011385 if( handshake == NULL )
11386 return;
11387
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011388#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11389 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11390 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011391 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011392 handshake->async_in_progress = 0;
11393 }
11394#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11395
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011396#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11397 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11398 mbedtls_md5_free( &handshake->fin_md5 );
11399 mbedtls_sha1_free( &handshake->fin_sha1 );
11400#endif
11401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11402#if defined(MBEDTLS_SHA256_C)
11403 mbedtls_sha256_free( &handshake->fin_sha256 );
11404#endif
11405#if defined(MBEDTLS_SHA512_C)
11406 mbedtls_sha512_free( &handshake->fin_sha512 );
11407#endif
11408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011410#if defined(MBEDTLS_DHM_C)
11411 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011413#if defined(MBEDTLS_ECDH_C)
11414 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011415#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011416#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011417 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011418#if defined(MBEDTLS_SSL_CLI_C)
11419 mbedtls_free( handshake->ecjpake_cache );
11420 handshake->ecjpake_cache = NULL;
11421 handshake->ecjpake_cache_len = 0;
11422#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011423#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011424
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011425#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11426 if( handshake->psk != NULL )
11427 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011428 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011429 mbedtls_free( handshake->psk );
11430 }
11431#endif
11432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011433#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11434 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011435 /*
11436 * Free only the linked list wrapper, not the keys themselves
11437 * since the belong to the SNI callback
11438 */
11439 if( handshake->sni_key_cert != NULL )
11440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011441 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011442
11443 while( cur != NULL )
11444 {
11445 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011446 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011447 cur = next;
11448 }
11449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011450#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011451
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011452#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011453 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011454 if( handshake->ecrs_peer_cert != NULL )
11455 {
11456 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11457 mbedtls_free( handshake->ecrs_peer_cert );
11458 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011459#endif
11460
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011461#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11462 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11463 mbedtls_pk_free( &handshake->peer_pubkey );
11464#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011466#if defined(MBEDTLS_SSL_PROTO_DTLS)
11467 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011468 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011469 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011470#endif
11471
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011472 mbedtls_platform_zeroize( handshake,
11473 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011474}
11475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011476void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011477{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011478 if( session == NULL )
11479 return;
11480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011481#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011482 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011483#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011484
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011485#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011486 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011487#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011489 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011490}
11491
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011492#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011493
11494#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11495#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11496#else
11497#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11499
11500#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11501#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11502#else
11503#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11504#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11505
11506#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11507#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11508#else
11509#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11510#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11511
11512#if defined(MBEDTLS_SSL_ALPN)
11513#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11514#else
11515#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11516#endif /* MBEDTLS_SSL_ALPN */
11517
11518#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11519#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11520#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11521#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11522
11523#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11524 ( (uint32_t) ( \
11525 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11526 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11527 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11528 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11529 0u ) )
11530
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011531static unsigned char ssl_serialized_context_header[] = {
11532 MBEDTLS_VERSION_MAJOR,
11533 MBEDTLS_VERSION_MINOR,
11534 MBEDTLS_VERSION_PATCH,
11535 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11536 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011537 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11538 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11539 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011540};
11541
Paul Bakker5121ce52009-01-03 21:22:43 +000011542/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011543 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011544 *
11545 * The format of the serialized data is:
11546 * (in the presentation language of TLS, RFC 8446 section 3)
11547 *
11548 * // header
11549 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011550 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011551 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011552 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011553 * Note: When updating the format, remember to keep these
11554 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011555 *
11556 * // session sub-structure
11557 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11558 * // transform sub-structure
11559 * uint8 random[64]; // ServerHello.random+ClientHello.random
11560 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11561 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11562 * // fields from ssl_context
11563 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11564 * uint64 in_window_top; // DTLS: last validated record seq_num
11565 * uint64 in_window; // DTLS: bitmask for replay protection
11566 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11567 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11568 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11569 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11570 *
11571 * Note that many fields of the ssl_context or sub-structures are not
11572 * serialized, as they fall in one of the following categories:
11573 *
11574 * 1. forced value (eg in_left must be 0)
11575 * 2. pointer to dynamically-allocated memory (eg session, transform)
11576 * 3. value can be re-derived from other data (eg session keys from MS)
11577 * 4. value was temporary (eg content of input buffer)
11578 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011579 */
11580int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11581 unsigned char *buf,
11582 size_t buf_len,
11583 size_t *olen )
11584{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011585 unsigned char *p = buf;
11586 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011587 size_t session_len;
11588 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011589
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011590 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011591 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11592 * this function's documentation.
11593 *
11594 * These are due to assumptions/limitations in the implementation. Some of
11595 * them are likely to stay (no handshake in progress) some might go away
11596 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011597 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011598 /* The initial handshake must be over */
11599 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011601 if( ssl->handshake != NULL )
11602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11603 /* Double-check that sub-structures are indeed ready */
11604 if( ssl->transform == NULL || ssl->session == NULL )
11605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11606 /* There must be no pending incoming or outgoing data */
11607 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11609 if( ssl->out_left != 0 )
11610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11611 /* Protocol must be DLTS, not TLS */
11612 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11614 /* Version must be 1.2 */
11615 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11617 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11619 /* We must be using an AEAD ciphersuite */
11620 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11622 /* Renegotiation must not be enabled */
11623 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011625
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011626 /*
11627 * Version and format identifier
11628 */
11629 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011630
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011631 if( used <= buf_len )
11632 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011633 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011634 sizeof( ssl_serialized_context_header ) );
11635 p += sizeof( ssl_serialized_context_header );
11636 }
11637
11638 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011639 * Session (length + data)
11640 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011641 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011642 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11643 return( ret );
11644
11645 used += 4 + session_len;
11646 if( used <= buf_len )
11647 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011648 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011649
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011650 ret = ssl_session_save( ssl->session, 1,
11651 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011652 if( ret != 0 )
11653 return( ret );
11654
11655 p += session_len;
11656 }
11657
11658 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011659 * Transform
11660 */
11661 used += sizeof( ssl->transform->randbytes );
11662 if( used <= buf_len )
11663 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011664 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011665 sizeof( ssl->transform->randbytes ) );
11666 p += sizeof( ssl->transform->randbytes );
11667 }
11668
11669#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11670 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11671 if( used <= buf_len )
11672 {
11673 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011674 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11675 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011676 p += ssl->transform->in_cid_len;
11677
11678 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011679 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11680 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011681 p += ssl->transform->out_cid_len;
11682 }
11683#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11684
11685 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011686 * Saved fields from top-level ssl_context structure
11687 */
11688#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11689 used += 4;
11690 if( used <= buf_len )
11691 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011692 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011693 }
11694#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11695
11696#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11697 used += 16;
11698 if( used <= buf_len )
11699 {
11700 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11701 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11702 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11703 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11704 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11705 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11706 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11707 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11708
11709 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11710 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11711 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11712 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11713 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11714 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11715 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11716 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11717 }
11718#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11719
11720#if defined(MBEDTLS_SSL_PROTO_DTLS)
11721 used += 1;
11722 if( used <= buf_len )
11723 {
11724 *p++ = ssl->disable_datagram_packing;
11725 }
11726#endif /* MBEDTLS_SSL_PROTO_DTLS */
11727
11728 used += 8;
11729 if( used <= buf_len )
11730 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011731 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011732 p += 8;
11733 }
11734
11735#if defined(MBEDTLS_SSL_PROTO_DTLS)
11736 used += 2;
11737 if( used <= buf_len )
11738 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011739 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011740 }
11741#endif /* MBEDTLS_SSL_PROTO_DTLS */
11742
11743#if defined(MBEDTLS_SSL_ALPN)
11744 {
11745 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011746 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011747 : 0;
11748
11749 used += 1 + alpn_len;
11750 if( used <= buf_len )
11751 {
11752 *p++ = alpn_len;
11753
11754 if( ssl->alpn_chosen != NULL )
11755 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011756 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011757 p += alpn_len;
11758 }
11759 }
11760 }
11761#endif /* MBEDTLS_SSL_ALPN */
11762
11763 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011764 * Done
11765 */
11766 *olen = used;
11767
11768 if( used > buf_len )
11769 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011770
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011771 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11772
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011773 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011774}
11775
11776/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011777 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011778 *
11779 * This internal version is wrapped by a public function that cleans up in
11780 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011781 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011782static int ssl_context_load( mbedtls_ssl_context *ssl,
11783 const unsigned char *buf,
11784 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011785{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011786 const unsigned char *p = buf;
11787 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011788 size_t session_len;
11789 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011790
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011791 /*
11792 * The context should have been freshly setup or reset.
11793 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011794 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011795 * renegotiating, or if the user mistakenly loaded a session first.)
11796 */
11797 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11798 ssl->session != NULL )
11799 {
11800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11801 }
11802
11803 /*
11804 * We can't check that the config matches the initial one, but we can at
11805 * least check it matches the requirements for serializing.
11806 */
11807 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011808 mbedtls_ssl_ver_lt(
11809 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11810 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11811 mbedtls_ssl_ver_gt(
11812 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11813 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11814 mbedtls_ssl_ver_lt(
11815 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11816 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11817 mbedtls_ssl_ver_gt(
11818 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11819 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011820 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011821 {
11822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11823 }
11824
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011825 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11826
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011827 /*
11828 * Check version identifier
11829 */
11830 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11832
Teppo Järvelin650343c2019-10-03 15:36:59 +030011833 // use regular memcmp as header is not that critical
11834 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011835 sizeof( ssl_serialized_context_header ) ) != 0 )
11836 {
11837 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11838 }
11839 p += sizeof( ssl_serialized_context_header );
11840
11841 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011842 * Session
11843 */
11844 if( (size_t)( end - p ) < 4 )
11845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11846
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011847 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011848 p += 4;
11849
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011850 /* This has been allocated by ssl_handshake_init(), called by
11851 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11852 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011853 ssl->session_in = ssl->session;
11854 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011855 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011856
11857 if( (size_t)( end - p ) < session_len )
11858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11859
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011860 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011861 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011862 {
11863 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011864 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011865 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011866
11867 p += session_len;
11868
11869 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011870 * Transform
11871 */
11872
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011873 /* This has been allocated by ssl_handshake_init(), called by
11874 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11875 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011876 ssl->transform_in = ssl->transform;
11877 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011878 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011879
11880 /* Read random bytes and populate structure */
11881 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11883
11884 ret = ssl_populate_transform( ssl->transform,
11885 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11886 ssl->session->master,
11887#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11889 ssl->session->encrypt_then_mac,
11890#endif
11891#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11892 ssl->session->trunc_hmac,
11893#endif
11894#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11895#if defined(MBEDTLS_ZLIB_SUPPORT)
11896 ssl->session->compression,
11897#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011898 p, /* currently pointing to randbytes */
11899 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11900 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11901 ssl );
11902 if( ret != 0 )
11903 return( ret );
11904
11905 p += sizeof( ssl->transform->randbytes );
11906
11907#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11908 /* Read connection IDs and store them */
11909 if( (size_t)( end - p ) < 1 )
11910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11911
11912 ssl->transform->in_cid_len = *p++;
11913
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011914 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11916
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011917 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11918 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011919 p += ssl->transform->in_cid_len;
11920
11921 ssl->transform->out_cid_len = *p++;
11922
11923 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11925
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011926 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11927 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011928 p += ssl->transform->out_cid_len;
11929#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11930
11931 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011932 * Saved fields from top-level ssl_context structure
11933 */
11934#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11935 if( (size_t)( end - p ) < 4 )
11936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11937
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011938 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011939 p += 4;
11940#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11941
11942#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11943 if( (size_t)( end - p ) < 16 )
11944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11945
11946 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11947 ( (uint64_t) p[1] << 48 ) |
11948 ( (uint64_t) p[2] << 40 ) |
11949 ( (uint64_t) p[3] << 32 ) |
11950 ( (uint64_t) p[4] << 24 ) |
11951 ( (uint64_t) p[5] << 16 ) |
11952 ( (uint64_t) p[6] << 8 ) |
11953 ( (uint64_t) p[7] );
11954 p += 8;
11955
11956 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11957 ( (uint64_t) p[1] << 48 ) |
11958 ( (uint64_t) p[2] << 40 ) |
11959 ( (uint64_t) p[3] << 32 ) |
11960 ( (uint64_t) p[4] << 24 ) |
11961 ( (uint64_t) p[5] << 16 ) |
11962 ( (uint64_t) p[6] << 8 ) |
11963 ( (uint64_t) p[7] );
11964 p += 8;
11965#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11966
11967#if defined(MBEDTLS_SSL_PROTO_DTLS)
11968 if( (size_t)( end - p ) < 1 )
11969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11970
11971 ssl->disable_datagram_packing = *p++;
11972#endif /* MBEDTLS_SSL_PROTO_DTLS */
11973
11974 if( (size_t)( end - p ) < 8 )
11975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11976
Teppo Järvelin91d79382019-10-02 09:09:31 +030011977 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011978 p += 8;
11979
11980#if defined(MBEDTLS_SSL_PROTO_DTLS)
11981 if( (size_t)( end - p ) < 2 )
11982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011983 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011984 p += 2;
11985#endif /* MBEDTLS_SSL_PROTO_DTLS */
11986
11987#if defined(MBEDTLS_SSL_ALPN)
11988 {
11989 uint8_t alpn_len;
11990 const char **cur;
11991
11992 if( (size_t)( end - p ) < 1 )
11993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11994
11995 alpn_len = *p++;
11996
11997 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11998 {
11999 /* alpn_chosen should point to an item in the configured list */
12000 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12001 {
12002 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012003 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012004 {
12005 ssl->alpn_chosen = *cur;
12006 break;
12007 }
12008 }
12009 }
12010
12011 /* can only happen on conf mismatch */
12012 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12014
12015 p += alpn_len;
12016 }
12017#endif /* MBEDTLS_SSL_ALPN */
12018
12019 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012020 * Forced fields from top-level ssl_context structure
12021 *
12022 * Most of them already set to the correct value by mbedtls_ssl_init() and
12023 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12024 */
12025 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12026
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012027#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012028 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012029#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12030#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012031 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012032#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012033
Hanno Becker83985822019-08-30 10:42:49 +010012034 /* Adjust pointers for header fields of outgoing records to
12035 * the given transform, accounting for explicit IV and CID. */
12036 ssl_update_out_pointers( ssl, ssl->transform );
12037
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012038#if defined(MBEDTLS_SSL_PROTO_DTLS)
12039 ssl->in_epoch = 1;
12040#endif
12041
12042 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12043 * which we don't want - otherwise we'd end up freeing the wrong transform
12044 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12045 if( ssl->handshake != NULL )
12046 {
12047 mbedtls_ssl_handshake_free( ssl );
12048 mbedtls_free( ssl->handshake );
12049 ssl->handshake = NULL;
12050 }
12051
12052 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012053 * Done - should have consumed entire buffer
12054 */
12055 if( p != end )
12056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012057
12058 return( 0 );
12059}
12060
12061/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012062 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012063 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012064int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012065 const unsigned char *buf,
12066 size_t len )
12067{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012068 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012069
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012070 if( ret != 0 )
12071 mbedtls_ssl_free( context );
12072
12073 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012074}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012075#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012076
12077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012078 * Free an SSL context
12079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012080void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012081{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012082 if( ssl == NULL )
12083 return;
12084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012086
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012087 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012088 {
Angus Grattond8213d02016-05-25 20:56:48 +100012089 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012090 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012091 }
12092
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012093 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012094 {
Angus Grattond8213d02016-05-25 20:56:48 +100012095 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012096 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012097 }
12098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012099#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012100 if( ssl->compress_buf != NULL )
12101 {
Angus Grattond8213d02016-05-25 20:56:48 +100012102 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012103 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012104 }
12105#endif
12106
Paul Bakker48916f92012-09-16 19:57:18 +000012107 if( ssl->transform )
12108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012109 mbedtls_ssl_transform_free( ssl->transform );
12110 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012111 }
12112
12113 if( ssl->handshake )
12114 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012115 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012116 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12117 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012119 mbedtls_free( ssl->handshake );
12120 mbedtls_free( ssl->transform_negotiate );
12121 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012122 }
12123
Paul Bakkerc0463502013-02-14 11:19:38 +010012124 if( ssl->session )
12125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012126 mbedtls_ssl_session_free( ssl->session );
12127 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012128 }
12129
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012130#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012131 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012132 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012133 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012134 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012135 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012138#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12139 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12142 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012143 }
12144#endif
12145
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012146#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012147 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012148#endif
12149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012151
Paul Bakker86f04f42013-02-14 11:20:09 +010012152 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012153 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012154}
12155
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012156/*
12157 * Initialze mbedtls_ssl_config
12158 */
12159void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12160{
12161 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012162
12163#if !defined(MBEDTLS_SSL_PROTO_TLS)
12164 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12165#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012166}
12167
Simon Butcherc97b6972015-12-27 23:48:17 +000012168#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012169#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012170static int ssl_preset_default_hashes[] = {
12171#if defined(MBEDTLS_SHA512_C)
12172 MBEDTLS_MD_SHA512,
12173 MBEDTLS_MD_SHA384,
12174#endif
12175#if defined(MBEDTLS_SHA256_C)
12176 MBEDTLS_MD_SHA256,
12177 MBEDTLS_MD_SHA224,
12178#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012179#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012180 MBEDTLS_MD_SHA1,
12181#endif
12182 MBEDTLS_MD_NONE
12183};
Simon Butcherc97b6972015-12-27 23:48:17 +000012184#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012185#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012186
Hanno Becker73f4cb12019-06-27 13:51:07 +010012187#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012188static int ssl_preset_suiteb_ciphersuites[] = {
12189 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12190 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12191 0
12192};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012193#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012194
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012195#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é-Gonnardb31c5f62015-06-17 13:53:47 +020012197static int ssl_preset_suiteb_hashes[] = {
12198 MBEDTLS_MD_SHA256,
12199 MBEDTLS_MD_SHA384,
12200 MBEDTLS_MD_NONE
12201};
12202#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012203#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012204
Hanno Beckerc1096e72019-06-19 12:30:41 +010012205#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012206static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012207#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012208 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012209#endif
12210#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012211 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012212#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012213 MBEDTLS_ECP_DP_NONE
12214};
12215#endif
12216
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012217/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012218 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012219 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012220int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012221 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012222{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012223#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012224 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012225#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012226
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012227 /* Use the functions here so that they are covered in tests,
12228 * but otherwise access member directly for efficiency */
12229 mbedtls_ssl_conf_endpoint( conf, endpoint );
12230 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012231
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012232 /*
12233 * Things that are common to all presets
12234 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012235#if defined(MBEDTLS_SSL_CLI_C)
12236 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12237 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012238#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012239 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012240#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012241#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12242 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12243#endif
12244 }
12245#endif
12246
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012247#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012248 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012249#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012250
12251#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12252 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12253#endif
12254
12255#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012256#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012257 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012258#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12259#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012260 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012261 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012262#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012263#endif
12264
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012265#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12266 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12267#endif
12268
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012269#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012270 conf->f_cookie_write = ssl_cookie_write_dummy;
12271 conf->f_cookie_check = ssl_cookie_check_dummy;
12272#endif
12273
Hanno Becker7f376f42019-06-12 16:20:48 +010012274#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12275 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012276 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12277#endif
12278
Janos Follath088ce432017-04-10 12:42:31 +010012279#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012280#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012281 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012282#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12283#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012284
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012285#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012286#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012287 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012288#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12289#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012290 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012291#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12292#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012293
12294#if defined(MBEDTLS_SSL_RENEGOTIATION)
12295 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012296 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12297 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012298#endif
12299
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012300#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12301 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12302 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012303 const unsigned char dhm_p[] =
12304 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12305 const unsigned char dhm_g[] =
12306 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12307
Hanno Beckera90658f2017-10-04 15:29:08 +010012308 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12309 dhm_p, sizeof( dhm_p ),
12310 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012311 {
12312 return( ret );
12313 }
12314 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012315#endif
12316
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012317 /*
12318 * Preset-specific defaults
12319 */
12320 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012321 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012322 /*
12323 * NSA Suite B
12324 */
12325 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012326#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012327 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012328#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12329#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012330 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012331#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12332#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012333 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012334#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12335#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012336 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012337#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012338
Hanno Becker73f4cb12019-06-27 13:51:07 +010012339#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012340 conf->ciphersuite_list[0] =
12341 conf->ciphersuite_list[1] =
12342 conf->ciphersuite_list[2] =
12343 conf->ciphersuite_list[3] =
12344 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012345#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012346
12347#if defined(MBEDTLS_X509_CRT_PARSE_C)
12348 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012349#endif
12350
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012351#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012352#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012353 conf->sig_hashes = ssl_preset_suiteb_hashes;
12354#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012355#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012356
12357#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012358#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012359 conf->curve_list = ssl_preset_suiteb_curves;
12360#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012361#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012362 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012363
12364 /*
12365 * Default
12366 */
12367 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012368#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012369 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12370 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12371 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12372 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012373#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12374#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012375 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12376 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12377 MBEDTLS_SSL_MIN_MINOR_VERSION :
12378 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012379#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012380 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012381 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12382#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012383#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12384#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12385 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12386#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12387#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12388 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12389#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012390
Hanno Becker73f4cb12019-06-27 13:51:07 +010012391#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012392 conf->ciphersuite_list[0] =
12393 conf->ciphersuite_list[1] =
12394 conf->ciphersuite_list[2] =
12395 conf->ciphersuite_list[3] =
12396 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012397#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012398
12399#if defined(MBEDTLS_X509_CRT_PARSE_C)
12400 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12401#endif
12402
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012403#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012404#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012405 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012406#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012407#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012408
12409#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012410#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012411 conf->curve_list = mbedtls_ecp_grp_id_list();
12412#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012413#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012414
12415#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12416 conf->dhm_min_bitlen = 1024;
12417#endif
12418 }
12419
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012420 return( 0 );
12421}
12422
12423/*
12424 * Free mbedtls_ssl_config
12425 */
12426void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12427{
12428#if defined(MBEDTLS_DHM_C)
12429 mbedtls_mpi_free( &conf->dhm_P );
12430 mbedtls_mpi_free( &conf->dhm_G );
12431#endif
12432
12433#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12434 if( conf->psk != NULL )
12435 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012436 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012437 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012438 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012439 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012440 }
12441
12442 if( conf->psk_identity != NULL )
12443 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012444 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012445 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012446 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012447 conf->psk_identity_len = 0;
12448 }
12449#endif
12450
12451#if defined(MBEDTLS_X509_CRT_PARSE_C)
12452 ssl_key_cert_free( conf->key_cert );
12453#endif
12454
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012455 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012456}
12457
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012458#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012459 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12460 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012461/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012462 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012464unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012466#if defined(MBEDTLS_RSA_C)
12467 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12468 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012469#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012470#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012471 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12472 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012474 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012475}
12476
Hanno Becker7e5437a2017-04-28 17:15:26 +010012477unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12478{
12479 switch( type ) {
12480 case MBEDTLS_PK_RSA:
12481 return( MBEDTLS_SSL_SIG_RSA );
12482 case MBEDTLS_PK_ECDSA:
12483 case MBEDTLS_PK_ECKEY:
12484 return( MBEDTLS_SSL_SIG_ECDSA );
12485 default:
12486 return( MBEDTLS_SSL_SIG_ANON );
12487 }
12488}
12489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012490mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012491{
12492 switch( sig )
12493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012494#if defined(MBEDTLS_RSA_C)
12495 case MBEDTLS_SSL_SIG_RSA:
12496 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012497#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012498#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012499 case MBEDTLS_SSL_SIG_ECDSA:
12500 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012501#endif
12502 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012503 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012504 }
12505}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012506#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012507
Hanno Becker7e5437a2017-04-28 17:15:26 +010012508#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12509 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12510
12511/* Find an entry in a signature-hash set matching a given hash algorithm. */
12512mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12513 mbedtls_pk_type_t sig_alg )
12514{
12515 switch( sig_alg )
12516 {
12517 case MBEDTLS_PK_RSA:
12518 return( set->rsa );
12519 case MBEDTLS_PK_ECDSA:
12520 return( set->ecdsa );
12521 default:
12522 return( MBEDTLS_MD_NONE );
12523 }
12524}
12525
12526/* Add a signature-hash-pair to a signature-hash set */
12527void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12528 mbedtls_pk_type_t sig_alg,
12529 mbedtls_md_type_t md_alg )
12530{
12531 switch( sig_alg )
12532 {
12533 case MBEDTLS_PK_RSA:
12534 if( set->rsa == MBEDTLS_MD_NONE )
12535 set->rsa = md_alg;
12536 break;
12537
12538 case MBEDTLS_PK_ECDSA:
12539 if( set->ecdsa == MBEDTLS_MD_NONE )
12540 set->ecdsa = md_alg;
12541 break;
12542
12543 default:
12544 break;
12545 }
12546}
12547
12548/* Allow exactly one hash algorithm for each signature. */
12549void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12550 mbedtls_md_type_t md_alg )
12551{
12552 set->rsa = md_alg;
12553 set->ecdsa = md_alg;
12554}
12555
12556#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12557 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12558
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012559/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012560 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012562mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012563{
12564 switch( hash )
12565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012566#if defined(MBEDTLS_MD5_C)
12567 case MBEDTLS_SSL_HASH_MD5:
12568 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012570#if defined(MBEDTLS_SHA1_C)
12571 case MBEDTLS_SSL_HASH_SHA1:
12572 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012574#if defined(MBEDTLS_SHA256_C)
12575 case MBEDTLS_SSL_HASH_SHA224:
12576 return( MBEDTLS_MD_SHA224 );
12577 case MBEDTLS_SSL_HASH_SHA256:
12578 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012580#if defined(MBEDTLS_SHA512_C)
12581 case MBEDTLS_SSL_HASH_SHA384:
12582 return( MBEDTLS_MD_SHA384 );
12583 case MBEDTLS_SSL_HASH_SHA512:
12584 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012585#endif
12586 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012587 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012588 }
12589}
12590
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012591/*
12592 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12593 */
12594unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12595{
12596 switch( md )
12597 {
12598#if defined(MBEDTLS_MD5_C)
12599 case MBEDTLS_MD_MD5:
12600 return( MBEDTLS_SSL_HASH_MD5 );
12601#endif
12602#if defined(MBEDTLS_SHA1_C)
12603 case MBEDTLS_MD_SHA1:
12604 return( MBEDTLS_SSL_HASH_SHA1 );
12605#endif
12606#if defined(MBEDTLS_SHA256_C)
12607 case MBEDTLS_MD_SHA224:
12608 return( MBEDTLS_SSL_HASH_SHA224 );
12609 case MBEDTLS_MD_SHA256:
12610 return( MBEDTLS_SSL_HASH_SHA256 );
12611#endif
12612#if defined(MBEDTLS_SHA512_C)
12613 case MBEDTLS_MD_SHA384:
12614 return( MBEDTLS_SSL_HASH_SHA384 );
12615 case MBEDTLS_MD_SHA512:
12616 return( MBEDTLS_SSL_HASH_SHA512 );
12617#endif
12618 default:
12619 return( MBEDTLS_SSL_HASH_NONE );
12620 }
12621}
12622
Hanno Beckeree902df2019-08-23 13:47:47 +010012623#if defined(MBEDTLS_USE_TINYCRYPT)
12624/*
12625 * Check if a curve proposed by the peer is in our list.
12626 * Return 0 if we're willing to use it, -1 otherwise.
12627 */
12628int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12629 mbedtls_uecc_group_id grp_id )
12630{
12631 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12632 if( own_ec_id == grp_id )
12633 return( 0 );
12634 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12635
12636 return( -1 );
12637}
12638#endif /* MBEDTLS_USE_TINYCRYPT */
12639
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012640#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012641/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012642 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012643 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012644 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012645int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12646 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012647{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012648 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12649 if( own_ec_id == grp_id )
12650 return( 0 );
12651 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012652
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012653 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012654}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012655#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012656
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012657#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012658/*
12659 * Check if a hash proposed by the peer is in our list.
12660 * Return 0 if we're willing to use it, -1 otherwise.
12661 */
12662int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12663 mbedtls_md_type_t md )
12664{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012665 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12666 if( md_alg == md )
12667 return( 0 );
12668 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012669
12670 return( -1 );
12671}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012672#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012674#if defined(MBEDTLS_X509_CRT_PARSE_C)
12675int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012676 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012677 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012678 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012679{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012680 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012681#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012682 int usage = 0;
12683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012684#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012685 const char *ext_oid;
12686 size_t ext_len;
12687#endif
12688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012689#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12690 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012691 ((void) cert);
12692 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012693 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012694#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012696#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12697 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012698 {
12699 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012700 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012702 case MBEDTLS_KEY_EXCHANGE_RSA:
12703 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012704 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012705 break;
12706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012707 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12708 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12709 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12710 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012711 break;
12712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012713 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12714 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012715 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012716 break;
12717
12718 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012719 case MBEDTLS_KEY_EXCHANGE_NONE:
12720 case MBEDTLS_KEY_EXCHANGE_PSK:
12721 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12722 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012723 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012724 usage = 0;
12725 }
12726 }
12727 else
12728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012729 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12730 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012731 }
12732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012733 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012734 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012735 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012736 ret = -1;
12737 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012738#else
12739 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012740#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012742#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12743 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012745 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12746 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012747 }
12748 else
12749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012750 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12751 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012752 }
12753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012754 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012755 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012756 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012757 ret = -1;
12758 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012759#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012760
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012761 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012763#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012764
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012765#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12766 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12767int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12768 unsigned char *output,
12769 unsigned char *data, size_t data_len )
12770{
12771 int ret = 0;
12772 mbedtls_md5_context mbedtls_md5;
12773 mbedtls_sha1_context mbedtls_sha1;
12774
12775 mbedtls_md5_init( &mbedtls_md5 );
12776 mbedtls_sha1_init( &mbedtls_sha1 );
12777
12778 /*
12779 * digitally-signed struct {
12780 * opaque md5_hash[16];
12781 * opaque sha_hash[20];
12782 * };
12783 *
12784 * md5_hash
12785 * MD5(ClientHello.random + ServerHello.random
12786 * + ServerParams);
12787 * sha_hash
12788 * SHA(ClientHello.random + ServerHello.random
12789 * + ServerParams);
12790 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012791 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012792 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012794 goto exit;
12795 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012796 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012797 ssl->handshake->randbytes, 64 ) ) != 0 )
12798 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012800 goto exit;
12801 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012802 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012803 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012805 goto exit;
12806 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012807 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012808 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012810 goto exit;
12811 }
12812
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012813 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012814 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012816 goto exit;
12817 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012818 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012819 ssl->handshake->randbytes, 64 ) ) != 0 )
12820 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012822 goto exit;
12823 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012824 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012825 data_len ) ) != 0 )
12826 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012828 goto exit;
12829 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012830 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012831 output + 16 ) ) != 0 )
12832 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012834 goto exit;
12835 }
12836
12837exit:
12838 mbedtls_md5_free( &mbedtls_md5 );
12839 mbedtls_sha1_free( &mbedtls_sha1 );
12840
12841 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012842 mbedtls_ssl_pend_fatal_alert( ssl,
12843 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012844
12845 return( ret );
12846
12847}
12848#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12849 MBEDTLS_SSL_PROTO_TLS1_1 */
12850
12851#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12852 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12853int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012854 unsigned char *hash, size_t *hashlen,
12855 unsigned char *data, size_t data_len,
12856 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012857{
12858 int ret = 0;
12859 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012860 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012861 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012862
12863 mbedtls_md_init( &ctx );
12864
12865 /*
12866 * digitally-signed struct {
12867 * opaque client_random[32];
12868 * opaque server_random[32];
12869 * ServerDHParams params;
12870 * };
12871 */
12872 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12873 {
12874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12875 goto exit;
12876 }
12877 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12878 {
12879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12880 goto exit;
12881 }
12882 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12883 {
12884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12885 goto exit;
12886 }
12887 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12888 {
12889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12890 goto exit;
12891 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012892 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012893 {
12894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12895 goto exit;
12896 }
12897
12898exit:
12899 mbedtls_md_free( &ctx );
12900
12901 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012902 mbedtls_ssl_pend_fatal_alert( ssl,
12903 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012904
12905 return( ret );
12906}
12907#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12908 MBEDTLS_SSL_PROTO_TLS1_2 */
12909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012910#endif /* MBEDTLS_SSL_TLS_C */