blob: 4fc6aa832307045307423d9252902b07c94c150a [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 );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001977 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_UNSET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01001978#if defined(MBEDTLS_USE_TINYCRYPT)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001980 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001981 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001982 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1983 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1984 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1985 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1986 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001987 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001988 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1989 ssl->handshake->ecdh_privkey,
1990 ssl->handshake->premaster );
1991 if( ret == UECC_FAULT_DETECTED )
1992 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1993 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001994 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02001995 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01001996
1997 ssl->handshake->pmslen = NUM_ECC_BYTES;
1998 }
1999 else
2000#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002001#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2004 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002005 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002006 ssl->handshake->premaster,
2007 MBEDTLS_PREMASTER_SIZE,
2008 &ssl->handshake->pmslen,
2009 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002010 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2011 if( ret == 0 )
2012 {
2013 mbedtls_platform_enforce_volatile_reads();
2014 if( ret == 0 )
2015 {
2016 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2017 }
2018 else
2019 {
2020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2021 return( ret );
2022 }
2023 }
2024 else
Hanno Becker09d23642019-07-22 17:18:18 +01002025 {
2026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2027 return( ret );
2028 }
2029
2030 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2031 }
2032 else
2033#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002034#if defined(MBEDTLS_ECDH_C) && \
2035 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2036 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2037 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2038 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002039 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2040 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2041 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2042 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2043 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2044 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2045 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2046 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2047 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002048 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002049 &ssl->handshake->pmslen,
2050 ssl->handshake->premaster,
2051 MBEDTLS_MPI_MAX_SIZE,
2052 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002053 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2054 if( ret == 0 )
2055 {
2056 mbedtls_platform_enforce_volatile_reads();
2057 if( ret == 0 )
2058 {
2059 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2060 }
2061 else
2062 {
2063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2064 return( ret );
2065 }
2066 }
2067 else
Hanno Becker09d23642019-07-22 17:18:18 +01002068 {
2069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2070 return( ret );
2071 }
2072
2073 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2074 }
2075 else
2076#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2077 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2078 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2079 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2080#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2081 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2082 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002083 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2084 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2085 if( ret == 0 )
2086 {
2087 mbedtls_platform_enforce_volatile_reads();
2088 if( ret == 0 )
2089 {
2090 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2091 }
2092 else
2093 {
2094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2095 return( ret );
2096 }
2097 }
2098 else
Hanno Becker09d23642019-07-22 17:18:18 +01002099 {
2100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2101 return( ret );
2102 }
2103 }
2104 else
2105#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2106#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2107 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2108 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2109 {
2110 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2111 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2112 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002113 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002114 if( ret == 0 )
2115 {
2116 mbedtls_platform_enforce_volatile_reads();
2117 if( ret == 0 )
2118 {
2119 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2120 }
2121 else
2122 {
2123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2124 return( ret );
2125 }
2126 }
2127 else
Hanno Becker09d23642019-07-22 17:18:18 +01002128 {
2129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2130 return( ret );
2131 }
2132 }
2133 else
2134#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2135#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2136 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2137 == MBEDTLS_KEY_EXCHANGE_RSA )
2138 {
2139 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002140 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002141 * ssl_rsa_generate_partial_pms(). Only the
2142 * PMS length needs to be set. */
2143 ssl->handshake->pmslen = 48;
2144 }
2145 else
2146#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2147 {
2148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2150 }
2151
2152 return( 0 );
2153}
2154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2156int 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 +02002157{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002158 unsigned char *p = ssl->handshake->premaster;
2159 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002160 const unsigned char *psk = ssl->conf->psk;
2161 size_t psk_len = ssl->conf->psk_len;
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002162 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002163
2164 /* If the psk callback was called, use its result */
2165 if( ssl->handshake->psk != NULL )
2166 {
2167 psk = ssl->handshake->psk;
2168 psk_len = ssl->handshake->psk_len;
2169 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002170
2171 /*
2172 * PMS = struct {
2173 * opaque other_secret<0..2^16-1>;
2174 * opaque psk<0..2^16-1>;
2175 * };
2176 * with "other_secret" depending on the particular key exchange
2177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2179 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002180 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002183
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002184 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002185
2186 if( end < p || (size_t)( end - p ) < psk_len )
2187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2188
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002189 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002190 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 }
2192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2194#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2195 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002196 {
2197 /*
2198 * other_secret already set by the ClientKeyExchange message,
2199 * and is 48 bytes long
2200 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002201 if( end - p < 2 )
2202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2203
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002204 *p++ = 0;
2205 *p++ = 48;
2206 p += 48;
2207 }
2208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2210#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2211 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002212 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002213 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002214 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002215
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002216 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002218 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002219 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002220 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002223 return( ret );
2224 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002225 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002226 p += len;
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002229 }
2230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2232#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2233 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002235 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002236 size_t zlen;
2237
Hanno Becker982da7e2019-09-02 09:47:39 +01002238#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002239 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2240 ssl->handshake->ecdh_privkey,
2241 p + 2 );
2242 if( ret == UECC_FAULT_DETECTED )
2243 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2244 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002246
2247 zlen = NUM_ECC_BYTES;
2248#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002250 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002251 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002252 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002255 return( ret );
2256 }
2257
Hanno Becker982da7e2019-09-02 09:47:39 +01002258 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2259 MBEDTLS_DEBUG_ECDH_Z );
2260#endif /* MBEDTLS_USE_TINYCRYPT */
2261
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002262 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 }
2265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002270 }
2271
2272 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002273 if( end - p < 2 )
2274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002275
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002276 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002277
2278 if( end < p || (size_t)( end - p ) < psk_len )
2279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2280
Teppo Järvelin91d79382019-10-02 09:09:31 +03002281 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002282 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002283
2284 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2285
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002286 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2287
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002288 return( 0 );
2289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002293/*
2294 * SSLv3.0 MAC functions
2295 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002296#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002297static void ssl_mac( mbedtls_md_context_t *md_ctx,
2298 const unsigned char *secret,
2299 const unsigned char *buf, size_t len,
2300 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002301 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302{
2303 unsigned char header[11];
2304 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002305 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2307 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002308
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002309 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002312 else
Paul Bakker68884e32013-01-07 18:20:04 +01002313 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
Teppo Järvelin91d79382019-10-02 09:09:31 +03002315 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002316 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002317 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002319 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 mbedtls_md_starts( md_ctx );
2321 mbedtls_md_update( md_ctx, secret, md_size );
2322 mbedtls_md_update( md_ctx, padding, padlen );
2323 mbedtls_md_update( md_ctx, header, 11 );
2324 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002325 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002327 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 mbedtls_md_starts( md_ctx );
2329 mbedtls_md_update( md_ctx, secret, md_size );
2330 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002331 mbedtls_md_update( md_ctx, out, md_size );
2332 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002335
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002336/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002337 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002338#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002339 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2340 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2341 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2342/* This function makes sure every byte in the memory region is accessed
2343 * (in ascending addresses order) */
2344static void ssl_read_memory( unsigned char *p, size_t len )
2345{
2346 unsigned char acc = 0;
2347 volatile unsigned char force;
2348
2349 for( ; len != 0; p++, len-- )
2350 acc ^= *p;
2351
2352 force = acc;
2353 (void) force;
2354}
2355#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2356
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002359 */
Hanno Becker3307b532017-12-27 21:37:21 +00002360
Hanno Beckera5a2b082019-05-15 14:03:01 +01002361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002362/* This functions transforms a DTLS plaintext fragment and a record content
2363 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002364 *
2365 * struct {
2366 * opaque content[DTLSPlaintext.length];
2367 * ContentType real_type;
2368 * uint8 zeros[length_of_padding];
2369 * } DTLSInnerPlaintext;
2370 *
2371 * Input:
2372 * - `content`: The beginning of the buffer holding the
2373 * plaintext to be wrapped.
2374 * - `*content_size`: The length of the plaintext in Bytes.
2375 * - `max_len`: The number of Bytes available starting from
2376 * `content`. This must be `>= *content_size`.
2377 * - `rec_type`: The desired record content type.
2378 *
2379 * Output:
2380 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2381 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2382 *
2383 * Returns:
2384 * - `0` on success.
2385 * - A negative error code if `max_len` didn't offer enough space
2386 * for the expansion.
2387 */
2388static int ssl_cid_build_inner_plaintext( unsigned char *content,
2389 size_t *content_size,
2390 size_t remaining,
2391 uint8_t rec_type )
2392{
2393 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002394 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2395 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2396 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002397
2398 /* Write real content type */
2399 if( remaining == 0 )
2400 return( -1 );
2401 content[ len ] = rec_type;
2402 len++;
2403 remaining--;
2404
2405 if( remaining < pad )
2406 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002407 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002408 len += pad;
2409 remaining -= pad;
2410
2411 *content_size = len;
2412 return( 0 );
2413}
2414
Hanno Becker7dc25772019-05-20 15:08:01 +01002415/* This function parses a DTLSInnerPlaintext structure.
2416 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002417static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2418 size_t *content_size,
2419 uint8_t *rec_type )
2420{
2421 size_t remaining = *content_size;
2422
2423 /* Determine length of padding by skipping zeroes from the back. */
2424 do
2425 {
2426 if( remaining == 0 )
2427 return( -1 );
2428 remaining--;
2429 } while( content[ remaining ] == 0 );
2430
2431 *content_size = remaining;
2432 *rec_type = content[ remaining ];
2433
2434 return( 0 );
2435}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002437
Hanno Becker99abf512019-05-20 14:50:53 +01002438/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002439 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002440static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002441 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002442 mbedtls_record *rec )
2443{
Hanno Becker99abf512019-05-20 14:50:53 +01002444 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002445 *
2446 * additional_data = seq_num + TLSCompressed.type +
2447 * TLSCompressed.version + TLSCompressed.length;
2448 *
Hanno Becker99abf512019-05-20 14:50:53 +01002449 * For the CID extension, this is extended as follows
2450 * (quoting draft-ietf-tls-dtls-connection-id-05,
2451 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 *
2453 * additional_data = seq_num + DTLSPlaintext.type +
2454 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002455 * cid +
2456 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002457 * length_of_DTLSInnerPlaintext;
2458 */
2459
Teppo Järvelin91d79382019-10-02 09:09:31 +03002460 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002461 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002462 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002463
Hanno Beckera5a2b082019-05-15 14:03:01 +01002464#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002465 if( rec->cid_len != 0 )
2466 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002467 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002468 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002469 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2470 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002471 *add_data_len = 13 + 1 + rec->cid_len;
2472 }
2473 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002475 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002476 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 *add_data_len = 13;
2478 }
Hanno Becker3307b532017-12-27 21:37:21 +00002479}
2480
Hanno Becker611a83b2018-01-03 14:27:32 +00002481int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2482 mbedtls_ssl_transform *transform,
2483 mbedtls_record *rec,
2484 int (*f_rng)(void *, unsigned char *, size_t),
2485 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002489 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002490 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002491 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002492 size_t post_avail;
2493
2494 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002495#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002496 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002497 ((void) ssl);
2498#endif
2499
2500 /* The PRNG is used for dynamic IV generation that's used
2501 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2502#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2503 ( defined(MBEDTLS_AES_C) || \
2504 defined(MBEDTLS_ARIA_C) || \
2505 defined(MBEDTLS_CAMELLIA_C) ) && \
2506 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2507 ((void) f_rng);
2508 ((void) p_rng);
2509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Hanno Becker3307b532017-12-27 21:37:21 +00002513 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002514 {
Hanno Becker3307b532017-12-27 21:37:21 +00002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2517 }
Hanno Becker505089d2019-05-01 09:45:57 +01002518 if( rec == NULL
2519 || rec->buf == NULL
2520 || rec->buf_len < rec->data_offset
2521 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002522#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002523 || rec->cid_len != 0
2524#endif
2525 )
Hanno Becker3307b532017-12-27 21:37:21 +00002526 {
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002529 }
2530
Hanno Becker3307b532017-12-27 21:37:21 +00002531 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002532 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002534 data, rec->data_len );
2535
2536 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2537
2538 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2539 {
2540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2541 (unsigned) rec->data_len,
2542 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2544 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002545
Hanno Beckera5a2b082019-05-15 14:03:01 +01002546#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002547 /*
2548 * Add CID information
2549 */
2550 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002551 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2552 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002553 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002554
2555 if( rec->cid_len != 0 )
2556 {
2557 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002558 * Wrap plaintext into DTLSInnerPlaintext structure.
2559 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002560 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002561 * Note that this changes `rec->data_len`, and hence
2562 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002563 */
2564 if( ssl_cid_build_inner_plaintext( data,
2565 &rec->data_len,
2566 post_avail,
2567 rec->type ) != 0 )
2568 {
2569 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2570 }
2571
2572 rec->type = MBEDTLS_SSL_MSG_CID;
2573 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002574#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002575
Hanno Becker92c930f2019-04-29 17:31:37 +01002576 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2577
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002579 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002581#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 if( mode == MBEDTLS_MODE_STREAM ||
2583 ( mode == MBEDTLS_MODE_CBC
2584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002585 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002586#endif
2587 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( post_avail < transform->maclen )
2590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2592 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002596 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2597 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002599 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002600 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2601 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002602 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002603 }
2604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2607 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002608 if( mbedtls_ssl_ver_geq(
2609 mbedtls_ssl_transform_get_minor_ver( transform ),
2610 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002611 {
Hanno Becker992b6872017-11-09 18:57:39 +00002612 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2613
Hanno Beckere83efe62019-04-29 13:52:53 +01002614 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002615
Hanno Becker3307b532017-12-27 21:37:21 +00002616 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002617 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002618 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2619 data, rec->data_len );
2620 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2621 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2622
Teppo Järvelin91d79382019-10-02 09:09:31 +03002623 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002624 }
2625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002630 }
2631
Hanno Becker3307b532017-12-27 21:37:21 +00002632 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2633 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002634
Hanno Becker3307b532017-12-27 21:37:21 +00002635 rec->data_len += transform->maclen;
2636 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002637 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002638 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002639#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641 /*
2642 * Encrypt
2643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2645 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002647 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002648 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002650 "including %d bytes of padding",
2651 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Hanno Becker3307b532017-12-27 21:37:21 +00002653 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2654 transform->iv_enc, transform->ivlen,
2655 data, rec->data_len,
2656 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002659 return( ret );
2660 }
2661
Hanno Becker3307b532017-12-27 21:37:21 +00002662 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2665 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
Paul Bakker68884e32013-01-07 18:20:04 +01002668 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002670
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671#if defined(MBEDTLS_GCM_C) || \
2672 defined(MBEDTLS_CCM_C) || \
2673 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002675 mode == MBEDTLS_MODE_CCM ||
2676 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002677 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002678 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002679 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002680 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002681
Hanno Becker3307b532017-12-27 21:37:21 +00002682 /* Check that there's space for both the authentication tag
2683 * and the explicit IV before and after the record content. */
2684 if( post_avail < transform->taglen ||
2685 rec->data_offset < explicit_iv_len )
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2688 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2689 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690
Paul Bakker68884e32013-01-07 18:20:04 +01002691 /*
2692 * Generate IV
2693 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002694 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2695 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002696 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002697 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2698 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002699 explicit_iv_len );
2700 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002701 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002702 }
2703 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2704 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002705 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 unsigned char i;
2707
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002709
2710 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002711 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 }
2713 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002714 {
2715 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002718 }
2719
Hanno Beckere83efe62019-04-29 13:52:53 +01002720 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002721
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002722 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2723 iv, transform->ivlen );
2724 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002725 data - explicit_iv_len, explicit_iv_len );
2726 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002727 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002729 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002730 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002731
Paul Bakker68884e32013-01-07 18:20:04 +01002732 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002733 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002734 */
Hanno Becker3307b532017-12-27 21:37:21 +00002735
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002736 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002737 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002738 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002739 data, rec->data_len, /* source */
2740 data, &rec->data_len, /* destination */
2741 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002744 return( ret );
2745 }
2746
Hanno Becker3307b532017-12-27 21:37:21 +00002747 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2748 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002749
Hanno Becker3307b532017-12-27 21:37:21 +00002750 rec->data_len += transform->taglen + explicit_iv_len;
2751 rec->data_offset -= explicit_iv_len;
2752 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002753 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002754 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2757#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002758 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002761 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002762 size_t padlen, i;
2763 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002764
Hanno Becker3307b532017-12-27 21:37:21 +00002765 /* Currently we're always using minimal padding
2766 * (up to 255 bytes would be allowed). */
2767 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2768 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 padlen = 0;
2770
Hanno Becker3307b532017-12-27 21:37:21 +00002771 /* Check there's enough space in the buffer for the padding. */
2772 if( post_avail < padlen + 1 )
2773 {
2774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2775 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2776 }
2777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002779 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Hanno Becker3307b532017-12-27 21:37:21 +00002781 rec->data_len += padlen + 1;
2782 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002785 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002786 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2787 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002788 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002789 if( mbedtls_ssl_ver_geq(
2790 mbedtls_ssl_transform_get_minor_ver( transform ),
2791 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002792 {
Hanno Becker3307b532017-12-27 21:37:21 +00002793 if( f_rng == NULL )
2794 {
2795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2797 }
2798
2799 if( rec->data_offset < transform->ivlen )
2800 {
2801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2802 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2803 }
2804
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 /*
2806 * Generate IV
2807 */
Hanno Becker3307b532017-12-27 21:37:21 +00002808 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002809 if( ret != 0 )
2810 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Teppo Järvelin91d79382019-10-02 09:09:31 +03002812 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002813 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002820 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002821 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Hanno Becker3307b532017-12-27 21:37:21 +00002823 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2824 transform->iv_enc,
2825 transform->ivlen,
2826 data, rec->data_len,
2827 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002830 return( ret );
2831 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002832
Hanno Becker3307b532017-12-27 21:37:21 +00002833 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002837 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002840 if( mbedtls_ssl_ver_lt(
2841 mbedtls_ssl_transform_get_minor_ver( transform ),
2842 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 {
2844 /*
2845 * Save IV in SSL3 and TLS1
2846 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002847 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002848 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 }
Hanno Becker3307b532017-12-27 21:37:21 +00002850 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002851#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002852 {
2853 data -= transform->ivlen;
2854 rec->data_offset -= transform->ivlen;
2855 rec->data_len += transform->ivlen;
2856 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002860 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002861 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2862
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002863 /*
2864 * MAC(MAC_write_key, seq_num +
2865 * TLSCipherText.type +
2866 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002867 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868 * IV + // except for TLS 1.0
2869 * ENC(content + padding + padding_length));
2870 */
Hanno Becker3307b532017-12-27 21:37:21 +00002871
2872 if( post_avail < transform->maclen)
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2875 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2876 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877
Hanno Beckere83efe62019-04-29 13:52:53 +01002878 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002881 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002882 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002883
Hanno Becker3307b532017-12-27 21:37:21 +00002884 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002885 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002886 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2887 data, rec->data_len );
2888 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2889 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002890
Teppo Järvelin91d79382019-10-02 09:09:31 +03002891 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002892
Hanno Becker3307b532017-12-27 21:37:21 +00002893 rec->data_len += transform->maclen;
2894 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002895 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002896 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002901 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002907 /* Make extra sure authentication was performed, exactly once */
2908 if( auth_done != 1 )
2909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002912 }
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
2916 return( 0 );
2917}
2918
Hanno Becker40478be2019-07-12 08:23:59 +01002919int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002920 mbedtls_ssl_transform *transform,
2921 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002923 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002925 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002926#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002927 size_t padlen = 0, correct = 1;
2928#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002930 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002931 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002932
Hanno Becker611a83b2018-01-03 14:27:32 +00002933#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002934 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 ((void) ssl);
2936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939 if( rec == NULL ||
2940 rec->buf == NULL ||
2941 rec->buf_len < rec->data_offset ||
2942 rec->buf_len - rec->data_offset < rec->data_len )
2943 {
2944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002946 }
2947
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 data = rec->buf + rec->data_offset;
2949 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Hanno Beckera5a2b082019-05-15 14:03:01 +01002951#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002952 /*
2953 * Match record's CID with incoming CID.
2954 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002955 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002956 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002957 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002958 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002959 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002960#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2963 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002964 {
2965 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2967 transform->iv_dec,
2968 transform->ivlen,
2969 data, rec->data_len,
2970 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002973 return( ret );
2974 }
2975
Hanno Becker4c6876b2017-12-27 21:28:58 +00002976 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 }
Paul Bakker68884e32013-01-07 18:20:04 +01002982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002984#if defined(MBEDTLS_GCM_C) || \
2985 defined(MBEDTLS_CCM_C) || \
2986 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002988 mode == MBEDTLS_MODE_CCM ||
2989 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002990 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002991 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002992 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002994 /*
2995 * Prepare IV from explicit and implicit data.
2996 */
2997
2998 /* Check that there's enough space for the explicit IV
2999 * (at the beginning of the record) and the MAC (at the
3000 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003001 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003004 "+ taglen (%d)", rec->data_len,
3005 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003007 }
Paul Bakker68884e32013-01-07 18:20:04 +01003008
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003009#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003010 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3011 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003012 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003013
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003014 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003015 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003016 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003017 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003018 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003019 else
3020#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3021#if defined(MBEDTLS_CHACHAPOLY_C)
3022 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003023 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003024 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003025 unsigned char i;
3026
Teppo Järvelin91d79382019-10-02 09:09:31 +03003027 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003028
3029 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003030 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003031 }
3032 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003033#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 {
3035 /* Reminder if we ever add an AEAD mode with a different size */
3036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3038 }
3039
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003040 /* Group changes to data, data_len, and add_data, because
3041 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003042 data += explicit_iv_len;
3043 rec->data_offset += explicit_iv_len;
3044 rec->data_len -= explicit_iv_len + transform->taglen;
3045
Hanno Beckere83efe62019-04-29 13:52:53 +01003046 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003048 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003050 /* Because of the check above, we know that there are
3051 * explicit_iv_len Bytes preceeding data, and taglen
3052 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003053 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003054 * mbedtls_cipher_auth_decrypt() below. */
3055
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003056 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003058 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003059
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003060 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003061 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003062 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3064 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003065 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 data, rec->data_len,
3067 data, &olen,
3068 data + rec->data_len,
3069 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3074 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003075
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003076 return( ret );
3077 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003078 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003080 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003081 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003085 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3089#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003090 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003093 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003094
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 /*
Paul Bakker45829992013-01-03 14:52:21 +01003096 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003099 if( mbedtls_ssl_ver_geq(
3100 mbedtls_ssl_transform_get_minor_ver( transform ),
3101 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 {
3103 /* The ciphertext is prefixed with the CBC IV. */
3104 minlen += transform->ivlen;
3105 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003106#endif
Paul Bakker45829992013-01-03 14:52:21 +01003107
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 /* Size considerations:
3109 *
3110 * - The CBC cipher text must not be empty and hence
3111 * at least of size transform->ivlen.
3112 *
3113 * Together with the potential IV-prefix, this explains
3114 * the first of the two checks below.
3115 *
3116 * - The record must contain a MAC, either in plain or
3117 * encrypted, depending on whether Encrypt-then-MAC
3118 * is used or not.
3119 * - If it is, the message contains the IV-prefix,
3120 * the CBC ciphertext, and the MAC.
3121 * - If it is not, the padded plaintext, and hence
3122 * the CBC ciphertext, has at least length maclen + 1
3123 * because there is at least the padding length byte.
3124 *
3125 * As the CBC ciphertext is not empty, both cases give the
3126 * lower bound minlen + maclen + 1 on the record size, which
3127 * we test for in the second check below.
3128 */
3129 if( rec->data_len < minlen + transform->ivlen ||
3130 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133 "+ 1 ) ( + expl IV )", rec->data_len,
3134 transform->ivlen,
3135 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003137 }
3138
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003139 /*
3140 * Authenticate before decrypt if enabled
3141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003144 {
Hanno Becker992b6872017-11-09 18:57:39 +00003145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003148
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003149 /* Update data_len in tandem with add_data.
3150 *
3151 * The subtraction is safe because of the previous check
3152 * data_len >= minlen + maclen + 1.
3153 *
3154 * Afterwards, we know that data + data_len is followed by at
3155 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003156 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 *
3158 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003159 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003160 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003161
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003162 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003163 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3164 add_data_len );
3165 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3166 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3168 data, rec->data_len );
3169 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3170 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003171
Hanno Becker4c6876b2017-12-27 21:28:58 +00003172 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3173 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003174 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003175 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003176
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003177 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003178 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003183 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003184 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187
3188 /*
3189 * Check length sanity
3190 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003191
3192 /* We know from above that data_len > minlen >= 0,
3193 * so the following check in particular implies that
3194 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003195 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003198 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003200 }
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003203 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003204 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003205 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003206 if( mbedtls_ssl_ver_geq(
3207 mbedtls_ssl_transform_get_minor_ver( transform ),
3208 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003209 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003210 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003211 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003212
Hanno Becker4c6876b2017-12-27 21:28:58 +00003213 data += transform->ivlen;
3214 rec->data_offset += transform->ivlen;
3215 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003218
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003219 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3220
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3222 transform->iv_dec, transform->ivlen,
3223 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003226 return( ret );
3227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003228
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003229 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003234 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003237 if( mbedtls_ssl_ver_lt(
3238 mbedtls_ssl_transform_get_minor_ver( transform ),
3239 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003240 {
3241 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003242 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3243 * records is equivalent to CBC decryption of the concatenation
3244 * of the records; in other words, IVs are maintained across
3245 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003246 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003247 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003248 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Hanno Becker4c6876b2017-12-27 21:28:58 +00003252 /* Safe since data_len >= minlen + maclen + 1, so after having
3253 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003254 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3255 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003257
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 if( auth_done == 1 )
3259 {
3260 correct *= ( rec->data_len >= padlen + 1 );
3261 padlen *= ( rec->data_len >= padlen + 1 );
3262 }
3263 else
Paul Bakker45829992013-01-03 14:52:21 +01003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 if( rec->data_len < transform->maclen + padlen + 1 )
3267 {
3268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3269 rec->data_len,
3270 transform->maclen,
3271 padlen + 1 ) );
3272 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003273#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274
3275 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3276 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker4c6876b2017-12-27 21:28:58 +00003279 padlen++;
3280
3281 /* Regardless of the validity of the padding,
3282 * we have data_len >= padlen here. */
3283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003285 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3286 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_SSL_DEBUG_ALL)
3291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003292 "should be no more than %d",
3293 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003294#endif
Paul Bakker45829992013-01-03 14:52:21 +01003295 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 }
3297 }
3298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3300#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3301 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003302 if( mbedtls_ssl_ver_gt(
3303 mbedtls_ssl_transform_get_minor_ver( transform ),
3304 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 /* The padding check involves a series of up to 256
3307 * consecutive memory reads at the end of the record
3308 * plaintext buffer. In order to hide the length and
3309 * validity of the padding, always perform exactly
3310 * `min(256,plaintext_len)` reads (but take into account
3311 * only the last `padlen` bytes for the padding check). */
3312 size_t pad_count = 0;
3313 size_t real_count = 0;
3314 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003315
Hanno Becker4c6876b2017-12-27 21:28:58 +00003316 /* Index of first padding byte; it has been ensured above
3317 * that the subtraction is safe. */
3318 size_t const padding_idx = rec->data_len - padlen;
3319 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3320 size_t const start_idx = rec->data_len - num_checks;
3321 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003322
Hanno Becker4c6876b2017-12-27 21:28:58 +00003323 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003324 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003325 real_count |= ( idx >= padding_idx );
3326 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003327 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003328 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003331 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003333#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003334 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3338 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003342 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003343
Hanno Becker4c6876b2017-12-27 21:28:58 +00003344 /* If the padding was found to be invalid, padlen == 0
3345 * and the subtraction is safe. If the padding was found valid,
3346 * padlen hasn't been changed and the previous assertion
3347 * data_len >= padlen still holds. */
3348 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003352 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003358#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003360 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
3363 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003364 * Authenticate if not done yet.
3365 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003367#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003368 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003369 {
Hanno Becker992b6872017-11-09 18:57:39 +00003370 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003371
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 /* If the initial value of padlen was such that
3373 * data_len < maclen + padlen + 1, then padlen
3374 * got reset to 1, and the initial check
3375 * data_len >= minlen + maclen + 1
3376 * guarantees that at this point we still
3377 * have at least data_len >= maclen.
3378 *
3379 * If the initial value of padlen was such that
3380 * data_len >= maclen + padlen + 1, then we have
3381 * subtracted either padlen + 1 (if the padding was correct)
3382 * or 0 (if the padding was incorrect) since then,
3383 * hence data_len >= maclen in any case.
3384 */
3385 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003386 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003389 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3390 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003391 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003392 ssl_mac( &transform->md_ctx_dec,
3393 transform->mac_dec,
3394 data, rec->data_len,
3395 rec->ctr, rec->type,
3396 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 }
3398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003402 if( mbedtls_ssl_ver_gt(
3403 mbedtls_ssl_transform_get_minor_ver( transform ),
3404 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003405 {
3406 /*
3407 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003408 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 *
3410 * Known timing attacks:
3411 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3412 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003413 * To compensate for different timings for the MAC calculation
3414 * depending on how much padding was removed (which is determined
3415 * by padlen), process extra_run more blocks through the hash
3416 * function.
3417 *
3418 * The formula in the paper is
3419 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3420 * where L1 is the size of the header plus the decrypted message
3421 * plus CBC padding and L2 is the size of the header plus the
3422 * decrypted message. This is for an underlying hash function
3423 * with 64-byte blocks.
3424 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3425 * correctly. We round down instead of up, so -56 is the correct
3426 * value for our calculations instead of -55.
3427 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003428 * Repeat the formula rather than defining a block_size variable.
3429 * This avoids requiring division by a variable at runtime
3430 * (which would be marginally less efficient and would require
3431 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003432 */
3433 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003434 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003435
3436 /*
3437 * The next two sizes are the minimum and maximum values of
3438 * in_msglen over all padlen values.
3439 *
3440 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003441 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442 *
3443 * Note that max_len + maclen is never more than the buffer
3444 * length, as we previously did in_msglen -= maclen too.
3445 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003446 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003447 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3448
Hanno Becker4c6876b2017-12-27 21:28:58 +00003449 memset( tmp, 0, sizeof( tmp ) );
3450
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003451 switch( mbedtls_md_get_type(
3452 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003453 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003454#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3455 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003456 case MBEDTLS_MD_MD5:
3457 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003458 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003459 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003460 extra_run =
3461 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3462 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003463 break;
3464#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003465#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003466 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003468 extra_run =
3469 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3470 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 break;
3472#endif
3473 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3476 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003477
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003478 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003479
Hanno Beckere83efe62019-04-29 13:52:53 +01003480 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3481 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003482 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3483 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003484 /* Make sure we access everything even when padlen > 0. This
3485 * makes the synchronisation requirements for just-in-time
3486 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003487 ssl_read_memory( data + rec->data_len, padlen );
3488 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003489
3490 /* Call mbedtls_md_process at least once due to cache attacks
3491 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003492 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003493 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003494
Hanno Becker4c6876b2017-12-27 21:28:58 +00003495 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496
3497 /* Make sure we access all the memory that could contain the MAC,
3498 * before we check it in the next code block. This makes the
3499 * synchronisation requirements for just-in-time Prime+Probe
3500 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003501 ssl_read_memory( data + min_len,
3502 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003503 }
3504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3506 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003512#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3514 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003517 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003518 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#if defined(MBEDTLS_SSL_DEBUG_ALL)
3521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003522#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003523 correct = 0;
3524 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003525 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003526 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003527
3528 /*
3529 * Finally check the correct flag
3530 */
3531 if( correct == 0 )
3532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003533#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003534
3535 /* Make extra sure authentication was performed, exactly once */
3536 if( auth_done != 1 )
3537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Beckera5a2b082019-05-15 14:03:01 +01003542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003543 if( rec->cid_len != 0 )
3544 {
3545 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3546 &rec->type );
3547 if( ret != 0 )
3548 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3549 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 return( 0 );
3555}
3556
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003557#undef MAC_NONE
3558#undef MAC_PLAINTEXT
3559#undef MAC_CIPHERTEXT
3560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562/*
3563 * Compression/decompression functions
3564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003566{
3567 int ret;
3568 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003569 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003571 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003575 if( len_pre == 0 )
3576 return( 0 );
3577
Teppo Järvelin91d79382019-10-02 09:09:31 +03003578 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581 ssl->out_msglen ) );
3582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584 ssl->out_msg, ssl->out_msglen );
3585
Paul Bakker48916f92012-09-16 19:57:18 +00003586 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3587 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3588 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003589 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Paul Bakker48916f92012-09-16 19:57:18 +00003591 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592 if( ret != Z_OK )
3593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3595 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596 }
3597
Angus Grattond8213d02016-05-25 20:56:48 +10003598 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003599 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 ssl->out_msglen ) );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003605 ssl->out_msg, ssl->out_msglen );
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608
3609 return( 0 );
3610}
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003613{
3614 int ret;
3615 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003616 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003618 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003621
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003622 if( len_pre == 0 )
3623 return( 0 );
3624
Teppo Järvelin91d79382019-10-02 09:09:31 +03003625 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628 ssl->in_msglen ) );
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631 ssl->in_msg, ssl->in_msglen );
3632
Paul Bakker48916f92012-09-16 19:57:18 +00003633 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3634 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3635 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003636 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003637 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638
Paul Bakker48916f92012-09-16 19:57:18 +00003639 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 if( ret != Z_OK )
3641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3643 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003644 }
3645
Angus Grattond8213d02016-05-25 20:56:48 +10003646 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003647 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650 ssl->in_msglen ) );
3651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003653 ssl->in_msg, ssl->in_msglen );
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
3657 return( 0 );
3658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3662static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
3665static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003666{
3667 /* If renegotiation is not enforced, retransmit until we would reach max
3668 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003669 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003670 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003671 uint32_t ratio =
3672 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3673 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674 unsigned char doublings = 1;
3675
3676 while( ratio != 0 )
3677 {
3678 ++doublings;
3679 ratio >>= 1;
3680 }
3681
3682 if( ++ssl->renego_records_seen > doublings )
3683 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003685 return( 0 );
3686 }
3687 }
3688
3689 return( ssl_write_hello_request( ssl ) );
3690}
3691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003693
Paul Bakker5121ce52009-01-03 21:22:43 +00003694/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003695 * Fill the input message buffer by appending data to it.
3696 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 *
3698 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3699 * available (from this read and/or a previous one). Otherwise, an error code
3700 * is returned (possibly EOF or WANT_READ).
3701 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003702 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3703 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3704 * since we always read a whole datagram at once.
3705 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003706 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003707 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003710{
Paul Bakker23986e52011-04-24 08:57:21 +00003711 int ret;
3712 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Hanno Beckera58a8962019-06-13 16:11:15 +01003716 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3717 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003720 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003722 }
3723
Angus Grattond8213d02016-05-25 20:56:48 +10003724 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003728 }
3729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003733 uint32_t timeout;
3734
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003735 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003736 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3737 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3740 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3741 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3742 }
3743
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003744 /*
3745 * The point is, we need to always read a full datagram at once, so we
3746 * sometimes read more then requested, and handle the additional data.
3747 * It could be the rest of the current record (while fetching the
3748 * header) and/or some other records in the same datagram.
3749 */
3750
3751 /*
3752 * Move to the next record in the already read datagram if applicable
3753 */
3754 if( ssl->next_record_offset != 0 )
3755 {
3756 if( ssl->in_left < ssl->next_record_offset )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003760 }
3761
3762 ssl->in_left -= ssl->next_record_offset;
3763
3764 if( ssl->in_left != 0 )
3765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003767 ssl->next_record_offset ) );
3768 memmove( ssl->in_hdr,
3769 ssl->in_hdr + ssl->next_record_offset,
3770 ssl->in_left );
3771 }
3772
3773 ssl->next_record_offset = 0;
3774 }
3775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003778
3779 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003780 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 */
3782 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003786 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003787
3788 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003789 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790 * are not at the beginning of a new record, the caller did something
3791 * wrong.
3792 */
3793 if( ssl->in_left != 0 )
3794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3796 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 }
3798
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003799 /*
3800 * Don't even try to read if time's out already.
3801 * This avoids by-passing the timer when repeatedly receiving messages
3802 * that will end up being dropped.
3803 */
3804 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003807 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003808 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003809 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003810 {
Angus Grattond8213d02016-05-25 20:56:48 +10003811 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003814 timeout = ssl->handshake->retransmit_timeout;
3815 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003816 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003819
Hanno Beckera58a8962019-06-13 16:11:15 +01003820 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3821 {
3822 ret = mbedtls_ssl_get_recv_timeout( ssl )
3823 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3824 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003825 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003826 {
3827 ret = mbedtls_ssl_get_recv( ssl )
3828 ( ssl->p_bio, ssl->in_hdr, len );
3829 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832
3833 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835 }
3836
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003837 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003843 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003844 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003847 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003848 }
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003853 return( ret );
3854 }
3855
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003856 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003859 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3860 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003862 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003863 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003866 return( ret );
3867 }
3868
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003869 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003870 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003872 }
3873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 if( ret < 0 )
3875 return( ret );
3876
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003877 ssl->in_left = ret;
3878 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003879 MBEDTLS_SSL_TRANSPORT_ELSE
3880#endif /* MBEDTLS_SSL_PROTO_DTLS */
3881#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003884 ssl->in_left, nb_want ) );
3885
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003886 while( ssl->in_left < nb_want )
3887 {
3888 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003889
3890 if( ssl_check_timer( ssl ) != 0 )
3891 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3892 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003893 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003894 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003895 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003896 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3897 ssl->in_hdr + ssl->in_left, len,
3898 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003899 }
3900 else
3901 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003902 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003903 ssl->in_hdr + ssl->in_left, len );
3904 }
3905 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003908 ssl->in_left, nb_want ) );
3909 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003910
3911 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003913
3914 if( ret < 0 )
3915 return( ret );
3916
mohammad160352aecb92018-03-28 23:41:40 -07003917 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003918 {
Darryl Green11999bb2018-03-13 15:22:58 +00003919 MBEDTLS_SSL_DEBUG_MSG( 1,
3920 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003921 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3923 }
3924
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003925 ssl->in_left += ret;
3926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003928#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931
3932 return( 0 );
3933}
3934
3935/*
3936 * Flush any data not yet written
3937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003940 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003941 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Hanno Beckera58a8962019-06-13 16:11:15 +01003945 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003948 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003950 }
3951
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003952 /* Avoid incrementing counter if data is flushed */
3953 if( ssl->out_left == 0 )
3954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003956 return( 0 );
3957 }
3958
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 while( ssl->out_left > 0 )
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003962 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Hanno Becker2b1e3542018-08-06 11:19:13 +01003964 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003965 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
3969 if( ret <= 0 )
3970 return( ret );
3971
mohammad160352aecb92018-03-28 23:41:40 -07003972 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003973 {
Darryl Green11999bb2018-03-13 15:22:58 +00003974 MBEDTLS_SSL_DEBUG_MSG( 1,
3975 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003976 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3978 }
3979
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 ssl->out_left -= ret;
3981 }
3982
Hanno Becker2b1e3542018-08-06 11:19:13 +01003983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003984 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003985 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003986 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003987 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003988 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003989#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003990#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003991 {
3992 ssl->out_hdr = ssl->out_buf + 8;
3993 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003994#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
3999 return( 0 );
4000}
4001
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002/*
4003 * Functions to handle the DTLS retransmission state machine
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004006/*
4007 * Append current handshake message to current outgoing flight
4008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4013 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4014 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015
4016 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004017 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004021 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022 }
4023
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004024 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004025 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004029 }
4030
4031 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004032 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004035 msg->next = NULL;
4036
4037 /* Append to the current flight */
4038 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004039 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 else
4041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 while( cur->next != NULL )
4044 cur = cur->next;
4045 cur->next = msg;
4046 }
4047
Hanno Becker3b235902018-08-06 09:54:53 +01004048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 return( 0 );
4050}
4051
4052/*
4053 * Free the current flight of handshake messages
4054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 mbedtls_ssl_flight_item *cur = flight;
4058 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059
4060 while( cur != NULL )
4061 {
4062 next = cur->next;
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 mbedtls_free( cur->p );
4065 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 cur = next;
4068 }
4069}
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4072static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004073#endif
4074
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004076 * Swap transform_out and out_ctr with the alternative ones
4077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004081 unsigned char tmp_out_ctr[8];
4082
4083 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086 return;
4087 }
4088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004091 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092 tmp_transform = ssl->transform_out;
4093 ssl->transform_out = ssl->handshake->alt_transform_out;
4094 ssl->handshake->alt_transform_out = tmp_transform;
4095
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004096 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004097 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4098 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4099 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004100
4101 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004102 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4105 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004111 }
4112 }
4113#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004114}
4115
4116/*
4117 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004118 */
4119int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4120{
4121 int ret = 0;
4122
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4124
4125 ret = mbedtls_ssl_flight_transmit( ssl );
4126
4127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4128
4129 return( ret );
4130}
4131
4132/*
4133 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004134 *
4135 * Need to remember the current message in case flush_output returns
4136 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004137 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004138 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004139int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004140{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004145 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004147
4148 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004149 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004150 ssl_swap_epochs( ssl );
4151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004153 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154
4155 while( ssl->handshake->cur_msg != NULL )
4156 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004159
Hanno Beckere1dcb032018-08-17 16:47:58 +01004160 int const is_finished =
4161 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4162 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4163
Hanno Becker04da1892018-08-14 13:22:10 +01004164 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4165 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4166
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004167 /* Swap epochs before sending Finished: we can't do it after
4168 * sending ChangeCipherSpec, in case write returns WANT_READ.
4169 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004170 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004171 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004173 ssl_swap_epochs( ssl );
4174 }
4175
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176 ret = ssl_get_remaining_payload_in_datagram( ssl );
4177 if( ret < 0 )
4178 return( ret );
4179 max_frag_len = (size_t) ret;
4180
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004181 /* CCS is copied as is, while HS messages may need fragmentation */
4182 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4183 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004184 if( max_frag_len == 0 )
4185 {
4186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4187 return( ret );
4188
4189 continue;
4190 }
4191
Teppo Järvelin91d79382019-10-02 09:09:31 +03004192 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004193 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004194 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004195
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004196 /* Update position inside current message */
4197 ssl->handshake->cur_msg_p += cur->len;
4198 }
4199 else
4200 {
4201 const unsigned char * const p = ssl->handshake->cur_msg_p;
4202 const size_t hs_len = cur->len - 12;
4203 const size_t frag_off = p - ( cur->p + 12 );
4204 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004205 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004206
Hanno Beckere1dcb032018-08-17 16:47:58 +01004207 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004208 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004209 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004211
Hanno Becker67bc7c32018-08-06 11:33:50 +01004212 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4213 return( ret );
4214
4215 continue;
4216 }
4217 max_hs_frag_len = max_frag_len - 12;
4218
4219 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4220 max_hs_frag_len : rem_len;
4221
4222 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004223 {
4224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004225 (unsigned) cur_hs_frag_len,
4226 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004227 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004228
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004229 /* Messages are stored with handshake headers as if not fragmented,
4230 * copy beginning of headers then fill fragmentation fields.
4231 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004232 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004233
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004234 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4235 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4236 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004237
4238 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4239
Hanno Becker3f7b9732018-08-28 09:53:25 +01004240 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004241 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004243 ssl->out_msgtype = cur->type;
4244
4245 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004247 }
4248
4249 /* If done with the current message move to the next one if any */
4250 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4251 {
4252 if( cur->next != NULL )
4253 {
4254 ssl->handshake->cur_msg = cur->next;
4255 ssl->handshake->cur_msg_p = cur->next->p + 12;
4256 }
4257 else
4258 {
4259 ssl->handshake->cur_msg = NULL;
4260 ssl->handshake->cur_msg_p = NULL;
4261 }
4262 }
4263
4264 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004265 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004268 return( ret );
4269 }
4270 }
4271
Hanno Becker67bc7c32018-08-06 11:33:50 +01004272 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4273 return( ret );
4274
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004275 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004278 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004281 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4282 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004283
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004285
4286 return( 0 );
4287}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004288
4289/*
4290 * To be called when the last message of an incoming flight is received.
4291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004293{
4294 /* We won't need to resend that one any more */
4295 ssl_flight_free( ssl->handshake->flight );
4296 ssl->handshake->flight = NULL;
4297 ssl->handshake->cur_msg = NULL;
4298
4299 /* The next incoming flight will start with this msg_seq */
4300 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4301
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004302 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004303 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004304
Hanno Becker0271f962018-08-16 13:23:47 +01004305 /* Clear future message buffering structure. */
4306 ssl_buffering_free( ssl );
4307
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004308 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004309 ssl_set_timer( ssl, 0 );
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4312 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004315 }
4316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004318}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004319
4320/*
4321 * To be called when the last message of an outgoing flight is send.
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004324{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004325 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004326 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4329 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004332 }
4333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004339 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004340 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004341
4342/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004343 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004344 *
4345 * - fill in handshake headers
4346 * - update handshake checksum
4347 * - DTLS: save message for resending
4348 * - then pass to the record layer
4349 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004350 * DTLS: except for HelloRequest, messages are only queued, and will only be
4351 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004352 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004353 * Inputs:
4354 * - ssl->out_msglen: 4 + actual handshake message len
4355 * (4 is the size of handshake headers for TLS)
4356 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4357 * - ssl->out_msg + 4: the handshake message body
4358 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004359 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004360 * - ssl->out_msglen: the length of the record contents
4361 * (including handshake headers but excluding record headers)
4362 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004363 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004364int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004366 int ret;
4367 const size_t hs_len = ssl->out_msglen - 4;
4368 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4371
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004372 /*
4373 * Sanity checks
4374 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004375 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004376 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4377 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004378 /* In SSLv3, the client might send a NoCertificate alert. */
4379#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004380 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004381 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004382 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4383 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004384#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4385 {
4386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4388 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004391 /* Whenever we send anything different from a
4392 * HelloRequest we should be in a handshake - double check. */
4393 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4394 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004395 ssl->handshake == NULL )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004402 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004403 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004405 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004408 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004409#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004410
Hanno Beckerb50a2532018-08-06 11:52:54 +01004411 /* Double-check that we did not exceed the bounds
4412 * of the outgoing record buffer.
4413 * This should never fail as the various message
4414 * writing functions must obey the bounds of the
4415 * outgoing record buffer, but better be safe.
4416 *
4417 * Note: We deliberately do not check for the MTU or MFL here.
4418 */
4419 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4422 "size %u, maximum %u",
4423 (unsigned) ssl->out_msglen,
4424 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4426 }
4427
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004428 /*
4429 * Fill handshake headers
4430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004433 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004435 /*
4436 * DTLS has additional fields in the Handshake layer,
4437 * between the length field and the actual payload:
4438 * uint16 message_seq;
4439 * uint24 fragment_offset;
4440 * uint24 fragment_length;
4441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004443 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004444 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004445 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004446 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4449 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004450 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004451 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4453 }
4454
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004455 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004456 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004457
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004458 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004459 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004460 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004461 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4462 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004463 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004464 }
4465 else
4466 {
4467 ssl->out_msg[4] = 0;
4468 ssl->out_msg[5] = 0;
4469 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004470
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004471 /* Handshake hashes are computed without fragmentation,
4472 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004473 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004474 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004477
Hanno Becker0207e532018-08-28 10:28:28 +01004478 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004479 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004480 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 }
4482
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004483 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004485 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004486 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4487 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004488 {
4489 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004492 return( ret );
4493 }
4494 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004495 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004496#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004498 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004499 {
4500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4501 return( ret );
4502 }
4503 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004504
4505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4506
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004507 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004508}
4509
4510/*
4511 * Record layer functions
4512 */
4513
4514/*
4515 * Write current record.
4516 *
4517 * Uses:
4518 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4519 * - ssl->out_msglen: length of the record content (excl headers)
4520 * - ssl->out_msg: record content
4521 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004522int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004523{
4524 int ret, done = 0;
4525 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004526 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004527
4528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004531 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004533 {
4534 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004537 return( ret );
4538 }
4539
4540 len = ssl->out_msglen;
4541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4545 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 ret = mbedtls_ssl_hw_record_write( ssl );
4550 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004554 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004555
4556 if( ret == 0 )
4557 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004560 if( !done )
4561 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004562 unsigned i;
4563 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004564 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004565
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004566 /* Skip writing the record content type to after the encryption,
4567 * as it may change when using the CID extension. */
4568
Hanno Becker2881d802019-05-22 14:44:53 +01004569 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4570 mbedtls_ssl_get_minor_ver( ssl ),
4571 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004572
Teppo Järvelin91d79382019-10-02 09:09:31 +03004573 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004574 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575
Paul Bakker48916f92012-09-16 19:57:18 +00004576 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004577 {
Hanno Becker3307b532017-12-27 21:37:21 +00004578 mbedtls_record rec;
4579
4580 rec.buf = ssl->out_iv;
4581 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4582 ( ssl->out_iv - ssl->out_buf );
4583 rec.data_len = ssl->out_msglen;
4584 rec.data_offset = ssl->out_msg - rec.buf;
4585
Teppo Järvelin91d79382019-10-02 09:09:31 +03004586 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004587 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4588 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004589 ssl->conf->transport, rec.ver );
4590 rec.type = ssl->out_msgtype;
4591
Hanno Beckera5a2b082019-05-15 14:03:01 +01004592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004593 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004594 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004595#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004596
Hanno Becker611a83b2018-01-03 14:27:32 +00004597 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004598 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004599 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004602 return( ret );
4603 }
4604
Hanno Becker3307b532017-12-27 21:37:21 +00004605 if( rec.data_offset != 0 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4609 }
4610
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004611 /* Update the record content type and CID. */
4612 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004614 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4615 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004616#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004617 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004618 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004619 encrypted_fi = 1;
4620 }
4621
4622 //Double check to ensure the encryption has been done
4623 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4624 {
4625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker05ef8352012-05-08 09:17:57 +00004626 }
4627
Hanno Becker43395762019-05-03 14:46:38 +01004628 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004629
4630#if defined(MBEDTLS_SSL_PROTO_DTLS)
4631 /* In case of DTLS, double-check that we don't exceed
4632 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004634 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004635 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004636 if( ret < 0 )
4637 return( ret );
4638
4639 if( protected_record_size > (size_t) ret )
4640 {
4641 /* Should never happen */
4642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4643 }
4644 }
4645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004646
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004647 /* Now write the potentially updated record content type. */
4648 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004651 "version = [%d:%d], msglen = %d",
4652 ssl->out_hdr[0], ssl->out_hdr[1],
4653 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004656 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004657
4658 ssl->out_left += protected_record_size;
4659 ssl->out_hdr += protected_record_size;
4660 ssl_update_out_pointers( ssl, ssl->transform_out );
4661
Hanno Becker04484622018-08-06 09:49:38 +01004662 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4663 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4664 break;
4665
4666 /* The loop goes to its end iff the counter is wrapping */
4667 if( i == ssl_ep_len( ssl ) )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4670 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004672 }
4673
Hanno Becker67bc7c32018-08-06 11:33:50 +01004674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004675 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004676 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004677 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004678 size_t remaining;
4679 ret = ssl_get_remaining_payload_in_datagram( ssl );
4680 if( ret < 0 )
4681 {
4682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4683 ret );
4684 return( ret );
4685 }
4686
4687 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004688 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004689 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004690 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004691 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004692 else
4693 {
Hanno Becker513815a2018-08-20 11:56:09 +01004694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004695 }
4696 }
4697#endif /* MBEDTLS_SSL_PROTO_DTLS */
4698
4699 if( ( flush == SSL_FORCE_FLUSH ) &&
4700 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 return( ret );
4704 }
4705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004707
4708 return( 0 );
4709}
4710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004712
4713static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4714{
4715 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004716 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4717 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004718 {
4719 return( 1 );
4720 }
4721 return( 0 );
4722}
Hanno Becker44650b72018-08-16 12:51:11 +01004723
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004724static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004725{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004726 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004727}
4728
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004729static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004730{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004731 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004732}
4733
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004734static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004735{
4736 uint32_t msg_len, frag_off, frag_len;
4737
4738 msg_len = ssl_get_hs_total_len( ssl );
4739 frag_off = ssl_get_hs_frag_off( ssl );
4740 frag_len = ssl_get_hs_frag_len( ssl );
4741
4742 if( frag_off > msg_len )
4743 return( -1 );
4744
4745 if( frag_len > msg_len - frag_off )
4746 return( -1 );
4747
4748 if( frag_len + 12 > ssl->in_msglen )
4749 return( -1 );
4750
4751 return( 0 );
4752}
4753
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004754/*
4755 * Mark bits in bitmask (used for DTLS HS reassembly)
4756 */
4757static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4758{
4759 unsigned int start_bits, end_bits;
4760
4761 start_bits = 8 - ( offset % 8 );
4762 if( start_bits != 8 )
4763 {
4764 size_t first_byte_idx = offset / 8;
4765
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004766 /* Special case */
4767 if( len <= start_bits )
4768 {
4769 for( ; len != 0; len-- )
4770 mask[first_byte_idx] |= 1 << ( start_bits - len );
4771
4772 /* Avoid potential issues with offset or len becoming invalid */
4773 return;
4774 }
4775
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004776 offset += start_bits; /* Now offset % 8 == 0 */
4777 len -= start_bits;
4778
4779 for( ; start_bits != 0; start_bits-- )
4780 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4781 }
4782
4783 end_bits = len % 8;
4784 if( end_bits != 0 )
4785 {
4786 size_t last_byte_idx = ( offset + len ) / 8;
4787
4788 len -= end_bits; /* Now len % 8 == 0 */
4789
4790 for( ; end_bits != 0; end_bits-- )
4791 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4792 }
4793
4794 memset( mask + offset / 8, 0xFF, len / 8 );
4795}
4796
4797/*
4798 * Check that bitmask is full
4799 */
4800static int ssl_bitmask_check( unsigned char *mask, size_t len )
4801{
4802 size_t i;
4803
4804 for( i = 0; i < len / 8; i++ )
4805 if( mask[i] != 0xFF )
4806 return( -1 );
4807
4808 for( i = 0; i < len % 8; i++ )
4809 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4810 return( -1 );
4811
4812 return( 0 );
4813}
4814
Hanno Becker56e205e2018-08-16 09:06:12 +01004815/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004816static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004817 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004818{
Hanno Becker56e205e2018-08-16 09:06:12 +01004819 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004820
Hanno Becker56e205e2018-08-16 09:06:12 +01004821 alloc_len = 12; /* Handshake header */
4822 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004823
Hanno Beckerd07df862018-08-16 09:14:58 +01004824 if( add_bitmap )
4825 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004826
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004827 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828}
Hanno Becker56e205e2018-08-16 09:06:12 +01004829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004831
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004832static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004833{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004834 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004835}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004836
Simon Butcher99000142016-10-13 17:21:01 +01004837int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004842 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004844 }
4845
Hanno Becker12555c62018-08-16 12:47:53 +01004846 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004850 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004853 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004854 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004855 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004856 unsigned int recv_msg_seq = (unsigned int)
4857 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004858
Hanno Becker44650b72018-08-16 12:51:11 +01004859 if( ssl_check_hs_header( ssl ) != 0 )
4860 {
4861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4862 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4863 }
4864
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004865 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004866 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4867 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4868 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4869 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004870 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004871 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4874 recv_msg_seq,
4875 ssl->handshake->in_msg_seq ) );
4876 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4877 }
4878
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004879 /* Retransmit only on last message from previous flight, to avoid
4880 * too many retransmissions.
4881 * Besides, No sane server ever retransmits HelloVerifyRequest */
4882 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004886 "message_seq = %d, start_of_flight = %d",
4887 recv_msg_seq,
4888 ssl->handshake->in_flight_start_seq ) );
4889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004893 return( ret );
4894 }
4895 }
4896 else
4897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004899 "message_seq = %d, expected = %d",
4900 recv_msg_seq,
4901 ssl->handshake->in_msg_seq ) );
4902 }
4903
Hanno Becker90333da2017-10-10 11:27:13 +01004904 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004905 }
4906 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004907
Hanno Becker6d97ef52018-08-16 13:09:04 +01004908 /* Message reassembly is handled alongside buffering of future
4909 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004910 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004911 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004912 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004915 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004916 }
4917 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004918 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004920#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004921 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* With TLS we don't handle fragmentation (for now) */
4923 if( ssl->in_msglen < ssl->in_hslen )
4924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4927 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004928 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004929#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004930
Simon Butcher99000142016-10-13 17:21:01 +01004931 return( 0 );
4932}
4933
4934void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4935{
Hanno Becker0271f962018-08-16 13:23:47 +01004936 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004937
Hanno Becker0271f962018-08-16 13:23:47 +01004938 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004939 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004940
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004941 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004944 ssl->handshake != NULL )
4945 {
Hanno Becker0271f962018-08-16 13:23:47 +01004946 unsigned offset;
4947 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004948
Hanno Becker0271f962018-08-16 13:23:47 +01004949 /* Increment handshake sequence number */
4950 hs->in_msg_seq++;
4951
4952 /*
4953 * Clear up handshake buffering and reassembly structure.
4954 */
4955
4956 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004957 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004958
4959 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004960 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4961 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004962 offset++, hs_buf++ )
4963 {
4964 *hs_buf = *(hs_buf + 1);
4965 }
4966
4967 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004968 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004969 }
4970#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004971}
4972
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974 * DTLS anti-replay: RFC 6347 4.1.2.6
4975 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004976 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4977 * Bit n is set iff record number in_window_top - n has been seen.
4978 *
4979 * Usually, in_window_top is the last record number seen and the lsb of
4980 * in_window is set. The only exception is the initial state (record number 0
4981 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4984static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004985{
4986 ssl->in_window_top = 0;
4987 ssl->in_window = 0;
4988}
4989
4990static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4991{
4992 return( ( (uint64_t) buf[0] << 40 ) |
4993 ( (uint64_t) buf[1] << 32 ) |
4994 ( (uint64_t) buf[2] << 24 ) |
4995 ( (uint64_t) buf[3] << 16 ) |
4996 ( (uint64_t) buf[4] << 8 ) |
4997 ( (uint64_t) buf[5] ) );
4998}
4999
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005000static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5001{
5002 int ret;
5003 unsigned char *original_in_ctr;
5004
5005 // save original in_ctr
5006 original_in_ctr = ssl->in_ctr;
5007
5008 // use counter from record
5009 ssl->in_ctr = record_in_ctr;
5010
5011 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5012
5013 // restore the counter
5014 ssl->in_ctr = original_in_ctr;
5015
5016 return ret;
5017}
5018
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005019/*
5020 * Return 0 if sequence number is acceptable, -1 otherwise
5021 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005022int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005023{
5024 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5025 uint64_t bit;
5026
Hanno Becker7f376f42019-06-12 16:20:48 +01005027 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5028 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5029 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005030 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005031 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005032
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005033 if( rec_seqnum > ssl->in_window_top )
5034 return( 0 );
5035
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005036 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005037
5038 if( bit >= 64 )
5039 return( -1 );
5040
5041 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5042 return( -1 );
5043
5044 return( 0 );
5045}
5046
5047/*
5048 * Update replay window on new validated record
5049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005051{
5052 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5053
Hanno Becker7f376f42019-06-12 16:20:48 +01005054 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5055 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5056 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005057 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005058 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005059
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005060 if( rec_seqnum > ssl->in_window_top )
5061 {
5062 /* Update window_top and the contents of the window */
5063 uint64_t shift = rec_seqnum - ssl->in_window_top;
5064
5065 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005066 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005067 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005068 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005069 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005070 ssl->in_window |= 1;
5071 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005072
5073 ssl->in_window_top = rec_seqnum;
5074 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005075 else
5076 {
5077 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005078 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079
5080 if( bit < 64 ) /* Always true, but be extra sure */
5081 ssl->in_window |= (uint64_t) 1 << bit;
5082 }
5083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005085
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005086#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005087/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005088static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5089
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005090/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005091 * Without any SSL context, check if a datagram looks like a ClientHello with
5092 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005093 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005094 *
5095 * - if cookie is valid, return 0
5096 * - if ClientHello looks superficially valid but cookie is not,
5097 * fill obuf and set olen, then
5098 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5099 * - otherwise return a specific error code
5100 */
5101static int ssl_check_dtls_clihlo_cookie(
5102 mbedtls_ssl_cookie_write_t *f_cookie_write,
5103 mbedtls_ssl_cookie_check_t *f_cookie_check,
5104 void *p_cookie,
5105 const unsigned char *cli_id, size_t cli_id_len,
5106 const unsigned char *in, size_t in_len,
5107 unsigned char *obuf, size_t buf_len, size_t *olen )
5108{
5109 size_t sid_len, cookie_len;
5110 unsigned char *p;
5111
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 /*
5113 * Structure of ClientHello with record and handshake headers,
5114 * and expected values. We don't need to check a lot, more checks will be
5115 * done when actually parsing the ClientHello - skipping those checks
5116 * avoids code duplication and does not make cookie forging any easier.
5117 *
5118 * 0-0 ContentType type; copied, must be handshake
5119 * 1-2 ProtocolVersion version; copied
5120 * 3-4 uint16 epoch; copied, must be 0
5121 * 5-10 uint48 sequence_number; copied
5122 * 11-12 uint16 length; (ignored)
5123 *
5124 * 13-13 HandshakeType msg_type; (ignored)
5125 * 14-16 uint24 length; (ignored)
5126 * 17-18 uint16 message_seq; copied
5127 * 19-21 uint24 fragment_offset; copied, must be 0
5128 * 22-24 uint24 fragment_length; (ignored)
5129 *
5130 * 25-26 ProtocolVersion client_version; (ignored)
5131 * 27-58 Random random; (ignored)
5132 * 59-xx SessionID session_id; 1 byte len + sid_len content
5133 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5134 * ...
5135 *
5136 * Minimum length is 61 bytes.
5137 */
5138 if( in_len < 61 ||
5139 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5140 in[3] != 0 || in[4] != 0 ||
5141 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5142 {
5143 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5144 }
5145
5146 sid_len = in[59];
5147 if( sid_len > in_len - 61 )
5148 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5149
5150 cookie_len = in[60 + sid_len];
5151 if( cookie_len > in_len - 60 )
5152 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5153
5154 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5155 cli_id, cli_id_len ) == 0 )
5156 {
5157 /* Valid cookie */
5158 return( 0 );
5159 }
5160
5161 /*
5162 * If we get here, we've got an invalid cookie, let's prepare HVR.
5163 *
5164 * 0-0 ContentType type; copied
5165 * 1-2 ProtocolVersion version; copied
5166 * 3-4 uint16 epoch; copied
5167 * 5-10 uint48 sequence_number; copied
5168 * 11-12 uint16 length; olen - 13
5169 *
5170 * 13-13 HandshakeType msg_type; hello_verify_request
5171 * 14-16 uint24 length; olen - 25
5172 * 17-18 uint16 message_seq; copied
5173 * 19-21 uint24 fragment_offset; copied
5174 * 22-24 uint24 fragment_length; olen - 25
5175 *
5176 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5177 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5178 *
5179 * Minimum length is 28.
5180 */
5181 if( buf_len < 28 )
5182 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5183
5184 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005185 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005186 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5187 obuf[25] = 0xfe;
5188 obuf[26] = 0xff;
5189
5190 /* Generate and write actual cookie */
5191 p = obuf + 28;
5192 if( f_cookie_write( p_cookie,
5193 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5194 {
5195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5196 }
5197
5198 *olen = p - obuf;
5199
5200 /* Go back and fill length fields */
5201 obuf[27] = (unsigned char)( *olen - 28 );
5202
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005203 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005204 obuf[22] = obuf[14];
5205 obuf[23] = obuf[15];
5206 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005207
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005208 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005209
5210 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5211}
5212
5213/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005214 * Handle possible client reconnect with the same UDP quadruplet
5215 * (RFC 6347 Section 4.2.8).
5216 *
5217 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5218 * that looks like a ClientHello.
5219 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005220 * - if the input looks like a ClientHello without cookies,
5221 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005222 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005223 * - if the input looks like a ClientHello with a valid cookie,
5224 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005225 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005226 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005227 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005228 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005229 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5230 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005231 */
5232static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5233{
5234 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005235 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005236
Hanno Becker87b56262019-07-10 14:37:41 +01005237 if( ssl->conf->f_cookie_write == NULL ||
5238 ssl->conf->f_cookie_check == NULL )
5239 {
5240 /* If we can't use cookies to verify reachability of the peer,
5241 * drop the record. */
5242 return( 0 );
5243 }
5244
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005245 ret = ssl_check_dtls_clihlo_cookie(
5246 ssl->conf->f_cookie_write,
5247 ssl->conf->f_cookie_check,
5248 ssl->conf->p_cookie,
5249 ssl->cli_id, ssl->cli_id_len,
5250 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005251 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005252
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5254
5255 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005256 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005257 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005258 * If the error is permanent we'll catch it later,
5259 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005260 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005261 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005262 }
5263
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005264 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005265 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005266 /* Got a valid cookie, partially reset context */
5267 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5268 {
5269 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5270 return( ret );
5271 }
5272
5273 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005274 }
5275
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005276 return( ret );
5277}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005278#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005279
Hanno Becker46483f12019-05-03 13:25:54 +01005280static int ssl_check_record_type( uint8_t record_type )
5281{
5282 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5283 record_type != MBEDTLS_SSL_MSG_ALERT &&
5284 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5285 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5286 {
5287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5288 }
5289
5290 return( 0 );
5291}
5292
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005293/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294 * ContentType type;
5295 * ProtocolVersion version;
5296 * uint16 epoch; // DTLS only
5297 * uint48 sequence_number; // DTLS only
5298 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005299 *
5300 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005301 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005302 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5303 *
5304 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005305 * 1. proceed with the record if this function returns 0
5306 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5307 * 3. return CLIENT_RECONNECT if this function return that value
5308 * 4. drop the whole datagram if this function returns anything else.
5309 * Point 2 is needed when the peer is resending, and we have already received
5310 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005311 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005312static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 unsigned char *buf,
5314 size_t len,
5315 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005317 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319 size_t const rec_hdr_type_offset = 0;
5320 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005321
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005322 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5323 rec_hdr_type_len;
5324 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005326 size_t const rec_hdr_ctr_len = 8;
5327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005328 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5330 rec_hdr_version_len;
5331
Hanno Beckera5a2b082019-05-15 14:03:01 +01005332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5334 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005335 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5337#endif /* MBEDTLS_SSL_PROTO_DTLS */
5338
5339 size_t rec_hdr_len_offset; /* To be determined */
5340 size_t const rec_hdr_len_len = 2;
5341
5342 /*
5343 * Check minimum lengths for record header.
5344 */
5345
5346#if defined(MBEDTLS_SSL_PROTO_DTLS)
5347 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5348 {
5349 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5350 }
5351 MBEDTLS_SSL_TRANSPORT_ELSE
5352#endif /* MBEDTLS_SSL_PROTO_DTLS */
5353#if defined(MBEDTLS_SSL_PROTO_TLS)
5354 {
5355 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5356 }
5357#endif /* MBEDTLS_SSL_PROTO_DTLS */
5358
5359 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5362 (unsigned) len,
5363 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5365 }
5366
5367 /*
5368 * Parse and validate record content type
5369 */
5370
5371 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005372
5373 /* Check record content type */
5374#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5375 rec->cid_len = 0;
5376
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5379 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005380 {
5381 /* Shift pointers to account for record header including CID
5382 * struct {
5383 * ContentType special_type = tls12_cid;
5384 * ProtocolVersion version;
5385 * uint16 epoch;
5386 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005387 * opaque cid[cid_length]; // Additional field compared to
5388 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005389 * uint16 length;
5390 * opaque enc_content[DTLSCiphertext.length];
5391 * } DTLSCiphertext;
5392 */
5393
5394 /* So far, we only support static CID lengths
5395 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005396 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5397 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005398
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005399 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005400 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5402 (unsigned) len,
5403 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005405 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005407 /* configured CID len is guaranteed at most 255, see
5408 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5409 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005410 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5411 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005412 }
5413 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005415 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005416 if( ssl_check_record_type( rec->type ) )
5417 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5419 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5421 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005422 }
5423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005424 /*
5425 * Parse and validate record version
5426 */
5427
Hanno Becker8061c6e2019-07-26 08:07:03 +01005428 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5429 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005430 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5431 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005432 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005433
Hanno Becker2881d802019-05-22 14:44:53 +01005434 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5437 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005440 if( mbedtls_ssl_ver_gt( minor_ver,
5441 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5444 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 }
5446
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005447 /*
5448 * Parse/Copy record sequence number.
5449 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005450
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005451#if defined(MBEDTLS_SSL_PROTO_DTLS)
5452 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5453 {
5454 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005455 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5456 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005457 rec_hdr_ctr_len );
5458 }
5459 MBEDTLS_SSL_TRANSPORT_ELSE
5460#endif /* MBEDTLS_SSL_PROTO_DTLS */
5461#if defined(MBEDTLS_SSL_PROTO_TLS)
5462 {
5463 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005464 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5465 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005466 }
5467#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005468
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005469 /*
5470 * Parse record length.
5471 */
5472
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005473 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005474 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5476
Hanno Becker8b09b732019-05-08 12:03:28 +01005477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005478 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005479 rec->type,
5480 major_ver, minor_ver, rec->data_len ) );
5481
5482 rec->buf = buf;
5483 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005484
Hanno Beckerec014082019-07-26 08:20:27 +01005485 if( rec->data_len == 0 )
5486 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5487
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005488 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005489 * DTLS-related tests.
5490 * Check epoch before checking length constraint because
5491 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5492 * message gets duplicated before the corresponding Finished message,
5493 * the second ChangeCipherSpec should be discarded because it belongs
5494 * to an old epoch, but not because its length is shorter than
5495 * the minimum record length for packets using the new record transform.
5496 * Note that these two kinds of failures are handled differently,
5497 * as an unexpected record is silently skipped but an invalid
5498 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005499 */
5500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005501 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005502 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005503 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005504
Hanno Beckere0452772019-07-10 17:12:07 +01005505 /* Check that the datagram is large enough to contain a record
5506 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005507 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005508 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5510 (unsigned) len,
5511 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005512 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5513 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005514
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005515 /* Records from other, non-matching epochs are silently discarded.
5516 * (The case of same-port Client reconnects must be considered in
5517 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005518 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005519 {
5520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5521 "expected %d, received %d",
5522 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005523
5524 /* Records from the next epoch are considered for buffering
5525 * (concretely: early Finished messages). */
5526 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5527 {
5528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5530 }
5531
Hanno Becker87b56262019-07-10 14:37:41 +01005532 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005534#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005535 /* For records from the correct epoch, check whether their
5536 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005537 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5538 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005539 {
5540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5542 }
5543#endif
5544 }
5545#endif /* MBEDTLS_SSL_PROTO_DTLS */
5546
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005547 return( 0 );
5548}
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
Hanno Becker87b56262019-07-10 14:37:41 +01005550
5551#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5552static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5553{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005554 unsigned int rec_epoch = (unsigned int)
5555 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005556
5557 /*
5558 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5559 * access the first byte of record content (handshake type), as we
5560 * have an active transform (possibly iv_len != 0), so use the
5561 * fact that the record header len is 13 instead.
5562 */
5563 if( rec_epoch == 0 &&
5564 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5565 MBEDTLS_SSL_IS_SERVER &&
5566 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5567 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5568 ssl->in_left > 13 &&
5569 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5572 "from the same port" ) );
5573 return( ssl_handle_possible_reconnect( ssl ) );
5574 }
5575
5576 return( 0 );
5577}
5578#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5579
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005580/*
5581 * If applicable, decrypt (and decompress) record content
5582 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005583static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5584 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005585{
5586 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005589 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5592 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 ret = mbedtls_ssl_hw_record_read( ssl );
5597 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5600 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005601 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005602
5603 if( ret == 0 )
5604 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005607 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005609 unsigned char const old_msg_type = rec->type;
5610
Hanno Becker611a83b2018-01-03 14:27:32 +00005611 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005612 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005615
Hanno Beckera5a2b082019-05-15 14:03:01 +01005616#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005617 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005618 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5619 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005620 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005622 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005623 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005624#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005625
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 return( ret );
5627 }
5628
Hanno Becker106f3da2019-07-12 09:35:58 +01005629 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005630 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005631 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005632 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005634
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005636 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Hanno Beckera5a2b082019-05-15 14:03:01 +01005638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005639 /* We have already checked the record content type
5640 * in ssl_parse_record_header(), failing or silently
5641 * dropping the record in the case of an unknown type.
5642 *
5643 * Since with the use of CIDs, the record content type
5644 * might change during decryption, re-check the record
5645 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005646 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005647 {
5648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5650 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005651#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005652
Hanno Becker106f3da2019-07-12 09:35:58 +01005653 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005654 {
5655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005656 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005657 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005658 {
5659 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5662 }
5663#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5664
5665 ssl->nb_zero++;
5666
5667 /*
5668 * Three or more empty messages may be a DoS attack
5669 * (excessive CPU consumption).
5670 */
5671 if( ssl->nb_zero > 3 )
5672 {
5673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005674 "messages, possible DoS attack" ) );
5675 /* Treat the records as if they were not properly authenticated,
5676 * thereby failing the connection if we see more than allowed
5677 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005678 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5679 }
5680 }
5681 else
5682 ssl->nb_zero = 0;
5683
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005684 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5685#if defined(MBEDTLS_SSL_PROTO_TLS)
5686 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005687 {
5688 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005689 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005690 if( ++ssl->in_ctr[i - 1] != 0 )
5691 break;
5692
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005693 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005694 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005695 {
5696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5697 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5698 }
5699 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005700#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005701
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 }
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005705 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005707 {
5708 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005711 return( ret );
5712 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005717 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005720 }
5721#endif
5722
Hanno Beckerf0242852019-07-09 17:30:02 +01005723 /* Check actual (decrypted) record content length against
5724 * configured maximum. */
5725 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5728 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5729 }
5730
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005731 return( 0 );
5732}
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005735
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736/*
5737 * Read a record.
5738 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005739 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5740 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5741 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005742 */
Hanno Becker1097b342018-08-15 14:09:41 +01005743
5744/* Helper functions for mbedtls_ssl_read_record(). */
5745static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005746static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5747static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005748
Hanno Becker327c93b2018-08-15 13:56:18 +01005749int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005750 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005751{
5752 int ret;
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005755
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005756 if( ssl->keep_current_message == 0 )
5757 {
5758 do {
Simon Butcher99000142016-10-13 17:21:01 +01005759
Hanno Becker26994592018-08-15 14:14:59 +01005760 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005761 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005762 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005763
Hanno Beckere74d5562018-08-15 14:26:08 +01005764 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005765 {
Hanno Becker40f50842018-08-15 14:48:01 +01005766#if defined(MBEDTLS_SSL_PROTO_DTLS)
5767 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005768
Hanno Becker40f50842018-08-15 14:48:01 +01005769 /* We only check for buffered messages if the
5770 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005771 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005772 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005773 {
Hanno Becker40f50842018-08-15 14:48:01 +01005774 if( ssl_load_buffered_message( ssl ) == 0 )
5775 have_buffered = 1;
5776 }
5777
5778 if( have_buffered == 0 )
5779#endif /* MBEDTLS_SSL_PROTO_DTLS */
5780 {
5781 ret = ssl_get_next_record( ssl );
5782 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5783 continue;
5784
5785 if( ret != 0 )
5786 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005787 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005788 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005789 return( ret );
5790 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005791 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005792 }
5793
5794 ret = mbedtls_ssl_handle_message_type( ssl );
5795
Hanno Becker40f50842018-08-15 14:48:01 +01005796#if defined(MBEDTLS_SSL_PROTO_DTLS)
5797 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5798 {
5799 /* Buffer future message */
5800 ret = ssl_buffer_message( ssl );
5801 if( ret != 0 )
5802 return( ret );
5803
5804 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5805 }
5806#endif /* MBEDTLS_SSL_PROTO_DTLS */
5807
Hanno Becker90333da2017-10-10 11:27:13 +01005808 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5809 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005810
5811 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005812 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005813 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005814 return( ret );
5815 }
5816
Hanno Becker327c93b2018-08-15 13:56:18 +01005817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005818 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005819 {
5820 mbedtls_ssl_update_handshake_status( ssl );
5821 }
Simon Butcher99000142016-10-13 17:21:01 +01005822 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005823 else
Simon Butcher99000142016-10-13 17:21:01 +01005824 {
Hanno Becker02f59072018-08-15 14:00:24 +01005825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005826 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005827 }
5828
5829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5830
5831 return( 0 );
5832}
5833
Hanno Becker40f50842018-08-15 14:48:01 +01005834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005835static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005836{
Hanno Becker40f50842018-08-15 14:48:01 +01005837 if( ssl->in_left > ssl->next_record_offset )
5838 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005839
Hanno Becker40f50842018-08-15 14:48:01 +01005840 return( 0 );
5841}
5842
5843static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5844{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005846 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005847 int ret = 0;
5848
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005849 if( hs == NULL )
5850 return( -1 );
5851
Hanno Beckere00ae372018-08-20 09:39:42 +01005852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5853
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005854 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5855 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5856 {
5857 /* Check if we have seen a ChangeCipherSpec before.
5858 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005859 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5862 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005863 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 }
5865
Hanno Becker39b8bc92018-08-28 17:17:13 +01005866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5868 ssl->in_msglen = 1;
5869 ssl->in_msg[0] = 1;
5870
5871 /* As long as they are equal, the exact value doesn't matter. */
5872 ssl->in_left = 0;
5873 ssl->next_record_offset = 0;
5874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005875 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 goto exit;
5877 }
Hanno Becker37f95322018-08-16 13:55:32 +01005878
Hanno Beckerb8f50142018-08-28 10:01:34 +01005879#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005880 /* Debug only */
5881 {
5882 unsigned offset;
5883 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5884 {
5885 hs_buf = &hs->buffering.hs[offset];
5886 if( hs_buf->is_valid == 1 )
5887 {
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5889 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005890 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005891 }
5892 }
5893 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005894#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005895
5896 /* Check if we have buffered and/or fully reassembled the
5897 * next handshake message. */
5898 hs_buf = &hs->buffering.hs[0];
5899 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5900 {
5901 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005902 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005903
5904 /* Double-check that we haven't accidentally buffered
5905 * a message that doesn't fit into the input buffer. */
5906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5910 }
5911
5912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5913 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5914 hs_buf->data, msg_len + 12 );
5915
5916 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5917 ssl->in_hslen = msg_len + 12;
5918 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005919 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005920
5921 ret = 0;
5922 goto exit;
5923 }
5924 else
5925 {
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5927 hs->in_msg_seq ) );
5928 }
5929
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005930 ret = -1;
5931
5932exit:
5933
5934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5935 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005936}
5937
Hanno Beckera02b0b42018-08-21 17:20:27 +01005938static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5939 size_t desired )
5940{
5941 int offset;
5942 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5944 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005945
Hanno Becker01315ea2018-08-21 17:22:17 +01005946 /* Get rid of future records epoch first, if such exist. */
5947 ssl_free_buffered_record( ssl );
5948
5949 /* Check if we have enough space available now. */
5950 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5951 hs->buffering.total_bytes_buffered ) )
5952 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005954 return( 0 );
5955 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956
Hanno Becker4f432ad2018-08-28 10:02:32 +01005957 /* We don't have enough space to buffer the next expected handshake
5958 * message. Remove buffers used for future messages to gain space,
5959 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005960 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5961 offset >= 0; offset-- )
5962 {
5963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5964 offset ) );
5965
Hanno Beckerb309b922018-08-23 13:18:05 +01005966 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005967
5968 /* Check if we have enough space available now. */
5969 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5970 hs->buffering.total_bytes_buffered ) )
5971 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005973 return( 0 );
5974 }
5975 }
5976
5977 return( -1 );
5978}
5979
Hanno Becker40f50842018-08-15 14:48:01 +01005980static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5981{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005982 int ret = 0;
5983 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5984
5985 if( hs == NULL )
5986 return( 0 );
5987
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5989
5990 switch( ssl->in_msgtype )
5991 {
5992 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005994
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005995 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005996 break;
5997
5998 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005999 {
6000 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006001 unsigned recv_msg_seq = (unsigned)
6002 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006003
Hanno Becker37f95322018-08-16 13:55:32 +01006004 mbedtls_ssl_hs_buffer *hs_buf;
6005 size_t msg_len = ssl->in_hslen - 12;
6006
6007 /* We should never receive an old handshake
6008 * message - double-check nonetheless. */
6009 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6010 {
6011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6013 }
6014
6015 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6016 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6017 {
6018 /* Silently ignore -- message too far in the future */
6019 MBEDTLS_SSL_DEBUG_MSG( 2,
6020 ( "Ignore future HS message with sequence number %u, "
6021 "buffering window %u - %u",
6022 recv_msg_seq, ssl->handshake->in_msg_seq,
6023 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6024
6025 goto exit;
6026 }
6027
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6029 recv_msg_seq, recv_msg_seq_offset ) );
6030
6031 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6032
6033 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006034 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006035 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006036 size_t reassembly_buf_sz;
6037
Hanno Becker37f95322018-08-16 13:55:32 +01006038 hs_buf->is_fragmented =
6039 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
6040
6041 /* We copy the message back into the input buffer
6042 * after reassembly, so check that it's not too large.
6043 * This is an implementation-specific limitation
6044 * and not one from the standard, hence it is not
6045 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006046 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006047 {
6048 /* Ignore message */
6049 goto exit;
6050 }
6051
Hanno Beckere0b150f2018-08-21 15:51:03 +01006052 /* Check if we have enough space to buffer the message. */
6053 if( hs->buffering.total_bytes_buffered >
6054 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6055 {
6056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6058 }
6059
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006060 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6061 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006062
6063 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6064 hs->buffering.total_bytes_buffered ) )
6065 {
6066 if( recv_msg_seq_offset > 0 )
6067 {
6068 /* If we can't buffer a future message because
6069 * of space limitations -- ignore. */
6070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
6071 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6072 (unsigned) hs->buffering.total_bytes_buffered ) );
6073 goto exit;
6074 }
Hanno Beckere1801392018-08-21 16:51:05 +01006075 else
6076 {
6077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
6078 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6079 (unsigned) hs->buffering.total_bytes_buffered ) );
6080 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006081
Hanno Beckera02b0b42018-08-21 17:20:27 +01006082 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006083 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
6085 (unsigned) msg_len,
6086 (unsigned) reassembly_buf_sz,
6087 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006088 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006089 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6090 goto exit;
6091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006092 }
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6095 msg_len ) );
6096
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006097 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6098 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006099 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006100 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006101 goto exit;
6102 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006103 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006104
6105 /* Prepare final header: copy msg_type, length and message_seq,
6106 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006107 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006108 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006109 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006110
6111 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006112
6113 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006114 }
6115 else
6116 {
6117 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006118 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) // use regular memcmp as msg type is public
Hanno Becker37f95322018-08-16 13:55:32 +01006119 {
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6121 /* Ignore */
6122 goto exit;
6123 }
6124 }
6125
Hanno Becker4422bbb2018-08-20 09:40:19 +01006126 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006127 {
6128 size_t frag_len, frag_off;
6129 unsigned char * const msg = hs_buf->data + 12;
6130
6131 /*
6132 * Check and copy current fragment
6133 */
6134
6135 /* Validation of header fields already done in
6136 * mbedtls_ssl_prepare_handshake_record(). */
6137 frag_off = ssl_get_hs_frag_off( ssl );
6138 frag_len = ssl_get_hs_frag_len( ssl );
6139
6140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6141 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006142 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006143
6144 if( hs_buf->is_fragmented )
6145 {
6146 unsigned char * const bitmask = msg + msg_len;
6147 ssl_bitmask_set( bitmask, frag_off, frag_len );
6148 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6149 msg_len ) == 0 );
6150 }
6151 else
6152 {
6153 hs_buf->is_complete = 1;
6154 }
6155
6156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6157 hs_buf->is_complete ? "" : "not yet " ) );
6158 }
6159
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006160 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006161 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006162
6163 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006164 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006165 break;
6166 }
6167
6168exit:
6169
6170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6171 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006172}
6173#endif /* MBEDTLS_SSL_PROTO_DTLS */
6174
Hanno Becker1097b342018-08-15 14:09:41 +01006175static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006176{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006178 * Consume last content-layer message and potentially
6179 * update in_msglen which keeps track of the contents'
6180 * consumption state.
6181 *
6182 * (1) Handshake messages:
6183 * Remove last handshake message, move content
6184 * and adapt in_msglen.
6185 *
6186 * (2) Alert messages:
6187 * Consume whole record content, in_msglen = 0.
6188 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006189 * (3) Change cipher spec:
6190 * Consume whole record content, in_msglen = 0.
6191 *
6192 * (4) Application data:
6193 * Don't do anything - the record layer provides
6194 * the application data as a stream transport
6195 * and consumes through mbedtls_ssl_read only.
6196 *
6197 */
6198
6199 /* Case (1): Handshake messages */
6200 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006202 /* Hard assertion to be sure that no application data
6203 * is in flight, as corrupting ssl->in_msglen during
6204 * ssl->in_offt != NULL is fatal. */
6205 if( ssl->in_offt != NULL )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6209 }
6210
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006211 /*
6212 * Get next Handshake message in the current record
6213 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006214
Hanno Becker4a810fb2017-05-24 16:27:30 +01006215 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006216 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006217 * current handshake content: If DTLS handshake
6218 * fragmentation is used, that's the fragment
6219 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006220 * size here is faulty and should be changed at
6221 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006222 * (2) While it doesn't seem to cause problems, one
6223 * has to be very careful not to assume that in_hslen
6224 * is always <= in_msglen in a sensible communication.
6225 * Again, it's wrong for DTLS handshake fragmentation.
6226 * The following check is therefore mandatory, and
6227 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006228 * Additionally, ssl->in_hslen might be arbitrarily out of
6229 * bounds after handling a DTLS message with an unexpected
6230 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006231 */
6232 if( ssl->in_hslen < ssl->in_msglen )
6233 {
6234 ssl->in_msglen -= ssl->in_hslen;
6235 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6236 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006237
Hanno Becker4a810fb2017-05-24 16:27:30 +01006238 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6239 ssl->in_msg, ssl->in_msglen );
6240 }
6241 else
6242 {
6243 ssl->in_msglen = 0;
6244 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006245
Hanno Becker4a810fb2017-05-24 16:27:30 +01006246 ssl->in_hslen = 0;
6247 }
6248 /* Case (4): Application data */
6249 else if( ssl->in_offt != NULL )
6250 {
6251 return( 0 );
6252 }
6253 /* Everything else (CCS & Alerts) */
6254 else
6255 {
6256 ssl->in_msglen = 0;
6257 }
6258
Hanno Becker1097b342018-08-15 14:09:41 +01006259 return( 0 );
6260}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006261
Hanno Beckere74d5562018-08-15 14:26:08 +01006262static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6263{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006264 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006265 return( 1 );
6266
6267 return( 0 );
6268}
6269
Hanno Becker5f066e72018-08-16 14:56:31 +01006270#if defined(MBEDTLS_SSL_PROTO_DTLS)
6271
6272static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6273{
6274 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6275 if( hs == NULL )
6276 return;
6277
Hanno Becker01315ea2018-08-21 17:22:17 +01006278 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006279 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006280 hs->buffering.total_bytes_buffered -=
6281 hs->buffering.future_record.len;
6282
6283 mbedtls_free( hs->buffering.future_record.data );
6284 hs->buffering.future_record.data = NULL;
6285 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006286}
6287
6288static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6289{
6290 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6291 unsigned char * rec;
6292 size_t rec_len;
6293 unsigned rec_epoch;
6294
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006295 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006296 return( 0 );
6297
6298 if( hs == NULL )
6299 return( 0 );
6300
Hanno Becker5f066e72018-08-16 14:56:31 +01006301 rec = hs->buffering.future_record.data;
6302 rec_len = hs->buffering.future_record.len;
6303 rec_epoch = hs->buffering.future_record.epoch;
6304
6305 if( rec == NULL )
6306 return( 0 );
6307
Hanno Becker4cb782d2018-08-20 11:19:05 +01006308 /* Only consider loading future records if the
6309 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006310 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006311 return( 0 );
6312
Hanno Becker5f066e72018-08-16 14:56:31 +01006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6314
6315 if( rec_epoch != ssl->in_epoch )
6316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6318 goto exit;
6319 }
6320
6321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6322
6323 /* Double-check that the record is not too large */
6324 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6325 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6329 }
6330
Teppo Järvelin91d79382019-10-02 09:09:31 +03006331 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 ssl->in_left = rec_len;
6333 ssl->next_record_offset = 0;
6334
6335 ssl_free_buffered_record( ssl );
6336
6337exit:
6338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6339 return( 0 );
6340}
6341
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006342static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6343 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006344{
6345 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006346
6347 /* Don't buffer future records outside handshakes. */
6348 if( hs == NULL )
6349 return( 0 );
6350
6351 /* Only buffer handshake records (we are only interested
6352 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006353 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006354 return( 0 );
6355
6356 /* Don't buffer more than one future epoch record. */
6357 if( hs->buffering.future_record.data != NULL )
6358 return( 0 );
6359
Hanno Becker01315ea2018-08-21 17:22:17 +01006360 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006361 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006362 hs->buffering.total_bytes_buffered ) )
6363 {
6364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006365 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006366 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006367 return( 0 );
6368 }
6369
Hanno Becker5f066e72018-08-16 14:56:31 +01006370 /* Buffer record */
6371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6372 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006373 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006374
6375 /* ssl_parse_record_header() only considers records
6376 * of the next epoch as candidates for buffering. */
6377 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006378 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006379
6380 hs->buffering.future_record.data =
6381 mbedtls_calloc( 1, hs->buffering.future_record.len );
6382 if( hs->buffering.future_record.data == NULL )
6383 {
6384 /* If we run out of RAM trying to buffer a
6385 * record from the next epoch, just ignore. */
6386 return( 0 );
6387 }
6388
Teppo Järvelin91d79382019-10-02 09:09:31 +03006389 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006390
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006391 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006392 return( 0 );
6393}
6394
6395#endif /* MBEDTLS_SSL_PROTO_DTLS */
6396
Hanno Beckere74d5562018-08-15 14:26:08 +01006397static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006398{
6399 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006400 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006401
Hanno Becker5f066e72018-08-16 14:56:31 +01006402#if defined(MBEDTLS_SSL_PROTO_DTLS)
6403 /* We might have buffered a future record; if so,
6404 * and if the epoch matches now, load it.
6405 * On success, this call will set ssl->in_left to
6406 * the length of the buffered record, so that
6407 * the calls to ssl_fetch_input() below will
6408 * essentially be no-ops. */
6409 ret = ssl_load_buffered_record( ssl );
6410 if( ret != 0 )
6411 return( ret );
6412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006413
Hanno Becker8b09b732019-05-08 12:03:28 +01006414 /* Ensure that we have enough space available for the default form
6415 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6416 * with no space for CIDs counted in). */
6417 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6418 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006421 return( ret );
6422 }
6423
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006424 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6425 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006428 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006429 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006430 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6431 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006432 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006433 if( ret != 0 )
6434 return( ret );
6435
6436 /* Fall through to handling of unexpected records */
6437 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6438 }
6439
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006440 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6441 {
Hanno Becker87b56262019-07-10 14:37:41 +01006442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006443 /* Reset in pointers to default state for TLS/DTLS records,
6444 * assuming no CID and no offset between record content and
6445 * record plaintext. */
6446 ssl_update_in_pointers( ssl );
6447
Hanno Becker69412452019-07-12 08:33:49 +01006448 /* Setup internal message pointers from record structure. */
6449 ssl->in_msgtype = rec.type;
6450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6451 ssl->in_len = ssl->in_cid + rec.cid_len;
6452#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006453 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006454 ssl->in_msglen = rec.data_len;
6455
Hanno Becker87b56262019-07-10 14:37:41 +01006456 ret = ssl_check_client_reconnect( ssl );
6457 if( ret != 0 )
6458 return( ret );
6459#endif
6460
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006461 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006462 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006463
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6465 "(header)" ) );
6466 }
6467 else
6468 {
6469 /* Skip invalid record and the rest of the datagram */
6470 ssl->next_record_offset = 0;
6471 ssl->in_left = 0;
6472
6473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6474 "(header)" ) );
6475 }
6476
6477 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006478 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006479 }
Hanno Becker87b56262019-07-10 14:37:41 +01006480 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006481#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006482 {
6483 return( ret );
6484 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006485 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006488 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006489 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006490 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006491 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006492 if( ssl->next_record_offset < ssl->in_left )
6493 {
6494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6495 }
6496 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006497 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006499#if defined(MBEDTLS_SSL_PROTO_TLS)
6500 {
Hanno Beckere0452772019-07-10 17:12:07 +01006501 /*
6502 * Fetch record contents from underlying transport.
6503 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006504 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006505 if( ret != 0 )
6506 {
6507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6508 return( ret );
6509 }
6510
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006511 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006512 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006513#endif /* MBEDTLS_SSL_PROTO_TLS */
6514
6515 /*
6516 * Decrypt record contents.
6517 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006518
Hanno Beckera89610a2019-07-11 13:07:45 +01006519 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006522 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006523 {
6524 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006525 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006526 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006527 /* Except when waiting for Finished as a bad mac here
6528 * probably means something went wrong in the handshake
6529 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6530 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6531 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6532 {
6533#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6534 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6535 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006536 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006537 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6538 }
6539#endif
6540 return( ret );
6541 }
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006544 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6545 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006549 }
6550#endif
6551
Hanno Becker4a810fb2017-05-24 16:27:30 +01006552 /* As above, invalid records cause
6553 * dismissal of the whole datagram. */
6554
6555 ssl->next_record_offset = 0;
6556 ssl->in_left = 0;
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006559 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006560 }
6561
6562 return( ret );
6563 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006564 MBEDTLS_SSL_TRANSPORT_ELSE
6565#endif /* MBEDTLS_SSL_PROTO_DTLS */
6566#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006567 {
6568 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6570 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006572 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006574 }
6575#endif
6576 return( ret );
6577 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006578#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006579 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006580
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006581
6582 /* Reset in pointers to default state for TLS/DTLS records,
6583 * assuming no CID and no offset between record content and
6584 * record plaintext. */
6585 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6587 ssl->in_len = ssl->in_cid + rec.cid_len;
6588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006589 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006590
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006591 /* The record content type may change during decryption,
6592 * so re-read it. */
6593 ssl->in_msgtype = rec.type;
6594 /* Also update the input buffer, because unfortunately
6595 * the server-side ssl_parse_client_hello() reparses the
6596 * record header when receiving a ClientHello initiating
6597 * a renegotiation. */
6598 ssl->in_hdr[0] = rec.type;
6599 ssl->in_msg = rec.buf + rec.data_offset;
6600 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006601 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006602
Simon Butcher99000142016-10-13 17:21:01 +01006603 return( 0 );
6604}
6605
6606int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6607{
6608 int ret;
6609
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006610 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006611 * Handle particular types of records
6612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 {
Simon Butcher99000142016-10-13 17:21:01 +01006615 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6616 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006617 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 }
6620
Hanno Beckere678eaa2018-08-21 14:57:46 +01006621 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006622 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006623 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006624 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6626 ssl->in_msglen ) );
6627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006628 }
6629
Hanno Beckere678eaa2018-08-21 14:57:46 +01006630 if( ssl->in_msg[0] != 1 )
6631 {
6632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6633 ssl->in_msg[0] ) );
6634 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6635 }
6636
6637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006638 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006639 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6640 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6641 {
6642 if( ssl->handshake == NULL )
6643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6645 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6646 }
6647
6648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6649 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6650 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006651#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006652 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006656 if( ssl->in_msglen != 2 )
6657 {
6658 /* Note: Standard allows for more than one 2 byte alert
6659 to be packed in a single message, but Mbed TLS doesn't
6660 currently support this. */
6661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6662 ssl->in_msglen ) );
6663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6664 }
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006667 ssl->in_msg[0], ssl->in_msg[1] ) );
6668
6669 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006670 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006675 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6680 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6683 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006685
6686#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6687 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6688 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6689 {
Hanno Becker90333da2017-10-10 11:27:13 +01006690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006691 /* Will be handled when trying to parse ServerHello */
6692 return( 0 );
6693 }
6694#endif
6695
6696#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006697 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006698 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6699 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006700 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6701 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6704 /* Will be handled in mbedtls_ssl_parse_certificate() */
6705 return( 0 );
6706 }
6707#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6708
6709 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006710 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 }
6712
Hanno Beckerc76c6192017-06-06 10:03:17 +01006713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006715 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006716 /* Drop unexpected ApplicationData records,
6717 * except at the beginning of renegotiations */
6718 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6719 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6720#if defined(MBEDTLS_SSL_RENEGOTIATION)
6721 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6722 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006723#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006724 )
6725 {
6726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6727 return( MBEDTLS_ERR_SSL_NON_FATAL );
6728 }
6729
6730 if( ssl->handshake != NULL &&
6731 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6732 {
6733 ssl_handshake_wrapup_free_hs_transform( ssl );
6734 }
6735 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006737
Paul Bakker5121ce52009-01-03 21:22:43 +00006738 return( 0 );
6739}
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006742{
6743 int ret;
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6747 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006748 {
6749 return( ret );
6750 }
6751
6752 return( 0 );
6753}
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006756 unsigned char level,
6757 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006758{
6759 int ret;
6760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006761 if( ssl == NULL || ssl->conf == NULL )
6762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006768 ssl->out_msglen = 2;
6769 ssl->out_msg[0] = level;
6770 ssl->out_msg[1] = message;
6771
Hanno Becker67bc7c32018-08-06 11:33:50 +01006772 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006775 return( ret );
6776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006778
6779 return( 0 );
6780}
6781
Hanno Becker17572472019-02-08 07:19:04 +00006782#if defined(MBEDTLS_X509_CRT_PARSE_C)
6783static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6784{
6785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6786 if( session->peer_cert != NULL )
6787 {
6788 mbedtls_x509_crt_free( session->peer_cert );
6789 mbedtls_free( session->peer_cert );
6790 session->peer_cert = NULL;
6791 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006792#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006793 if( session->peer_cert_digest != NULL )
6794 {
6795 /* Zeroization is not necessary. */
6796 mbedtls_free( session->peer_cert_digest );
6797 session->peer_cert_digest = NULL;
6798 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6799 session->peer_cert_digest_len = 0;
6800 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006801#else
6802 ((void) session);
6803#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006804}
6805#endif /* MBEDTLS_X509_CRT_PARSE_C */
6806
Paul Bakker5121ce52009-01-03 21:22:43 +00006807/*
6808 * Handshake functions
6809 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006810#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006811/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813{
Hanno Beckerdf645962019-06-26 13:02:22 +01006814 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6815 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Hanno Becker5097cba2019-02-05 13:36:46 +00006819 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006822 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6823 {
6824 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6825 }
6826 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6827 {
6828 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6829 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006830 else
6831 {
6832 ssl->state = MBEDTLS_SSL_INVALID;
6833 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006834 return( 0 );
6835 }
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006839}
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006842{
Hanno Beckerdf645962019-06-26 13:02:22 +01006843 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6844 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006847
Hanno Becker5097cba2019-02-05 13:36:46 +00006848 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006851 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6852 {
6853 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6854 }
6855 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6856 {
6857 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6858 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006859 else
6860 {
6861 ssl->state = MBEDTLS_SSL_INVALID;
6862 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006863 return( 0 );
6864 }
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006868}
Gilles Peskinef9828522017-05-03 12:28:43 +02006869
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006870#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006871/* Some certificate support -> implement write and parse */
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006876 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006878 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6879 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882
Hanno Becker5097cba2019-02-05 13:36:46 +00006883 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006886 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6887 {
6888 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6889 }
6890 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6891 {
6892 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6893 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006894 else
6895 {
6896 ssl->state = MBEDTLS_SSL_INVALID;
6897 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006898 return( 0 );
6899 }
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006902 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6903 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
6905 if( ssl->client_auth == 0 )
6906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006908 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6909 {
6910 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6911 }
6912 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6913 {
6914 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6915 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006916 else
6917 {
6918 ssl->state = MBEDTLS_SSL_INVALID;
6919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 return( 0 );
6921 }
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 /*
6925 * If using SSLv3 and got no cert, send an Alert message
6926 * (otherwise an empty Certificate message will be sent).
6927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006929 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006930 {
6931 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6933 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6934 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006937 goto write_msg;
6938 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#endif /* MBEDTLS_SSL_CLI_C */
6942#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006943 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6948 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 }
6950 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954
6955 /*
6956 * 0 . 0 handshake type
6957 * 1 . 3 handshake length
6958 * 4 . 6 length of all certs
6959 * 7 . 9 length of cert. 1
6960 * 10 . n-1 peer certificate
6961 * n . n+2 length of cert. 2
6962 * n+3 . ... upper level cert, etc.
6963 */
6964 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Paul Bakker29087132010-03-21 21:03:34 +00006967 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
6969 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006970 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006973 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006977 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
Teppo Järvelin91d79382019-10-02 09:09:31 +03006979 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 i += n; crt = crt->next;
6981 }
6982
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006983 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006984
6985 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6987 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006989#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006990write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Jarno Lamsa2b205162019-11-12 15:36:21 +02006993 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6994 {
6995 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6996 }
6997 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6998 {
6999 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7000 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007001 else
7002 {
7003 ssl->state = MBEDTLS_SSL_INVALID;
7004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007006 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009 return( ret );
7010 }
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Paul Bakkered27a042013-04-18 22:46:23 +02007014 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015}
7016
Hanno Becker285ff0c2019-01-31 07:44:03 +00007017#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007018
7019#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7021 unsigned char *crt_buf,
7022 size_t crt_buf_len )
7023{
7024 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7025
7026 if( peer_crt == NULL )
7027 return( -1 );
7028
7029 if( peer_crt->raw.len != crt_buf_len )
7030 return( -1 );
7031
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007032 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007033}
Hanno Becker5882dd02019-06-06 16:25:57 +01007034#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007035static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7036 unsigned char *crt_buf,
7037 size_t crt_buf_len )
7038{
7039 int ret;
7040 unsigned char const * const peer_cert_digest =
7041 ssl->session->peer_cert_digest;
7042 mbedtls_md_type_t const peer_cert_digest_type =
7043 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007044 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007045 mbedtls_md_info_from_type( peer_cert_digest_type );
7046 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7047 size_t digest_len;
7048
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007049 if( peer_cert_digest == NULL ||
7050 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7051 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007052 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007053 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007054
7055 digest_len = mbedtls_md_get_size( digest_info );
7056 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7057 return( -1 );
7058
7059 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7060 if( ret != 0 )
7061 return( -1 );
7062
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007063 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007064}
Hanno Becker5882dd02019-06-06 16:25:57 +01007065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007066#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007067
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007068/*
7069 * Once the certificate message is read, parse it into a cert chain and
7070 * perform basic checks, but leave actual verification to the caller
7071 */
Hanno Becker35e41772019-02-05 15:37:23 +00007072static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7073 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007075 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007076#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7077 int crt_cnt=0;
7078#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007079 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007080 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007085 mbedtls_ssl_pend_fatal_alert( ssl,
7086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 }
7089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7091 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007094 mbedtls_ssl_pend_fatal_alert( ssl,
7095 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 }
7098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007100
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007104 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007106 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007110 mbedtls_ssl_pend_fatal_alert( ssl,
7111 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Hanno Becker33c3dc82019-01-30 14:46:46 +00007115 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7116 i += 3;
7117
Hanno Becker33c3dc82019-01-30 14:46:46 +00007118 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 while( i < ssl->in_hslen )
7120 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007121 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007122 if ( i + 3 > ssl->in_hslen ) {
7123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007124 mbedtls_ssl_pend_fatal_alert( ssl,
7125 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007126 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7127 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007128 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7129 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 if( ssl->in_msg[i] != 0 )
7131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007133 mbedtls_ssl_pend_fatal_alert( ssl,
7134 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 }
7137
Hanno Becker33c3dc82019-01-30 14:46:46 +00007138 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007139 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 i += 3;
7141
7142 if( n < 128 || i + n > ssl->in_hslen )
7143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007145 mbedtls_ssl_pend_fatal_alert( ssl,
7146 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 }
7149
Hanno Becker33c3dc82019-01-30 14:46:46 +00007150 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007151#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7152 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007153 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7154 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007155 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007156 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007157 /* During client-side renegotiation, check that the server's
7158 * end-CRTs hasn't changed compared to the initial handshake,
7159 * mitigating the triple handshake attack. On success, reuse
7160 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7162 if( ssl_check_peer_crt_unchanged( ssl,
7163 &ssl->in_msg[i],
7164 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007165 {
Hanno Becker35e41772019-02-05 15:37:23 +00007166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007167 mbedtls_ssl_pend_fatal_alert( ssl,
7168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007169 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007170 }
Hanno Becker35e41772019-02-05 15:37:23 +00007171
7172 /* Now we can safely free the original chain. */
7173 ssl_clear_peer_cert( ssl->session );
7174 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007175#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7176
Hanno Becker33c3dc82019-01-30 14:46:46 +00007177 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007178#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007179 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007180#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007181 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007182 * it in-place from the input buffer instead of making a copy. */
7183 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7184#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007185 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007187 case 0: /*ok*/
7188 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7189 /* Ignore certificate with an unknown algorithm: maybe a
7190 prior certificate was already trusted. */
7191 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007192
Hanno Becker33c3dc82019-01-30 14:46:46 +00007193 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7194 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7195 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007196
Hanno Becker33c3dc82019-01-30 14:46:46 +00007197 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7198 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7199 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007200
Hanno Becker33c3dc82019-01-30 14:46:46 +00007201 default:
7202 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7203 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007204 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007205 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007207 }
7208
7209 i += n;
7210 }
7211
Hanno Becker35e41772019-02-05 15:37:23 +00007212 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007213 return( 0 );
7214}
7215
Hanno Beckerb8a08572019-02-05 12:49:06 +00007216#if defined(MBEDTLS_SSL_SRV_C)
7217static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7218{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007219 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007220 return( -1 );
7221
7222#if defined(MBEDTLS_SSL_PROTO_SSL3)
7223 /*
7224 * Check if the client sent an empty certificate
7225 */
Hanno Becker2881d802019-05-22 14:44:53 +01007226 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007227 {
7228 if( ssl->in_msglen == 2 &&
7229 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7230 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7231 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7232 {
7233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7234 return( 0 );
7235 }
7236
7237 return( -1 );
7238 }
7239#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7240
7241#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7242 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7243 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7244 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7245 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007246 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) // use regular memcmp as comparing public data
Hanno Beckerb8a08572019-02-05 12:49:06 +00007247 {
7248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7249 return( 0 );
7250 }
7251
Hanno Beckerb8a08572019-02-05 12:49:06 +00007252#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7253 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007254
7255 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007256}
7257#endif /* MBEDTLS_SSL_SRV_C */
7258
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007259/* Check if a certificate message is expected.
7260 * Return either
7261 * - SSL_CERTIFICATE_EXPECTED, or
7262 * - SSL_CERTIFICATE_SKIP
7263 * indicating whether a Certificate message is expected or not.
7264 */
7265#define SSL_CERTIFICATE_EXPECTED 0
7266#define SSL_CERTIFICATE_SKIP 1
7267static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7268 int authmode )
7269{
Hanno Becker473f98f2019-06-26 10:27:32 +01007270 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007271 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007272
7273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7274 return( SSL_CERTIFICATE_SKIP );
7275
7276#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007277 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007278 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007279 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7280 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7281 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007282 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007283 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007284
7285 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7286 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007287 ssl->session_negotiate->verify_result =
7288 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7289 return( SSL_CERTIFICATE_SKIP );
7290 }
7291 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007292#else
7293 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007294#endif /* MBEDTLS_SSL_SRV_C */
7295
7296 return( SSL_CERTIFICATE_EXPECTED );
7297}
7298
Hanno Becker3cf50612019-02-05 14:36:34 +00007299static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007300 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007301 mbedtls_x509_crt *chain,
7302 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007303{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007304 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7305 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
7306 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007307 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007308 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007309 mbedtls_x509_crt *ca_chain;
7310 mbedtls_x509_crl *ca_crl;
7311
7312 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007313 {
7314 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Becker3cf50612019-02-05 14:36:34 +00007315 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007316 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007317
Jarno Lamsae1621d42019-12-19 08:58:56 +02007318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007319#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7320 if( ssl->handshake->sni_ca_chain != NULL )
7321 {
7322 ca_chain = ssl->handshake->sni_ca_chain;
7323 ca_crl = ssl->handshake->sni_ca_crl;
7324 }
7325 else
7326#endif
7327 {
7328 ca_chain = ssl->conf->ca_chain;
7329 ca_crl = ssl->conf->ca_crl;
7330 }
7331
7332 /*
7333 * Main check: verify certificate
7334 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007335 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007336 chain,
7337 ca_chain, ca_crl,
7338 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007339#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007340 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007341#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007342 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007343#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7344 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7345#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7346 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007347
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007348 if( verify_ret == 0 )
7349 {
7350 mbedtls_platform_enforce_volatile_reads();
7351 if( verify_ret == 0 )
7352 {
7353 flow_counter++;
7354 }
7355 else
7356 {
7357 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7358 }
7359 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007360 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007361 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007362 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007363 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007364 }
7365
7366#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007367 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007368 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7369#endif
7370
7371 /*
7372 * Secondary checks: always done, but change 'ret' only if it was 0
7373 */
7374
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007375#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007376 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007377#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007378 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007379#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007380 mbedtls_pk_context *pk;
7381 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7382 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007383 {
7384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007385 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007386 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007387
7388 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007389 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007390 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007391 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007392 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007393
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007394 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007395#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007396
7397 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007398 {
7399 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7400
7401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007402 if( verify_ret == 0 )
7403 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007404 flow_counter++;
7405 }
7406 if( ret == 0 )
7407 {
7408 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007409 }
7410 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007411#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007412
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007413 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007414 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007415 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7416 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007417 &ssl->session_negotiate->verify_result );
7418 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007419 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007420 flow_counter++;
7421 }
7422 else
7423 {
7424 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007426 if( verify_ret == 0 )
7427 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007428 }
7429
7430 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7431 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7432 * with details encoded in the verification flags. All other kinds
7433 * of error codes, including those from the user provided f_vrfy
7434 * functions, are treated as fatal and lead to a failure of
7435 * ssl_parse_certificate even if verification was optional. */
7436 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007437 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7438 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007439 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007440 mbedtls_platform_enforce_volatile_reads();
7441 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7442 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7443 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7444 {
7445 verify_ret = 0;
7446 flow_counter++;
7447 }
7448 else
7449 {
7450 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7451 }
7452 } else {
7453 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007454 }
7455
7456 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7457 {
7458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007459 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007460 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007461 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007462 else
7463 {
7464 flow_counter++;
7465 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007466
Hanno Becker8c13ee62019-02-26 16:48:17 +00007467 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007468 {
7469 uint8_t alert;
7470
7471 /* The certificate may have been rejected for several reasons.
7472 Pick one and send the corresponding alert. Which alert to send
7473 may be a subject of debate in some cases. */
7474 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7475 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7476 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7477 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7478 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7479 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7480 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7481 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7482 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7483 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7484 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7485 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7486 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7487 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7488 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7489 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7490 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7491 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7492 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7493 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7494 else
7495 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007496 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007497 }
7498
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007499 if( verify_ret == 0 &&
7500#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7501 flow_counter == 5 )
7502#else
7503 flow_counter == 4 )
7504#endif
7505 {
7506 mbedtls_platform_enforce_volatile_reads();
7507 if( verify_ret == 0 &&
7508#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7509 flow_counter == 5 )
7510#else
7511 flow_counter == 4 )
7512#endif
7513 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007515 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7516 }
7517 else
7518 {
7519 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7520 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007521 } else {
7522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007523 }
7524
Hanno Becker3cf50612019-02-05 14:36:34 +00007525#if defined(MBEDTLS_DEBUG_C)
7526 if( ssl->session_negotiate->verify_result != 0 )
7527 {
7528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7529 ssl->session_negotiate->verify_result ) );
7530 }
7531 else
7532 {
7533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7534 }
7535#endif /* MBEDTLS_DEBUG_C */
7536
Hanno Becker8c13ee62019-02-26 16:48:17 +00007537 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007538}
7539
Hanno Becker34106f62019-02-08 14:59:05 +00007540#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007541
7542#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007543static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7544 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007545{
7546 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007547 /* Remember digest of the peer's end-CRT. */
7548 ssl->session_negotiate->peer_cert_digest =
7549 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7550 if( ssl->session_negotiate->peer_cert_digest == NULL )
7551 {
7552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7553 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007554 mbedtls_ssl_pend_fatal_alert( ssl,
7555 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007556
7557 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7558 }
7559
7560 ret = mbedtls_md( mbedtls_md_info_from_type(
7561 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7562 start, len,
7563 ssl->session_negotiate->peer_cert_digest );
7564
7565 ssl->session_negotiate->peer_cert_digest_type =
7566 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7567 ssl->session_negotiate->peer_cert_digest_len =
7568 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7569
7570 return( ret );
7571}
Hanno Becker5882dd02019-06-06 16:25:57 +01007572#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007573
7574static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7575 unsigned char *start, size_t len )
7576{
7577 unsigned char *end = start + len;
7578 int ret;
7579
7580 /* Make a copy of the peer's raw public key. */
7581 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7582 ret = mbedtls_pk_parse_subpubkey( &start, end,
7583 &ssl->handshake->peer_pubkey );
7584 if( ret != 0 )
7585 {
7586 /* We should have parsed the public key before. */
7587 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7588 }
7589
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007590 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007591 return( 0 );
7592}
7593#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7594
Hanno Becker3cf50612019-02-05 14:36:34 +00007595int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7596{
7597 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007598 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007599#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7600 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7601 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007602 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007603#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007604 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007605#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007606 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007607 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007608
7609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7610
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007611 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7612 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007613 {
7614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007615 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Becker613d4902019-02-05 13:11:17 +00007616 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007617 }
7618
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007619#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7620 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007621 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007622 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007623 chain = ssl->handshake->ecrs_peer_cert;
7624 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007625 goto crt_verify;
7626 }
7627#endif
7628
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007629 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007630 {
7631 /* mbedtls_ssl_read_record may have sent an alert already. We
7632 let it decide whether to alert. */
7633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007634 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007635 }
7636
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007637#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007638 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7639 {
7640 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007641
Hanno Beckerb8a08572019-02-05 12:49:06 +00007642 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007643 ret = 0;
7644 else
7645 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007646
Hanno Becker613d4902019-02-05 13:11:17 +00007647 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007648 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007649#endif /* MBEDTLS_SSL_SRV_C */
7650
Hanno Becker35e41772019-02-05 15:37:23 +00007651 /* Clear existing peer CRT structure in case we tried to
7652 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007653 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007654
7655 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7656 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007657 {
7658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7659 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007660 mbedtls_ssl_pend_fatal_alert( ssl,
7661 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007662
Hanno Beckere4aeb762019-02-05 17:19:52 +00007663 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7664 goto exit;
7665 }
7666 mbedtls_x509_crt_init( chain );
7667
7668 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007669 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007670 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007671
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007672#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7673 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007674 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007675
7676crt_verify:
7677 if( ssl->handshake->ecrs_enabled)
7678 rs_ctx = &ssl->handshake->ecrs_ctx;
7679#endif
7680
Hanno Becker3cf50612019-02-05 14:36:34 +00007681 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007682 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007683 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007684 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007685
Hanno Becker3008d282019-02-05 17:02:28 +00007686#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007687 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007688 size_t pk_len;
7689 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Hanno Becker34106f62019-02-08 14:59:05 +00007691 /* We parse the CRT chain without copying, so
7692 * these pointers point into the input buffer,
7693 * and are hence still valid after freeing the
7694 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007695
Hanno Becker5882dd02019-06-06 16:25:57 +01007696#if defined(MBEDTLS_SSL_RENEGOTIATION)
7697 unsigned char *crt_start;
7698 size_t crt_len;
7699
Hanno Becker34106f62019-02-08 14:59:05 +00007700 crt_start = chain->raw.p;
7701 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007702#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
Hanno Becker8c13ee62019-02-26 16:48:17 +00007704 pk_start = chain->cache->pk_raw.p;
7705 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007706
7707 /* Free the CRT structures before computing
7708 * digest and copying the peer's public key. */
7709 mbedtls_x509_crt_free( chain );
7710 mbedtls_free( chain );
7711 chain = NULL;
7712
Hanno Becker5882dd02019-06-06 16:25:57 +01007713#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007714 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007716 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007717#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Hanno Becker34106f62019-02-08 14:59:05 +00007719 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007721 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007722 }
Hanno Becker34106f62019-02-08 14:59:05 +00007723#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7724 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007725 ssl->session_negotiate->peer_cert = chain;
7726 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007727#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Hanno Becker613d4902019-02-05 13:11:17 +00007731exit:
7732
Hanno Beckere4aeb762019-02-05 17:19:52 +00007733 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007734 {
7735 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7736 {
7737 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7738 }
7739 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7740 {
7741 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7742 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007743 else
7744 {
7745 ssl->state = MBEDTLS_SSL_INVALID;
7746 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007747 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007748
7749#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7750 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7751 {
7752 ssl->handshake->ecrs_peer_cert = chain;
7753 chain = NULL;
7754 }
7755#endif
7756
7757 if( chain != NULL )
7758 {
7759 mbedtls_x509_crt_free( chain );
7760 mbedtls_free( chain );
7761 }
7762
Paul Bakker5121ce52009-01-03 21:22:43 +00007763 return( ret );
7764}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007765#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007768{
7769 int ret;
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007774 ssl->out_msglen = 1;
7775 ssl->out_msg[0] = 1;
7776
Jarno Lamsa2b205162019-11-12 15:36:21 +02007777 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7778 {
7779 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7780 }
7781 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7782 {
7783 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7784 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007785 else
7786 {
7787 ssl->state = MBEDTLS_SSL_INVALID;
7788 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007789
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007790 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007793 return( ret );
7794 }
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007797
7798 return( 0 );
7799}
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007802{
7803 int ret;
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
Hanno Becker327c93b2018-08-15 13:56:18 +01007807 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 return( ret );
7811 }
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007816 mbedtls_ssl_pend_fatal_alert( ssl,
7817 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819 }
7820
Hanno Beckere678eaa2018-08-21 14:57:46 +01007821 /* CCS records are only accepted if they have length 1 and content '1',
7822 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007824 /*
7825 * Switch to our negotiated transform and session parameters for inbound
7826 * data.
7827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007829 ssl->transform_in = ssl->transform_negotiate;
7830 ssl->session_in = ssl->session_negotiate;
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007833 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007836 ssl_dtls_replay_reset( ssl );
7837#endif
7838
7839 /* Increment epoch */
7840 if( ++ssl->in_epoch == 0 )
7841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007843 /* This is highly unlikely to happen for legitimate reasons, so
7844 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007846 }
7847 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007848 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007850#if defined(MBEDTLS_SSL_PROTO_TLS)
7851 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007852 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007853 }
7854#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007855
Hanno Beckerf5970a02019-05-08 09:38:41 +01007856 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7859 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007864 mbedtls_ssl_pend_fatal_alert( ssl,
7865 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007867 }
7868 }
7869#endif
7870
Jarno Lamsa2b205162019-11-12 15:36:21 +02007871 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7872 {
7873 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7874 }
7875 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7876 {
7877 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7878 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007879 else
7880 {
7881 ssl->state = MBEDTLS_SSL_INVALID;
7882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007885
7886 return( 0 );
7887}
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7892 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007893 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007894 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7897#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007898 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007901 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007904}
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007909
7910 /*
7911 * Free our handshake params
7912 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007913 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007915 ssl->handshake = NULL;
7916
7917 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007918 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007919 */
7920 if( ssl->transform )
7921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 mbedtls_ssl_transform_free( ssl->transform );
7923 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007924 }
7925 ssl->transform = ssl->transform_negotiate;
7926 ssl->transform_negotiate = NULL;
7927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007929}
7930
Jarno Lamsae1621d42019-12-19 08:58:56 +02007931int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007932{
Jarno Lamsae1621d42019-12-19 08:58:56 +02007933 volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_RENEGOTIATION)
7937 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007940 ssl->renego_records_seen = 0;
7941 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007942#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007943
7944 /*
7945 * Free the previous session and switch in the current one
7946 */
Paul Bakker0a597072012-09-25 21:55:46 +00007947 if( ssl->session )
7948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007950 /* RFC 7366 3.1: keep the EtM state */
7951 ssl->session_negotiate->encrypt_then_mac =
7952 ssl->session->encrypt_then_mac;
7953#endif
7954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 mbedtls_ssl_session_free( ssl->session );
7956 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007957 }
7958 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007959 ssl->session_negotiate = NULL;
7960
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007961#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007962 /*
7963 * Add cache entry
7964 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007965 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007966 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007967 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007968 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007969 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007971 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007972#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007973
Jarno Lamsae1621d42019-12-19 08:58:56 +02007974#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
7975 if( ssl->handshake->resume )
7976 {
7977 mbedtls_platform_enforce_volatile_reads();
7978 if( ssl->handshake->resume )
7979 {
7980 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7981 }
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;
8000 }
8001 }
8002 else
8003 {
8004 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
8005 }
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008008 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008009 ssl->handshake->flight != NULL )
8010 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008011 /* Cancel handshake timer */
8012 ssl_set_timer( ssl, 0 );
8013
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008014 /* Keep last flight around in case we need to resend it:
8015 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008017 }
8018 else
8019#endif
8020 ssl_handshake_wrapup_free_hs_transform( ssl );
8021
Jarno Lamsa2b205162019-11-12 15:36:21 +02008022 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008025 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008026}
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008029{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008030 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008033
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008034 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008035
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008036 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8037 mbedtls_ssl_suite_get_mac(
8038 mbedtls_ssl_ciphersuite_from_id(
8039 mbedtls_ssl_session_get_ciphersuite(
8040 ssl->session_negotiate ) ) ),
8041 ssl, ssl->out_msg + 4,
8042 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008043
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008044 /*
8045 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8046 * may define some other value. Currently (early 2016), no defined
8047 * ciphersuite does this (and this is unlikely to change as activity has
8048 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8049 */
Hanno Becker2881d802019-05-22 14:44:53 +01008050 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008053 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008054 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008055#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008056
Paul Bakker5121ce52009-01-03 21:22:43 +00008057 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8059 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008060
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008061#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008062 /*
8063 * In case of session resuming, invert the client and server
8064 * ChangeCipherSpec messages order.
8065 */
Paul Bakker0a597072012-09-25 21:55:46 +00008066 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008069 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8070 MBEDTLS_SSL_IS_CLIENT )
8071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008073 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008076 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8077 MBEDTLS_SSL_IS_SERVER )
8078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008080 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008082 }
8083 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008084#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008085 {
8086 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8087 {
8088 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8089 }
8090 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8091 {
8092 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8093 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008094 else
8095 {
8096 ssl->state = MBEDTLS_SSL_INVALID;
8097 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008099
Paul Bakker48916f92012-09-16 19:57:18 +00008100 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008101 * Switch to our negotiated transform and session parameters for outbound
8102 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008107 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008108 {
8109 unsigned char i;
8110
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008111 /* Remember current epoch settings for resending */
8112 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008113 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008114
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008115 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008116 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008117
8118 /* Increment epoch */
8119 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008120 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008121 break;
8122
8123 /* The loop goes to its end iff the counter is wrapping */
8124 if( i == 0 )
8125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8127 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008128 }
8129 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008130 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008132#if defined(MBEDTLS_SSL_PROTO_TLS)
8133 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008134 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008135 }
8136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008137
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008138 ssl->transform_out = ssl->transform_negotiate;
8139 ssl->session_out = ssl->session_negotiate;
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8142 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8147 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008148 }
8149 }
8150#endif
8151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008153 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008155#endif
8156
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008157 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008158 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008160 return( ret );
8161 }
8162
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008164 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008165 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8166 {
8167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8168 return( ret );
8169 }
8170#endif
8171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008173
8174 return( 0 );
8175}
8176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008178#define SSL_MAX_HASH_LEN 36
8179#else
8180#define SSL_MAX_HASH_LEN 12
8181#endif
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008184{
Paul Bakker23986e52011-04-24 08:57:21 +00008185 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008186 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008187 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008190
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008191 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8192 mbedtls_ssl_suite_get_mac(
8193 mbedtls_ssl_ciphersuite_from_id(
8194 mbedtls_ssl_session_get_ciphersuite(
8195 ssl->session_negotiate ) ) ),
8196 ssl, buf,
8197 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008198
Hanno Becker327c93b2018-08-15 13:56:18 +01008199 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008202 return( ret );
8203 }
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008208 mbedtls_ssl_pend_fatal_alert( ssl,
8209 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008211 }
8212
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008213 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008215 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008216 hash_len = 36;
8217 else
8218#endif
8219 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8222 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008225 mbedtls_ssl_pend_fatal_alert( ssl,
8226 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008228 }
8229
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008230 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008231 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008234 mbedtls_ssl_pend_fatal_alert( ssl,
8235 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008237 }
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008240 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008241 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008242#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008243
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008244#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00008245 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008248 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008252 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008254#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008255 }
8256 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008257#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008258 {
8259 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8260 {
8261 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8262 }
8263 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8264 {
8265 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8266 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008267 else
8268 {
8269 ssl->state = MBEDTLS_SSL_INVALID;
8270 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008274 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008276#endif
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008279
8280 return( 0 );
8281}
8282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8288 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8289 mbedtls_md5_init( &handshake->fin_md5 );
8290 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008291 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8292 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8295#if defined(MBEDTLS_SHA256_C)
8296 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008297 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_SHA512_C)
8300 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008301 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008304
Hanno Becker7e5437a2017-04-28 17:15:26 +01008305#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8306 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8307 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8308#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_DHM_C)
8311 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#if defined(MBEDTLS_ECDH_C)
8314 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008315#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008316#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008317 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008318#if defined(MBEDTLS_SSL_CLI_C)
8319 handshake->ecjpake_cache = NULL;
8320 handshake->ecjpake_cache_len = 0;
8321#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008322#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008323
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008324#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008325 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008326#endif
8327
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008328#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8329 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8330#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008331
8332#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8333 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8334 mbedtls_pk_init( &handshake->peer_pubkey );
8335#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008336}
8337
Hanno Becker611a83b2018-01-03 14:27:32 +00008338void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8343 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008344
Hanno Becker92231322018-01-03 15:32:51 +00008345#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 mbedtls_md_init( &transform->md_ctx_enc );
8347 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008348#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008349}
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008354}
8355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008357{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008358 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008359 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008361 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008363 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008364 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008365
8366 /*
8367 * Either the pointers are now NULL or cleared properly and can be freed.
8368 * Now allocate missing structures.
8369 */
8370 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008371 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008372 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008373 }
Paul Bakker48916f92012-09-16 19:57:18 +00008374
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008375 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008376 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008377 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008378 }
Paul Bakker48916f92012-09-16 19:57:18 +00008379
Paul Bakker82788fb2014-10-20 13:59:19 +02008380 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008381 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008382 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008383 }
Paul Bakker48916f92012-09-16 19:57:18 +00008384
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008385 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008386 if( ssl->handshake == NULL ||
8387 ssl->transform_negotiate == NULL ||
8388 ssl->session_negotiate == NULL )
8389 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392 mbedtls_free( ssl->handshake );
8393 mbedtls_free( ssl->transform_negotiate );
8394 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008395
8396 ssl->handshake = NULL;
8397 ssl->transform_negotiate = NULL;
8398 ssl->session_negotiate = NULL;
8399
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008400 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008401 }
8402
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008403 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008405 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008406 ssl_handshake_params_init( ssl->handshake );
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008409 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008410 {
8411 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008412
Hanno Becker2d9623f2019-06-13 12:07:05 +01008413 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008414 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8415 else
8416 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8417 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008418#endif
8419
Paul Bakker48916f92012-09-16 19:57:18 +00008420 return( 0 );
8421}
8422
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008423#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008424/* Dummy cookie callbacks for defaults */
8425static int ssl_cookie_write_dummy( void *ctx,
8426 unsigned char **p, unsigned char *end,
8427 const unsigned char *cli_id, size_t cli_id_len )
8428{
8429 ((void) ctx);
8430 ((void) p);
8431 ((void) end);
8432 ((void) cli_id);
8433 ((void) cli_id_len);
8434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008436}
8437
8438static int ssl_cookie_check_dummy( void *ctx,
8439 const unsigned char *cookie, size_t cookie_len,
8440 const unsigned char *cli_id, size_t cli_id_len )
8441{
8442 ((void) ctx);
8443 ((void) cookie);
8444 ((void) cookie_len);
8445 ((void) cli_id);
8446 ((void) cli_id_len);
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008449}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008450#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008451
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008452/* Once ssl->out_hdr as the address of the beginning of the
8453 * next outgoing record is set, deduce the other pointers.
8454 *
8455 * Note: For TLS, we save the implicit record sequence number
8456 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8457 * and the caller has to make sure there's space for this.
8458 */
8459
8460static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8461 mbedtls_ssl_transform *transform )
8462{
8463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008464 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008465 {
8466 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008467#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008468 ssl->out_cid = ssl->out_ctr + 8;
8469 ssl->out_len = ssl->out_cid;
8470 if( transform != NULL )
8471 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008472#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008473 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008474#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008475 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008476 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008477 MBEDTLS_SSL_TRANSPORT_ELSE
8478#endif /* MBEDTLS_SSL_PROTO_DTLS */
8479#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008480 {
8481 ssl->out_ctr = ssl->out_hdr - 8;
8482 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008483#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008484 ssl->out_cid = ssl->out_len;
8485#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008486 ssl->out_iv = ssl->out_hdr + 5;
8487 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008488#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008489
8490 /* Adjust out_msg to make space for explicit IV, if used. */
8491 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008492 mbedtls_ssl_ver_geq(
8493 mbedtls_ssl_get_minor_ver( ssl ),
8494 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008495 {
8496 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8497 }
8498 else
8499 ssl->out_msg = ssl->out_iv;
8500}
8501
8502/* Once ssl->in_hdr as the address of the beginning of the
8503 * next incoming record is set, deduce the other pointers.
8504 *
8505 * Note: For TLS, we save the implicit record sequence number
8506 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8507 * and the caller has to make sure there's space for this.
8508 */
8509
Hanno Beckerf5970a02019-05-08 09:38:41 +01008510static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008511{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008512 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008513 * of unprotected TLS/DTLS records, with ssl->in_msg
8514 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008515 *
8516 * When decrypting a protected record, ssl->in_msg
8517 * will be shifted to point to the beginning of the
8518 * record plaintext.
8519 */
8520
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008522 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008523 {
Hanno Becker70e79282019-05-03 14:34:53 +01008524 /* This sets the header pointers to match records
8525 * without CID. When we receive a record containing
8526 * a CID, the fields are shifted accordingly in
8527 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008528 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008529#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008530 ssl->in_cid = ssl->in_ctr + 8;
8531 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008532#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008533 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008534#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008535 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008536 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008537 MBEDTLS_SSL_TRANSPORT_ELSE
8538#endif /* MBEDTLS_SSL_PROTO_DTLS */
8539#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008540 {
8541 ssl->in_ctr = ssl->in_hdr - 8;
8542 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008543#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008544 ssl->in_cid = ssl->in_len;
8545#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008546 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008547 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008548#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008549}
8550
Paul Bakker5121ce52009-01-03 21:22:43 +00008551/*
8552 * Initialize an SSL context
8553 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008554void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8555{
8556 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8557}
8558
8559/*
8560 * Setup an SSL context
8561 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008562
8563static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8564{
8565 /* Set the incoming and outgoing record pointers. */
8566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008567 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008568 {
8569 ssl->out_hdr = ssl->out_buf;
8570 ssl->in_hdr = ssl->in_buf;
8571 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008572 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008573#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008574#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008575 {
8576 ssl->out_hdr = ssl->out_buf + 8;
8577 ssl->in_hdr = ssl->in_buf + 8;
8578 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008579#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008580
8581 /* Derive other internal pointers. */
8582 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008583 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008584}
8585
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008586int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008587 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008588{
Paul Bakker48916f92012-09-16 19:57:18 +00008589 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008590
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008591 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008592
Hanno Beckeref982d52019-07-23 15:56:18 +01008593#if defined(MBEDTLS_USE_TINYCRYPT)
8594 uECC_set_rng( &uecc_rng_wrapper );
8595#endif
8596
Paul Bakker62f2dee2012-09-28 07:31:51 +00008597 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008598 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008599 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008600
8601 /* Set to NULL in case of an error condition */
8602 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008603
Angus Grattond8213d02016-05-25 20:56:48 +10008604 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8605 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008606 {
Angus Grattond8213d02016-05-25 20:56:48 +10008607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008608 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008609 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008610 }
8611
8612 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8613 if( ssl->out_buf == NULL )
8614 {
8615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008616 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008617 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008618 }
8619
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008620 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008621
Paul Bakker48916f92012-09-16 19:57:18 +00008622 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008623 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008624
Hanno Beckerc8f52992019-07-25 11:15:08 +01008625 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008626
Paul Bakker5121ce52009-01-03 21:22:43 +00008627 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008628
8629error:
8630 mbedtls_free( ssl->in_buf );
8631 mbedtls_free( ssl->out_buf );
8632
8633 ssl->conf = NULL;
8634
8635 ssl->in_buf = NULL;
8636 ssl->out_buf = NULL;
8637
8638 ssl->in_hdr = NULL;
8639 ssl->in_ctr = NULL;
8640 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008641 ssl->in_msg = NULL;
8642
8643 ssl->out_hdr = NULL;
8644 ssl->out_ctr = NULL;
8645 ssl->out_len = NULL;
8646 ssl->out_iv = NULL;
8647 ssl->out_msg = NULL;
8648
k-stachowiak9f7798e2018-07-31 16:52:32 +02008649 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008650}
8651
8652/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008653 * Reset an initialized and used SSL context for re-use while retaining
8654 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008655 *
8656 * If partial is non-zero, keep data in the input buffer and client ID.
8657 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008658 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008659static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008660{
Paul Bakker48916f92012-09-16 19:57:18 +00008661 int ret;
8662
Hanno Becker7e772132018-08-10 12:38:21 +01008663#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8664 !defined(MBEDTLS_SSL_SRV_C)
8665 ((void) partial);
8666#endif
8667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008669
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008670 /* Cancel any possibly running timer */
8671 ssl_set_timer( ssl, 0 );
8672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673#if defined(MBEDTLS_SSL_RENEGOTIATION)
8674 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008675 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008676
8677 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008678 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8679 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008682
Paul Bakker7eb013f2011-10-06 12:37:39 +00008683 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008684 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008685
8686 ssl->in_msgtype = 0;
8687 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008689 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008690 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008693 ssl_dtls_replay_reset( ssl );
8694#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008695
8696 ssl->in_hslen = 0;
8697 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008698
8699 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008700
8701 ssl->out_msgtype = 0;
8702 ssl->out_msglen = 0;
8703 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8705 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008706 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008707#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008708
Hanno Becker19859472018-08-06 09:40:20 +01008709 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8710
Paul Bakker48916f92012-09-16 19:57:18 +00008711 ssl->transform_in = NULL;
8712 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008713
Hanno Becker78640902018-08-13 16:35:15 +01008714 ssl->session_in = NULL;
8715 ssl->session_out = NULL;
8716
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008717 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008718
8719#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008720 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008721#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8722 {
8723 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008724 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008725 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8728 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8731 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8734 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008735 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008736 }
8737#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008738
Paul Bakker48916f92012-09-16 19:57:18 +00008739 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 mbedtls_ssl_transform_free( ssl->transform );
8742 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008743 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008744 }
Paul Bakker48916f92012-09-16 19:57:18 +00008745
Paul Bakkerc0463502013-02-14 11:19:38 +01008746 if( ssl->session )
8747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748 mbedtls_ssl_session_free( ssl->session );
8749 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008750 ssl->session = NULL;
8751 }
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008754 ssl->alpn_chosen = NULL;
8755#endif
8756
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008757#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008758#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008759 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008760#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008761 {
8762 mbedtls_free( ssl->cli_id );
8763 ssl->cli_id = NULL;
8764 ssl->cli_id_len = 0;
8765 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008766#endif
8767
Paul Bakker48916f92012-09-16 19:57:18 +00008768 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8769 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008770
8771 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008772}
8773
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008774/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008775 * Reset an initialized and used SSL context for re-use while retaining
8776 * all application-set variables, function pointers and data.
8777 */
8778int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8779{
8780 return( ssl_session_reset_int( ssl, 0 ) );
8781}
8782
8783/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008784 * SSL set accessors
8785 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008786#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008787void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008788{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008789 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008790}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008791#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008792
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008793void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008794{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008795 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008796}
8797
Hanno Becker7f376f42019-06-12 16:20:48 +01008798#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8799 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008800void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008801{
Hanno Becker7f376f42019-06-12 16:20:48 +01008802 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008803}
Hanno Becker7f376f42019-06-12 16:20:48 +01008804#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008805
Hanno Beckerde671542019-06-12 16:30:46 +01008806#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8807 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8808void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8809 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008810{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008811 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008812}
Hanno Beckerde671542019-06-12 16:30:46 +01008813#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008816
Hanno Becker1841b0a2018-08-24 11:13:57 +01008817void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8818 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008819{
8820 ssl->disable_datagram_packing = !allow_packing;
8821}
8822
Hanno Becker1f835fa2019-06-13 10:14:59 +01008823#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8824 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008825void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8826 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008827{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008828 conf->hs_timeout_min = min;
8829 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008830}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008831#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8832 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8833void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8834 uint32_t min, uint32_t max )
8835{
8836 ((void) conf);
8837 ((void) min);
8838 ((void) max);
8839}
8840#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8841 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8842
8843#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008844
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008845void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008846{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008847#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8848 conf->authmode = authmode;
8849#else
8850 ((void) conf);
8851 ((void) authmode);
8852#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008853}
8854
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008855#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8856 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008858 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008859 void *p_vrfy )
8860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008861 conf->f_vrfy = f_vrfy;
8862 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008863}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008864#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008865
Hanno Beckerece325c2019-06-13 15:39:27 +01008866#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008867void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008868 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008869 void *p_rng )
8870{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008871 conf->f_rng = f_rng;
8872 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008873}
Hanno Beckerece325c2019-06-13 15:39:27 +01008874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Hanno Becker14a4a442019-07-02 17:00:34 +01008876#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008877void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008878 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008879 void *p_dbg )
8880{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008881 conf->f_dbg = f_dbg;
8882 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008883}
Hanno Becker14a4a442019-07-02 17:00:34 +01008884#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008885
Hanno Beckera58a8962019-06-13 16:11:15 +01008886#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8887 !defined(MBEDTLS_SSL_CONF_SEND) && \
8888 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008890 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008891 mbedtls_ssl_send_t *f_send,
8892 mbedtls_ssl_recv_t *f_recv,
8893 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008894{
Hanno Beckera58a8962019-06-13 16:11:15 +01008895 ssl->p_bio = p_bio;
8896 ssl->f_send = f_send;
8897 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008898 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008899}
Hanno Beckera58a8962019-06-13 16:11:15 +01008900#else
8901void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8902 void *p_bio )
8903{
8904 ssl->p_bio = p_bio;
8905}
8906#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008907
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008908#if defined(MBEDTLS_SSL_PROTO_DTLS)
8909void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8910{
8911 ssl->mtu = mtu;
8912}
8913#endif
8914
Hanno Becker1f835fa2019-06-13 10:14:59 +01008915#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008916void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008917{
8918 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008919}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008920#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008921
Hanno Becker0ae6b242019-06-13 16:45:36 +01008922#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8923 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008924void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8925 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008926 mbedtls_ssl_set_timer_t *f_set_timer,
8927 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008928{
8929 ssl->p_timer = p_timer;
8930 ssl->f_set_timer = f_set_timer;
8931 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008932 /* Make sure we start with no timer running */
8933 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008934}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008935#else
8936void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8937 void *p_timer )
8938{
8939 ssl->p_timer = p_timer;
8940 /* Make sure we start with no timer running */
8941 ssl_set_timer( ssl, 0 );
8942}
8943#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008944
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008945#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008946void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008947 void *p_cache,
8948 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8949 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008950{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008951 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008952 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008953 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008954}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008955#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008956
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008957#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008959{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008960 int ret;
8961
8962 if( ssl == NULL ||
8963 session == NULL ||
8964 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008965 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008968 }
8969
Hanno Becker58fccf22019-02-06 14:30:46 +00008970 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8971 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008972 return( ret );
8973
Paul Bakker0a597072012-09-25 21:55:46 +00008974 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008975
8976 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008977}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008978#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008979
Hanno Becker73f4cb12019-06-27 13:51:07 +01008980#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008981void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008982 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008983{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008984 conf->ciphersuite_list[0] = ciphersuites;
8985 conf->ciphersuite_list[1] = ciphersuites;
8986 conf->ciphersuite_list[2] = ciphersuites;
8987 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008988}
8989
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008990void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008991 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008992 int major, int minor )
8993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008995 return;
8996
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008997 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
8998 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
8999 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009000 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009001 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009002
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009003 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9004 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009005}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009006#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009009void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009010 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009011{
9012 conf->cert_profile = profile;
9013}
9014
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009015/* Append a new keycert entry to a (possibly empty) list */
9016static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9017 mbedtls_x509_crt *cert,
9018 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009019{
niisato8ee24222018-06-25 19:05:48 +09009020 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009021
niisato8ee24222018-06-25 19:05:48 +09009022 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9023 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009025
niisato8ee24222018-06-25 19:05:48 +09009026 new_cert->cert = cert;
9027 new_cert->key = key;
9028 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009029
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009030 /* Update head is the list was null, else add to the end */
9031 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009032 {
niisato8ee24222018-06-25 19:05:48 +09009033 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009034 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009035 else
9036 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009037 mbedtls_ssl_key_cert *cur = *head;
9038 while( cur->next != NULL )
9039 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009040 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009041 }
9042
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009043 return( 0 );
9044}
9045
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009046int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009047 mbedtls_x509_crt *own_cert,
9048 mbedtls_pk_context *pk_key )
9049{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009050 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009051}
9052
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009053void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009054 mbedtls_x509_crt *ca_chain,
9055 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009057 conf->ca_chain = ca_chain;
9058 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009061
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009062#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9063int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9064 mbedtls_x509_crt *own_cert,
9065 mbedtls_pk_context *pk_key )
9066{
9067 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9068 own_cert, pk_key ) );
9069}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009070
9071void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9072 mbedtls_x509_crt *ca_chain,
9073 mbedtls_x509_crl *ca_crl )
9074{
9075 ssl->handshake->sni_ca_chain = ca_chain;
9076 ssl->handshake->sni_ca_crl = ca_crl;
9077}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009078
9079void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9080 int authmode )
9081{
9082 ssl->handshake->sni_authmode = authmode;
9083}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009084#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9085
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009086#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009087/*
9088 * Set EC J-PAKE password for current handshake
9089 */
9090int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9091 const unsigned char *pw,
9092 size_t pw_len )
9093{
9094 mbedtls_ecjpake_role role;
9095
Janos Follath8eb64132016-06-03 15:40:57 +01009096 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9098
Hanno Becker2d9623f2019-06-13 12:07:05 +01009099 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009100 role = MBEDTLS_ECJPAKE_SERVER;
9101 else
9102 role = MBEDTLS_ECJPAKE_CLIENT;
9103
9104 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9105 role,
9106 MBEDTLS_MD_SHA256,
9107 MBEDTLS_ECP_DP_SECP256R1,
9108 pw, pw_len ) );
9109}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009110#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009113int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009114 const unsigned char *psk, size_t psk_len,
9115 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009116{
Paul Bakker6db455e2013-09-18 17:29:31 +02009117 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009122
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009123 /* Identity len will be encoded on two bytes */
9124 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009125 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009126 {
9127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9128 }
9129
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009130 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009131 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009132 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009133
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009134 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009135 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009136 conf->psk_len = 0;
9137 }
9138 if( conf->psk_identity != NULL )
9139 {
9140 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009141 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009142 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009143 }
9144
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009145 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9146 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009147 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009148 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009149 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009150 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009151 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009152 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009153 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009154
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009155 conf->psk_len = psk_len;
9156 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009157
Teppo Järvelin91d79382019-10-02 09:09:31 +03009158 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9159 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009160
9161 return( 0 );
9162}
9163
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009164int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9165 const unsigned char *psk, size_t psk_len )
9166{
9167 if( psk == NULL || ssl->handshake == NULL )
9168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9169
9170 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9172
9173 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009175 mbedtls_platform_zeroize( ssl->handshake->psk,
9176 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009177 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009178 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009179 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009180
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009181 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009182 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009183
9184 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009185 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009186
9187 return( 0 );
9188}
9189
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009190void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009192 size_t),
9193 void *p_psk )
9194{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009195 conf->f_psk = f_psk;
9196 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009199
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009200#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009201
9202#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009203int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009204{
9205 int ret;
9206
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009207 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9208 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9209 {
9210 mbedtls_mpi_free( &conf->dhm_P );
9211 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009212 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009214
9215 return( 0 );
9216}
Hanno Becker470a8c42017-10-04 15:28:46 +01009217#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009218
Hanno Beckera90658f2017-10-04 15:29:08 +01009219int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9220 const unsigned char *dhm_P, size_t P_len,
9221 const unsigned char *dhm_G, size_t G_len )
9222{
9223 int ret;
9224
9225 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9226 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9227 {
9228 mbedtls_mpi_free( &conf->dhm_P );
9229 mbedtls_mpi_free( &conf->dhm_G );
9230 return( ret );
9231 }
9232
9233 return( 0 );
9234}
Paul Bakker5121ce52009-01-03 21:22:43 +00009235
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009236int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009237{
9238 int ret;
9239
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009240 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9241 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9242 {
9243 mbedtls_mpi_free( &conf->dhm_P );
9244 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009245 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009246 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009247
9248 return( 0 );
9249}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009250#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009251
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009252#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9253/*
9254 * Set the minimum length for Diffie-Hellman parameters
9255 */
9256void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9257 unsigned int bitlen )
9258{
9259 conf->dhm_min_bitlen = bitlen;
9260}
9261#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9262
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009263#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009264/*
9265 * Set allowed/preferred hashes for handshake signatures
9266 */
9267void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9268 const int *hashes )
9269{
Hanno Becker56595f42019-06-19 16:31:38 +01009270#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009271 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009272#else
9273 ((void) conf);
9274 ((void) hashes);
9275#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009276}
Hanno Becker947194e2017-04-07 13:25:49 +01009277#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009278
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009279#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009280#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009281/*
9282 * Set the allowed elliptic curves
9283 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009284void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009285 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009286{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009287 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009288}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009289#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009290#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009291
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009292#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009294{
Hanno Becker947194e2017-04-07 13:25:49 +01009295 /* Initialize to suppress unnecessary compiler warning */
9296 size_t hostname_len = 0;
9297
9298 /* Check if new hostname is valid before
9299 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009300 if( hostname != NULL )
9301 {
9302 hostname_len = strlen( hostname );
9303
9304 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9306 }
9307
9308 /* Now it's clear that we will overwrite the old hostname,
9309 * so we can free it safely */
9310
9311 if( ssl->hostname != NULL )
9312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009313 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009314 mbedtls_free( ssl->hostname );
9315 }
9316
9317 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009318
Paul Bakker5121ce52009-01-03 21:22:43 +00009319 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009320 {
9321 ssl->hostname = NULL;
9322 }
9323 else
9324 {
9325 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009326 if( ssl->hostname == NULL )
9327 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009328
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009329 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9330 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009331
Hanno Becker947194e2017-04-07 13:25:49 +01009332 ssl->hostname[hostname_len] = '\0';
9333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009334
9335 return( 0 );
9336}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009337#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009338
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009339#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009340void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009342 const unsigned char *, size_t),
9343 void *p_sni )
9344{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009345 conf->f_sni = f_sni;
9346 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009351int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009352{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009353 size_t cur_len, tot_len;
9354 const char **p;
9355
9356 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009357 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9358 * MUST NOT be truncated."
9359 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009360 */
9361 tot_len = 0;
9362 for( p = protos; *p != NULL; p++ )
9363 {
9364 cur_len = strlen( *p );
9365 tot_len += cur_len;
9366
9367 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009369 }
9370
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009371 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009372
9373 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009374}
9375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009377{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009378 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009381
Hanno Becker33b9b252019-07-05 11:23:25 +01009382#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9383 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9384void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9385 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009386{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009387 conf->max_major_ver = major;
9388 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009389}
Hanno Becker33b9b252019-07-05 11:23:25 +01009390#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9391 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009392
Hanno Becker33b9b252019-07-05 11:23:25 +01009393#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9394 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9395void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9396 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009397{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009398 conf->min_major_ver = major;
9399 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009400}
Hanno Becker33b9b252019-07-05 11:23:25 +01009401#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9402 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009405void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009406{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009407 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009408}
9409#endif
9410
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009411#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009412void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9413 char cert_req_ca_list )
9414{
9415 conf->cert_req_ca_list = cert_req_ca_list;
9416}
9417#endif
9418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009420void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009421{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009422 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009423}
9424#endif
9425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009427#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009428void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009429{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009430 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009431}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009432#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9433#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009434void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009435 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009436{
9437 conf->enforce_extended_master_secret = ems_enf;
9438}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009439#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009440#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009441
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009442#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009443void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009444{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009445 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009446}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009447#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009450int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009453 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009456 }
9457
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009458 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009459
9460 return( 0 );
9461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009465void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009467 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009472void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009473{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009474 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009475}
9476#endif
9477
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009478#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009479void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009481 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009482}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009483#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009486void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009487{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009488 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009489}
9490
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009491void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009492{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009493 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009494}
9495
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009496void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009497 const unsigned char period[8] )
9498{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009499 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009504#if defined(MBEDTLS_SSL_CLI_C)
9505void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009506{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009507 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009508}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009509#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009510
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009511#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009512void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9513 mbedtls_ssl_ticket_write_t *f_ticket_write,
9514 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9515 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009516{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009517 conf->f_ticket_write = f_ticket_write;
9518 conf->f_ticket_parse = f_ticket_parse;
9519 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009520}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009523
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009524#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9525void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9526 mbedtls_ssl_export_keys_t *f_export_keys,
9527 void *p_export_keys )
9528{
9529 conf->f_export_keys = f_export_keys;
9530 conf->p_export_keys = p_export_keys;
9531}
9532#endif
9533
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009534#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009535void mbedtls_ssl_conf_async_private_cb(
9536 mbedtls_ssl_config *conf,
9537 mbedtls_ssl_async_sign_t *f_async_sign,
9538 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9539 mbedtls_ssl_async_resume_t *f_async_resume,
9540 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009541 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009542{
9543 conf->f_async_sign_start = f_async_sign;
9544 conf->f_async_decrypt_start = f_async_decrypt;
9545 conf->f_async_resume = f_async_resume;
9546 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009547 conf->p_async_config_data = async_config_data;
9548}
9549
Gilles Peskine8f97af72018-04-26 11:46:10 +02009550void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9551{
9552 return( conf->p_async_config_data );
9553}
9554
Gilles Peskine1febfef2018-04-30 11:54:39 +02009555void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009556{
9557 if( ssl->handshake == NULL )
9558 return( NULL );
9559 else
9560 return( ssl->handshake->user_async_ctx );
9561}
9562
Gilles Peskine1febfef2018-04-30 11:54:39 +02009563void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009564 void *ctx )
9565{
9566 if( ssl->handshake != NULL )
9567 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009568}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009569#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009570
Paul Bakker5121ce52009-01-03 21:22:43 +00009571/*
9572 * SSL get accessors
9573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009575{
9576 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9577}
9578
Hanno Becker8b170a02017-10-10 11:51:19 +01009579int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9580{
9581 /*
9582 * Case A: We're currently holding back
9583 * a message for further processing.
9584 */
9585
9586 if( ssl->keep_current_message == 1 )
9587 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009589 return( 1 );
9590 }
9591
9592 /*
9593 * Case B: Further records are pending in the current datagram.
9594 */
9595
9596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009597 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009598 ssl->in_left > ssl->next_record_offset )
9599 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009601 return( 1 );
9602 }
9603#endif /* MBEDTLS_SSL_PROTO_DTLS */
9604
9605 /*
9606 * Case C: A handshake message is being processed.
9607 */
9608
Hanno Becker8b170a02017-10-10 11:51:19 +01009609 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9610 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009612 return( 1 );
9613 }
9614
9615 /*
9616 * Case D: An application data message is being processed
9617 */
9618 if( ssl->in_offt != NULL )
9619 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009621 return( 1 );
9622 }
9623
9624 /*
9625 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009626 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009627 * we implement support for multiple alerts in single records.
9628 */
9629
9630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9631 return( 0 );
9632}
9633
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009634uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009635{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009636 if( ssl->session != NULL )
9637 return( ssl->session->verify_result );
9638
9639 if( ssl->session_negotiate != NULL )
9640 return( ssl->session_negotiate->verify_result );
9641
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009642 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009643}
9644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009646{
Hanno Beckere02758c2019-06-26 15:31:31 +01009647 int suite;
9648
Paul Bakker926c8e42013-03-06 10:23:34 +01009649 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009650 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009651
Hanno Beckere02758c2019-06-26 15:31:31 +01009652 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9653 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009654}
9655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009657{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009659 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009660 {
Hanno Becker2881d802019-05-22 14:44:53 +01009661 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009664 return( "DTLSv1.0" );
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009667 return( "DTLSv1.2" );
9668
9669 default:
9670 return( "unknown (DTLS)" );
9671 }
9672 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009673 MBEDTLS_SSL_TRANSPORT_ELSE
9674#endif /* MBEDTLS_SSL_PROTO_DTLS */
9675#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009676 {
Hanno Becker2881d802019-05-22 14:44:53 +01009677 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009678 {
9679 case MBEDTLS_SSL_MINOR_VERSION_0:
9680 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009681
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009682 case MBEDTLS_SSL_MINOR_VERSION_1:
9683 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009684
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009685 case MBEDTLS_SSL_MINOR_VERSION_2:
9686 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009687
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009688 case MBEDTLS_SSL_MINOR_VERSION_3:
9689 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009690
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009691 default:
9692 return( "unknown" );
9693 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009694 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009695#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009696}
9697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009699{
Hanno Becker3136ede2018-08-17 15:28:19 +01009700 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009702
Hanno Becker43395762019-05-03 14:46:38 +01009703 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9704
Hanno Becker78640902018-08-13 16:35:15 +01009705 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009706 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#if defined(MBEDTLS_ZLIB_SUPPORT)
9709 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9710 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009711#endif
9712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009714 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009715#if defined(MBEDTLS_GCM_C) || \
9716 defined(MBEDTLS_CCM_C) || \
9717 defined(MBEDTLS_CHACHAPOLY_C)
9718#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009720#endif
9721#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009723#endif
9724#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009725 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009726#endif
9727 transform_expansion =
9728 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009729 break;
9730
Hanno Beckera9d5c452019-07-25 16:47:12 +01009731#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9732 MBEDTLS_CHACHAPOLY_C */
9733
9734#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9735 case MBEDTLS_MODE_STREAM:
9736 transform_expansion = transform->maclen;
9737 break;
9738#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9739
9740#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009742 {
9743 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009744
9745 block_size = mbedtls_cipher_get_block_size(
9746 &transform->cipher_ctx_enc );
9747
Hanno Becker3136ede2018-08-17 15:28:19 +01009748 /* Expansion due to the addition of the MAC. */
9749 transform_expansion += transform->maclen;
9750
9751 /* Expansion due to the addition of CBC padding;
9752 * Theoretically up to 256 bytes, but we never use
9753 * more than the block size of the underlying cipher. */
9754 transform_expansion += block_size;
9755
9756 /* For TLS 1.1 or higher, an explicit IV is added
9757 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009758#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009759 if( mbedtls_ssl_ver_geq(
9760 mbedtls_ssl_get_minor_ver( ssl ),
9761 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9762 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009763 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009764 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009765#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009766
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009767 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009768 }
9769#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009770
9771 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009774 }
9775
Hanno Beckera5a2b082019-05-15 14:03:01 +01009776#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009777 if( transform->out_cid_len != 0 )
9778 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009779#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009780
Hanno Becker43395762019-05-03 14:46:38 +01009781 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009782}
9783
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009784#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9785size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9786{
9787 size_t max_len;
9788
9789 /*
9790 * Assume mfl_code is correct since it was checked when set
9791 */
Angus Grattond8213d02016-05-25 20:56:48 +10009792 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009793
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009794 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009795 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009796 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009797 {
Angus Grattond8213d02016-05-25 20:56:48 +10009798 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009799 }
9800
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009801 /* During a handshake, use the value being negotiated */
9802 if( ssl->session_negotiate != NULL &&
9803 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9804 {
9805 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9806 }
9807
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009808 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009809}
9810#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9811
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009812#if defined(MBEDTLS_SSL_PROTO_DTLS)
9813static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9814{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009815 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009816 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009817 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9818 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009819 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009820
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009821 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9822 return( ssl->mtu );
9823
9824 if( ssl->mtu == 0 )
9825 return( ssl->handshake->mtu );
9826
9827 return( ssl->mtu < ssl->handshake->mtu ?
9828 ssl->mtu : ssl->handshake->mtu );
9829}
9830#endif /* MBEDTLS_SSL_PROTO_DTLS */
9831
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009832int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9833{
9834 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9835
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009836#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9837 !defined(MBEDTLS_SSL_PROTO_DTLS)
9838 (void) ssl;
9839#endif
9840
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009841#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9842 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9843
9844 if( max_len > mfl )
9845 max_len = mfl;
9846#endif
9847
9848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009849 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009850 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009851 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009852 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9853 const size_t overhead = (size_t) ret;
9854
9855 if( ret < 0 )
9856 return( ret );
9857
9858 if( mtu <= overhead )
9859 {
9860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9861 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9862 }
9863
9864 if( max_len > mtu - overhead )
9865 max_len = mtu - overhead;
9866 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009867#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009868
Hanno Becker0defedb2018-08-10 12:35:02 +01009869#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9870 !defined(MBEDTLS_SSL_PROTO_DTLS)
9871 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009872#endif
9873
9874 return( (int) max_len );
9875}
9876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877#if defined(MBEDTLS_X509_CRT_PARSE_C)
9878const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009879{
9880 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009881 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009882
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009883#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009884 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009885#else
9886 return( NULL );
9887#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009892int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9893 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009894{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009895 if( ssl == NULL ||
9896 dst == NULL ||
9897 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009898 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009901 }
9902
Hanno Becker58fccf22019-02-06 14:30:46 +00009903 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009906
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009907const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9908{
9909 if( ssl == NULL )
9910 return( NULL );
9911
9912 return( ssl->session );
9913}
9914
Paul Bakker5121ce52009-01-03 21:22:43 +00009915/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009916 * Define ticket header determining Mbed TLS version
9917 * and structure of the ticket.
9918 */
9919
Hanno Becker41527622019-05-16 12:50:45 +01009920/*
Hanno Becker26829e92019-05-28 14:30:45 +01009921 * Define bitflag determining compile-time settings influencing
9922 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009923 */
9924
Hanno Becker26829e92019-05-28 14:30:45 +01009925#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009926#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009927#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009928#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009929#endif /* MBEDTLS_HAVE_TIME */
9930
9931#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009932#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009933#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009934#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009935#endif /* MBEDTLS_X509_CRT_PARSE_C */
9936
9937#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009938#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009939#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009940#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009941#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9942
9943#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009944#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009945#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009946#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009947#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9948
9949#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009950#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009951#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009952#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009953#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9954
9955#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009956#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009957#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009958#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009959#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9960
Hanno Becker41527622019-05-16 12:50:45 +01009961#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9962#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9963#else
9964#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9965#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9966
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009967#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9968#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9969#else
9970#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9971#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9972
Hanno Becker88440552019-07-03 14:16:13 +01009973#if defined(MBEDTLS_ZLIB_SUPPORT)
9974#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9975#else
9976#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9977#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9978
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009979#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9980#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9981#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9982#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9983#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9984#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9985#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009986#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009987#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009988
Hanno Becker26829e92019-05-28 14:30:45 +01009989#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009990 ( (uint16_t) ( \
9991 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9992 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9993 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9994 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9995 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9996 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009997 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009998 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009999 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010000
Hanno Becker557fe9f2019-05-16 12:41:07 +010010001static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010002 MBEDTLS_VERSION_MAJOR,
10003 MBEDTLS_VERSION_MINOR,
10004 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010005 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10006 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010007};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010008
10009/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010010 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010011 * (in the presentation language of TLS, RFC 8446 section 3)
10012 *
Hanno Becker26829e92019-05-28 14:30:45 +010010013 * opaque mbedtls_version[3]; // major, minor, patch
10014 * opaque session_format[2]; // version-specific 16-bit field determining
10015 * // the format of the remaining
10016 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010017 *
10018 * Note: When updating the format, remember to keep
10019 * these version+format bytes.
10020 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010021 * // In this version, `session_format` determines
10022 * // the setting of those compile-time
10023 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010024 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010025 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010026 * uint8 ciphersuite[2]; // defined by the standard
10027 * uint8 compression; // 0 or 1
10028 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010029 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010030 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010031 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010032 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10033 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10034 * case disabled: uint8_t peer_cert_digest_type;
10035 * opaque peer_cert_digest<0..2^8-1>;
10036 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010037 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010038 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010039 * uint8 mfl_code; // up to 255 according to standard
10040 * uint8 trunc_hmac; // 0 or 1
10041 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010042 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010043 * The order is the same as in the definition of the structure, except
10044 * verify_result is put before peer_cert so that all mandatory fields come
10045 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010046 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010047static int ssl_session_save( const mbedtls_ssl_session *session,
10048 unsigned char omit_header,
10049 unsigned char *buf,
10050 size_t buf_len,
10051 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010052{
10053 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010054 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010055#if defined(MBEDTLS_HAVE_TIME)
10056 uint64_t start;
10057#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010059#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010060 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010061#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010062#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010063
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010064 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010065 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010066 /*
10067 * Add version identifier
10068 */
10069
10070 used += sizeof( ssl_serialized_session_header );
10071
10072 if( used <= buf_len )
10073 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010074 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010075 sizeof( ssl_serialized_session_header ) );
10076 p += sizeof( ssl_serialized_session_header );
10077 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010078 }
10079
10080 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010081 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010082 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010083#if defined(MBEDTLS_HAVE_TIME)
10084 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010085
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010086 if( used <= buf_len )
10087 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010088 start = (uint64_t) session->start;
10089
10090 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10091 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10092 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10093 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10094 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10095 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10096 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10097 *p++ = (unsigned char)( ( start ) & 0xFF );
10098 }
10099#endif /* MBEDTLS_HAVE_TIME */
10100
10101 /*
10102 * Basic mandatory fields
10103 */
Hanno Becker88440552019-07-03 14:16:13 +010010104 {
10105 size_t const ciphersuite_len = 2;
10106#if defined(MBEDTLS_ZLIB_SUPPORT)
10107 size_t const compression_len = 1;
10108#else
10109 size_t const compression_len = 0;
10110#endif
10111 size_t const id_len_len = 1;
10112 size_t const id_len = 32;
10113 size_t const master_len = 48;
10114 size_t const verif_result_len = 4;
10115
10116 size_t const basic_len =
10117 ciphersuite_len +
10118 compression_len +
10119 id_len_len +
10120 id_len +
10121 master_len +
10122 verif_result_len;
10123
10124 used += basic_len;
10125 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126
10127 if( used <= buf_len )
10128 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010129 const int ciphersuite =
10130 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010131 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010132
Hanno Becker88440552019-07-03 14:16:13 +010010133#if defined(MBEDTLS_ZLIB_SUPPORT)
10134 *p++ = (unsigned char)(
10135 mbedtls_ssl_session_get_compression( session ) );
10136#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137
10138 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010139 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10140 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010141 p += 32;
10142
Teppo Järvelin91d79382019-10-02 09:09:31 +030010143 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010144 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010145 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010146 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010147
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010148 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010149 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010150 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010151#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010152#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010153 if( session->peer_cert == NULL )
10154 cert_len = 0;
10155 else
10156 cert_len = session->peer_cert->raw.len;
10157
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010158 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010159
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010160 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010161 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010162 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010163
10164 if( session->peer_cert != NULL )
10165 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010166 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010167 p += cert_len;
10168 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010169 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010170
Hanno Becker5882dd02019-06-06 16:25:57 +010010171#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010172 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010173 if( session->peer_cert_digest != NULL )
10174 {
10175 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10176 if( used <= buf_len )
10177 {
10178 *p++ = (unsigned char) session->peer_cert_digest_type;
10179 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010180 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010181 session->peer_cert_digest_len );
10182 p += session->peer_cert_digest_len;
10183 }
10184 }
10185 else
10186 {
10187 used += 2;
10188 if( used <= buf_len )
10189 {
10190 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10191 *p++ = 0;
10192 }
10193 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010194#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010195#endif /* MBEDTLS_X509_CRT_PARSE_C */
10196
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010197 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010198 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010199 */
10200#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010201 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010202
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010203 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010204 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010205 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010206
10207 if( session->ticket != NULL )
10208 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010209 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010210 p += session->ticket_len;
10211 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010212
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010213 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010214 }
10215#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10216
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010217 /*
10218 * Misc extension-related info
10219 */
10220#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10221 used += 1;
10222
10223 if( used <= buf_len )
10224 *p++ = session->mfl_code;
10225#endif
10226
10227#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10228 used += 1;
10229
10230 if( used <= buf_len )
10231 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10232#endif
10233
10234#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10235 used += 1;
10236
10237 if( used <= buf_len )
10238 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10239#endif
10240
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010241 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010242 *olen = used;
10243
10244 if( used > buf_len )
10245 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010246
10247 return( 0 );
10248}
10249
10250/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010251 * Public wrapper for ssl_session_save()
10252 */
10253int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10254 unsigned char *buf,
10255 size_t buf_len,
10256 size_t *olen )
10257{
10258 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10259}
10260
10261/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010262 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010263 *
10264 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010265 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010266 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010267static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010268 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010269 const unsigned char *buf,
10270 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010271{
10272 const unsigned char *p = buf;
10273 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010274 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010275#if defined(MBEDTLS_HAVE_TIME)
10276 uint64_t start;
10277#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010278#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010279#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010280 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010281#endif
10282#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010283
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010284 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010285 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010286 /*
10287 * Check version identifier
10288 */
10289
10290 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10292
Teppo Järvelin0efac532019-10-04 13:21:08 +030010293 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010294 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010295 sizeof( ssl_serialized_session_header ) ) != 0 )
10296 {
10297 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10298 }
10299 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010300 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010301
10302 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010303 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010304 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010305#if defined(MBEDTLS_HAVE_TIME)
10306 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10308
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010309 start = ( (uint64_t) p[0] << 56 ) |
10310 ( (uint64_t) p[1] << 48 ) |
10311 ( (uint64_t) p[2] << 40 ) |
10312 ( (uint64_t) p[3] << 32 ) |
10313 ( (uint64_t) p[4] << 24 ) |
10314 ( (uint64_t) p[5] << 16 ) |
10315 ( (uint64_t) p[6] << 8 ) |
10316 ( (uint64_t) p[7] );
10317 p += 8;
10318
10319 session->start = (time_t) start;
10320#endif /* MBEDTLS_HAVE_TIME */
10321
10322 /*
10323 * Basic mandatory fields
10324 */
Hanno Becker88440552019-07-03 14:16:13 +010010325 {
10326 size_t const ciphersuite_len = 2;
10327#if defined(MBEDTLS_ZLIB_SUPPORT)
10328 size_t const compression_len = 1;
10329#else
10330 size_t const compression_len = 0;
10331#endif
10332 size_t const id_len_len = 1;
10333 size_t const id_len = 32;
10334 size_t const master_len = 48;
10335 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010336
Hanno Becker88440552019-07-03 14:16:13 +010010337 size_t const basic_len =
10338 ciphersuite_len +
10339 compression_len +
10340 id_len_len +
10341 id_len +
10342 master_len +
10343 verif_result_len;
10344
10345 if( basic_len > (size_t)( end - p ) )
10346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10347 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010348
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010349 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010350 p += 2;
10351
Hanno Becker73f4cb12019-06-27 13:51:07 +010010352#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010353 session->ciphersuite = ciphersuite;
10354#else
10355 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010356 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010357 {
10358 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10359 }
10360#endif
10361
Hanno Becker88440552019-07-03 14:16:13 +010010362#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010363 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010364#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010365
10366 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010367 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10368 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010369 p += 32;
10370
Teppo Järvelin91d79382019-10-02 09:09:31 +030010371 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010372 p += 48;
10373
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010374 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010375 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010376
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010377 /* Immediately clear invalid pointer values that have been read, in case
10378 * we exit early before we replaced them with valid ones. */
10379#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010380#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010381 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010382#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010383 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010384#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010385#endif
10386#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10387 session->ticket = NULL;
10388#endif
10389
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010390 /*
10391 * Peer certificate
10392 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010393#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010395 if( 3 > (size_t)( end - p ) )
10396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10397
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010398 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10399
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010400 p += 3;
10401
10402 if( cert_len == 0 )
10403 {
10404 session->peer_cert = NULL;
10405 }
10406 else
10407 {
10408 int ret;
10409
10410 if( cert_len > (size_t)( end - p ) )
10411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10412
10413 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10414
10415 if( session->peer_cert == NULL )
10416 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10417
10418 mbedtls_x509_crt_init( session->peer_cert );
10419
10420 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10421 p, cert_len ) ) != 0 )
10422 {
10423 mbedtls_x509_crt_free( session->peer_cert );
10424 mbedtls_free( session->peer_cert );
10425 session->peer_cert = NULL;
10426 return( ret );
10427 }
10428
10429 p += cert_len;
10430 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010431#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010432 /* Deserialize CRT digest from the end of the ticket. */
10433 if( 2 > (size_t)( end - p ) )
10434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10435
10436 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10437 session->peer_cert_digest_len = (size_t) *p++;
10438
10439 if( session->peer_cert_digest_len != 0 )
10440 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010441 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010442 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010443 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10445 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10447
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010448 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10450
10451 session->peer_cert_digest =
10452 mbedtls_calloc( 1, session->peer_cert_digest_len );
10453 if( session->peer_cert_digest == NULL )
10454 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10455
Teppo Järvelin91d79382019-10-02 09:09:31 +030010456 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010457 session->peer_cert_digest_len );
10458 p += session->peer_cert_digest_len;
10459 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010460#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010461#endif /* MBEDTLS_X509_CRT_PARSE_C */
10462
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010463 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010464 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010465 */
10466#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10467 if( 3 > (size_t)( end - p ) )
10468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10469
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010470 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010471 p += 3;
10472
10473 if( session->ticket_len != 0 )
10474 {
10475 if( session->ticket_len > (size_t)( end - p ) )
10476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10477
10478 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10479 if( session->ticket == NULL )
10480 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10481
Teppo Järvelin91d79382019-10-02 09:09:31 +030010482 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010483 p += session->ticket_len;
10484 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010485
10486 if( 4 > (size_t)( end - p ) )
10487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10488
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010489 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010490 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010491#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10492
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010493 /*
10494 * Misc extension-related info
10495 */
10496#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10497 if( 1 > (size_t)( end - p ) )
10498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10499
10500 session->mfl_code = *p++;
10501#endif
10502
10503#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10504 if( 1 > (size_t)( end - p ) )
10505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10506
10507 session->trunc_hmac = *p++;
10508#endif
10509
10510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10511 if( 1 > (size_t)( end - p ) )
10512 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10513
10514 session->encrypt_then_mac = *p++;
10515#endif
10516
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010517 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010518 if( p != end )
10519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10520
10521 return( 0 );
10522}
10523
10524/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010525 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010526 */
10527int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10528 const unsigned char *buf,
10529 size_t len )
10530{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010531 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010532
10533 if( ret != 0 )
10534 mbedtls_ssl_session_free( session );
10535
10536 return( ret );
10537}
10538
10539/*
Paul Bakker1961b702013-01-25 14:49:24 +010010540 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010542int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010545
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010546 if( ssl == NULL || ssl->conf == NULL )
10547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010549#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010550 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010553#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010554 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010555 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010556#endif
10557
Hanno Beckerb82350b2019-07-26 07:24:05 +010010558 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010559 return( ret );
10560}
10561
10562/*
10563 * Perform the SSL handshake
10564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010566{
10567 int ret = 0;
10568
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010569 if( ssl == NULL || ssl->conf == NULL )
10570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010577
10578 if( ret != 0 )
10579 break;
10580 }
10581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010583
10584 return( ret );
10585}
10586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_SSL_RENEGOTIATION)
10588#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010589/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010590 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010593{
10594 int ret;
10595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010597
10598 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10600 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010601
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010602 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010603 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010605 return( ret );
10606 }
10607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010609
10610 return( 0 );
10611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010613
10614/*
10615 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 * - any side: calling mbedtls_ssl_renegotiate(),
10617 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10618 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010619 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010620 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010624{
10625 int ret;
10626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010628
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010629 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10630 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010631
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010632 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10633 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010635 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010637 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010638 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10639 MBEDTLS_SSL_IS_SERVER )
10640 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010641 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010642 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010643 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010644 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010645 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010646 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010647 }
10648#endif
10649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10651 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010656 return( ret );
10657 }
10658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010660
10661 return( 0 );
10662}
10663
10664/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010665 * Renegotiate current connection on client,
10666 * or request renegotiation on server
10667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010671
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010672 if( ssl == NULL || ssl->conf == NULL )
10673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010676 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010677 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010683
10684 /* Did we already try/start sending HelloRequest? */
10685 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010687
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010688 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010689 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010693 /*
10694 * On client, either start the renegotiation process or,
10695 * if already in progress, continue the handshake
10696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10700 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010701
10702 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010705 return( ret );
10706 }
10707 }
10708 else
10709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010713 return( ret );
10714 }
10715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010717
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010718 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010719}
10720
10721/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010722 * Check record counters and renegotiate if they're above the limit.
10723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010725{
Andres AG2196c7f2016-12-15 17:01:16 +000010726 size_t ep_len = ssl_ep_len( ssl );
10727 int in_ctr_cmp;
10728 int out_ctr_cmp;
10729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10731 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010732 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010733 {
10734 return( 0 );
10735 }
10736
Teppo Järvelin0efac532019-10-04 13:21:08 +030010737 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010738 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010739 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010740 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010741 ssl->conf->renego_period + ep_len, 8 - ep_len );
10742
10743 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010744 {
10745 return( 0 );
10746 }
10747
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010752
10753/*
10754 * Receive application data decrypted from the SSL layer
10755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010757{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010758 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010759 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010760
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010761 if( ssl == NULL || ssl->conf == NULL )
10762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010770 return( ret );
10771
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010772 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010774 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010775 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010776 return( ret );
10777 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010778 }
10779#endif
10780
Hanno Becker4a810fb2017-05-24 16:27:30 +010010781 /*
10782 * Check if renegotiation is necessary and/or handshake is
10783 * in process. If yes, perform/continue, and fall through
10784 * if an unexpected packet is received while the client
10785 * is waiting for the ServerHello.
10786 *
10787 * (There is no equivalent to the last condition on
10788 * the server-side as it is not treated as within
10789 * a handshake while waiting for the ClientHello
10790 * after a renegotiation request.)
10791 */
10792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010794 ret = ssl_check_ctr_renegotiate( ssl );
10795 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10796 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010799 return( ret );
10800 }
10801#endif
10802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010806 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10807 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010810 return( ret );
10811 }
10812 }
10813
Hanno Beckere41158b2017-10-23 13:30:32 +010010814 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010815 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010816 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010817 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010818 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10819 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010820 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010821 ssl_set_timer( ssl,
10822 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010823 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010824
Hanno Becker327c93b2018-08-15 13:56:18 +010010825 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010826 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010827 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10828 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010829
Hanno Becker4a810fb2017-05-24 16:27:30 +010010830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10831 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010832 }
10833
10834 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010836 {
10837 /*
10838 * OpenSSL sends empty messages to randomize the IV
10839 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010840 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010842 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010843 return( 0 );
10844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010846 return( ret );
10847 }
10848 }
10849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010850 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010853
Hanno Becker4a810fb2017-05-24 16:27:30 +010010854 /*
10855 * - For client-side, expect SERVER_HELLO_REQUEST.
10856 * - For server-side, expect CLIENT_HELLO.
10857 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10858 */
10859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010861 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10862 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010864 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010867
10868 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010871 {
10872 continue;
10873 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010874 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010875#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010876#if defined(MBEDTLS_SSL_PROTO_TLS)
10877 {
10878 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10879 }
10880#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010881 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010882#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010883
Hanno Becker4a810fb2017-05-24 16:27:30 +010010884#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010885 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10886 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010890
10891 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010893 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010894 {
10895 continue;
10896 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010897 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010898#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010899#if defined(MBEDTLS_SSL_PROTO_TLS)
10900 {
10901 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10902 }
10903#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010904 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010905#endif /* MBEDTLS_SSL_SRV_C */
10906
Hanno Becker21df7f92017-10-17 11:03:26 +010010907#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010908 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010909 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10910 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010911 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010912 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10913 {
10914 /*
10915 * Accept renegotiation request
10916 */
Paul Bakker48916f92012-09-16 19:57:18 +000010917
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010918 /* DTLS clients need to know renego is server-initiated */
10919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010920 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010921 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10922 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010923 {
10924 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10925 }
10926#endif
10927 ret = ssl_start_renegotiation( ssl );
10928 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10929 ret != 0 )
10930 {
10931 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10932 return( ret );
10933 }
10934 }
10935 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010936#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010937 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010938 /*
10939 * Refuse renegotiation
10940 */
10941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010945 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010946 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010947 /* SSLv3 does not have a "no_renegotiation" warning, so
10948 we send a fatal alert and abort the connection. */
10949 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10950 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10951 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010952 }
10953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10956 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010957 if( mbedtls_ssl_ver_geq(
10958 mbedtls_ssl_get_minor_ver( ssl ),
10959 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010960 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010961 ret = mbedtls_ssl_send_alert_message( ssl,
10962 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10963 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10964 if( ret != 0 )
10965 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010966 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10969 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010973 }
Paul Bakker48916f92012-09-16 19:57:18 +000010974 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010975
Hanno Becker90333da2017-10-10 11:27:13 +010010976 /* At this point, we don't know whether the renegotiation has been
10977 * completed or not. The cases to consider are the following:
10978 * 1) The renegotiation is complete. In this case, no new record
10979 * has been read yet.
10980 * 2) The renegotiation is incomplete because the client received
10981 * an application data record while awaiting the ServerHello.
10982 * 3) The renegotiation is incomplete because the client received
10983 * a non-handshake, non-application data message while awaiting
10984 * the ServerHello.
10985 * In each of these case, looping will be the proper action:
10986 * - For 1), the next iteration will read a new record and check
10987 * if it's application data.
10988 * - For 2), the loop condition isn't satisfied as application data
10989 * is present, hence continue is the same as break
10990 * - For 3), the loop condition is satisfied and read_record
10991 * will re-deliver the message that was held back by the client
10992 * when expecting the ServerHello.
10993 */
10994 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010995 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010996#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010998 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010999 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011000 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011001 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011004 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011006 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011007 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011011 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11012 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011015 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011016 }
11017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011018 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11021 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011022 }
11023
11024 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011025
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011026 /* We're going to return something now, cancel timer,
11027 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011029 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011030
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011032 /* If we requested renego but received AppData, resend HelloRequest.
11033 * Do it now, after setting in_offt, to avoid taking this branch
11034 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011036 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11037 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011039 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011040 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011043 return( ret );
11044 }
11045 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011047#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011048 }
11049
11050 n = ( len < ssl->in_msglen )
11051 ? len : ssl->in_msglen;
11052
Teppo Järvelin91d79382019-10-02 09:09:31 +030011053 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011054 ssl->in_msglen -= n;
11055
11056 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011057 {
11058 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011059 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011060 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011061 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011062 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011063 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011064 /* more data available */
11065 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011066 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011069
Paul Bakker23986e52011-04-24 08:57:21 +000011070 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011071}
11072
11073/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011074 * Send application data to be encrypted by the SSL layer, taking care of max
11075 * fragment length and buffer size.
11076 *
11077 * According to RFC 5246 Section 6.2.1:
11078 *
11079 * Zero-length fragments of Application data MAY be sent as they are
11080 * potentially useful as a traffic analysis countermeasure.
11081 *
11082 * Therefore, it is possible that the input message length is 0 and the
11083 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011084 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011085static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011086 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011087{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011088 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11089 const size_t max_len = (size_t) ret;
11090
11091 if( ret < 0 )
11092 {
11093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11094 return( ret );
11095 }
11096
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011097 if( len > max_len )
11098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011099#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011100 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011103 "maximum fragment length: %d > %d",
11104 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011106 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011107 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011108#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011109#if defined(MBEDTLS_SSL_PROTO_TLS)
11110 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011111 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011112 }
11113#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011114 }
Paul Bakker887bd502011-06-08 13:10:54 +000011115
Paul Bakker5121ce52009-01-03 21:22:43 +000011116 if( ssl->out_left != 0 )
11117 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011118 /*
11119 * The user has previously tried to send the data and
11120 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11121 * written. In this case, we expect the high-level write function
11122 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011127 return( ret );
11128 }
11129 }
Paul Bakker887bd502011-06-08 13:10:54 +000011130 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011131 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011132 /*
11133 * The user is trying to send a message the first time, so we need to
11134 * copy the data into the internal buffers and setup the data structure
11135 * to keep track of partial writes
11136 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011137 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011139 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011140
Hanno Becker67bc7c32018-08-06 11:33:50 +010011141 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011144 return( ret );
11145 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011146 }
11147
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011148 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011149}
11150
11151/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011152 * Write application data, doing 1/n-1 splitting if necessary.
11153 *
11154 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011155 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011156 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011158#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011159static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011160 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011161{
11162 int ret;
11163
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011164 if( ssl->conf->cbc_record_splitting ==
11165 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011166 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011167 mbedtls_ssl_ver_gt(
11168 mbedtls_ssl_get_minor_ver( ssl ),
11169 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11171 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011172 {
11173 return( ssl_write_real( ssl, buf, len ) );
11174 }
11175
11176 if( ssl->split_done == 0 )
11177 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011178 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011179 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011180 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011181 }
11182
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011183 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11184 return( ret );
11185 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011186
11187 return( ret + 1 );
11188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011190
11191/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011192 * Write application data (public-facing wrapper)
11193 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011194int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011195{
11196 int ret;
11197
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011199
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011200 if( ssl == NULL || ssl->conf == NULL )
11201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11202
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011203#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011204 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11205 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011206 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011207 return( ret );
11208 }
11209#endif
11210
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011211 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011212 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011213 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011214 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011216 return( ret );
11217 }
11218 }
11219
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011220#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011221 ret = ssl_write_split( ssl, buf, len );
11222#else
11223 ret = ssl_write_real( ssl, buf, len );
11224#endif
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
11228 return( ret );
11229}
11230
11231/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011232 * Notify the peer that the connection is being closed
11233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011235{
11236 int ret;
11237
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011238 if( ssl == NULL || ssl->conf == NULL )
11239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011242
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011243 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011246 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011248 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11249 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11250 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011253 return( ret );
11254 }
11255 }
11256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011258
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011259 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011260}
11261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011262void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011263{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011264 if( transform == NULL )
11265 return;
11266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011267#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011268 deflateEnd( &transform->ctx_deflate );
11269 inflateEnd( &transform->ctx_inflate );
11270#endif
11271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11273 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011274
Hanno Becker92231322018-01-03 15:32:51 +000011275#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011276 mbedtls_md_free( &transform->md_ctx_enc );
11277 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011278#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011279
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011280 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011281}
11282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011283#if defined(MBEDTLS_X509_CRT_PARSE_C)
11284static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011286 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011287
11288 while( cur != NULL )
11289 {
11290 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011291 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011292 cur = next;
11293 }
11294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011296
Hanno Becker0271f962018-08-16 13:23:47 +010011297#if defined(MBEDTLS_SSL_PROTO_DTLS)
11298
11299static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11300{
11301 unsigned offset;
11302 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11303
11304 if( hs == NULL )
11305 return;
11306
Hanno Becker283f5ef2018-08-24 09:34:47 +010011307 ssl_free_buffered_record( ssl );
11308
Hanno Becker0271f962018-08-16 13:23:47 +010011309 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011310 ssl_buffering_free_slot( ssl, offset );
11311}
11312
11313static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11314 uint8_t slot )
11315{
11316 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11317 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011318
11319 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11320 return;
11321
Hanno Beckere605b192018-08-21 15:59:07 +010011322 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011323 {
Hanno Beckere605b192018-08-21 15:59:07 +010011324 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011325 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011326 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011327 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011328 }
11329}
11330
11331#endif /* MBEDTLS_SSL_PROTO_DTLS */
11332
Gilles Peskine9b562d52018-04-25 20:32:43 +020011333void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011334{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011335 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11336
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011337 if( handshake == NULL )
11338 return;
11339
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011340#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11341 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11342 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011343 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011344 handshake->async_in_progress = 0;
11345 }
11346#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11347
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011348#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11349 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11350 mbedtls_md5_free( &handshake->fin_md5 );
11351 mbedtls_sha1_free( &handshake->fin_sha1 );
11352#endif
11353#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11354#if defined(MBEDTLS_SHA256_C)
11355 mbedtls_sha256_free( &handshake->fin_sha256 );
11356#endif
11357#if defined(MBEDTLS_SHA512_C)
11358 mbedtls_sha512_free( &handshake->fin_sha512 );
11359#endif
11360#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011362#if defined(MBEDTLS_DHM_C)
11363 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365#if defined(MBEDTLS_ECDH_C)
11366 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011367#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011368#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011369 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011370#if defined(MBEDTLS_SSL_CLI_C)
11371 mbedtls_free( handshake->ecjpake_cache );
11372 handshake->ecjpake_cache = NULL;
11373 handshake->ecjpake_cache_len = 0;
11374#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011375#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011376
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011377#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11378 if( handshake->psk != NULL )
11379 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011380 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011381 mbedtls_free( handshake->psk );
11382 }
11383#endif
11384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011385#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11386 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011387 /*
11388 * Free only the linked list wrapper, not the keys themselves
11389 * since the belong to the SNI callback
11390 */
11391 if( handshake->sni_key_cert != NULL )
11392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011393 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011394
11395 while( cur != NULL )
11396 {
11397 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011398 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011399 cur = next;
11400 }
11401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011402#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011403
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011404#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011405 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011406 if( handshake->ecrs_peer_cert != NULL )
11407 {
11408 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11409 mbedtls_free( handshake->ecrs_peer_cert );
11410 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011411#endif
11412
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011413#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11414 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11415 mbedtls_pk_free( &handshake->peer_pubkey );
11416#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011418#if defined(MBEDTLS_SSL_PROTO_DTLS)
11419 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011420 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011421 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011422#endif
11423
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011424 mbedtls_platform_zeroize( handshake,
11425 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011426}
11427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011428void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011429{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011430 if( session == NULL )
11431 return;
11432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011433#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011434 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011435#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011436
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011437#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011438 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011439#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011440
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011441 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011442}
11443
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011444#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011445
11446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11447#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11448#else
11449#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11450#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11451
11452#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11453#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11454#else
11455#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11456#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11457
11458#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11459#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11460#else
11461#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11462#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11463
11464#if defined(MBEDTLS_SSL_ALPN)
11465#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11466#else
11467#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11468#endif /* MBEDTLS_SSL_ALPN */
11469
11470#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11471#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11472#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11473#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11474
11475#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11476 ( (uint32_t) ( \
11477 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11478 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11479 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11480 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11481 0u ) )
11482
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011483static unsigned char ssl_serialized_context_header[] = {
11484 MBEDTLS_VERSION_MAJOR,
11485 MBEDTLS_VERSION_MINOR,
11486 MBEDTLS_VERSION_PATCH,
11487 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11488 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011489 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11490 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11491 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011492};
11493
Paul Bakker5121ce52009-01-03 21:22:43 +000011494/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011495 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011496 *
11497 * The format of the serialized data is:
11498 * (in the presentation language of TLS, RFC 8446 section 3)
11499 *
11500 * // header
11501 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011502 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011503 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011504 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011505 * Note: When updating the format, remember to keep these
11506 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011507 *
11508 * // session sub-structure
11509 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11510 * // transform sub-structure
11511 * uint8 random[64]; // ServerHello.random+ClientHello.random
11512 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11513 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11514 * // fields from ssl_context
11515 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11516 * uint64 in_window_top; // DTLS: last validated record seq_num
11517 * uint64 in_window; // DTLS: bitmask for replay protection
11518 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11519 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11520 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11521 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11522 *
11523 * Note that many fields of the ssl_context or sub-structures are not
11524 * serialized, as they fall in one of the following categories:
11525 *
11526 * 1. forced value (eg in_left must be 0)
11527 * 2. pointer to dynamically-allocated memory (eg session, transform)
11528 * 3. value can be re-derived from other data (eg session keys from MS)
11529 * 4. value was temporary (eg content of input buffer)
11530 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011531 */
11532int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11533 unsigned char *buf,
11534 size_t buf_len,
11535 size_t *olen )
11536{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011537 unsigned char *p = buf;
11538 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011539 size_t session_len;
11540 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011541
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011542 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011543 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11544 * this function's documentation.
11545 *
11546 * These are due to assumptions/limitations in the implementation. Some of
11547 * them are likely to stay (no handshake in progress) some might go away
11548 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011549 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011550 /* The initial handshake must be over */
11551 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011553 if( ssl->handshake != NULL )
11554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11555 /* Double-check that sub-structures are indeed ready */
11556 if( ssl->transform == NULL || ssl->session == NULL )
11557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11558 /* There must be no pending incoming or outgoing data */
11559 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11561 if( ssl->out_left != 0 )
11562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11563 /* Protocol must be DLTS, not TLS */
11564 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11566 /* Version must be 1.2 */
11567 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11569 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11571 /* We must be using an AEAD ciphersuite */
11572 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11574 /* Renegotiation must not be enabled */
11575 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011577
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011578 /*
11579 * Version and format identifier
11580 */
11581 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011582
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011583 if( used <= buf_len )
11584 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011585 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011586 sizeof( ssl_serialized_context_header ) );
11587 p += sizeof( ssl_serialized_context_header );
11588 }
11589
11590 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011591 * Session (length + data)
11592 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011593 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011594 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11595 return( ret );
11596
11597 used += 4 + session_len;
11598 if( used <= buf_len )
11599 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011600 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011601
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011602 ret = ssl_session_save( ssl->session, 1,
11603 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011604 if( ret != 0 )
11605 return( ret );
11606
11607 p += session_len;
11608 }
11609
11610 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011611 * Transform
11612 */
11613 used += sizeof( ssl->transform->randbytes );
11614 if( used <= buf_len )
11615 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011616 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011617 sizeof( ssl->transform->randbytes ) );
11618 p += sizeof( ssl->transform->randbytes );
11619 }
11620
11621#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11622 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11623 if( used <= buf_len )
11624 {
11625 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011626 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11627 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011628 p += ssl->transform->in_cid_len;
11629
11630 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011631 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11632 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011633 p += ssl->transform->out_cid_len;
11634 }
11635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11636
11637 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011638 * Saved fields from top-level ssl_context structure
11639 */
11640#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11641 used += 4;
11642 if( used <= buf_len )
11643 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011644 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011645 }
11646#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11647
11648#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11649 used += 16;
11650 if( used <= buf_len )
11651 {
11652 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11653 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11654 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11655 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11656 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11657 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11658 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11659 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11660
11661 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11662 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11663 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11664 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11665 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11666 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11667 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11668 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11669 }
11670#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11671
11672#if defined(MBEDTLS_SSL_PROTO_DTLS)
11673 used += 1;
11674 if( used <= buf_len )
11675 {
11676 *p++ = ssl->disable_datagram_packing;
11677 }
11678#endif /* MBEDTLS_SSL_PROTO_DTLS */
11679
11680 used += 8;
11681 if( used <= buf_len )
11682 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011683 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011684 p += 8;
11685 }
11686
11687#if defined(MBEDTLS_SSL_PROTO_DTLS)
11688 used += 2;
11689 if( used <= buf_len )
11690 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011691 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011692 }
11693#endif /* MBEDTLS_SSL_PROTO_DTLS */
11694
11695#if defined(MBEDTLS_SSL_ALPN)
11696 {
11697 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011698 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011699 : 0;
11700
11701 used += 1 + alpn_len;
11702 if( used <= buf_len )
11703 {
11704 *p++ = alpn_len;
11705
11706 if( ssl->alpn_chosen != NULL )
11707 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011708 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011709 p += alpn_len;
11710 }
11711 }
11712 }
11713#endif /* MBEDTLS_SSL_ALPN */
11714
11715 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011716 * Done
11717 */
11718 *olen = used;
11719
11720 if( used > buf_len )
11721 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011722
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011723 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11724
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011725 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011726}
11727
11728/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011729 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011730 *
11731 * This internal version is wrapped by a public function that cleans up in
11732 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011733 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011734static int ssl_context_load( mbedtls_ssl_context *ssl,
11735 const unsigned char *buf,
11736 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011737{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011738 const unsigned char *p = buf;
11739 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011740 size_t session_len;
11741 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011742
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011743 /*
11744 * The context should have been freshly setup or reset.
11745 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011746 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011747 * renegotiating, or if the user mistakenly loaded a session first.)
11748 */
11749 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11750 ssl->session != NULL )
11751 {
11752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11753 }
11754
11755 /*
11756 * We can't check that the config matches the initial one, but we can at
11757 * least check it matches the requirements for serializing.
11758 */
11759 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011760 mbedtls_ssl_ver_lt(
11761 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11762 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11763 mbedtls_ssl_ver_gt(
11764 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11765 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11766 mbedtls_ssl_ver_lt(
11767 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11768 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11769 mbedtls_ssl_ver_gt(
11770 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11771 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011772 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011773 {
11774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11775 }
11776
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011777 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11778
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011779 /*
11780 * Check version identifier
11781 */
11782 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11784
Teppo Järvelin650343c2019-10-03 15:36:59 +030011785 // use regular memcmp as header is not that critical
11786 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011787 sizeof( ssl_serialized_context_header ) ) != 0 )
11788 {
11789 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11790 }
11791 p += sizeof( ssl_serialized_context_header );
11792
11793 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011794 * Session
11795 */
11796 if( (size_t)( end - p ) < 4 )
11797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11798
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011799 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011800 p += 4;
11801
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011802 /* This has been allocated by ssl_handshake_init(), called by
11803 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11804 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011805 ssl->session_in = ssl->session;
11806 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011807 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011808
11809 if( (size_t)( end - p ) < session_len )
11810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11811
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011812 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011813 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011814 {
11815 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011816 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011817 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011818
11819 p += session_len;
11820
11821 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011822 * Transform
11823 */
11824
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011825 /* This has been allocated by ssl_handshake_init(), called by
11826 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11827 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011828 ssl->transform_in = ssl->transform;
11829 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011830 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011831
11832 /* Read random bytes and populate structure */
11833 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11835
11836 ret = ssl_populate_transform( ssl->transform,
11837 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11838 ssl->session->master,
11839#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11840#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11841 ssl->session->encrypt_then_mac,
11842#endif
11843#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11844 ssl->session->trunc_hmac,
11845#endif
11846#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11847#if defined(MBEDTLS_ZLIB_SUPPORT)
11848 ssl->session->compression,
11849#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011850 p, /* currently pointing to randbytes */
11851 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11852 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11853 ssl );
11854 if( ret != 0 )
11855 return( ret );
11856
11857 p += sizeof( ssl->transform->randbytes );
11858
11859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11860 /* Read connection IDs and store them */
11861 if( (size_t)( end - p ) < 1 )
11862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11863
11864 ssl->transform->in_cid_len = *p++;
11865
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011866 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11868
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011869 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11870 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011871 p += ssl->transform->in_cid_len;
11872
11873 ssl->transform->out_cid_len = *p++;
11874
11875 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11877
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011878 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11879 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011880 p += ssl->transform->out_cid_len;
11881#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11882
11883 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011884 * Saved fields from top-level ssl_context structure
11885 */
11886#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11887 if( (size_t)( end - p ) < 4 )
11888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11889
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011890 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011891 p += 4;
11892#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11893
11894#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11895 if( (size_t)( end - p ) < 16 )
11896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11897
11898 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11899 ( (uint64_t) p[1] << 48 ) |
11900 ( (uint64_t) p[2] << 40 ) |
11901 ( (uint64_t) p[3] << 32 ) |
11902 ( (uint64_t) p[4] << 24 ) |
11903 ( (uint64_t) p[5] << 16 ) |
11904 ( (uint64_t) p[6] << 8 ) |
11905 ( (uint64_t) p[7] );
11906 p += 8;
11907
11908 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11909 ( (uint64_t) p[1] << 48 ) |
11910 ( (uint64_t) p[2] << 40 ) |
11911 ( (uint64_t) p[3] << 32 ) |
11912 ( (uint64_t) p[4] << 24 ) |
11913 ( (uint64_t) p[5] << 16 ) |
11914 ( (uint64_t) p[6] << 8 ) |
11915 ( (uint64_t) p[7] );
11916 p += 8;
11917#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11918
11919#if defined(MBEDTLS_SSL_PROTO_DTLS)
11920 if( (size_t)( end - p ) < 1 )
11921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11922
11923 ssl->disable_datagram_packing = *p++;
11924#endif /* MBEDTLS_SSL_PROTO_DTLS */
11925
11926 if( (size_t)( end - p ) < 8 )
11927 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11928
Teppo Järvelin91d79382019-10-02 09:09:31 +030011929 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011930 p += 8;
11931
11932#if defined(MBEDTLS_SSL_PROTO_DTLS)
11933 if( (size_t)( end - p ) < 2 )
11934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011935 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011936 p += 2;
11937#endif /* MBEDTLS_SSL_PROTO_DTLS */
11938
11939#if defined(MBEDTLS_SSL_ALPN)
11940 {
11941 uint8_t alpn_len;
11942 const char **cur;
11943
11944 if( (size_t)( end - p ) < 1 )
11945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11946
11947 alpn_len = *p++;
11948
11949 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11950 {
11951 /* alpn_chosen should point to an item in the configured list */
11952 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11953 {
11954 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030011955 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011956 {
11957 ssl->alpn_chosen = *cur;
11958 break;
11959 }
11960 }
11961 }
11962
11963 /* can only happen on conf mismatch */
11964 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11966
11967 p += alpn_len;
11968 }
11969#endif /* MBEDTLS_SSL_ALPN */
11970
11971 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011972 * Forced fields from top-level ssl_context structure
11973 *
11974 * Most of them already set to the correct value by mbedtls_ssl_init() and
11975 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11976 */
11977 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11978
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011979#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011980 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011981#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11982#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011983 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011984#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011985
Hanno Becker83985822019-08-30 10:42:49 +010011986 /* Adjust pointers for header fields of outgoing records to
11987 * the given transform, accounting for explicit IV and CID. */
11988 ssl_update_out_pointers( ssl, ssl->transform );
11989
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011990#if defined(MBEDTLS_SSL_PROTO_DTLS)
11991 ssl->in_epoch = 1;
11992#endif
11993
11994 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11995 * which we don't want - otherwise we'd end up freeing the wrong transform
11996 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11997 if( ssl->handshake != NULL )
11998 {
11999 mbedtls_ssl_handshake_free( ssl );
12000 mbedtls_free( ssl->handshake );
12001 ssl->handshake = NULL;
12002 }
12003
12004 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012005 * Done - should have consumed entire buffer
12006 */
12007 if( p != end )
12008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012009
12010 return( 0 );
12011}
12012
12013/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012014 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012015 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012016int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012017 const unsigned char *buf,
12018 size_t len )
12019{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012020 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012021
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012022 if( ret != 0 )
12023 mbedtls_ssl_free( context );
12024
12025 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012026}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012027#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012028
12029/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012030 * Free an SSL context
12031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012032void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012033{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012034 if( ssl == NULL )
12035 return;
12036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012038
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012039 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012040 {
Angus Grattond8213d02016-05-25 20:56:48 +100012041 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012042 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012043 }
12044
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012045 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012046 {
Angus Grattond8213d02016-05-25 20:56:48 +100012047 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012048 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012049 }
12050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012051#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012052 if( ssl->compress_buf != NULL )
12053 {
Angus Grattond8213d02016-05-25 20:56:48 +100012054 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012055 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012056 }
12057#endif
12058
Paul Bakker48916f92012-09-16 19:57:18 +000012059 if( ssl->transform )
12060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012061 mbedtls_ssl_transform_free( ssl->transform );
12062 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012063 }
12064
12065 if( ssl->handshake )
12066 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012067 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012068 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12069 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012071 mbedtls_free( ssl->handshake );
12072 mbedtls_free( ssl->transform_negotiate );
12073 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012074 }
12075
Paul Bakkerc0463502013-02-14 11:19:38 +010012076 if( ssl->session )
12077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012078 mbedtls_ssl_session_free( ssl->session );
12079 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012080 }
12081
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012082#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012083 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012084 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012085 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012086 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012087 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012090#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12091 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12094 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012095 }
12096#endif
12097
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012098#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012099 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012100#endif
12101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012103
Paul Bakker86f04f42013-02-14 11:20:09 +010012104 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012105 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012106}
12107
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012108/*
12109 * Initialze mbedtls_ssl_config
12110 */
12111void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12112{
12113 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012114
12115#if !defined(MBEDTLS_SSL_PROTO_TLS)
12116 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12117#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012118}
12119
Simon Butcherc97b6972015-12-27 23:48:17 +000012120#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012121#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012122static int ssl_preset_default_hashes[] = {
12123#if defined(MBEDTLS_SHA512_C)
12124 MBEDTLS_MD_SHA512,
12125 MBEDTLS_MD_SHA384,
12126#endif
12127#if defined(MBEDTLS_SHA256_C)
12128 MBEDTLS_MD_SHA256,
12129 MBEDTLS_MD_SHA224,
12130#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012131#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012132 MBEDTLS_MD_SHA1,
12133#endif
12134 MBEDTLS_MD_NONE
12135};
Simon Butcherc97b6972015-12-27 23:48:17 +000012136#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012137#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012138
Hanno Becker73f4cb12019-06-27 13:51:07 +010012139#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012140static int ssl_preset_suiteb_ciphersuites[] = {
12141 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12142 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12143 0
12144};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012145#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012146
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012147#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012148#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012149static int ssl_preset_suiteb_hashes[] = {
12150 MBEDTLS_MD_SHA256,
12151 MBEDTLS_MD_SHA384,
12152 MBEDTLS_MD_NONE
12153};
12154#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012155#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012156
Hanno Beckerc1096e72019-06-19 12:30:41 +010012157#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012158static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012159#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012160 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012161#endif
12162#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012163 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012164#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012165 MBEDTLS_ECP_DP_NONE
12166};
12167#endif
12168
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012169/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012170 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012171 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012172int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012173 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012174{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012175#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012176 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012177#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012178
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012179 /* Use the functions here so that they are covered in tests,
12180 * but otherwise access member directly for efficiency */
12181 mbedtls_ssl_conf_endpoint( conf, endpoint );
12182 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012183
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012184 /*
12185 * Things that are common to all presets
12186 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012187#if defined(MBEDTLS_SSL_CLI_C)
12188 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12189 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012190#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012191 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012192#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012193#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12194 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12195#endif
12196 }
12197#endif
12198
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012199#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012200 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012201#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012202
12203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12204 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12205#endif
12206
12207#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012208#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012209 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012210#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12211#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012212 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012213 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012214#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012215#endif
12216
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012217#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12218 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12219#endif
12220
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012221#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012222 conf->f_cookie_write = ssl_cookie_write_dummy;
12223 conf->f_cookie_check = ssl_cookie_check_dummy;
12224#endif
12225
Hanno Becker7f376f42019-06-12 16:20:48 +010012226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12227 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012228 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12229#endif
12230
Janos Follath088ce432017-04-10 12:42:31 +010012231#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012232#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012233 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012234#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12235#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012236
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012238#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012239 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012240#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12241#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012242 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012243#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012245
12246#if defined(MBEDTLS_SSL_RENEGOTIATION)
12247 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012248 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12249 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012250#endif
12251
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012252#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12253 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12254 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012255 const unsigned char dhm_p[] =
12256 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12257 const unsigned char dhm_g[] =
12258 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12259
Hanno Beckera90658f2017-10-04 15:29:08 +010012260 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12261 dhm_p, sizeof( dhm_p ),
12262 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012263 {
12264 return( ret );
12265 }
12266 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012267#endif
12268
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012269 /*
12270 * Preset-specific defaults
12271 */
12272 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012273 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012274 /*
12275 * NSA Suite B
12276 */
12277 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012278#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012279 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012280#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12281#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012282 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012283#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12284#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012285 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012286#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12287#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012288 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012289#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012290
Hanno Becker73f4cb12019-06-27 13:51:07 +010012291#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012292 conf->ciphersuite_list[0] =
12293 conf->ciphersuite_list[1] =
12294 conf->ciphersuite_list[2] =
12295 conf->ciphersuite_list[3] =
12296 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012297#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012298
12299#if defined(MBEDTLS_X509_CRT_PARSE_C)
12300 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012301#endif
12302
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012303#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012304#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012305 conf->sig_hashes = ssl_preset_suiteb_hashes;
12306#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012307#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012308
12309#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012310#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012311 conf->curve_list = ssl_preset_suiteb_curves;
12312#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012313#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012314 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012315
12316 /*
12317 * Default
12318 */
12319 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012320#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012321 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12322 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12323 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12324 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012325#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12326#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012327 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12328 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12329 MBEDTLS_SSL_MIN_MINOR_VERSION :
12330 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012332 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012333 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12334#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012335#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12336#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12337 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12338#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12339#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12340 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12341#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012342
Hanno Becker73f4cb12019-06-27 13:51:07 +010012343#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012344 conf->ciphersuite_list[0] =
12345 conf->ciphersuite_list[1] =
12346 conf->ciphersuite_list[2] =
12347 conf->ciphersuite_list[3] =
12348 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012349#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012350
12351#if defined(MBEDTLS_X509_CRT_PARSE_C)
12352 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12353#endif
12354
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012355#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012356#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012357 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012358#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012359#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012360
12361#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012362#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012363 conf->curve_list = mbedtls_ecp_grp_id_list();
12364#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012365#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012366
12367#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12368 conf->dhm_min_bitlen = 1024;
12369#endif
12370 }
12371
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012372 return( 0 );
12373}
12374
12375/*
12376 * Free mbedtls_ssl_config
12377 */
12378void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12379{
12380#if defined(MBEDTLS_DHM_C)
12381 mbedtls_mpi_free( &conf->dhm_P );
12382 mbedtls_mpi_free( &conf->dhm_G );
12383#endif
12384
12385#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12386 if( conf->psk != NULL )
12387 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012388 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012389 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012390 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012391 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012392 }
12393
12394 if( conf->psk_identity != NULL )
12395 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012396 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012397 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012398 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012399 conf->psk_identity_len = 0;
12400 }
12401#endif
12402
12403#if defined(MBEDTLS_X509_CRT_PARSE_C)
12404 ssl_key_cert_free( conf->key_cert );
12405#endif
12406
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012407 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012408}
12409
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012410#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012411 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12412 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012413/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012414 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012416unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012418#if defined(MBEDTLS_RSA_C)
12419 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12420 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012421#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012422#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012423 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12424 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012426 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012427}
12428
Hanno Becker7e5437a2017-04-28 17:15:26 +010012429unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12430{
12431 switch( type ) {
12432 case MBEDTLS_PK_RSA:
12433 return( MBEDTLS_SSL_SIG_RSA );
12434 case MBEDTLS_PK_ECDSA:
12435 case MBEDTLS_PK_ECKEY:
12436 return( MBEDTLS_SSL_SIG_ECDSA );
12437 default:
12438 return( MBEDTLS_SSL_SIG_ANON );
12439 }
12440}
12441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012442mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012443{
12444 switch( sig )
12445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012446#if defined(MBEDTLS_RSA_C)
12447 case MBEDTLS_SSL_SIG_RSA:
12448 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +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 case MBEDTLS_SSL_SIG_ECDSA:
12452 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012453#endif
12454 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012455 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012456 }
12457}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012458#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012459
Hanno Becker7e5437a2017-04-28 17:15:26 +010012460#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12461 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12462
12463/* Find an entry in a signature-hash set matching a given hash algorithm. */
12464mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12465 mbedtls_pk_type_t sig_alg )
12466{
12467 switch( sig_alg )
12468 {
12469 case MBEDTLS_PK_RSA:
12470 return( set->rsa );
12471 case MBEDTLS_PK_ECDSA:
12472 return( set->ecdsa );
12473 default:
12474 return( MBEDTLS_MD_NONE );
12475 }
12476}
12477
12478/* Add a signature-hash-pair to a signature-hash set */
12479void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12480 mbedtls_pk_type_t sig_alg,
12481 mbedtls_md_type_t md_alg )
12482{
12483 switch( sig_alg )
12484 {
12485 case MBEDTLS_PK_RSA:
12486 if( set->rsa == MBEDTLS_MD_NONE )
12487 set->rsa = md_alg;
12488 break;
12489
12490 case MBEDTLS_PK_ECDSA:
12491 if( set->ecdsa == MBEDTLS_MD_NONE )
12492 set->ecdsa = md_alg;
12493 break;
12494
12495 default:
12496 break;
12497 }
12498}
12499
12500/* Allow exactly one hash algorithm for each signature. */
12501void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12502 mbedtls_md_type_t md_alg )
12503{
12504 set->rsa = md_alg;
12505 set->ecdsa = md_alg;
12506}
12507
12508#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12509 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12510
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012511/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012512 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012514mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012515{
12516 switch( hash )
12517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012518#if defined(MBEDTLS_MD5_C)
12519 case MBEDTLS_SSL_HASH_MD5:
12520 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012522#if defined(MBEDTLS_SHA1_C)
12523 case MBEDTLS_SSL_HASH_SHA1:
12524 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012526#if defined(MBEDTLS_SHA256_C)
12527 case MBEDTLS_SSL_HASH_SHA224:
12528 return( MBEDTLS_MD_SHA224 );
12529 case MBEDTLS_SSL_HASH_SHA256:
12530 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012532#if defined(MBEDTLS_SHA512_C)
12533 case MBEDTLS_SSL_HASH_SHA384:
12534 return( MBEDTLS_MD_SHA384 );
12535 case MBEDTLS_SSL_HASH_SHA512:
12536 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012537#endif
12538 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012539 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012540 }
12541}
12542
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012543/*
12544 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12545 */
12546unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12547{
12548 switch( md )
12549 {
12550#if defined(MBEDTLS_MD5_C)
12551 case MBEDTLS_MD_MD5:
12552 return( MBEDTLS_SSL_HASH_MD5 );
12553#endif
12554#if defined(MBEDTLS_SHA1_C)
12555 case MBEDTLS_MD_SHA1:
12556 return( MBEDTLS_SSL_HASH_SHA1 );
12557#endif
12558#if defined(MBEDTLS_SHA256_C)
12559 case MBEDTLS_MD_SHA224:
12560 return( MBEDTLS_SSL_HASH_SHA224 );
12561 case MBEDTLS_MD_SHA256:
12562 return( MBEDTLS_SSL_HASH_SHA256 );
12563#endif
12564#if defined(MBEDTLS_SHA512_C)
12565 case MBEDTLS_MD_SHA384:
12566 return( MBEDTLS_SSL_HASH_SHA384 );
12567 case MBEDTLS_MD_SHA512:
12568 return( MBEDTLS_SSL_HASH_SHA512 );
12569#endif
12570 default:
12571 return( MBEDTLS_SSL_HASH_NONE );
12572 }
12573}
12574
Hanno Beckeree902df2019-08-23 13:47:47 +010012575#if defined(MBEDTLS_USE_TINYCRYPT)
12576/*
12577 * Check if a curve proposed by the peer is in our list.
12578 * Return 0 if we're willing to use it, -1 otherwise.
12579 */
12580int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12581 mbedtls_uecc_group_id grp_id )
12582{
12583 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12584 if( own_ec_id == grp_id )
12585 return( 0 );
12586 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12587
12588 return( -1 );
12589}
12590#endif /* MBEDTLS_USE_TINYCRYPT */
12591
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012592#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012593/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012594 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012595 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012596 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012597int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12598 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012599{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012600 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12601 if( own_ec_id == grp_id )
12602 return( 0 );
12603 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012604
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012605 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012606}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012607#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012608
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012609#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012610/*
12611 * Check if a hash proposed by the peer is in our list.
12612 * Return 0 if we're willing to use it, -1 otherwise.
12613 */
12614int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12615 mbedtls_md_type_t md )
12616{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012617 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12618 if( md_alg == md )
12619 return( 0 );
12620 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012621
12622 return( -1 );
12623}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012624#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012626#if defined(MBEDTLS_X509_CRT_PARSE_C)
12627int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012628 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012629 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012630 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012631{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012632 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012633#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012634 int usage = 0;
12635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012636#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012637 const char *ext_oid;
12638 size_t ext_len;
12639#endif
12640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012641#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12642 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012643 ((void) cert);
12644 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012645 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012646#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012648#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12649 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012650 {
12651 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012652 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012654 case MBEDTLS_KEY_EXCHANGE_RSA:
12655 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012656 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012657 break;
12658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012659 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12660 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12661 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12662 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012663 break;
12664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012665 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12666 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012667 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012668 break;
12669
12670 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012671 case MBEDTLS_KEY_EXCHANGE_NONE:
12672 case MBEDTLS_KEY_EXCHANGE_PSK:
12673 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12674 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012675 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012676 usage = 0;
12677 }
12678 }
12679 else
12680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012681 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12682 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012683 }
12684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012685 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012686 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012687 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012688 ret = -1;
12689 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012690#else
12691 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012692#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012694#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12695 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012697 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12698 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012699 }
12700 else
12701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012702 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12703 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012704 }
12705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012706 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012707 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012708 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012709 ret = -1;
12710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012711#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012712
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012713 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012715#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012716
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012717#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12718 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12719int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12720 unsigned char *output,
12721 unsigned char *data, size_t data_len )
12722{
12723 int ret = 0;
12724 mbedtls_md5_context mbedtls_md5;
12725 mbedtls_sha1_context mbedtls_sha1;
12726
12727 mbedtls_md5_init( &mbedtls_md5 );
12728 mbedtls_sha1_init( &mbedtls_sha1 );
12729
12730 /*
12731 * digitally-signed struct {
12732 * opaque md5_hash[16];
12733 * opaque sha_hash[20];
12734 * };
12735 *
12736 * md5_hash
12737 * MD5(ClientHello.random + ServerHello.random
12738 * + ServerParams);
12739 * sha_hash
12740 * SHA(ClientHello.random + ServerHello.random
12741 * + ServerParams);
12742 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012743 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012744 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012746 goto exit;
12747 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012748 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012749 ssl->handshake->randbytes, 64 ) ) != 0 )
12750 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012752 goto exit;
12753 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012754 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012755 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012757 goto exit;
12758 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012759 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012760 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012762 goto exit;
12763 }
12764
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012765 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012766 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012768 goto exit;
12769 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012770 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012771 ssl->handshake->randbytes, 64 ) ) != 0 )
12772 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012777 data_len ) ) != 0 )
12778 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_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_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012783 output + 16 ) ) != 0 )
12784 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012786 goto exit;
12787 }
12788
12789exit:
12790 mbedtls_md5_free( &mbedtls_md5 );
12791 mbedtls_sha1_free( &mbedtls_sha1 );
12792
12793 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012794 mbedtls_ssl_pend_fatal_alert( ssl,
12795 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012796
12797 return( ret );
12798
12799}
12800#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12801 MBEDTLS_SSL_PROTO_TLS1_1 */
12802
12803#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12804 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12805int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012806 unsigned char *hash, size_t *hashlen,
12807 unsigned char *data, size_t data_len,
12808 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012809{
12810 int ret = 0;
12811 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012812 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012813 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012814
12815 mbedtls_md_init( &ctx );
12816
12817 /*
12818 * digitally-signed struct {
12819 * opaque client_random[32];
12820 * opaque server_random[32];
12821 * ServerDHParams params;
12822 * };
12823 */
12824 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12825 {
12826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12827 goto exit;
12828 }
12829 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12830 {
12831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12832 goto exit;
12833 }
12834 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12835 {
12836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12837 goto exit;
12838 }
12839 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12840 {
12841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12842 goto exit;
12843 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012844 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012845 {
12846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12847 goto exit;
12848 }
12849
12850exit:
12851 mbedtls_md_free( &ctx );
12852
12853 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012854 mbedtls_ssl_pend_fatal_alert( ssl,
12855 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012856
12857 return( ret );
12858}
12859#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12860 MBEDTLS_SSL_PROTO_TLS1_2 */
12861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012862#endif /* MBEDTLS_SSL_TLS_C */