blob: 20683366c21a3ffef557e41759389a77064625ec [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)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 if( handshake->resume != 0 )
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
4620 //Double check to ensure the encryption has been done
4621 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4622 {
4623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
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 {
7312 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Becker3cf50612019-02-05 14:36:34 +00007313 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007314 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007315
Jarno Lamsae1621d42019-12-19 08:58:56 +02007316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007317#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7318 if( ssl->handshake->sni_ca_chain != NULL )
7319 {
7320 ca_chain = ssl->handshake->sni_ca_chain;
7321 ca_crl = ssl->handshake->sni_ca_crl;
7322 }
7323 else
7324#endif
7325 {
7326 ca_chain = ssl->conf->ca_chain;
7327 ca_crl = ssl->conf->ca_crl;
7328 }
7329
7330 /*
7331 * Main check: verify certificate
7332 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007333 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007334 chain,
7335 ca_chain, ca_crl,
7336 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007337#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007338 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007339#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007340 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007341#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7342 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7343#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7344 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007345
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007346 if( verify_ret == 0 )
7347 {
7348 mbedtls_platform_enforce_volatile_reads();
7349 if( verify_ret == 0 )
7350 {
7351 flow_counter++;
7352 }
7353 else
7354 {
7355 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7356 }
7357 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007358 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007359 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007360 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007361 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007362 }
7363
7364#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007365 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007366 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7367#endif
7368
7369 /*
7370 * Secondary checks: always done, but change 'ret' only if it was 0
7371 */
7372
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007373#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007374 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007375#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007376 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007377#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007378 mbedtls_pk_context *pk;
7379 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7380 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007381 {
7382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007383 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007384 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007385
7386 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007387 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007388 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007389 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007390 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007391
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007392 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007393#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007394
7395 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007396 {
7397 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7398
7399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007400 if( verify_ret == 0 )
7401 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007402 flow_counter++;
7403 }
7404 if( ret == 0 )
7405 {
7406 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007407 }
7408 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007409#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007410
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007411 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007412 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007413 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7414 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007415 &ssl->session_negotiate->verify_result );
7416 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007417 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007418 flow_counter++;
7419 }
7420 else
7421 {
7422 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007424 if( verify_ret == 0 )
7425 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007426 }
7427
7428 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7429 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7430 * with details encoded in the verification flags. All other kinds
7431 * of error codes, including those from the user provided f_vrfy
7432 * functions, are treated as fatal and lead to a failure of
7433 * ssl_parse_certificate even if verification was optional. */
7434 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007435 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7436 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007437 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007438 mbedtls_platform_enforce_volatile_reads();
7439 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7440 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7441 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7442 {
7443 verify_ret = 0;
7444 flow_counter++;
7445 }
7446 else
7447 {
7448 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7449 }
7450 } else {
7451 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007452 }
7453
7454 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7455 {
7456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007457 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007458 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007459 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007460 else
7461 {
7462 flow_counter++;
7463 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007464
Hanno Becker8c13ee62019-02-26 16:48:17 +00007465 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007466 {
7467 uint8_t alert;
7468
7469 /* The certificate may have been rejected for several reasons.
7470 Pick one and send the corresponding alert. Which alert to send
7471 may be a subject of debate in some cases. */
7472 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7473 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7474 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7475 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7476 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7477 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7478 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7479 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7480 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7481 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7482 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7483 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7484 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7485 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7486 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7487 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7488 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7489 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7490 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7491 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7492 else
7493 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007494 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007495 }
7496
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007497 if( verify_ret == 0 &&
7498#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7499 flow_counter == 5 )
7500#else
7501 flow_counter == 4 )
7502#endif
7503 {
7504 mbedtls_platform_enforce_volatile_reads();
7505 if( verify_ret == 0 &&
7506#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7507 flow_counter == 5 )
7508#else
7509 flow_counter == 4 )
7510#endif
7511 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007513 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7514 }
7515 else
7516 {
7517 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7518 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007519 } else {
7520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007521 }
7522
Hanno Becker3cf50612019-02-05 14:36:34 +00007523#if defined(MBEDTLS_DEBUG_C)
7524 if( ssl->session_negotiate->verify_result != 0 )
7525 {
7526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7527 ssl->session_negotiate->verify_result ) );
7528 }
7529 else
7530 {
7531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7532 }
7533#endif /* MBEDTLS_DEBUG_C */
7534
Hanno Becker8c13ee62019-02-26 16:48:17 +00007535 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007536}
7537
Hanno Becker34106f62019-02-08 14:59:05 +00007538#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007539
7540#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007541static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7542 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007543{
7544 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007545 /* Remember digest of the peer's end-CRT. */
7546 ssl->session_negotiate->peer_cert_digest =
7547 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7548 if( ssl->session_negotiate->peer_cert_digest == NULL )
7549 {
7550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7551 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007552 mbedtls_ssl_pend_fatal_alert( ssl,
7553 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007554
7555 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7556 }
7557
7558 ret = mbedtls_md( mbedtls_md_info_from_type(
7559 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7560 start, len,
7561 ssl->session_negotiate->peer_cert_digest );
7562
7563 ssl->session_negotiate->peer_cert_digest_type =
7564 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7565 ssl->session_negotiate->peer_cert_digest_len =
7566 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7567
7568 return( ret );
7569}
Hanno Becker5882dd02019-06-06 16:25:57 +01007570#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007571
7572static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7573 unsigned char *start, size_t len )
7574{
7575 unsigned char *end = start + len;
7576 int ret;
7577
7578 /* Make a copy of the peer's raw public key. */
7579 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7580 ret = mbedtls_pk_parse_subpubkey( &start, end,
7581 &ssl->handshake->peer_pubkey );
7582 if( ret != 0 )
7583 {
7584 /* We should have parsed the public key before. */
7585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7586 }
7587
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007588 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007589 return( 0 );
7590}
7591#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7592
Hanno Becker3cf50612019-02-05 14:36:34 +00007593int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7594{
7595 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007596 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007597#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7598 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7599 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007600 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007601#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007602 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007603#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007604 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007605 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007606
7607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7608
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007609 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7610 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007611 {
7612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007613 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Becker613d4902019-02-05 13:11:17 +00007614 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007615 }
7616
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007617#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7618 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007619 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007620 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007621 chain = ssl->handshake->ecrs_peer_cert;
7622 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007623 goto crt_verify;
7624 }
7625#endif
7626
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007627 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007628 {
7629 /* mbedtls_ssl_read_record may have sent an alert already. We
7630 let it decide whether to alert. */
7631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007632 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007633 }
7634
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007636 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7637 {
7638 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007639
Hanno Beckerb8a08572019-02-05 12:49:06 +00007640 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007641 ret = 0;
7642 else
7643 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007644
Hanno Becker613d4902019-02-05 13:11:17 +00007645 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007646 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007647#endif /* MBEDTLS_SSL_SRV_C */
7648
Hanno Becker35e41772019-02-05 15:37:23 +00007649 /* Clear existing peer CRT structure in case we tried to
7650 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007651 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007652
7653 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7654 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007655 {
7656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7657 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007658 mbedtls_ssl_pend_fatal_alert( ssl,
7659 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007660
Hanno Beckere4aeb762019-02-05 17:19:52 +00007661 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7662 goto exit;
7663 }
7664 mbedtls_x509_crt_init( chain );
7665
7666 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007667 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007668 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007669
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007670#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7671 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007672 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007673
7674crt_verify:
7675 if( ssl->handshake->ecrs_enabled)
7676 rs_ctx = &ssl->handshake->ecrs_ctx;
7677#endif
7678
Hanno Becker3cf50612019-02-05 14:36:34 +00007679 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007680 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007681 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007682 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
Hanno Becker3008d282019-02-05 17:02:28 +00007684#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007685 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007686 size_t pk_len;
7687 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007688
Hanno Becker34106f62019-02-08 14:59:05 +00007689 /* We parse the CRT chain without copying, so
7690 * these pointers point into the input buffer,
7691 * and are hence still valid after freeing the
7692 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Hanno Becker5882dd02019-06-06 16:25:57 +01007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
7695 unsigned char *crt_start;
7696 size_t crt_len;
7697
Hanno Becker34106f62019-02-08 14:59:05 +00007698 crt_start = chain->raw.p;
7699 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007700#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007701
Hanno Becker8c13ee62019-02-26 16:48:17 +00007702 pk_start = chain->cache->pk_raw.p;
7703 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007704
7705 /* Free the CRT structures before computing
7706 * digest and copying the peer's public key. */
7707 mbedtls_x509_crt_free( chain );
7708 mbedtls_free( chain );
7709 chain = NULL;
7710
Hanno Becker5882dd02019-06-06 16:25:57 +01007711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007712 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007714 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007715#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007716
Hanno Becker34106f62019-02-08 14:59:05 +00007717 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007719 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 }
Hanno Becker34106f62019-02-08 14:59:05 +00007721#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7722 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007723 ssl->session_negotiate->peer_cert = chain;
7724 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007725#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Hanno Becker613d4902019-02-05 13:11:17 +00007729exit:
7730
Hanno Beckere4aeb762019-02-05 17:19:52 +00007731 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007732 {
7733 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7734 {
7735 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7736 }
7737 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7738 {
7739 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7740 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007741 else
7742 {
7743 ssl->state = MBEDTLS_SSL_INVALID;
7744 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007745 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007746
7747#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7748 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7749 {
7750 ssl->handshake->ecrs_peer_cert = chain;
7751 chain = NULL;
7752 }
7753#endif
7754
7755 if( chain != NULL )
7756 {
7757 mbedtls_x509_crt_free( chain );
7758 mbedtls_free( chain );
7759 }
7760
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 return( ret );
7762}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007763#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766{
7767 int ret;
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 ssl->out_msglen = 1;
7773 ssl->out_msg[0] = 1;
7774
Jarno Lamsa2b205162019-11-12 15:36:21 +02007775 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7776 {
7777 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7778 }
7779 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7780 {
7781 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7782 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007783 else
7784 {
7785 ssl->state = MBEDTLS_SSL_INVALID;
7786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007788 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007789 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 return( ret );
7792 }
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
7796 return( 0 );
7797}
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800{
7801 int ret;
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Hanno Becker327c93b2018-08-15 13:56:18 +01007805 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 return( ret );
7809 }
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007814 mbedtls_ssl_pend_fatal_alert( ssl,
7815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 }
7818
Hanno Beckere678eaa2018-08-21 14:57:46 +01007819 /* CCS records are only accepted if they have length 1 and content '1',
7820 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007822 /*
7823 * Switch to our negotiated transform and session parameters for inbound
7824 * data.
7825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007827 ssl->transform_in = ssl->transform_negotiate;
7828 ssl->session_in = ssl->session_negotiate;
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007831 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007834 ssl_dtls_replay_reset( ssl );
7835#endif
7836
7837 /* Increment epoch */
7838 if( ++ssl->in_epoch == 0 )
7839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007841 /* This is highly unlikely to happen for legitimate reasons, so
7842 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007844 }
7845 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848#if defined(MBEDTLS_SSL_PROTO_TLS)
7849 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007850 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851 }
7852#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007853
Hanno Beckerf5970a02019-05-08 09:38:41 +01007854 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7857 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007862 mbedtls_ssl_pend_fatal_alert( ssl,
7863 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007865 }
7866 }
7867#endif
7868
Jarno Lamsa2b205162019-11-12 15:36:21 +02007869 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7870 {
7871 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7872 }
7873 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7874 {
7875 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7876 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007877 else
7878 {
7879 ssl->state = MBEDTLS_SSL_INVALID;
7880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
7884 return( 0 );
7885}
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7890 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007891 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007892 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7895#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007896 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007907
7908 /*
7909 * Free our handshake params
7910 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007911 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007913 ssl->handshake = NULL;
7914
7915 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007916 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007917 */
7918 if( ssl->transform )
7919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 mbedtls_ssl_transform_free( ssl->transform );
7921 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007922 }
7923 ssl->transform = ssl->transform_negotiate;
7924 ssl->transform_negotiate = NULL;
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927}
7928
Jarno Lamsae1621d42019-12-19 08:58:56 +02007929int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007930{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007931 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_RENEGOTIATION)
7935 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007938 ssl->renego_records_seen = 0;
7939 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007940#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007941
7942 /*
7943 * Free the previous session and switch in the current one
7944 */
Paul Bakker0a597072012-09-25 21:55:46 +00007945 if( ssl->session )
7946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007948 /* RFC 7366 3.1: keep the EtM state */
7949 ssl->session_negotiate->encrypt_then_mac =
7950 ssl->session->encrypt_then_mac;
7951#endif
7952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 mbedtls_ssl_session_free( ssl->session );
7954 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007955 }
7956 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007957 ssl->session_negotiate = NULL;
7958
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007959#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007960 /*
7961 * Add cache entry
7962 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007963 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007964 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007965 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007966 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007967 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007969 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007970#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007971
Jarno Lamsae1621d42019-12-19 08:58:56 +02007972#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
7973 if( ssl->handshake->resume )
7974 {
7975 mbedtls_platform_enforce_volatile_reads();
7976 if( ssl->handshake->resume )
7977 {
Jarno Lamsa06164052019-12-19 14:40:36 +02007978 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02007979 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02007980 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02007981 }
7982 else
7983 {
7984 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
7985 return( ret );
7986 }
7987 }
7988#endif
7989
7990 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
7991 {
7992 mbedtls_platform_enforce_volatile_reads();
7993 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
7994 {
7995 ret = 0;
7996 }
7997 else
7998 {
7999 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008000 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008001 }
8002 }
8003 else
8004 {
8005 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008006 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008007 }
8008
Jarno Lamsa06164052019-12-19 14:40:36 +02008009 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8010 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8011 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8012 {
8013 mbedtls_platform_enforce_volatile_reads();
8014 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8015 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8016 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8017 {
8018 ret = 0;
8019 }
8020 else
8021 {
8022 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8023 goto cleanup;
8024 }
8025 }
8026 else
8027 {
8028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8031 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8032 }
8033
8034cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008036 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008037 ssl->handshake->flight != NULL )
8038 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008039 /* Cancel handshake timer */
8040 ssl_set_timer( ssl, 0 );
8041
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008042 /* Keep last flight around in case we need to resend it:
8043 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008045 }
8046 else
8047#endif
8048 ssl_handshake_wrapup_free_hs_transform( ssl );
8049
Jarno Lamsa2b205162019-11-12 15:36:21 +02008050 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008053 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008054}
8055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008057{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008058 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008061
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008062 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008063
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008064 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8065 mbedtls_ssl_suite_get_mac(
8066 mbedtls_ssl_ciphersuite_from_id(
8067 mbedtls_ssl_session_get_ciphersuite(
8068 ssl->session_negotiate ) ) ),
8069 ssl, ssl->out_msg + 4,
8070 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008071
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008072 /*
8073 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8074 * may define some other value. Currently (early 2016), no defined
8075 * ciphersuite does this (and this is unlikely to change as activity has
8076 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8077 */
Hanno Becker2881d802019-05-22 14:44:53 +01008078 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008081 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008082 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008083#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008084
Paul Bakker5121ce52009-01-03 21:22:43 +00008085 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8087 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008088
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008089#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008090 /*
8091 * In case of session resuming, invert the client and server
8092 * ChangeCipherSpec messages order.
8093 */
Paul Bakker0a597072012-09-25 21:55:46 +00008094 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008097 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8098 MBEDTLS_SSL_IS_CLIENT )
8099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008101 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008104 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8105 MBEDTLS_SSL_IS_SERVER )
8106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008108 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008110 }
8111 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008112#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008113 {
8114 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8115 {
8116 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8117 }
8118 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8119 {
8120 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8121 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008122 else
8123 {
8124 ssl->state = MBEDTLS_SSL_INVALID;
8125 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008126 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008127
Paul Bakker48916f92012-09-16 19:57:18 +00008128 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008129 * Switch to our negotiated transform and session parameters for outbound
8130 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008135 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008136 {
8137 unsigned char i;
8138
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008139 /* Remember current epoch settings for resending */
8140 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008141 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008142
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008143 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008144 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008145
8146 /* Increment epoch */
8147 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008148 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008149 break;
8150
8151 /* The loop goes to its end iff the counter is wrapping */
8152 if( i == 0 )
8153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8155 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008156 }
8157 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008158 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008160#if defined(MBEDTLS_SSL_PROTO_TLS)
8161 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008162 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008163 }
8164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008165
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008166 ssl->transform_out = ssl->transform_negotiate;
8167 ssl->session_out = ssl->session_negotiate;
8168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8170 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8175 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008176 }
8177 }
8178#endif
8179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008181 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008183#endif
8184
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008185 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008186 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008188 return( ret );
8189 }
8190
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008191#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008192 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008193 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8194 {
8195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8196 return( ret );
8197 }
8198#endif
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008201
8202 return( 0 );
8203}
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008206#define SSL_MAX_HASH_LEN 36
8207#else
8208#define SSL_MAX_HASH_LEN 12
8209#endif
8210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008212{
Paul Bakker23986e52011-04-24 08:57:21 +00008213 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008214 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008215 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008218
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008219 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8220 mbedtls_ssl_suite_get_mac(
8221 mbedtls_ssl_ciphersuite_from_id(
8222 mbedtls_ssl_session_get_ciphersuite(
8223 ssl->session_negotiate ) ) ),
8224 ssl, buf,
8225 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008226
Hanno Becker327c93b2018-08-15 13:56:18 +01008227 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008230 return( ret );
8231 }
8232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008236 mbedtls_ssl_pend_fatal_alert( ssl,
8237 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008239 }
8240
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008241 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008243 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008244 hash_len = 36;
8245 else
8246#endif
8247 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8250 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008253 mbedtls_ssl_pend_fatal_alert( ssl,
8254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008256 }
8257
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008258 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008259 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008262 mbedtls_ssl_pend_fatal_alert( ssl,
8263 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008265 }
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008268 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008269 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008270#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008271
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008272#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00008273 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008276 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008280 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008283 }
8284 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008285#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008286 {
8287 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8288 {
8289 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8290 }
8291 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8292 {
8293 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8294 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008295 else
8296 {
8297 ssl->state = MBEDTLS_SSL_INVALID;
8298 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008302 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008304#endif
8305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008307
8308 return( 0 );
8309}
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8316 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8317 mbedtls_md5_init( &handshake->fin_md5 );
8318 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008319 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8320 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8323#if defined(MBEDTLS_SHA256_C)
8324 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008325 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_SHA512_C)
8328 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008329 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008332
Hanno Becker7e5437a2017-04-28 17:15:26 +01008333#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8334 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8335 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8336#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#if defined(MBEDTLS_DHM_C)
8339 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#if defined(MBEDTLS_ECDH_C)
8342 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008343#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008344#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008345 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008346#if defined(MBEDTLS_SSL_CLI_C)
8347 handshake->ecjpake_cache = NULL;
8348 handshake->ecjpake_cache_len = 0;
8349#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008350#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008351
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008352#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008353 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008354#endif
8355
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008356#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8357 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8358#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008359
8360#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8361 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8362 mbedtls_pk_init( &handshake->peer_pubkey );
8363#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008364}
8365
Hanno Becker611a83b2018-01-03 14:27:32 +00008366void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008367{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8371 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008372
Hanno Becker92231322018-01-03 15:32:51 +00008373#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374 mbedtls_md_init( &transform->md_ctx_enc );
8375 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008376#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008377}
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008382}
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008385{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008386 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008387 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008389 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008391 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008392 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008393
8394 /*
8395 * Either the pointers are now NULL or cleared properly and can be freed.
8396 * Now allocate missing structures.
8397 */
8398 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008399 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008400 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008401 }
Paul Bakker48916f92012-09-16 19:57:18 +00008402
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008403 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008404 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008405 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008406 }
Paul Bakker48916f92012-09-16 19:57:18 +00008407
Paul Bakker82788fb2014-10-20 13:59:19 +02008408 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008409 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008410 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008411 }
Paul Bakker48916f92012-09-16 19:57:18 +00008412
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008413 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008414 if( ssl->handshake == NULL ||
8415 ssl->transform_negotiate == NULL ||
8416 ssl->session_negotiate == NULL )
8417 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 mbedtls_free( ssl->handshake );
8421 mbedtls_free( ssl->transform_negotiate );
8422 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008423
8424 ssl->handshake = NULL;
8425 ssl->transform_negotiate = NULL;
8426 ssl->session_negotiate = NULL;
8427
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008428 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008429 }
8430
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008431 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008433 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008434 ssl_handshake_params_init( ssl->handshake );
8435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008437 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008438 {
8439 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008440
Hanno Becker2d9623f2019-06-13 12:07:05 +01008441 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008442 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8443 else
8444 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8445 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008446#endif
8447
Paul Bakker48916f92012-09-16 19:57:18 +00008448 return( 0 );
8449}
8450
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008451#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008452/* Dummy cookie callbacks for defaults */
8453static int ssl_cookie_write_dummy( void *ctx,
8454 unsigned char **p, unsigned char *end,
8455 const unsigned char *cli_id, size_t cli_id_len )
8456{
8457 ((void) ctx);
8458 ((void) p);
8459 ((void) end);
8460 ((void) cli_id);
8461 ((void) cli_id_len);
8462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008464}
8465
8466static int ssl_cookie_check_dummy( void *ctx,
8467 const unsigned char *cookie, size_t cookie_len,
8468 const unsigned char *cli_id, size_t cli_id_len )
8469{
8470 ((void) ctx);
8471 ((void) cookie);
8472 ((void) cookie_len);
8473 ((void) cli_id);
8474 ((void) cli_id_len);
8475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008477}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008478#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008479
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008480/* Once ssl->out_hdr as the address of the beginning of the
8481 * next outgoing record is set, deduce the other pointers.
8482 *
8483 * Note: For TLS, we save the implicit record sequence number
8484 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8485 * and the caller has to make sure there's space for this.
8486 */
8487
8488static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8489 mbedtls_ssl_transform *transform )
8490{
8491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008492 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008493 {
8494 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008495#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008496 ssl->out_cid = ssl->out_ctr + 8;
8497 ssl->out_len = ssl->out_cid;
8498 if( transform != NULL )
8499 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008500#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008501 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008503 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008504 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008505 MBEDTLS_SSL_TRANSPORT_ELSE
8506#endif /* MBEDTLS_SSL_PROTO_DTLS */
8507#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008508 {
8509 ssl->out_ctr = ssl->out_hdr - 8;
8510 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008511#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008512 ssl->out_cid = ssl->out_len;
8513#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008514 ssl->out_iv = ssl->out_hdr + 5;
8515 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008516#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008517
8518 /* Adjust out_msg to make space for explicit IV, if used. */
8519 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008520 mbedtls_ssl_ver_geq(
8521 mbedtls_ssl_get_minor_ver( ssl ),
8522 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008523 {
8524 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8525 }
8526 else
8527 ssl->out_msg = ssl->out_iv;
8528}
8529
8530/* Once ssl->in_hdr as the address of the beginning of the
8531 * next incoming record is set, deduce the other pointers.
8532 *
8533 * Note: For TLS, we save the implicit record sequence number
8534 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8535 * and the caller has to make sure there's space for this.
8536 */
8537
Hanno Beckerf5970a02019-05-08 09:38:41 +01008538static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008539{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008540 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008541 * of unprotected TLS/DTLS records, with ssl->in_msg
8542 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008543 *
8544 * When decrypting a protected record, ssl->in_msg
8545 * will be shifted to point to the beginning of the
8546 * record plaintext.
8547 */
8548
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008550 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008551 {
Hanno Becker70e79282019-05-03 14:34:53 +01008552 /* This sets the header pointers to match records
8553 * without CID. When we receive a record containing
8554 * a CID, the fields are shifted accordingly in
8555 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008556 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008557#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008558 ssl->in_cid = ssl->in_ctr + 8;
8559 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008560#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008561 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008562#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008563 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008564 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008565 MBEDTLS_SSL_TRANSPORT_ELSE
8566#endif /* MBEDTLS_SSL_PROTO_DTLS */
8567#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008568 {
8569 ssl->in_ctr = ssl->in_hdr - 8;
8570 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008571#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008572 ssl->in_cid = ssl->in_len;
8573#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008574 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008575 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008576#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577}
8578
Paul Bakker5121ce52009-01-03 21:22:43 +00008579/*
8580 * Initialize an SSL context
8581 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008582void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8583{
8584 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8585}
8586
8587/*
8588 * Setup an SSL context
8589 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008590
8591static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8592{
8593 /* Set the incoming and outgoing record pointers. */
8594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008595 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008596 {
8597 ssl->out_hdr = ssl->out_buf;
8598 ssl->in_hdr = ssl->in_buf;
8599 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008600 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008601#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008602#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008603 {
8604 ssl->out_hdr = ssl->out_buf + 8;
8605 ssl->in_hdr = ssl->in_buf + 8;
8606 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008607#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008608
8609 /* Derive other internal pointers. */
8610 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008611 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008612}
8613
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008614int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008615 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008616{
Paul Bakker48916f92012-09-16 19:57:18 +00008617 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008618
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008619 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008620
Hanno Beckeref982d52019-07-23 15:56:18 +01008621#if defined(MBEDTLS_USE_TINYCRYPT)
8622 uECC_set_rng( &uecc_rng_wrapper );
8623#endif
8624
Paul Bakker62f2dee2012-09-28 07:31:51 +00008625 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008626 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008627 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008628
8629 /* Set to NULL in case of an error condition */
8630 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008631
Angus Grattond8213d02016-05-25 20:56:48 +10008632 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8633 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008634 {
Angus Grattond8213d02016-05-25 20:56:48 +10008635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008636 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008637 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008638 }
8639
8640 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8641 if( ssl->out_buf == NULL )
8642 {
8643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008644 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008645 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008646 }
8647
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008648 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008649
Paul Bakker48916f92012-09-16 19:57:18 +00008650 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008651 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008652
Hanno Beckerc8f52992019-07-25 11:15:08 +01008653 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008654
Paul Bakker5121ce52009-01-03 21:22:43 +00008655 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008656
8657error:
8658 mbedtls_free( ssl->in_buf );
8659 mbedtls_free( ssl->out_buf );
8660
8661 ssl->conf = NULL;
8662
8663 ssl->in_buf = NULL;
8664 ssl->out_buf = NULL;
8665
8666 ssl->in_hdr = NULL;
8667 ssl->in_ctr = NULL;
8668 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008669 ssl->in_msg = NULL;
8670
8671 ssl->out_hdr = NULL;
8672 ssl->out_ctr = NULL;
8673 ssl->out_len = NULL;
8674 ssl->out_iv = NULL;
8675 ssl->out_msg = NULL;
8676
k-stachowiak9f7798e2018-07-31 16:52:32 +02008677 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008678}
8679
8680/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008681 * Reset an initialized and used SSL context for re-use while retaining
8682 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008683 *
8684 * If partial is non-zero, keep data in the input buffer and client ID.
8685 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008686 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008687static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008688{
Paul Bakker48916f92012-09-16 19:57:18 +00008689 int ret;
8690
Hanno Becker7e772132018-08-10 12:38:21 +01008691#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8692 !defined(MBEDTLS_SSL_SRV_C)
8693 ((void) partial);
8694#endif
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008697
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008698 /* Cancel any possibly running timer */
8699 ssl_set_timer( ssl, 0 );
8700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#if defined(MBEDTLS_SSL_RENEGOTIATION)
8702 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008703 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008704
8705 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008706 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8707 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008710
Paul Bakker7eb013f2011-10-06 12:37:39 +00008711 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008712 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008713
8714 ssl->in_msgtype = 0;
8715 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008717 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008718 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008721 ssl_dtls_replay_reset( ssl );
8722#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008723
8724 ssl->in_hslen = 0;
8725 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008726
8727 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008728
8729 ssl->out_msgtype = 0;
8730 ssl->out_msglen = 0;
8731 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8733 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008734 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008735#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008736
Hanno Becker19859472018-08-06 09:40:20 +01008737 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8738
Paul Bakker48916f92012-09-16 19:57:18 +00008739 ssl->transform_in = NULL;
8740 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008741
Hanno Becker78640902018-08-13 16:35:15 +01008742 ssl->session_in = NULL;
8743 ssl->session_out = NULL;
8744
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008745 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008746
8747#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008748 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008749#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8750 {
8751 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008752 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008753 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8756 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8759 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8762 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008763 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008764 }
8765#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008766
Paul Bakker48916f92012-09-16 19:57:18 +00008767 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 mbedtls_ssl_transform_free( ssl->transform );
8770 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008771 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008772 }
Paul Bakker48916f92012-09-16 19:57:18 +00008773
Paul Bakkerc0463502013-02-14 11:19:38 +01008774 if( ssl->session )
8775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 mbedtls_ssl_session_free( ssl->session );
8777 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008778 ssl->session = NULL;
8779 }
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008782 ssl->alpn_chosen = NULL;
8783#endif
8784
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008785#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008786#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008787 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008788#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008789 {
8790 mbedtls_free( ssl->cli_id );
8791 ssl->cli_id = NULL;
8792 ssl->cli_id_len = 0;
8793 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008794#endif
8795
Paul Bakker48916f92012-09-16 19:57:18 +00008796 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8797 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008798
8799 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008800}
8801
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008802/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008803 * Reset an initialized and used SSL context for re-use while retaining
8804 * all application-set variables, function pointers and data.
8805 */
8806int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8807{
8808 return( ssl_session_reset_int( ssl, 0 ) );
8809}
8810
8811/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008812 * SSL set accessors
8813 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008814#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008815void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008817 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008818}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008819#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008820
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008821void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008822{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008823 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008824}
8825
Hanno Becker7f376f42019-06-12 16:20:48 +01008826#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8827 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008828void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008829{
Hanno Becker7f376f42019-06-12 16:20:48 +01008830 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008831}
Hanno Becker7f376f42019-06-12 16:20:48 +01008832#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008833
Hanno Beckerde671542019-06-12 16:30:46 +01008834#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8835 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8836void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8837 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008838{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008839 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008840}
Hanno Beckerde671542019-06-12 16:30:46 +01008841#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008844
Hanno Becker1841b0a2018-08-24 11:13:57 +01008845void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8846 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008847{
8848 ssl->disable_datagram_packing = !allow_packing;
8849}
8850
Hanno Becker1f835fa2019-06-13 10:14:59 +01008851#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8852 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008853void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8854 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008855{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008856 conf->hs_timeout_min = min;
8857 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008858}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008859#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8860 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8861void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8862 uint32_t min, uint32_t max )
8863{
8864 ((void) conf);
8865 ((void) min);
8866 ((void) max);
8867}
8868#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8869 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8870
8871#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008872
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008874{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008875#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8876 conf->authmode = authmode;
8877#else
8878 ((void) conf);
8879 ((void) authmode);
8880#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008881}
8882
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008883#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8884 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008885void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008886 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008887 void *p_vrfy )
8888{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008889 conf->f_vrfy = f_vrfy;
8890 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008891}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008892#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008893
Hanno Beckerece325c2019-06-13 15:39:27 +01008894#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008895void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008896 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 void *p_rng )
8898{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008899 conf->f_rng = f_rng;
8900 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008901}
Hanno Beckerece325c2019-06-13 15:39:27 +01008902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008903
Hanno Becker14a4a442019-07-02 17:00:34 +01008904#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008905void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008906 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008907 void *p_dbg )
8908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008909 conf->f_dbg = f_dbg;
8910 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008911}
Hanno Becker14a4a442019-07-02 17:00:34 +01008912#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008913
Hanno Beckera58a8962019-06-13 16:11:15 +01008914#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8915 !defined(MBEDTLS_SSL_CONF_SEND) && \
8916 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008918 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008919 mbedtls_ssl_send_t *f_send,
8920 mbedtls_ssl_recv_t *f_recv,
8921 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008922{
Hanno Beckera58a8962019-06-13 16:11:15 +01008923 ssl->p_bio = p_bio;
8924 ssl->f_send = f_send;
8925 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008926 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008927}
Hanno Beckera58a8962019-06-13 16:11:15 +01008928#else
8929void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8930 void *p_bio )
8931{
8932 ssl->p_bio = p_bio;
8933}
8934#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008935
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008936#if defined(MBEDTLS_SSL_PROTO_DTLS)
8937void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8938{
8939 ssl->mtu = mtu;
8940}
8941#endif
8942
Hanno Becker1f835fa2019-06-13 10:14:59 +01008943#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008944void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008945{
8946 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008947}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008948#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008949
Hanno Becker0ae6b242019-06-13 16:45:36 +01008950#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8951 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008952void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8953 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008954 mbedtls_ssl_set_timer_t *f_set_timer,
8955 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008956{
8957 ssl->p_timer = p_timer;
8958 ssl->f_set_timer = f_set_timer;
8959 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008960 /* Make sure we start with no timer running */
8961 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008962}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008963#else
8964void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8965 void *p_timer )
8966{
8967 ssl->p_timer = p_timer;
8968 /* Make sure we start with no timer running */
8969 ssl_set_timer( ssl, 0 );
8970}
8971#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008972
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008973#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008975 void *p_cache,
8976 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8977 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008978{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008979 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008980 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008981 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008982}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008983#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008984
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008985#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008987{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008988 int ret;
8989
8990 if( ssl == NULL ||
8991 session == NULL ||
8992 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008993 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008996 }
8997
Hanno Becker58fccf22019-02-06 14:30:46 +00008998 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8999 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009000 return( ret );
9001
Paul Bakker0a597072012-09-25 21:55:46 +00009002 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009003
9004 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009005}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009006#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009007
Hanno Becker73f4cb12019-06-27 13:51:07 +01009008#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009009void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009010 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009011{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009012 conf->ciphersuite_list[0] = ciphersuites;
9013 conf->ciphersuite_list[1] = ciphersuites;
9014 conf->ciphersuite_list[2] = ciphersuites;
9015 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009016}
9017
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009018void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009019 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009020 int major, int minor )
9021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009023 return;
9024
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009025 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9026 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9027 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009028 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009029 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009030
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009031 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9032 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009033}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009034#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009037void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009038 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009039{
9040 conf->cert_profile = profile;
9041}
9042
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009043/* Append a new keycert entry to a (possibly empty) list */
9044static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9045 mbedtls_x509_crt *cert,
9046 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009047{
niisato8ee24222018-06-25 19:05:48 +09009048 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009049
niisato8ee24222018-06-25 19:05:48 +09009050 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9051 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009052 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009053
niisato8ee24222018-06-25 19:05:48 +09009054 new_cert->cert = cert;
9055 new_cert->key = key;
9056 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009057
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009058 /* Update head is the list was null, else add to the end */
9059 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009060 {
niisato8ee24222018-06-25 19:05:48 +09009061 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009062 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009063 else
9064 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009065 mbedtls_ssl_key_cert *cur = *head;
9066 while( cur->next != NULL )
9067 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009068 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009069 }
9070
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009071 return( 0 );
9072}
9073
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009074int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009075 mbedtls_x509_crt *own_cert,
9076 mbedtls_pk_context *pk_key )
9077{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009078 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009079}
9080
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009081void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009082 mbedtls_x509_crt *ca_chain,
9083 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009084{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009085 conf->ca_chain = ca_chain;
9086 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009089
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009090#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9091int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9092 mbedtls_x509_crt *own_cert,
9093 mbedtls_pk_context *pk_key )
9094{
9095 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9096 own_cert, pk_key ) );
9097}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009098
9099void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9100 mbedtls_x509_crt *ca_chain,
9101 mbedtls_x509_crl *ca_crl )
9102{
9103 ssl->handshake->sni_ca_chain = ca_chain;
9104 ssl->handshake->sni_ca_crl = ca_crl;
9105}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009106
9107void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9108 int authmode )
9109{
9110 ssl->handshake->sni_authmode = authmode;
9111}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009112#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9113
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009114#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009115/*
9116 * Set EC J-PAKE password for current handshake
9117 */
9118int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9119 const unsigned char *pw,
9120 size_t pw_len )
9121{
9122 mbedtls_ecjpake_role role;
9123
Janos Follath8eb64132016-06-03 15:40:57 +01009124 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9126
Hanno Becker2d9623f2019-06-13 12:07:05 +01009127 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009128 role = MBEDTLS_ECJPAKE_SERVER;
9129 else
9130 role = MBEDTLS_ECJPAKE_CLIENT;
9131
9132 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9133 role,
9134 MBEDTLS_MD_SHA256,
9135 MBEDTLS_ECP_DP_SECP256R1,
9136 pw, pw_len ) );
9137}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009138#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009141int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009142 const unsigned char *psk, size_t psk_len,
9143 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009144{
Paul Bakker6db455e2013-09-18 17:29:31 +02009145 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009150
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009151 /* Identity len will be encoded on two bytes */
9152 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009153 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009154 {
9155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9156 }
9157
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009158 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009159 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009160 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009161
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009162 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009163 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009164 conf->psk_len = 0;
9165 }
9166 if( conf->psk_identity != NULL )
9167 {
9168 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009169 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009170 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009171 }
9172
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009173 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9174 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009175 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009176 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009177 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009178 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009179 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009180 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009181 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009182
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009183 conf->psk_len = psk_len;
9184 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009185
Teppo Järvelin91d79382019-10-02 09:09:31 +03009186 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9187 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009188
9189 return( 0 );
9190}
9191
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009192int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9193 const unsigned char *psk, size_t psk_len )
9194{
9195 if( psk == NULL || ssl->handshake == NULL )
9196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9197
9198 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9200
9201 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009202 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009203 mbedtls_platform_zeroize( ssl->handshake->psk,
9204 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009205 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009206 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009207 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009208
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009209 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009210 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009211
9212 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009213 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009214
9215 return( 0 );
9216}
9217
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009218void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009220 size_t),
9221 void *p_psk )
9222{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009223 conf->f_psk = f_psk;
9224 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009227
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009228#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009229
9230#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009231int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009232{
9233 int ret;
9234
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009235 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9236 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9237 {
9238 mbedtls_mpi_free( &conf->dhm_P );
9239 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009240 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009242
9243 return( 0 );
9244}
Hanno Becker470a8c42017-10-04 15:28:46 +01009245#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009246
Hanno Beckera90658f2017-10-04 15:29:08 +01009247int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9248 const unsigned char *dhm_P, size_t P_len,
9249 const unsigned char *dhm_G, size_t G_len )
9250{
9251 int ret;
9252
9253 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9254 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9255 {
9256 mbedtls_mpi_free( &conf->dhm_P );
9257 mbedtls_mpi_free( &conf->dhm_G );
9258 return( ret );
9259 }
9260
9261 return( 0 );
9262}
Paul Bakker5121ce52009-01-03 21:22:43 +00009263
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009264int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009265{
9266 int ret;
9267
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009268 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9269 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9270 {
9271 mbedtls_mpi_free( &conf->dhm_P );
9272 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009273 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009274 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009275
9276 return( 0 );
9277}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009278#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009279
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009280#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9281/*
9282 * Set the minimum length for Diffie-Hellman parameters
9283 */
9284void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9285 unsigned int bitlen )
9286{
9287 conf->dhm_min_bitlen = bitlen;
9288}
9289#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9290
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009291#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009292/*
9293 * Set allowed/preferred hashes for handshake signatures
9294 */
9295void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9296 const int *hashes )
9297{
Hanno Becker56595f42019-06-19 16:31:38 +01009298#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009299 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009300#else
9301 ((void) conf);
9302 ((void) hashes);
9303#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009304}
Hanno Becker947194e2017-04-07 13:25:49 +01009305#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009306
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009307#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009308#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009309/*
9310 * Set the allowed elliptic curves
9311 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009312void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009313 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009314{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009315 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009316}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009317#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009318#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009319
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009320#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009322{
Hanno Becker947194e2017-04-07 13:25:49 +01009323 /* Initialize to suppress unnecessary compiler warning */
9324 size_t hostname_len = 0;
9325
9326 /* Check if new hostname is valid before
9327 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009328 if( hostname != NULL )
9329 {
9330 hostname_len = strlen( hostname );
9331
9332 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9334 }
9335
9336 /* Now it's clear that we will overwrite the old hostname,
9337 * so we can free it safely */
9338
9339 if( ssl->hostname != NULL )
9340 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009341 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009342 mbedtls_free( ssl->hostname );
9343 }
9344
9345 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009346
Paul Bakker5121ce52009-01-03 21:22:43 +00009347 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009348 {
9349 ssl->hostname = NULL;
9350 }
9351 else
9352 {
9353 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009354 if( ssl->hostname == NULL )
9355 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009356
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009357 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9358 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009359
Hanno Becker947194e2017-04-07 13:25:49 +01009360 ssl->hostname[hostname_len] = '\0';
9361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009362
9363 return( 0 );
9364}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009365#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009366
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009367#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009368void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009370 const unsigned char *, size_t),
9371 void *p_sni )
9372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009373 conf->f_sni = f_sni;
9374 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009379int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009380{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009381 size_t cur_len, tot_len;
9382 const char **p;
9383
9384 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009385 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9386 * MUST NOT be truncated."
9387 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009388 */
9389 tot_len = 0;
9390 for( p = protos; *p != NULL; p++ )
9391 {
9392 cur_len = strlen( *p );
9393 tot_len += cur_len;
9394
9395 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009397 }
9398
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009399 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009400
9401 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009402}
9403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009405{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009406 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009409
Hanno Becker33b9b252019-07-05 11:23:25 +01009410#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9411 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9412void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9413 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009415 conf->max_major_ver = major;
9416 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009417}
Hanno Becker33b9b252019-07-05 11:23:25 +01009418#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9419 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009420
Hanno Becker33b9b252019-07-05 11:23:25 +01009421#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9422 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9423void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9424 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009425{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009426 conf->min_major_ver = major;
9427 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009428}
Hanno Becker33b9b252019-07-05 11:23:25 +01009429#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9430 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009433void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009434{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009435 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009436}
9437#endif
9438
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009439#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009440void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9441 char cert_req_ca_list )
9442{
9443 conf->cert_req_ca_list = cert_req_ca_list;
9444}
9445#endif
9446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009448void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009450 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009451}
9452#endif
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009455#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009456void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009457{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009458 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009459}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009460#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9461#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009462void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009463 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009464{
9465 conf->enforce_extended_master_secret = ems_enf;
9466}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009467#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009468#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009469
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009470#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009471void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009473 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009474}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009475#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009478int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009481 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009484 }
9485
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009486 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009487
9488 return( 0 );
9489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009493void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009495 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009500void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009501{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009502 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009503}
9504#endif
9505
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009506#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009507void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009509 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009510}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009511#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009514void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009516 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009517}
9518
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009519void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009520{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009521 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009522}
9523
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009524void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009525 const unsigned char period[8] )
9526{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009527 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009532#if defined(MBEDTLS_SSL_CLI_C)
9533void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009534{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009535 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009536}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009537#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009538
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009539#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009540void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9541 mbedtls_ssl_ticket_write_t *f_ticket_write,
9542 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9543 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009544{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009545 conf->f_ticket_write = f_ticket_write;
9546 conf->f_ticket_parse = f_ticket_parse;
9547 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009548}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009551
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009552#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9553void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9554 mbedtls_ssl_export_keys_t *f_export_keys,
9555 void *p_export_keys )
9556{
9557 conf->f_export_keys = f_export_keys;
9558 conf->p_export_keys = p_export_keys;
9559}
9560#endif
9561
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009562#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009563void mbedtls_ssl_conf_async_private_cb(
9564 mbedtls_ssl_config *conf,
9565 mbedtls_ssl_async_sign_t *f_async_sign,
9566 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9567 mbedtls_ssl_async_resume_t *f_async_resume,
9568 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009569 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009570{
9571 conf->f_async_sign_start = f_async_sign;
9572 conf->f_async_decrypt_start = f_async_decrypt;
9573 conf->f_async_resume = f_async_resume;
9574 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009575 conf->p_async_config_data = async_config_data;
9576}
9577
Gilles Peskine8f97af72018-04-26 11:46:10 +02009578void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9579{
9580 return( conf->p_async_config_data );
9581}
9582
Gilles Peskine1febfef2018-04-30 11:54:39 +02009583void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009584{
9585 if( ssl->handshake == NULL )
9586 return( NULL );
9587 else
9588 return( ssl->handshake->user_async_ctx );
9589}
9590
Gilles Peskine1febfef2018-04-30 11:54:39 +02009591void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009592 void *ctx )
9593{
9594 if( ssl->handshake != NULL )
9595 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009596}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009597#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009598
Paul Bakker5121ce52009-01-03 21:22:43 +00009599/*
9600 * SSL get accessors
9601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009603{
9604 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9605}
9606
Hanno Becker8b170a02017-10-10 11:51:19 +01009607int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9608{
9609 /*
9610 * Case A: We're currently holding back
9611 * a message for further processing.
9612 */
9613
9614 if( ssl->keep_current_message == 1 )
9615 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009617 return( 1 );
9618 }
9619
9620 /*
9621 * Case B: Further records are pending in the current datagram.
9622 */
9623
9624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009625 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009626 ssl->in_left > ssl->next_record_offset )
9627 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009629 return( 1 );
9630 }
9631#endif /* MBEDTLS_SSL_PROTO_DTLS */
9632
9633 /*
9634 * Case C: A handshake message is being processed.
9635 */
9636
Hanno Becker8b170a02017-10-10 11:51:19 +01009637 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9638 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009640 return( 1 );
9641 }
9642
9643 /*
9644 * Case D: An application data message is being processed
9645 */
9646 if( ssl->in_offt != NULL )
9647 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009649 return( 1 );
9650 }
9651
9652 /*
9653 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009654 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009655 * we implement support for multiple alerts in single records.
9656 */
9657
9658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9659 return( 0 );
9660}
9661
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009662uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009663{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009664 if( ssl->session != NULL )
9665 return( ssl->session->verify_result );
9666
9667 if( ssl->session_negotiate != NULL )
9668 return( ssl->session_negotiate->verify_result );
9669
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009670 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009671}
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009674{
Hanno Beckere02758c2019-06-26 15:31:31 +01009675 int suite;
9676
Paul Bakker926c8e42013-03-06 10:23:34 +01009677 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009678 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009679
Hanno Beckere02758c2019-06-26 15:31:31 +01009680 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9681 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009682}
9683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009687 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009688 {
Hanno Becker2881d802019-05-22 14:44:53 +01009689 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009692 return( "DTLSv1.0" );
9693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009695 return( "DTLSv1.2" );
9696
9697 default:
9698 return( "unknown (DTLS)" );
9699 }
9700 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009701 MBEDTLS_SSL_TRANSPORT_ELSE
9702#endif /* MBEDTLS_SSL_PROTO_DTLS */
9703#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009704 {
Hanno Becker2881d802019-05-22 14:44:53 +01009705 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009706 {
9707 case MBEDTLS_SSL_MINOR_VERSION_0:
9708 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009709
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009710 case MBEDTLS_SSL_MINOR_VERSION_1:
9711 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009712
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009713 case MBEDTLS_SSL_MINOR_VERSION_2:
9714 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009715
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009716 case MBEDTLS_SSL_MINOR_VERSION_3:
9717 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009718
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009719 default:
9720 return( "unknown" );
9721 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009722 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009723#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009724}
9725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009727{
Hanno Becker3136ede2018-08-17 15:28:19 +01009728 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009730
Hanno Becker43395762019-05-03 14:46:38 +01009731 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9732
Hanno Becker78640902018-08-13 16:35:15 +01009733 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009734 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736#if defined(MBEDTLS_ZLIB_SUPPORT)
9737 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9738 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009739#endif
9740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009742 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009743#if defined(MBEDTLS_GCM_C) || \
9744 defined(MBEDTLS_CCM_C) || \
9745 defined(MBEDTLS_CHACHAPOLY_C)
9746#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009748#endif
9749#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009751#endif
9752#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009753 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009754#endif
9755 transform_expansion =
9756 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009757 break;
9758
Hanno Beckera9d5c452019-07-25 16:47:12 +01009759#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9760 MBEDTLS_CHACHAPOLY_C */
9761
9762#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9763 case MBEDTLS_MODE_STREAM:
9764 transform_expansion = transform->maclen;
9765 break;
9766#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9767
9768#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009770 {
9771 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009772
9773 block_size = mbedtls_cipher_get_block_size(
9774 &transform->cipher_ctx_enc );
9775
Hanno Becker3136ede2018-08-17 15:28:19 +01009776 /* Expansion due to the addition of the MAC. */
9777 transform_expansion += transform->maclen;
9778
9779 /* Expansion due to the addition of CBC padding;
9780 * Theoretically up to 256 bytes, but we never use
9781 * more than the block size of the underlying cipher. */
9782 transform_expansion += block_size;
9783
9784 /* For TLS 1.1 or higher, an explicit IV is added
9785 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009786#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009787 if( mbedtls_ssl_ver_geq(
9788 mbedtls_ssl_get_minor_ver( ssl ),
9789 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9790 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009791 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009792 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009793#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009794
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009795 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009796 }
9797#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009798
9799 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009802 }
9803
Hanno Beckera5a2b082019-05-15 14:03:01 +01009804#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009805 if( transform->out_cid_len != 0 )
9806 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009807#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009808
Hanno Becker43395762019-05-03 14:46:38 +01009809 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009810}
9811
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009812#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9813size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9814{
9815 size_t max_len;
9816
9817 /*
9818 * Assume mfl_code is correct since it was checked when set
9819 */
Angus Grattond8213d02016-05-25 20:56:48 +10009820 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009821
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009822 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009823 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009824 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009825 {
Angus Grattond8213d02016-05-25 20:56:48 +10009826 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009827 }
9828
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009829 /* During a handshake, use the value being negotiated */
9830 if( ssl->session_negotiate != NULL &&
9831 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9832 {
9833 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9834 }
9835
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009836 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009837}
9838#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9839
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009840#if defined(MBEDTLS_SSL_PROTO_DTLS)
9841static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9842{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009843 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009844 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009845 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9846 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009847 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009848
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009849 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9850 return( ssl->mtu );
9851
9852 if( ssl->mtu == 0 )
9853 return( ssl->handshake->mtu );
9854
9855 return( ssl->mtu < ssl->handshake->mtu ?
9856 ssl->mtu : ssl->handshake->mtu );
9857}
9858#endif /* MBEDTLS_SSL_PROTO_DTLS */
9859
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009860int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9861{
9862 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9863
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009864#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9865 !defined(MBEDTLS_SSL_PROTO_DTLS)
9866 (void) ssl;
9867#endif
9868
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009869#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9870 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9871
9872 if( max_len > mfl )
9873 max_len = mfl;
9874#endif
9875
9876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009877 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009878 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009879 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009880 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9881 const size_t overhead = (size_t) ret;
9882
9883 if( ret < 0 )
9884 return( ret );
9885
9886 if( mtu <= overhead )
9887 {
9888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9889 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9890 }
9891
9892 if( max_len > mtu - overhead )
9893 max_len = mtu - overhead;
9894 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009895#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009896
Hanno Becker0defedb2018-08-10 12:35:02 +01009897#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9898 !defined(MBEDTLS_SSL_PROTO_DTLS)
9899 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009900#endif
9901
9902 return( (int) max_len );
9903}
9904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_X509_CRT_PARSE_C)
9906const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009907{
9908 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009909 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009910
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009911#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009912 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009913#else
9914 return( NULL );
9915#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009920int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9921 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009922{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009923 if( ssl == NULL ||
9924 dst == NULL ||
9925 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009926 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009929 }
9930
Hanno Becker58fccf22019-02-06 14:30:46 +00009931 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009934
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009935const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9936{
9937 if( ssl == NULL )
9938 return( NULL );
9939
9940 return( ssl->session );
9941}
9942
Paul Bakker5121ce52009-01-03 21:22:43 +00009943/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009944 * Define ticket header determining Mbed TLS version
9945 * and structure of the ticket.
9946 */
9947
Hanno Becker41527622019-05-16 12:50:45 +01009948/*
Hanno Becker26829e92019-05-28 14:30:45 +01009949 * Define bitflag determining compile-time settings influencing
9950 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009951 */
9952
Hanno Becker26829e92019-05-28 14:30:45 +01009953#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009954#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009955#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009956#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009957#endif /* MBEDTLS_HAVE_TIME */
9958
9959#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009960#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009961#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009962#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009963#endif /* MBEDTLS_X509_CRT_PARSE_C */
9964
9965#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009966#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009967#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009968#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009969#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9970
9971#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009972#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009973#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009974#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9976
9977#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009978#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009979#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009980#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009981#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9982
9983#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009984#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009985#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009986#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009987#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9988
Hanno Becker41527622019-05-16 12:50:45 +01009989#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9990#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9991#else
9992#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9993#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9994
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9996#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9997#else
9998#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9999#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10000
Hanno Becker88440552019-07-03 14:16:13 +010010001#if defined(MBEDTLS_ZLIB_SUPPORT)
10002#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10003#else
10004#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10005#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10006
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010007#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10008#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10009#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10010#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10011#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10012#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10013#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010014#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010015#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010016
Hanno Becker26829e92019-05-28 14:30:45 +010010017#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010018 ( (uint16_t) ( \
10019 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10020 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10021 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10022 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10023 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10024 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010025 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010026 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010027 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010028
Hanno Becker557fe9f2019-05-16 12:41:07 +010010029static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010030 MBEDTLS_VERSION_MAJOR,
10031 MBEDTLS_VERSION_MINOR,
10032 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010033 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10034 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010035};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010036
10037/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010038 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010039 * (in the presentation language of TLS, RFC 8446 section 3)
10040 *
Hanno Becker26829e92019-05-28 14:30:45 +010010041 * opaque mbedtls_version[3]; // major, minor, patch
10042 * opaque session_format[2]; // version-specific 16-bit field determining
10043 * // the format of the remaining
10044 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010045 *
10046 * Note: When updating the format, remember to keep
10047 * these version+format bytes.
10048 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010049 * // In this version, `session_format` determines
10050 * // the setting of those compile-time
10051 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010052 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010053 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010054 * uint8 ciphersuite[2]; // defined by the standard
10055 * uint8 compression; // 0 or 1
10056 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010057 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010058 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010059 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010060 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10061 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10062 * case disabled: uint8_t peer_cert_digest_type;
10063 * opaque peer_cert_digest<0..2^8-1>;
10064 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010065 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010066 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010067 * uint8 mfl_code; // up to 255 according to standard
10068 * uint8 trunc_hmac; // 0 or 1
10069 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010070 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010071 * The order is the same as in the definition of the structure, except
10072 * verify_result is put before peer_cert so that all mandatory fields come
10073 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010074 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010075static int ssl_session_save( const mbedtls_ssl_session *session,
10076 unsigned char omit_header,
10077 unsigned char *buf,
10078 size_t buf_len,
10079 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010080{
10081 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010082 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010083#if defined(MBEDTLS_HAVE_TIME)
10084 uint64_t start;
10085#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010086#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010087#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010088 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010089#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010090#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010091
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010092 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010093 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010094 /*
10095 * Add version identifier
10096 */
10097
10098 used += sizeof( ssl_serialized_session_header );
10099
10100 if( used <= buf_len )
10101 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010102 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010103 sizeof( ssl_serialized_session_header ) );
10104 p += sizeof( ssl_serialized_session_header );
10105 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010106 }
10107
10108 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010109 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010110 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010111#if defined(MBEDTLS_HAVE_TIME)
10112 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010113
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010114 if( used <= buf_len )
10115 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010116 start = (uint64_t) session->start;
10117
10118 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10119 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10120 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10121 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10122 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10123 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10124 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10125 *p++ = (unsigned char)( ( start ) & 0xFF );
10126 }
10127#endif /* MBEDTLS_HAVE_TIME */
10128
10129 /*
10130 * Basic mandatory fields
10131 */
Hanno Becker88440552019-07-03 14:16:13 +010010132 {
10133 size_t const ciphersuite_len = 2;
10134#if defined(MBEDTLS_ZLIB_SUPPORT)
10135 size_t const compression_len = 1;
10136#else
10137 size_t const compression_len = 0;
10138#endif
10139 size_t const id_len_len = 1;
10140 size_t const id_len = 32;
10141 size_t const master_len = 48;
10142 size_t const verif_result_len = 4;
10143
10144 size_t const basic_len =
10145 ciphersuite_len +
10146 compression_len +
10147 id_len_len +
10148 id_len +
10149 master_len +
10150 verif_result_len;
10151
10152 used += basic_len;
10153 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010154
10155 if( used <= buf_len )
10156 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010157 const int ciphersuite =
10158 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010159 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010160
Hanno Becker88440552019-07-03 14:16:13 +010010161#if defined(MBEDTLS_ZLIB_SUPPORT)
10162 *p++ = (unsigned char)(
10163 mbedtls_ssl_session_get_compression( session ) );
10164#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010165
10166 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010167 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10168 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010169 p += 32;
10170
Teppo Järvelin91d79382019-10-02 09:09:31 +030010171 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010172 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010173 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010174 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010175
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010176 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010177 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010178 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010179#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010180#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010181 if( session->peer_cert == NULL )
10182 cert_len = 0;
10183 else
10184 cert_len = session->peer_cert->raw.len;
10185
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010186 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010187
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010188 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010189 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010190 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010191
10192 if( session->peer_cert != NULL )
10193 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010194 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010195 p += cert_len;
10196 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010197 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010198
Hanno Becker5882dd02019-06-06 16:25:57 +010010199#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010200 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010201 if( session->peer_cert_digest != NULL )
10202 {
10203 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10204 if( used <= buf_len )
10205 {
10206 *p++ = (unsigned char) session->peer_cert_digest_type;
10207 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010208 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010209 session->peer_cert_digest_len );
10210 p += session->peer_cert_digest_len;
10211 }
10212 }
10213 else
10214 {
10215 used += 2;
10216 if( used <= buf_len )
10217 {
10218 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10219 *p++ = 0;
10220 }
10221 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010222#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010223#endif /* MBEDTLS_X509_CRT_PARSE_C */
10224
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010225 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010226 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010227 */
10228#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010229 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010230
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010231 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010232 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010233 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010234
10235 if( session->ticket != NULL )
10236 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010237 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010238 p += session->ticket_len;
10239 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010240
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010241 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010242 }
10243#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10244
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010245 /*
10246 * Misc extension-related info
10247 */
10248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10249 used += 1;
10250
10251 if( used <= buf_len )
10252 *p++ = session->mfl_code;
10253#endif
10254
10255#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10256 used += 1;
10257
10258 if( used <= buf_len )
10259 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10260#endif
10261
10262#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10263 used += 1;
10264
10265 if( used <= buf_len )
10266 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10267#endif
10268
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010269 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010270 *olen = used;
10271
10272 if( used > buf_len )
10273 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010274
10275 return( 0 );
10276}
10277
10278/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010279 * Public wrapper for ssl_session_save()
10280 */
10281int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10282 unsigned char *buf,
10283 size_t buf_len,
10284 size_t *olen )
10285{
10286 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10287}
10288
10289/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010290 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010291 *
10292 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010293 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010294 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010295static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010296 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010297 const unsigned char *buf,
10298 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010299{
10300 const unsigned char *p = buf;
10301 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010302 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010303#if defined(MBEDTLS_HAVE_TIME)
10304 uint64_t start;
10305#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010306#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010307#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010308 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010309#endif
10310#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010311
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010312 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010313 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010314 /*
10315 * Check version identifier
10316 */
10317
10318 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10320
Teppo Järvelin0efac532019-10-04 13:21:08 +030010321 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010322 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010323 sizeof( ssl_serialized_session_header ) ) != 0 )
10324 {
10325 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10326 }
10327 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010328 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010329
10330 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010331 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010332 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010333#if defined(MBEDTLS_HAVE_TIME)
10334 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10336
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010337 start = ( (uint64_t) p[0] << 56 ) |
10338 ( (uint64_t) p[1] << 48 ) |
10339 ( (uint64_t) p[2] << 40 ) |
10340 ( (uint64_t) p[3] << 32 ) |
10341 ( (uint64_t) p[4] << 24 ) |
10342 ( (uint64_t) p[5] << 16 ) |
10343 ( (uint64_t) p[6] << 8 ) |
10344 ( (uint64_t) p[7] );
10345 p += 8;
10346
10347 session->start = (time_t) start;
10348#endif /* MBEDTLS_HAVE_TIME */
10349
10350 /*
10351 * Basic mandatory fields
10352 */
Hanno Becker88440552019-07-03 14:16:13 +010010353 {
10354 size_t const ciphersuite_len = 2;
10355#if defined(MBEDTLS_ZLIB_SUPPORT)
10356 size_t const compression_len = 1;
10357#else
10358 size_t const compression_len = 0;
10359#endif
10360 size_t const id_len_len = 1;
10361 size_t const id_len = 32;
10362 size_t const master_len = 48;
10363 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010364
Hanno Becker88440552019-07-03 14:16:13 +010010365 size_t const basic_len =
10366 ciphersuite_len +
10367 compression_len +
10368 id_len_len +
10369 id_len +
10370 master_len +
10371 verif_result_len;
10372
10373 if( basic_len > (size_t)( end - p ) )
10374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10375 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010376
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010377 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010378 p += 2;
10379
Hanno Becker73f4cb12019-06-27 13:51:07 +010010380#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010381 session->ciphersuite = ciphersuite;
10382#else
10383 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010384 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010385 {
10386 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10387 }
10388#endif
10389
Hanno Becker88440552019-07-03 14:16:13 +010010390#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010391 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010392#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010393
10394 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010395 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10396 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010397 p += 32;
10398
Teppo Järvelin91d79382019-10-02 09:09:31 +030010399 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010400 p += 48;
10401
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010402 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010403 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010404
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010405 /* Immediately clear invalid pointer values that have been read, in case
10406 * we exit early before we replaced them with valid ones. */
10407#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010408#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010409 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010410#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010411 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010412#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010413#endif
10414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10415 session->ticket = NULL;
10416#endif
10417
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010418 /*
10419 * Peer certificate
10420 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010421#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010422#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010423 if( 3 > (size_t)( end - p ) )
10424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10425
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010426 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10427
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010428 p += 3;
10429
10430 if( cert_len == 0 )
10431 {
10432 session->peer_cert = NULL;
10433 }
10434 else
10435 {
10436 int ret;
10437
10438 if( cert_len > (size_t)( end - p ) )
10439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10440
10441 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10442
10443 if( session->peer_cert == NULL )
10444 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10445
10446 mbedtls_x509_crt_init( session->peer_cert );
10447
10448 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10449 p, cert_len ) ) != 0 )
10450 {
10451 mbedtls_x509_crt_free( session->peer_cert );
10452 mbedtls_free( session->peer_cert );
10453 session->peer_cert = NULL;
10454 return( ret );
10455 }
10456
10457 p += cert_len;
10458 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010459#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010460 /* Deserialize CRT digest from the end of the ticket. */
10461 if( 2 > (size_t)( end - p ) )
10462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10463
10464 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10465 session->peer_cert_digest_len = (size_t) *p++;
10466
10467 if( session->peer_cert_digest_len != 0 )
10468 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010469 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010470 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010471 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10473 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10475
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010476 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10478
10479 session->peer_cert_digest =
10480 mbedtls_calloc( 1, session->peer_cert_digest_len );
10481 if( session->peer_cert_digest == NULL )
10482 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10483
Teppo Järvelin91d79382019-10-02 09:09:31 +030010484 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010485 session->peer_cert_digest_len );
10486 p += session->peer_cert_digest_len;
10487 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010488#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010489#endif /* MBEDTLS_X509_CRT_PARSE_C */
10490
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010491 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010492 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010493 */
10494#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10495 if( 3 > (size_t)( end - p ) )
10496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10497
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010498 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010499 p += 3;
10500
10501 if( session->ticket_len != 0 )
10502 {
10503 if( session->ticket_len > (size_t)( end - p ) )
10504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10505
10506 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10507 if( session->ticket == NULL )
10508 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10509
Teppo Järvelin91d79382019-10-02 09:09:31 +030010510 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010511 p += session->ticket_len;
10512 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010513
10514 if( 4 > (size_t)( end - p ) )
10515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10516
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010517 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010518 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010519#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10520
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010521 /*
10522 * Misc extension-related info
10523 */
10524#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10525 if( 1 > (size_t)( end - p ) )
10526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10527
10528 session->mfl_code = *p++;
10529#endif
10530
10531#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10532 if( 1 > (size_t)( end - p ) )
10533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10534
10535 session->trunc_hmac = *p++;
10536#endif
10537
10538#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10539 if( 1 > (size_t)( end - p ) )
10540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10541
10542 session->encrypt_then_mac = *p++;
10543#endif
10544
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010545 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010546 if( p != end )
10547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10548
10549 return( 0 );
10550}
10551
10552/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010553 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010554 */
10555int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10556 const unsigned char *buf,
10557 size_t len )
10558{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010559 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010560
10561 if( ret != 0 )
10562 mbedtls_ssl_session_free( session );
10563
10564 return( ret );
10565}
10566
10567/*
Paul Bakker1961b702013-01-25 14:49:24 +010010568 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010570int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010571{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010573
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010574 if( ssl == NULL || ssl->conf == NULL )
10575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010578 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010582 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010584#endif
10585
Hanno Beckerb82350b2019-07-26 07:24:05 +010010586 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010587 return( ret );
10588}
10589
10590/*
10591 * Perform the SSL handshake
10592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010594{
10595 int ret = 0;
10596
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010597 if( ssl == NULL || ssl->conf == NULL )
10598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010605
10606 if( ret != 0 )
10607 break;
10608 }
10609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010611
10612 return( ret );
10613}
10614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615#if defined(MBEDTLS_SSL_RENEGOTIATION)
10616#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010617/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010618 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010621{
10622 int ret;
10623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010625
10626 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10628 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010629
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010630 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010631 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010633 return( ret );
10634 }
10635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010637
10638 return( 0 );
10639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010641
10642/*
10643 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 * - any side: calling mbedtls_ssl_renegotiate(),
10645 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10646 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010647 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010648 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010652{
10653 int ret;
10654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010656
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010657 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10658 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010659
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010660 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10661 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010663 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010665 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010666 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10667 MBEDTLS_SSL_IS_SERVER )
10668 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010669 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010670 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010671 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010672 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010673 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010674 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010675 }
10676#endif
10677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10679 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010684 return( ret );
10685 }
10686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010688
10689 return( 0 );
10690}
10691
10692/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010693 * Renegotiate current connection on client,
10694 * or request renegotiation on server
10695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010699
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010700 if( ssl == NULL || ssl->conf == NULL )
10701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010704 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010705 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010711
10712 /* Did we already try/start sending HelloRequest? */
10713 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010715
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010716 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010721 /*
10722 * On client, either start the renegotiation process or,
10723 * if already in progress, continue the handshake
10724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
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
10730 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010733 return( ret );
10734 }
10735 }
10736 else
10737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010741 return( ret );
10742 }
10743 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010745
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010746 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010747}
10748
10749/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010750 * Check record counters and renegotiate if they're above the limit.
10751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010753{
Andres AG2196c7f2016-12-15 17:01:16 +000010754 size_t ep_len = ssl_ep_len( ssl );
10755 int in_ctr_cmp;
10756 int out_ctr_cmp;
10757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10759 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010760 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010761 {
10762 return( 0 );
10763 }
10764
Teppo Järvelin0efac532019-10-04 13:21:08 +030010765 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010766 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010767 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010768 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010769 ssl->conf->renego_period + ep_len, 8 - ep_len );
10770
10771 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010772 {
10773 return( 0 );
10774 }
10775
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010780
10781/*
10782 * Receive application data decrypted from the SSL layer
10783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010785{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010786 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010787 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010788
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010789 if( ssl == NULL || ssl->conf == NULL )
10790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010795 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010798 return( ret );
10799
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010800 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010802 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010803 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010804 return( ret );
10805 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010806 }
10807#endif
10808
Hanno Becker4a810fb2017-05-24 16:27:30 +010010809 /*
10810 * Check if renegotiation is necessary and/or handshake is
10811 * in process. If yes, perform/continue, and fall through
10812 * if an unexpected packet is received while the client
10813 * is waiting for the ServerHello.
10814 *
10815 * (There is no equivalent to the last condition on
10816 * the server-side as it is not treated as within
10817 * a handshake while waiting for the ClientHello
10818 * after a renegotiation request.)
10819 */
10820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010822 ret = ssl_check_ctr_renegotiate( ssl );
10823 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10824 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010827 return( ret );
10828 }
10829#endif
10830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010834 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10835 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010838 return( ret );
10839 }
10840 }
10841
Hanno Beckere41158b2017-10-23 13:30:32 +010010842 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010843 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010844 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010845 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010846 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10847 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010848 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010849 ssl_set_timer( ssl,
10850 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010851 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010852
Hanno Becker327c93b2018-08-15 13:56:18 +010010853 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010854 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010855 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10856 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010857
Hanno Becker4a810fb2017-05-24 16:27:30 +010010858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10859 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010860 }
10861
10862 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010864 {
10865 /*
10866 * OpenSSL sends empty messages to randomize the IV
10867 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010868 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010871 return( 0 );
10872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010874 return( ret );
10875 }
10876 }
10877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010881
Hanno Becker4a810fb2017-05-24 16:27:30 +010010882 /*
10883 * - For client-side, expect SERVER_HELLO_REQUEST.
10884 * - For server-side, expect CLIENT_HELLO.
10885 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10886 */
10887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010889 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10890 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010892 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010895
10896 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010898 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010899 {
10900 continue;
10901 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010902 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010903#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010904#if defined(MBEDTLS_SSL_PROTO_TLS)
10905 {
10906 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10907 }
10908#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010909 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010910#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010911
Hanno Becker4a810fb2017-05-24 16:27:30 +010010912#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010913 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10914 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010918
10919 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010922 {
10923 continue;
10924 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010925 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010926#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010927#if defined(MBEDTLS_SSL_PROTO_TLS)
10928 {
10929 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10930 }
10931#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010932 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010933#endif /* MBEDTLS_SSL_SRV_C */
10934
Hanno Becker21df7f92017-10-17 11:03:26 +010010935#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010936 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010937 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10938 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010939 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010940 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10941 {
10942 /*
10943 * Accept renegotiation request
10944 */
Paul Bakker48916f92012-09-16 19:57:18 +000010945
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010946 /* DTLS clients need to know renego is server-initiated */
10947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010948 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010949 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10950 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010951 {
10952 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10953 }
10954#endif
10955 ret = ssl_start_renegotiation( ssl );
10956 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10957 ret != 0 )
10958 {
10959 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10960 return( ret );
10961 }
10962 }
10963 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010964#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010965 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010966 /*
10967 * Refuse renegotiation
10968 */
10969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010973 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010974 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010975 /* SSLv3 does not have a "no_renegotiation" warning, so
10976 we send a fatal alert and abort the connection. */
10977 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10978 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10979 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010980 }
10981 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10983#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10984 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010985 if( mbedtls_ssl_ver_geq(
10986 mbedtls_ssl_get_minor_ver( ssl ),
10987 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010988 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010989 ret = mbedtls_ssl_send_alert_message( ssl,
10990 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10991 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10992 if( ret != 0 )
10993 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010994 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10997 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011001 }
Paul Bakker48916f92012-09-16 19:57:18 +000011002 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011003
Hanno Becker90333da2017-10-10 11:27:13 +010011004 /* At this point, we don't know whether the renegotiation has been
11005 * completed or not. The cases to consider are the following:
11006 * 1) The renegotiation is complete. In this case, no new record
11007 * has been read yet.
11008 * 2) The renegotiation is incomplete because the client received
11009 * an application data record while awaiting the ServerHello.
11010 * 3) The renegotiation is incomplete because the client received
11011 * a non-handshake, non-application data message while awaiting
11012 * the ServerHello.
11013 * In each of these case, looping will be the proper action:
11014 * - For 1), the next iteration will read a new record and check
11015 * if it's application data.
11016 * - For 2), the loop condition isn't satisfied as application data
11017 * is present, hence continue is the same as break
11018 * - For 3), the loop condition is satisfied and read_record
11019 * will re-deliver the message that was held back by the client
11020 * when expecting the ServerHello.
11021 */
11022 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011023 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011024#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011026 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011027 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011028 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011029 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011032 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011034 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011035 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011036 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11040 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011043 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011044 }
11045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11049 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011050 }
11051
11052 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011053
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011054 /* We're going to return something now, cancel timer,
11055 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011057 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011058
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011060 /* If we requested renego but received AppData, resend HelloRequest.
11061 * Do it now, after setting in_offt, to avoid taking this branch
11062 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011064 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11065 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011067 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011068 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011071 return( ret );
11072 }
11073 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011075#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011076 }
11077
11078 n = ( len < ssl->in_msglen )
11079 ? len : ssl->in_msglen;
11080
Teppo Järvelin91d79382019-10-02 09:09:31 +030011081 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011082 ssl->in_msglen -= n;
11083
11084 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011085 {
11086 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011087 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011088 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011089 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011090 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011091 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011092 /* more data available */
11093 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011094 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011097
Paul Bakker23986e52011-04-24 08:57:21 +000011098 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011099}
11100
11101/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011102 * Send application data to be encrypted by the SSL layer, taking care of max
11103 * fragment length and buffer size.
11104 *
11105 * According to RFC 5246 Section 6.2.1:
11106 *
11107 * Zero-length fragments of Application data MAY be sent as they are
11108 * potentially useful as a traffic analysis countermeasure.
11109 *
11110 * Therefore, it is possible that the input message length is 0 and the
11111 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011112 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011113static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011114 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011115{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011116 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11117 const size_t max_len = (size_t) ret;
11118
11119 if( ret < 0 )
11120 {
11121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11122 return( ret );
11123 }
11124
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011125 if( len > max_len )
11126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011127#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011128 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011131 "maximum fragment length: %d > %d",
11132 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011134 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011135 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011136#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011137#if defined(MBEDTLS_SSL_PROTO_TLS)
11138 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011139 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011140 }
11141#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011142 }
Paul Bakker887bd502011-06-08 13:10:54 +000011143
Paul Bakker5121ce52009-01-03 21:22:43 +000011144 if( ssl->out_left != 0 )
11145 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011146 /*
11147 * The user has previously tried to send the data and
11148 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11149 * written. In this case, we expect the high-level write function
11150 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011152 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011155 return( ret );
11156 }
11157 }
Paul Bakker887bd502011-06-08 13:10:54 +000011158 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011159 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011160 /*
11161 * The user is trying to send a message the first time, so we need to
11162 * copy the data into the internal buffers and setup the data structure
11163 * to keep track of partial writes
11164 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011165 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011166 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011167 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011168
Hanno Becker67bc7c32018-08-06 11:33:50 +010011169 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011172 return( ret );
11173 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011174 }
11175
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011176 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011177}
11178
11179/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011180 * Write application data, doing 1/n-1 splitting if necessary.
11181 *
11182 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011183 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011184 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011187static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011188 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011189{
11190 int ret;
11191
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011192 if( ssl->conf->cbc_record_splitting ==
11193 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011194 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011195 mbedtls_ssl_ver_gt(
11196 mbedtls_ssl_get_minor_ver( ssl ),
11197 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011198 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11199 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011200 {
11201 return( ssl_write_real( ssl, buf, len ) );
11202 }
11203
11204 if( ssl->split_done == 0 )
11205 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011206 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011207 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011208 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011209 }
11210
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011211 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11212 return( ret );
11213 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011214
11215 return( ret + 1 );
11216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011218
11219/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011220 * Write application data (public-facing wrapper)
11221 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011222int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011223{
11224 int ret;
11225
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011227
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011228 if( ssl == NULL || ssl->conf == NULL )
11229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11230
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011231#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011232 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11233 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011234 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011235 return( ret );
11236 }
11237#endif
11238
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011239 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011240 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011241 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011242 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011244 return( ret );
11245 }
11246 }
11247
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011248#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011249 ret = ssl_write_split( ssl, buf, len );
11250#else
11251 ret = ssl_write_real( ssl, buf, len );
11252#endif
11253
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011255
11256 return( ret );
11257}
11258
11259/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011260 * Notify the peer that the connection is being closed
11261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011262int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011263{
11264 int ret;
11265
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011266 if( ssl == NULL || ssl->conf == NULL )
11267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011270
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011271 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011276 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11277 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11278 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011281 return( ret );
11282 }
11283 }
11284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011286
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011287 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011288}
11289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011290void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011291{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011292 if( transform == NULL )
11293 return;
11294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011296 deflateEnd( &transform->ctx_deflate );
11297 inflateEnd( &transform->ctx_inflate );
11298#endif
11299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011300 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11301 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011302
Hanno Becker92231322018-01-03 15:32:51 +000011303#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011304 mbedtls_md_free( &transform->md_ctx_enc );
11305 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011306#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011307
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011308 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011309}
11310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011311#if defined(MBEDTLS_X509_CRT_PARSE_C)
11312static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011314 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011315
11316 while( cur != NULL )
11317 {
11318 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011319 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011320 cur = next;
11321 }
11322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011324
Hanno Becker0271f962018-08-16 13:23:47 +010011325#if defined(MBEDTLS_SSL_PROTO_DTLS)
11326
11327static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11328{
11329 unsigned offset;
11330 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11331
11332 if( hs == NULL )
11333 return;
11334
Hanno Becker283f5ef2018-08-24 09:34:47 +010011335 ssl_free_buffered_record( ssl );
11336
Hanno Becker0271f962018-08-16 13:23:47 +010011337 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011338 ssl_buffering_free_slot( ssl, offset );
11339}
11340
11341static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11342 uint8_t slot )
11343{
11344 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11345 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011346
11347 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11348 return;
11349
Hanno Beckere605b192018-08-21 15:59:07 +010011350 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011351 {
Hanno Beckere605b192018-08-21 15:59:07 +010011352 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011353 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011354 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011355 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011356 }
11357}
11358
11359#endif /* MBEDTLS_SSL_PROTO_DTLS */
11360
Gilles Peskine9b562d52018-04-25 20:32:43 +020011361void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011362{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011363 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11364
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011365 if( handshake == NULL )
11366 return;
11367
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011368#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11369 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11370 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011371 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011372 handshake->async_in_progress = 0;
11373 }
11374#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11375
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011376#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11377 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11378 mbedtls_md5_free( &handshake->fin_md5 );
11379 mbedtls_sha1_free( &handshake->fin_sha1 );
11380#endif
11381#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11382#if defined(MBEDTLS_SHA256_C)
11383 mbedtls_sha256_free( &handshake->fin_sha256 );
11384#endif
11385#if defined(MBEDTLS_SHA512_C)
11386 mbedtls_sha512_free( &handshake->fin_sha512 );
11387#endif
11388#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011390#if defined(MBEDTLS_DHM_C)
11391 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011393#if defined(MBEDTLS_ECDH_C)
11394 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011395#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011396#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011397 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011398#if defined(MBEDTLS_SSL_CLI_C)
11399 mbedtls_free( handshake->ecjpake_cache );
11400 handshake->ecjpake_cache = NULL;
11401 handshake->ecjpake_cache_len = 0;
11402#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011403#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011404
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011405#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11406 if( handshake->psk != NULL )
11407 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011408 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011409 mbedtls_free( handshake->psk );
11410 }
11411#endif
11412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011413#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11414 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011415 /*
11416 * Free only the linked list wrapper, not the keys themselves
11417 * since the belong to the SNI callback
11418 */
11419 if( handshake->sni_key_cert != NULL )
11420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011421 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011422
11423 while( cur != NULL )
11424 {
11425 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011426 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011427 cur = next;
11428 }
11429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011430#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011431
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011432#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011433 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011434 if( handshake->ecrs_peer_cert != NULL )
11435 {
11436 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11437 mbedtls_free( handshake->ecrs_peer_cert );
11438 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011439#endif
11440
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011441#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11442 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11443 mbedtls_pk_free( &handshake->peer_pubkey );
11444#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011446#if defined(MBEDTLS_SSL_PROTO_DTLS)
11447 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011448 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011449 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011450#endif
11451
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011452 mbedtls_platform_zeroize( handshake,
11453 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011454}
11455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011456void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011457{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011458 if( session == NULL )
11459 return;
11460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011461#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011462 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011463#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011464
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011465#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011466 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011467#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011468
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011469 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011470}
11471
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011472#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011473
11474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11475#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11476#else
11477#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11478#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11479
11480#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11481#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11482#else
11483#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11484#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11485
11486#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11487#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11488#else
11489#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11490#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11491
11492#if defined(MBEDTLS_SSL_ALPN)
11493#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11494#else
11495#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11496#endif /* MBEDTLS_SSL_ALPN */
11497
11498#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11499#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11500#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11501#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11502
11503#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11504 ( (uint32_t) ( \
11505 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11506 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11507 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11508 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11509 0u ) )
11510
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011511static unsigned char ssl_serialized_context_header[] = {
11512 MBEDTLS_VERSION_MAJOR,
11513 MBEDTLS_VERSION_MINOR,
11514 MBEDTLS_VERSION_PATCH,
11515 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11516 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011517 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11518 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11519 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011520};
11521
Paul Bakker5121ce52009-01-03 21:22:43 +000011522/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011523 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011524 *
11525 * The format of the serialized data is:
11526 * (in the presentation language of TLS, RFC 8446 section 3)
11527 *
11528 * // header
11529 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011530 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011531 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011532 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011533 * Note: When updating the format, remember to keep these
11534 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011535 *
11536 * // session sub-structure
11537 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11538 * // transform sub-structure
11539 * uint8 random[64]; // ServerHello.random+ClientHello.random
11540 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11541 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11542 * // fields from ssl_context
11543 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11544 * uint64 in_window_top; // DTLS: last validated record seq_num
11545 * uint64 in_window; // DTLS: bitmask for replay protection
11546 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11547 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11548 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11549 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11550 *
11551 * Note that many fields of the ssl_context or sub-structures are not
11552 * serialized, as they fall in one of the following categories:
11553 *
11554 * 1. forced value (eg in_left must be 0)
11555 * 2. pointer to dynamically-allocated memory (eg session, transform)
11556 * 3. value can be re-derived from other data (eg session keys from MS)
11557 * 4. value was temporary (eg content of input buffer)
11558 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011559 */
11560int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11561 unsigned char *buf,
11562 size_t buf_len,
11563 size_t *olen )
11564{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011565 unsigned char *p = buf;
11566 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011567 size_t session_len;
11568 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011569
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011570 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011571 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11572 * this function's documentation.
11573 *
11574 * These are due to assumptions/limitations in the implementation. Some of
11575 * them are likely to stay (no handshake in progress) some might go away
11576 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011577 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011578 /* The initial handshake must be over */
11579 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011581 if( ssl->handshake != NULL )
11582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11583 /* Double-check that sub-structures are indeed ready */
11584 if( ssl->transform == NULL || ssl->session == NULL )
11585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11586 /* There must be no pending incoming or outgoing data */
11587 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11589 if( ssl->out_left != 0 )
11590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11591 /* Protocol must be DLTS, not TLS */
11592 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11594 /* Version must be 1.2 */
11595 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11597 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11599 /* We must be using an AEAD ciphersuite */
11600 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11602 /* Renegotiation must not be enabled */
11603 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011605
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011606 /*
11607 * Version and format identifier
11608 */
11609 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011610
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011611 if( used <= buf_len )
11612 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011613 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011614 sizeof( ssl_serialized_context_header ) );
11615 p += sizeof( ssl_serialized_context_header );
11616 }
11617
11618 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011619 * Session (length + data)
11620 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011621 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011622 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11623 return( ret );
11624
11625 used += 4 + session_len;
11626 if( used <= buf_len )
11627 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011628 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011629
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011630 ret = ssl_session_save( ssl->session, 1,
11631 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011632 if( ret != 0 )
11633 return( ret );
11634
11635 p += session_len;
11636 }
11637
11638 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011639 * Transform
11640 */
11641 used += sizeof( ssl->transform->randbytes );
11642 if( used <= buf_len )
11643 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011644 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011645 sizeof( ssl->transform->randbytes ) );
11646 p += sizeof( ssl->transform->randbytes );
11647 }
11648
11649#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11650 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11651 if( used <= buf_len )
11652 {
11653 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011654 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11655 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011656 p += ssl->transform->in_cid_len;
11657
11658 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011659 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11660 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011661 p += ssl->transform->out_cid_len;
11662 }
11663#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11664
11665 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011666 * Saved fields from top-level ssl_context structure
11667 */
11668#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11669 used += 4;
11670 if( used <= buf_len )
11671 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011672 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011673 }
11674#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11675
11676#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11677 used += 16;
11678 if( used <= buf_len )
11679 {
11680 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11681 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11682 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11683 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11684 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11685 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11686 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11687 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11688
11689 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11690 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11691 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11692 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11693 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11694 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11695 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11696 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11697 }
11698#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11699
11700#if defined(MBEDTLS_SSL_PROTO_DTLS)
11701 used += 1;
11702 if( used <= buf_len )
11703 {
11704 *p++ = ssl->disable_datagram_packing;
11705 }
11706#endif /* MBEDTLS_SSL_PROTO_DTLS */
11707
11708 used += 8;
11709 if( used <= buf_len )
11710 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011711 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011712 p += 8;
11713 }
11714
11715#if defined(MBEDTLS_SSL_PROTO_DTLS)
11716 used += 2;
11717 if( used <= buf_len )
11718 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011719 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011720 }
11721#endif /* MBEDTLS_SSL_PROTO_DTLS */
11722
11723#if defined(MBEDTLS_SSL_ALPN)
11724 {
11725 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011726 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011727 : 0;
11728
11729 used += 1 + alpn_len;
11730 if( used <= buf_len )
11731 {
11732 *p++ = alpn_len;
11733
11734 if( ssl->alpn_chosen != NULL )
11735 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011736 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011737 p += alpn_len;
11738 }
11739 }
11740 }
11741#endif /* MBEDTLS_SSL_ALPN */
11742
11743 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011744 * Done
11745 */
11746 *olen = used;
11747
11748 if( used > buf_len )
11749 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011750
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011751 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11752
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011753 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011754}
11755
11756/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011757 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011758 *
11759 * This internal version is wrapped by a public function that cleans up in
11760 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011761 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011762static int ssl_context_load( mbedtls_ssl_context *ssl,
11763 const unsigned char *buf,
11764 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011765{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011766 const unsigned char *p = buf;
11767 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011768 size_t session_len;
11769 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011770
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011771 /*
11772 * The context should have been freshly setup or reset.
11773 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011774 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011775 * renegotiating, or if the user mistakenly loaded a session first.)
11776 */
11777 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11778 ssl->session != NULL )
11779 {
11780 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11781 }
11782
11783 /*
11784 * We can't check that the config matches the initial one, but we can at
11785 * least check it matches the requirements for serializing.
11786 */
11787 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011788 mbedtls_ssl_ver_lt(
11789 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11790 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11791 mbedtls_ssl_ver_gt(
11792 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11793 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11794 mbedtls_ssl_ver_lt(
11795 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11796 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11797 mbedtls_ssl_ver_gt(
11798 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11799 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011800 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011801 {
11802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11803 }
11804
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011805 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11806
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011807 /*
11808 * Check version identifier
11809 */
11810 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11812
Teppo Järvelin650343c2019-10-03 15:36:59 +030011813 // use regular memcmp as header is not that critical
11814 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011815 sizeof( ssl_serialized_context_header ) ) != 0 )
11816 {
11817 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11818 }
11819 p += sizeof( ssl_serialized_context_header );
11820
11821 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011822 * Session
11823 */
11824 if( (size_t)( end - p ) < 4 )
11825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11826
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011827 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011828 p += 4;
11829
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011830 /* This has been allocated by ssl_handshake_init(), called by
11831 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11832 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011833 ssl->session_in = ssl->session;
11834 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011835 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011836
11837 if( (size_t)( end - p ) < session_len )
11838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11839
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011840 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011841 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011842 {
11843 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011844 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011845 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011846
11847 p += session_len;
11848
11849 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011850 * Transform
11851 */
11852
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011853 /* This has been allocated by ssl_handshake_init(), called by
11854 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11855 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011856 ssl->transform_in = ssl->transform;
11857 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011858 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011859
11860 /* Read random bytes and populate structure */
11861 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11863
11864 ret = ssl_populate_transform( ssl->transform,
11865 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11866 ssl->session->master,
11867#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11869 ssl->session->encrypt_then_mac,
11870#endif
11871#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11872 ssl->session->trunc_hmac,
11873#endif
11874#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11875#if defined(MBEDTLS_ZLIB_SUPPORT)
11876 ssl->session->compression,
11877#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011878 p, /* currently pointing to randbytes */
11879 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11880 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11881 ssl );
11882 if( ret != 0 )
11883 return( ret );
11884
11885 p += sizeof( ssl->transform->randbytes );
11886
11887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11888 /* Read connection IDs and store them */
11889 if( (size_t)( end - p ) < 1 )
11890 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11891
11892 ssl->transform->in_cid_len = *p++;
11893
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011894 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11896
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011897 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11898 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011899 p += ssl->transform->in_cid_len;
11900
11901 ssl->transform->out_cid_len = *p++;
11902
11903 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11905
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011906 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11907 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011908 p += ssl->transform->out_cid_len;
11909#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11910
11911 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011912 * Saved fields from top-level ssl_context structure
11913 */
11914#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11915 if( (size_t)( end - p ) < 4 )
11916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11917
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011918 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011919 p += 4;
11920#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11921
11922#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11923 if( (size_t)( end - p ) < 16 )
11924 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11925
11926 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11927 ( (uint64_t) p[1] << 48 ) |
11928 ( (uint64_t) p[2] << 40 ) |
11929 ( (uint64_t) p[3] << 32 ) |
11930 ( (uint64_t) p[4] << 24 ) |
11931 ( (uint64_t) p[5] << 16 ) |
11932 ( (uint64_t) p[6] << 8 ) |
11933 ( (uint64_t) p[7] );
11934 p += 8;
11935
11936 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11937 ( (uint64_t) p[1] << 48 ) |
11938 ( (uint64_t) p[2] << 40 ) |
11939 ( (uint64_t) p[3] << 32 ) |
11940 ( (uint64_t) p[4] << 24 ) |
11941 ( (uint64_t) p[5] << 16 ) |
11942 ( (uint64_t) p[6] << 8 ) |
11943 ( (uint64_t) p[7] );
11944 p += 8;
11945#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11946
11947#if defined(MBEDTLS_SSL_PROTO_DTLS)
11948 if( (size_t)( end - p ) < 1 )
11949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11950
11951 ssl->disable_datagram_packing = *p++;
11952#endif /* MBEDTLS_SSL_PROTO_DTLS */
11953
11954 if( (size_t)( end - p ) < 8 )
11955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11956
Teppo Järvelin91d79382019-10-02 09:09:31 +030011957 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011958 p += 8;
11959
11960#if defined(MBEDTLS_SSL_PROTO_DTLS)
11961 if( (size_t)( end - p ) < 2 )
11962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011963 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011964 p += 2;
11965#endif /* MBEDTLS_SSL_PROTO_DTLS */
11966
11967#if defined(MBEDTLS_SSL_ALPN)
11968 {
11969 uint8_t alpn_len;
11970 const char **cur;
11971
11972 if( (size_t)( end - p ) < 1 )
11973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11974
11975 alpn_len = *p++;
11976
11977 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11978 {
11979 /* alpn_chosen should point to an item in the configured list */
11980 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11981 {
11982 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030011983 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011984 {
11985 ssl->alpn_chosen = *cur;
11986 break;
11987 }
11988 }
11989 }
11990
11991 /* can only happen on conf mismatch */
11992 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11994
11995 p += alpn_len;
11996 }
11997#endif /* MBEDTLS_SSL_ALPN */
11998
11999 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012000 * Forced fields from top-level ssl_context structure
12001 *
12002 * Most of them already set to the correct value by mbedtls_ssl_init() and
12003 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12004 */
12005 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12006
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012007#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012008 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012009#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12010#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012011 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012012#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012013
Hanno Becker83985822019-08-30 10:42:49 +010012014 /* Adjust pointers for header fields of outgoing records to
12015 * the given transform, accounting for explicit IV and CID. */
12016 ssl_update_out_pointers( ssl, ssl->transform );
12017
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012018#if defined(MBEDTLS_SSL_PROTO_DTLS)
12019 ssl->in_epoch = 1;
12020#endif
12021
12022 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12023 * which we don't want - otherwise we'd end up freeing the wrong transform
12024 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12025 if( ssl->handshake != NULL )
12026 {
12027 mbedtls_ssl_handshake_free( ssl );
12028 mbedtls_free( ssl->handshake );
12029 ssl->handshake = NULL;
12030 }
12031
12032 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012033 * Done - should have consumed entire buffer
12034 */
12035 if( p != end )
12036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012037
12038 return( 0 );
12039}
12040
12041/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012042 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012043 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012044int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012045 const unsigned char *buf,
12046 size_t len )
12047{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012048 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012049
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012050 if( ret != 0 )
12051 mbedtls_ssl_free( context );
12052
12053 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012054}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012055#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012056
12057/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012058 * Free an SSL context
12059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012060void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012061{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012062 if( ssl == NULL )
12063 return;
12064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012066
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012067 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012068 {
Angus Grattond8213d02016-05-25 20:56:48 +100012069 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012070 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012071 }
12072
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012073 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012074 {
Angus Grattond8213d02016-05-25 20:56:48 +100012075 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012076 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012077 }
12078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012079#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012080 if( ssl->compress_buf != NULL )
12081 {
Angus Grattond8213d02016-05-25 20:56:48 +100012082 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012083 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012084 }
12085#endif
12086
Paul Bakker48916f92012-09-16 19:57:18 +000012087 if( ssl->transform )
12088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012089 mbedtls_ssl_transform_free( ssl->transform );
12090 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012091 }
12092
12093 if( ssl->handshake )
12094 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012095 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012096 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12097 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012099 mbedtls_free( ssl->handshake );
12100 mbedtls_free( ssl->transform_negotiate );
12101 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012102 }
12103
Paul Bakkerc0463502013-02-14 11:19:38 +010012104 if( ssl->session )
12105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012106 mbedtls_ssl_session_free( ssl->session );
12107 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012108 }
12109
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012110#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012111 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012112 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012113 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012114 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012115 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012118#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12119 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12122 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012123 }
12124#endif
12125
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012126#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012127 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012128#endif
12129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012131
Paul Bakker86f04f42013-02-14 11:20:09 +010012132 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012133 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012134}
12135
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012136/*
12137 * Initialze mbedtls_ssl_config
12138 */
12139void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12140{
12141 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012142
12143#if !defined(MBEDTLS_SSL_PROTO_TLS)
12144 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12145#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012146}
12147
Simon Butcherc97b6972015-12-27 23:48:17 +000012148#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012149#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012150static int ssl_preset_default_hashes[] = {
12151#if defined(MBEDTLS_SHA512_C)
12152 MBEDTLS_MD_SHA512,
12153 MBEDTLS_MD_SHA384,
12154#endif
12155#if defined(MBEDTLS_SHA256_C)
12156 MBEDTLS_MD_SHA256,
12157 MBEDTLS_MD_SHA224,
12158#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012159#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012160 MBEDTLS_MD_SHA1,
12161#endif
12162 MBEDTLS_MD_NONE
12163};
Simon Butcherc97b6972015-12-27 23:48:17 +000012164#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012165#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012166
Hanno Becker73f4cb12019-06-27 13:51:07 +010012167#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012168static int ssl_preset_suiteb_ciphersuites[] = {
12169 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12170 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12171 0
12172};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012173#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012174
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012175#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012176#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012177static int ssl_preset_suiteb_hashes[] = {
12178 MBEDTLS_MD_SHA256,
12179 MBEDTLS_MD_SHA384,
12180 MBEDTLS_MD_NONE
12181};
12182#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012183#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012184
Hanno Beckerc1096e72019-06-19 12:30:41 +010012185#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012186static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012187#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012188 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012189#endif
12190#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012191 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012192#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012193 MBEDTLS_ECP_DP_NONE
12194};
12195#endif
12196
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012197/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012198 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012199 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012200int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012201 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012202{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012203#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012204 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012205#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012206
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012207 /* Use the functions here so that they are covered in tests,
12208 * but otherwise access member directly for efficiency */
12209 mbedtls_ssl_conf_endpoint( conf, endpoint );
12210 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012211
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012212 /*
12213 * Things that are common to all presets
12214 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012215#if defined(MBEDTLS_SSL_CLI_C)
12216 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12217 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012218#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012219 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012220#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012221#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12222 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12223#endif
12224 }
12225#endif
12226
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012227#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012228 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012229#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012230
12231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12232 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12233#endif
12234
12235#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012236#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012237 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012238#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12239#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012240 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012241 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012242#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012243#endif
12244
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012245#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12246 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12247#endif
12248
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012249#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012250 conf->f_cookie_write = ssl_cookie_write_dummy;
12251 conf->f_cookie_check = ssl_cookie_check_dummy;
12252#endif
12253
Hanno Becker7f376f42019-06-12 16:20:48 +010012254#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12255 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012256 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12257#endif
12258
Janos Follath088ce432017-04-10 12:42:31 +010012259#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012260#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012261 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012262#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12263#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012264
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012266#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012267 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012268#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12269#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012270 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012271#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12272#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012273
12274#if defined(MBEDTLS_SSL_RENEGOTIATION)
12275 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012276 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12277 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012278#endif
12279
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012280#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12281 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12282 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012283 const unsigned char dhm_p[] =
12284 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12285 const unsigned char dhm_g[] =
12286 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12287
Hanno Beckera90658f2017-10-04 15:29:08 +010012288 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12289 dhm_p, sizeof( dhm_p ),
12290 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012291 {
12292 return( ret );
12293 }
12294 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012295#endif
12296
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012297 /*
12298 * Preset-specific defaults
12299 */
12300 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012301 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012302 /*
12303 * NSA Suite B
12304 */
12305 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012306#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012307 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012308#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12309#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012310 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012311#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12312#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012313 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012314#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12315#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012316 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012317#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012318
Hanno Becker73f4cb12019-06-27 13:51:07 +010012319#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012320 conf->ciphersuite_list[0] =
12321 conf->ciphersuite_list[1] =
12322 conf->ciphersuite_list[2] =
12323 conf->ciphersuite_list[3] =
12324 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012325#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012326
12327#if defined(MBEDTLS_X509_CRT_PARSE_C)
12328 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012329#endif
12330
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012331#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012332#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012333 conf->sig_hashes = ssl_preset_suiteb_hashes;
12334#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012335#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012336
12337#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012338#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012339 conf->curve_list = ssl_preset_suiteb_curves;
12340#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012341#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012342 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012343
12344 /*
12345 * Default
12346 */
12347 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012348#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012349 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12350 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12351 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12352 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012353#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12354#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012355 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12356 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12357 MBEDTLS_SSL_MIN_MINOR_VERSION :
12358 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012359#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012360 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012361 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12362#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012363#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12364#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12365 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12366#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12367#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12368 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12369#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012370
Hanno Becker73f4cb12019-06-27 13:51:07 +010012371#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012372 conf->ciphersuite_list[0] =
12373 conf->ciphersuite_list[1] =
12374 conf->ciphersuite_list[2] =
12375 conf->ciphersuite_list[3] =
12376 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012377#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012378
12379#if defined(MBEDTLS_X509_CRT_PARSE_C)
12380 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12381#endif
12382
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012383#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012384#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012385 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012386#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012387#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012388
12389#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012390#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012391 conf->curve_list = mbedtls_ecp_grp_id_list();
12392#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012393#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394
12395#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12396 conf->dhm_min_bitlen = 1024;
12397#endif
12398 }
12399
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012400 return( 0 );
12401}
12402
12403/*
12404 * Free mbedtls_ssl_config
12405 */
12406void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12407{
12408#if defined(MBEDTLS_DHM_C)
12409 mbedtls_mpi_free( &conf->dhm_P );
12410 mbedtls_mpi_free( &conf->dhm_G );
12411#endif
12412
12413#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12414 if( conf->psk != NULL )
12415 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012416 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012417 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012418 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012419 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012420 }
12421
12422 if( conf->psk_identity != NULL )
12423 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012424 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012425 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012426 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012427 conf->psk_identity_len = 0;
12428 }
12429#endif
12430
12431#if defined(MBEDTLS_X509_CRT_PARSE_C)
12432 ssl_key_cert_free( conf->key_cert );
12433#endif
12434
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012435 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012436}
12437
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012438#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012439 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12440 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012441/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012442 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012444unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012446#if defined(MBEDTLS_RSA_C)
12447 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12448 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012449#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012450#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012451 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12452 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012454 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012455}
12456
Hanno Becker7e5437a2017-04-28 17:15:26 +010012457unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12458{
12459 switch( type ) {
12460 case MBEDTLS_PK_RSA:
12461 return( MBEDTLS_SSL_SIG_RSA );
12462 case MBEDTLS_PK_ECDSA:
12463 case MBEDTLS_PK_ECKEY:
12464 return( MBEDTLS_SSL_SIG_ECDSA );
12465 default:
12466 return( MBEDTLS_SSL_SIG_ANON );
12467 }
12468}
12469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012470mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012471{
12472 switch( sig )
12473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012474#if defined(MBEDTLS_RSA_C)
12475 case MBEDTLS_SSL_SIG_RSA:
12476 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012477#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012478#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012479 case MBEDTLS_SSL_SIG_ECDSA:
12480 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012481#endif
12482 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012483 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012484 }
12485}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012486#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012487
Hanno Becker7e5437a2017-04-28 17:15:26 +010012488#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12489 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12490
12491/* Find an entry in a signature-hash set matching a given hash algorithm. */
12492mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12493 mbedtls_pk_type_t sig_alg )
12494{
12495 switch( sig_alg )
12496 {
12497 case MBEDTLS_PK_RSA:
12498 return( set->rsa );
12499 case MBEDTLS_PK_ECDSA:
12500 return( set->ecdsa );
12501 default:
12502 return( MBEDTLS_MD_NONE );
12503 }
12504}
12505
12506/* Add a signature-hash-pair to a signature-hash set */
12507void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12508 mbedtls_pk_type_t sig_alg,
12509 mbedtls_md_type_t md_alg )
12510{
12511 switch( sig_alg )
12512 {
12513 case MBEDTLS_PK_RSA:
12514 if( set->rsa == MBEDTLS_MD_NONE )
12515 set->rsa = md_alg;
12516 break;
12517
12518 case MBEDTLS_PK_ECDSA:
12519 if( set->ecdsa == MBEDTLS_MD_NONE )
12520 set->ecdsa = md_alg;
12521 break;
12522
12523 default:
12524 break;
12525 }
12526}
12527
12528/* Allow exactly one hash algorithm for each signature. */
12529void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12530 mbedtls_md_type_t md_alg )
12531{
12532 set->rsa = md_alg;
12533 set->ecdsa = md_alg;
12534}
12535
12536#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12537 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12538
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012539/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012540 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012542mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012543{
12544 switch( hash )
12545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012546#if defined(MBEDTLS_MD5_C)
12547 case MBEDTLS_SSL_HASH_MD5:
12548 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012550#if defined(MBEDTLS_SHA1_C)
12551 case MBEDTLS_SSL_HASH_SHA1:
12552 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012554#if defined(MBEDTLS_SHA256_C)
12555 case MBEDTLS_SSL_HASH_SHA224:
12556 return( MBEDTLS_MD_SHA224 );
12557 case MBEDTLS_SSL_HASH_SHA256:
12558 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012560#if defined(MBEDTLS_SHA512_C)
12561 case MBEDTLS_SSL_HASH_SHA384:
12562 return( MBEDTLS_MD_SHA384 );
12563 case MBEDTLS_SSL_HASH_SHA512:
12564 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012565#endif
12566 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012567 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012568 }
12569}
12570
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012571/*
12572 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12573 */
12574unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12575{
12576 switch( md )
12577 {
12578#if defined(MBEDTLS_MD5_C)
12579 case MBEDTLS_MD_MD5:
12580 return( MBEDTLS_SSL_HASH_MD5 );
12581#endif
12582#if defined(MBEDTLS_SHA1_C)
12583 case MBEDTLS_MD_SHA1:
12584 return( MBEDTLS_SSL_HASH_SHA1 );
12585#endif
12586#if defined(MBEDTLS_SHA256_C)
12587 case MBEDTLS_MD_SHA224:
12588 return( MBEDTLS_SSL_HASH_SHA224 );
12589 case MBEDTLS_MD_SHA256:
12590 return( MBEDTLS_SSL_HASH_SHA256 );
12591#endif
12592#if defined(MBEDTLS_SHA512_C)
12593 case MBEDTLS_MD_SHA384:
12594 return( MBEDTLS_SSL_HASH_SHA384 );
12595 case MBEDTLS_MD_SHA512:
12596 return( MBEDTLS_SSL_HASH_SHA512 );
12597#endif
12598 default:
12599 return( MBEDTLS_SSL_HASH_NONE );
12600 }
12601}
12602
Hanno Beckeree902df2019-08-23 13:47:47 +010012603#if defined(MBEDTLS_USE_TINYCRYPT)
12604/*
12605 * Check if a curve proposed by the peer is in our list.
12606 * Return 0 if we're willing to use it, -1 otherwise.
12607 */
12608int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12609 mbedtls_uecc_group_id grp_id )
12610{
12611 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12612 if( own_ec_id == grp_id )
12613 return( 0 );
12614 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12615
12616 return( -1 );
12617}
12618#endif /* MBEDTLS_USE_TINYCRYPT */
12619
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012620#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012621/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012622 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012623 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012624 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012625int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12626 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012627{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012628 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12629 if( own_ec_id == grp_id )
12630 return( 0 );
12631 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012632
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012633 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012634}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012635#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012636
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012637#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012638/*
12639 * Check if a hash proposed by the peer is in our list.
12640 * Return 0 if we're willing to use it, -1 otherwise.
12641 */
12642int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12643 mbedtls_md_type_t md )
12644{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012645 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12646 if( md_alg == md )
12647 return( 0 );
12648 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012649
12650 return( -1 );
12651}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012652#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012654#if defined(MBEDTLS_X509_CRT_PARSE_C)
12655int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012656 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012657 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012658 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012659{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012660 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012661#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012662 int usage = 0;
12663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012664#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012665 const char *ext_oid;
12666 size_t ext_len;
12667#endif
12668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012669#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12670 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012671 ((void) cert);
12672 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012673 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012674#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012676#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12677 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012678 {
12679 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012680 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012682 case MBEDTLS_KEY_EXCHANGE_RSA:
12683 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012684 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012685 break;
12686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012687 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12688 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12689 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12690 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012691 break;
12692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012693 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12694 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012695 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012696 break;
12697
12698 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012699 case MBEDTLS_KEY_EXCHANGE_NONE:
12700 case MBEDTLS_KEY_EXCHANGE_PSK:
12701 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12702 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012703 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012704 usage = 0;
12705 }
12706 }
12707 else
12708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012709 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12710 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012711 }
12712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012713 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012714 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012715 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012716 ret = -1;
12717 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012718#else
12719 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012720#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012722#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12723 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012725 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12726 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012727 }
12728 else
12729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012730 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12731 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012732 }
12733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012734 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012735 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012736 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012737 ret = -1;
12738 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012739#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012740
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012741 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012743#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012744
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012745#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12746 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12747int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12748 unsigned char *output,
12749 unsigned char *data, size_t data_len )
12750{
12751 int ret = 0;
12752 mbedtls_md5_context mbedtls_md5;
12753 mbedtls_sha1_context mbedtls_sha1;
12754
12755 mbedtls_md5_init( &mbedtls_md5 );
12756 mbedtls_sha1_init( &mbedtls_sha1 );
12757
12758 /*
12759 * digitally-signed struct {
12760 * opaque md5_hash[16];
12761 * opaque sha_hash[20];
12762 * };
12763 *
12764 * md5_hash
12765 * MD5(ClientHello.random + ServerHello.random
12766 * + ServerParams);
12767 * sha_hash
12768 * SHA(ClientHello.random + ServerHello.random
12769 * + ServerParams);
12770 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012771 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012772 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012774 goto exit;
12775 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012776 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012777 ssl->handshake->randbytes, 64 ) ) != 0 )
12778 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012780 goto exit;
12781 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012782 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012783 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012785 goto exit;
12786 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012787 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012788 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012790 goto exit;
12791 }
12792
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012793 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012794 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012796 goto exit;
12797 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012798 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012799 ssl->handshake->randbytes, 64 ) ) != 0 )
12800 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012802 goto exit;
12803 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012804 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012805 data_len ) ) != 0 )
12806 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012808 goto exit;
12809 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012810 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012811 output + 16 ) ) != 0 )
12812 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012814 goto exit;
12815 }
12816
12817exit:
12818 mbedtls_md5_free( &mbedtls_md5 );
12819 mbedtls_sha1_free( &mbedtls_sha1 );
12820
12821 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012822 mbedtls_ssl_pend_fatal_alert( ssl,
12823 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012824
12825 return( ret );
12826
12827}
12828#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12829 MBEDTLS_SSL_PROTO_TLS1_1 */
12830
12831#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12832 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12833int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012834 unsigned char *hash, size_t *hashlen,
12835 unsigned char *data, size_t data_len,
12836 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012837{
12838 int ret = 0;
12839 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012840 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012841 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012842
12843 mbedtls_md_init( &ctx );
12844
12845 /*
12846 * digitally-signed struct {
12847 * opaque client_random[32];
12848 * opaque server_random[32];
12849 * ServerDHParams params;
12850 * };
12851 */
12852 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12853 {
12854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12855 goto exit;
12856 }
12857 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12858 {
12859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12860 goto exit;
12861 }
12862 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12863 {
12864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12865 goto exit;
12866 }
12867 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12868 {
12869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12870 goto exit;
12871 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012872 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012873 {
12874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12875 goto exit;
12876 }
12877
12878exit:
12879 mbedtls_md_free( &ctx );
12880
12881 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012882 mbedtls_ssl_pend_fatal_alert( ssl,
12883 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012884
12885 return( ret );
12886}
12887#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12888 MBEDTLS_SSL_PROTO_TLS1_2 */
12889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012890#endif /* MBEDTLS_SSL_TLS_C */