blob: 6aebc081426d84a87e760012236aee7c769a589c [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
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
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
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
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 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 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, secret, slen ) ) != 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, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 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, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001955 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001957 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1958 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1959 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1960 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1961 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962 {
1963 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001964 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965
1966 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1967 ssl->handshake->ecdh_privkey,
1968 ssl->handshake->premaster,
1969 uecc_curve ) )
1970 {
1971 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1972 }
1973
1974 ssl->handshake->pmslen = NUM_ECC_BYTES;
1975 }
1976 else
1977#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001978#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1980 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1981 {
1982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1983 ssl->handshake->premaster,
1984 MBEDTLS_PREMASTER_SIZE,
1985 &ssl->handshake->pmslen,
1986 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001987 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001988 {
1989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1990 return( ret );
1991 }
1992
1993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1994 }
1995 else
1996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001997#if defined(MBEDTLS_ECDH_C) && \
1998 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2001 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2006 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2007 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2008 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2009 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2010 {
2011 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2012 &ssl->handshake->pmslen,
2013 ssl->handshake->premaster,
2014 MBEDTLS_MPI_MAX_SIZE,
2015 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002016 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002017 {
2018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2019 return( ret );
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2023 }
2024 else
2025#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2026 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2027 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2029#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2030 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2031 {
2032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002033 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2036 return( ret );
2037 }
2038 }
2039 else
2040#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2041#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2042 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2043 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2044 {
2045 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2046 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2047 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002048 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( ret != 0 )
2050 {
2051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2052 return( ret );
2053 }
2054 }
2055 else
2056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2057#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_RSA )
2060 {
2061 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002062 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002063 * ssl_rsa_generate_partial_pms(). Only the
2064 * PMS length needs to be set. */
2065 ssl->handshake->pmslen = 48;
2066 }
2067 else
2068#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2072 }
2073
2074 return( 0 );
2075}
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2078int 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 +02002079{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 unsigned char *p = ssl->handshake->premaster;
2081 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002082 const unsigned char *psk = ssl->conf->psk;
2083 size_t psk_len = ssl->conf->psk_len;
2084
2085 /* If the psk callback was called, use its result */
2086 if( ssl->handshake->psk != NULL )
2087 {
2088 psk = ssl->handshake->psk;
2089 psk_len = ssl->handshake->psk_len;
2090 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091
2092 /*
2093 * PMS = struct {
2094 * opaque other_secret<0..2^16-1>;
2095 * opaque psk<0..2^16-1>;
2096 * };
2097 * with "other_secret" depending on the particular key exchange
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2100 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002101 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002102 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002105 *(p++) = (unsigned char)( psk_len >> 8 );
2106 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002107
2108 if( end < p || (size_t)( end - p ) < psk_len )
2109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2110
2111 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002112 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 }
2114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2116#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2117 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002118 {
2119 /*
2120 * other_secret already set by the ClientKeyExchange message,
2121 * and is 48 bytes long
2122 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002123 if( end - p < 2 )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002126 *p++ = 0;
2127 *p++ = 48;
2128 p += 48;
2129 }
2130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2132#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2133 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002134 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002135 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002136 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002137
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002138 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002140 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002141 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002142 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145 return( ret );
2146 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002147 *(p++) = (unsigned char)( len >> 8 );
2148 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002149 p += len;
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002152 }
2153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2155#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2156 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002158 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 size_t zlen;
2160
Hanno Becker982da7e2019-09-02 09:47:39 +01002161#if defined(MBEDTLS_USE_TINYCRYPT)
2162 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2163 ((void) ret);
2164
2165 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2166 ssl->handshake->ecdh_privkey,
2167 p + 2,
2168 uecc_curve ) )
2169 {
2170 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2171 }
2172
2173 zlen = NUM_ECC_BYTES;
2174#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002176 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002177 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002178 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002181 return( ret );
2182 }
2183
Hanno Becker982da7e2019-09-02 09:47:39 +01002184 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2185 MBEDTLS_DEBUG_ECDH_Z );
2186#endif /* MBEDTLS_USE_TINYCRYPT */
2187
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 *(p++) = (unsigned char)( zlen >> 8 );
2189 *(p++) = (unsigned char)( zlen );
2190 p += zlen;
2191
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192 }
2193 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198 }
2199
2200 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002201 if( end - p < 2 )
2202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002203
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002204 *(p++) = (unsigned char)( psk_len >> 8 );
2205 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002206
2207 if( end < p || (size_t)( end - p ) < psk_len )
2208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2209
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002210 memcpy( p, psk, psk_len );
2211 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002212
2213 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2214
2215 return( 0 );
2216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002220/*
2221 * SSLv3.0 MAC functions
2222 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002223#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002224static void ssl_mac( mbedtls_md_context_t *md_ctx,
2225 const unsigned char *secret,
2226 const unsigned char *buf, size_t len,
2227 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002228 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229{
2230 unsigned char header[11];
2231 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002232 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2234 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002235
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002236 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002238 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002239 else
Paul Bakker68884e32013-01-07 18:20:04 +01002240 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 memcpy( header, ctr, 8 );
2243 header[ 8] = (unsigned char) type;
2244 header[ 9] = (unsigned char)( len >> 8 );
2245 header[10] = (unsigned char)( len );
2246
Paul Bakker68884e32013-01-07 18:20:04 +01002247 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_md_starts( md_ctx );
2249 mbedtls_md_update( md_ctx, secret, md_size );
2250 mbedtls_md_update( md_ctx, padding, padlen );
2251 mbedtls_md_update( md_ctx, header, 11 );
2252 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002253 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Paul Bakker68884e32013-01-07 18:20:04 +01002255 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_md_starts( md_ctx );
2257 mbedtls_md_update( md_ctx, secret, md_size );
2258 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002259 mbedtls_md_update( md_ctx, out, md_size );
2260 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002263
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002264/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002265 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002266#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002267 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2268 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2269 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2270/* This function makes sure every byte in the memory region is accessed
2271 * (in ascending addresses order) */
2272static void ssl_read_memory( unsigned char *p, size_t len )
2273{
2274 unsigned char acc = 0;
2275 volatile unsigned char force;
2276
2277 for( ; len != 0; p++, len-- )
2278 acc ^= *p;
2279
2280 force = acc;
2281 (void) force;
2282}
2283#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2284
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002285/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002287 */
Hanno Becker3307b532017-12-27 21:37:21 +00002288
Hanno Beckera5a2b082019-05-15 14:03:01 +01002289#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002290/* This functions transforms a DTLS plaintext fragment and a record content
2291 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002292 *
2293 * struct {
2294 * opaque content[DTLSPlaintext.length];
2295 * ContentType real_type;
2296 * uint8 zeros[length_of_padding];
2297 * } DTLSInnerPlaintext;
2298 *
2299 * Input:
2300 * - `content`: The beginning of the buffer holding the
2301 * plaintext to be wrapped.
2302 * - `*content_size`: The length of the plaintext in Bytes.
2303 * - `max_len`: The number of Bytes available starting from
2304 * `content`. This must be `>= *content_size`.
2305 * - `rec_type`: The desired record content type.
2306 *
2307 * Output:
2308 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2309 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2310 *
2311 * Returns:
2312 * - `0` on success.
2313 * - A negative error code if `max_len` didn't offer enough space
2314 * for the expansion.
2315 */
2316static int ssl_cid_build_inner_plaintext( unsigned char *content,
2317 size_t *content_size,
2318 size_t remaining,
2319 uint8_t rec_type )
2320{
2321 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002322 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2323 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2324 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002325
2326 /* Write real content type */
2327 if( remaining == 0 )
2328 return( -1 );
2329 content[ len ] = rec_type;
2330 len++;
2331 remaining--;
2332
2333 if( remaining < pad )
2334 return( -1 );
2335 memset( content + len, 0, pad );
2336 len += pad;
2337 remaining -= pad;
2338
2339 *content_size = len;
2340 return( 0 );
2341}
2342
Hanno Becker7dc25772019-05-20 15:08:01 +01002343/* This function parses a DTLSInnerPlaintext structure.
2344 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002345static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2346 size_t *content_size,
2347 uint8_t *rec_type )
2348{
2349 size_t remaining = *content_size;
2350
2351 /* Determine length of padding by skipping zeroes from the back. */
2352 do
2353 {
2354 if( remaining == 0 )
2355 return( -1 );
2356 remaining--;
2357 } while( content[ remaining ] == 0 );
2358
2359 *content_size = remaining;
2360 *rec_type = content[ remaining ];
2361
2362 return( 0 );
2363}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002364#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002365
Hanno Becker99abf512019-05-20 14:50:53 +01002366/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002367 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002368static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002369 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002370 mbedtls_record *rec )
2371{
Hanno Becker99abf512019-05-20 14:50:53 +01002372 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002373 *
2374 * additional_data = seq_num + TLSCompressed.type +
2375 * TLSCompressed.version + TLSCompressed.length;
2376 *
Hanno Becker99abf512019-05-20 14:50:53 +01002377 * For the CID extension, this is extended as follows
2378 * (quoting draft-ietf-tls-dtls-connection-id-05,
2379 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 *
2381 * additional_data = seq_num + DTLSPlaintext.type +
2382 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002383 * cid +
2384 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 * length_of_DTLSInnerPlaintext;
2386 */
2387
Hanno Becker3307b532017-12-27 21:37:21 +00002388 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2389 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002390 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002391
Hanno Beckera5a2b082019-05-15 14:03:01 +01002392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002393 if( rec->cid_len != 0 )
2394 {
2395 memcpy( add_data + 11, rec->cid, rec->cid_len );
2396 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2397 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2398 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2399 *add_data_len = 13 + 1 + rec->cid_len;
2400 }
2401 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002403 {
2404 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2405 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2406 *add_data_len = 13;
2407 }
Hanno Becker3307b532017-12-27 21:37:21 +00002408}
2409
Hanno Becker611a83b2018-01-03 14:27:32 +00002410int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2411 mbedtls_ssl_transform *transform,
2412 mbedtls_record *rec,
2413 int (*f_rng)(void *, unsigned char *, size_t),
2414 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002417 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002418 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002419 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002420 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002421 size_t post_avail;
2422
2423 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002424#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002425 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002426 ((void) ssl);
2427#endif
2428
2429 /* The PRNG is used for dynamic IV generation that's used
2430 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2431#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2432 ( defined(MBEDTLS_AES_C) || \
2433 defined(MBEDTLS_ARIA_C) || \
2434 defined(MBEDTLS_CAMELLIA_C) ) && \
2435 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2436 ((void) f_rng);
2437 ((void) p_rng);
2438#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Hanno Becker3307b532017-12-27 21:37:21 +00002442 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002443 {
Hanno Becker3307b532017-12-27 21:37:21 +00002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2446 }
Hanno Becker505089d2019-05-01 09:45:57 +01002447 if( rec == NULL
2448 || rec->buf == NULL
2449 || rec->buf_len < rec->data_offset
2450 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002451#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002452 || rec->cid_len != 0
2453#endif
2454 )
Hanno Becker3307b532017-12-27 21:37:21 +00002455 {
2456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002458 }
2459
Hanno Becker3307b532017-12-27 21:37:21 +00002460 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002461 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002463 data, rec->data_len );
2464
2465 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2466
2467 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2470 (unsigned) rec->data_len,
2471 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2473 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002474
Hanno Beckera5a2b082019-05-15 14:03:01 +01002475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002476 /*
2477 * Add CID information
2478 */
2479 rec->cid_len = transform->out_cid_len;
2480 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2481 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002482
2483 if( rec->cid_len != 0 )
2484 {
2485 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002486 * Wrap plaintext into DTLSInnerPlaintext structure.
2487 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002488 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002489 * Note that this changes `rec->data_len`, and hence
2490 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002491 */
2492 if( ssl_cid_build_inner_plaintext( data,
2493 &rec->data_len,
2494 post_avail,
2495 rec->type ) != 0 )
2496 {
2497 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2498 }
2499
2500 rec->type = MBEDTLS_SSL_MSG_CID;
2501 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002503
Hanno Becker92c930f2019-04-29 17:31:37 +01002504 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2505
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002507 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002509#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 if( mode == MBEDTLS_MODE_STREAM ||
2511 ( mode == MBEDTLS_MODE_CBC
2512#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002513 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002514#endif
2515 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
Hanno Becker3307b532017-12-27 21:37:21 +00002517 if( post_avail < transform->maclen )
2518 {
2519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2520 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2521 }
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002524 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2525 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002527 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002528 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2529 data, rec->data_len, rec->ctr, rec->type, mac );
2530 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 }
2532 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2535 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002536 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2537 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002538 {
Hanno Becker992b6872017-11-09 18:57:39 +00002539 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2540
Hanno Beckere83efe62019-04-29 13:52:53 +01002541 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002542
Hanno Becker3307b532017-12-27 21:37:21 +00002543 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002544 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002545 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2546 data, rec->data_len );
2547 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2548 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2549
2550 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002551 }
2552 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002553#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002557 }
2558
Hanno Becker3307b532017-12-27 21:37:21 +00002559 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2560 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561
Hanno Becker3307b532017-12-27 21:37:21 +00002562 rec->data_len += transform->maclen;
2563 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002565 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002566#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002568 /*
2569 * Encrypt
2570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2572 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002574 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002575 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002577 "including %d bytes of padding",
2578 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Becker3307b532017-12-27 21:37:21 +00002580 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2581 transform->iv_enc, transform->ivlen,
2582 data, rec->data_len,
2583 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002586 return( ret );
2587 }
2588
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
Paul Bakker68884e32013-01-07 18:20:04 +01002595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002598#if defined(MBEDTLS_GCM_C) || \
2599 defined(MBEDTLS_CCM_C) || \
2600 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002602 mode == MBEDTLS_MODE_CCM ||
2603 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002604 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002605 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002607 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002608
Hanno Becker3307b532017-12-27 21:37:21 +00002609 /* Check that there's space for both the authentication tag
2610 * and the explicit IV before and after the record content. */
2611 if( post_avail < transform->taglen ||
2612 rec->data_offset < explicit_iv_len )
2613 {
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2615 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2616 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002617
Paul Bakker68884e32013-01-07 18:20:04 +01002618 /*
2619 * Generate IV
2620 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2622 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002623 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002625 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2626 explicit_iv_len );
2627 /* Prefix record content with explicit IV. */
2628 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 }
2630 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 unsigned char i;
2634
2635 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2636
2637 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002638 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 }
2640 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002641 {
2642 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002645 }
2646
Hanno Beckere83efe62019-04-29 13:52:53 +01002647 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002648
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002649 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2650 iv, transform->ivlen );
2651 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002652 data - explicit_iv_len, explicit_iv_len );
2653 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002654 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002656 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002657 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002658
Paul Bakker68884e32013-01-07 18:20:04 +01002659 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002660 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002661 */
Hanno Becker3307b532017-12-27 21:37:21 +00002662
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002663 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002664 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002665 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002666 data, rec->data_len, /* source */
2667 data, &rec->data_len, /* destination */
2668 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002671 return( ret );
2672 }
2673
Hanno Becker3307b532017-12-27 21:37:21 +00002674 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2675 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002676
Hanno Becker3307b532017-12-27 21:37:21 +00002677 rec->data_len += transform->taglen + explicit_iv_len;
2678 rec->data_offset -= explicit_iv_len;
2679 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002680 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2684#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002685 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002688 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002689 size_t padlen, i;
2690 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002691
Hanno Becker3307b532017-12-27 21:37:21 +00002692 /* Currently we're always using minimal padding
2693 * (up to 255 bytes would be allowed). */
2694 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2695 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 padlen = 0;
2697
Hanno Becker3307b532017-12-27 21:37:21 +00002698 /* Check there's enough space in the buffer for the padding. */
2699 if( post_avail < padlen + 1 )
2700 {
2701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2702 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2703 }
2704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002706 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Becker3307b532017-12-27 21:37:21 +00002708 rec->data_len += padlen + 1;
2709 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002712 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002713 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2714 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002716 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2717 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002718 {
Hanno Becker3307b532017-12-27 21:37:21 +00002719 if( f_rng == NULL )
2720 {
2721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2723 }
2724
2725 if( rec->data_offset < transform->ivlen )
2726 {
2727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2728 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2729 }
2730
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002731 /*
2732 * Generate IV
2733 */
Hanno Becker3307b532017-12-27 21:37:21 +00002734 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002735 if( ret != 0 )
2736 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002737
Hanno Becker3307b532017-12-27 21:37:21 +00002738 memcpy( data - transform->ivlen, transform->iv_enc,
2739 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002740
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002745 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002746 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002747 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Hanno Becker3307b532017-12-27 21:37:21 +00002749 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2750 transform->iv_enc,
2751 transform->ivlen,
2752 data, rec->data_len,
2753 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002756 return( ret );
2757 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002758
Hanno Becker3307b532017-12-27 21:37:21 +00002759 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002766 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2767 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002768 {
2769 /*
2770 * Save IV in SSL3 and TLS1
2771 */
Hanno Becker3307b532017-12-27 21:37:21 +00002772 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2773 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 }
Hanno Becker3307b532017-12-27 21:37:21 +00002775 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002776#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002777 {
2778 data -= transform->ivlen;
2779 rec->data_offset -= transform->ivlen;
2780 rec->data_len += transform->ivlen;
2781 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002784 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002786 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2787
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 /*
2789 * MAC(MAC_write_key, seq_num +
2790 * TLSCipherText.type +
2791 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002792 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 * IV + // except for TLS 1.0
2794 * ENC(content + padding + padding_length));
2795 */
Hanno Becker3307b532017-12-27 21:37:21 +00002796
2797 if( post_avail < transform->maclen)
2798 {
2799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2800 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2801 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002802
Hanno Beckere83efe62019-04-29 13:52:53 +01002803 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002806 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002807 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002808
Hanno Becker3307b532017-12-27 21:37:21 +00002809 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002810 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002811 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2812 data, rec->data_len );
2813 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2814 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002815
Hanno Becker3307b532017-12-27 21:37:21 +00002816 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002817
Hanno Becker3307b532017-12-27 21:37:21 +00002818 rec->data_len += transform->maclen;
2819 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002820 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002826 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002832 /* Make extra sure authentication was performed, exactly once */
2833 if( auth_done != 1 )
2834 {
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 );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 }
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 return( 0 );
2842}
2843
Hanno Becker40478be2019-07-12 08:23:59 +01002844int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002845 mbedtls_ssl_transform *transform,
2846 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002847{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002848 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002850 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002852 size_t padlen = 0, correct = 1;
2853#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002854 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002856 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857
Hanno Becker611a83b2018-01-03 14:27:32 +00002858#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002859 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002860 ((void) ssl);
2861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002864 if( rec == NULL ||
2865 rec->buf == NULL ||
2866 rec->buf_len < rec->data_offset ||
2867 rec->buf_len - rec->data_offset < rec->data_len )
2868 {
2869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002871 }
2872
Hanno Becker4c6876b2017-12-27 21:28:58 +00002873 data = rec->buf + rec->data_offset;
2874 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Hanno Beckera5a2b082019-05-15 14:03:01 +01002876#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002877 /*
2878 * Match record's CID with incoming CID.
2879 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002880 if( rec->cid_len != transform->in_cid_len ||
2881 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2882 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002883 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002884 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002885#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2888 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002889 {
2890 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002891 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2892 transform->iv_dec,
2893 transform->ivlen,
2894 data, rec->data_len,
2895 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002898 return( ret );
2899 }
2900
Hanno Becker4c6876b2017-12-27 21:28:58 +00002901 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +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 );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 }
Paul Bakker68884e32013-01-07 18:20:04 +01002907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909#if defined(MBEDTLS_GCM_C) || \
2910 defined(MBEDTLS_CCM_C) || \
2911 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002913 mode == MBEDTLS_MODE_CCM ||
2914 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002915 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002916 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002917 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002918
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002919 /*
2920 * Prepare IV from explicit and implicit data.
2921 */
2922
2923 /* Check that there's enough space for the explicit IV
2924 * (at the beginning of the record) and the MAC (at the
2925 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002926 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 "+ taglen (%d)", rec->data_len,
2930 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002932 }
Paul Bakker68884e32013-01-07 18:20:04 +01002933
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002934#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002935 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2936 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002937 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002938
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002939 /* Fixed */
2940 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2941 /* Explicit */
2942 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002943 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002944 else
2945#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2946#if defined(MBEDTLS_CHACHAPOLY_C)
2947 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002948 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002949 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002950 unsigned char i;
2951
2952 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2953
2954 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002955 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002956 }
2957 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002958#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002959 {
2960 /* Reminder if we ever add an AEAD mode with a different size */
2961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2963 }
2964
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002965 /* Group changes to data, data_len, and add_data, because
2966 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002967 data += explicit_iv_len;
2968 rec->data_offset += explicit_iv_len;
2969 rec->data_len -= explicit_iv_len + transform->taglen;
2970
Hanno Beckere83efe62019-04-29 13:52:53 +01002971 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002973 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002974
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002975 /* Because of the check above, we know that there are
2976 * explicit_iv_len Bytes preceeding data, and taglen
2977 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002978 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002979 * mbedtls_cipher_auth_decrypt() below. */
2980
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002981 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002982 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002983 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002984
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002985 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002986 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002987 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002988 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2989 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002990 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002991 data, rec->data_len,
2992 data, &olen,
2993 data + rec->data_len,
2994 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003000
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003001 return( ret );
3002 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003003 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003005 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003006 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003010 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3014#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003015 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003018 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003019
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 /*
Paul Bakker45829992013-01-03 14:52:21 +01003021 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003024 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3025 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003026 {
3027 /* The ciphertext is prefixed with the CBC IV. */
3028 minlen += transform->ivlen;
3029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003030#endif
Paul Bakker45829992013-01-03 14:52:21 +01003031
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 /* Size considerations:
3033 *
3034 * - The CBC cipher text must not be empty and hence
3035 * at least of size transform->ivlen.
3036 *
3037 * Together with the potential IV-prefix, this explains
3038 * the first of the two checks below.
3039 *
3040 * - The record must contain a MAC, either in plain or
3041 * encrypted, depending on whether Encrypt-then-MAC
3042 * is used or not.
3043 * - If it is, the message contains the IV-prefix,
3044 * the CBC ciphertext, and the MAC.
3045 * - If it is not, the padded plaintext, and hence
3046 * the CBC ciphertext, has at least length maclen + 1
3047 * because there is at least the padding length byte.
3048 *
3049 * As the CBC ciphertext is not empty, both cases give the
3050 * lower bound minlen + maclen + 1 on the record size, which
3051 * we test for in the second check below.
3052 */
3053 if( rec->data_len < minlen + transform->ivlen ||
3054 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 "+ 1 ) ( + expl IV )", rec->data_len,
3058 transform->ivlen,
3059 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003061 }
3062
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003063 /*
3064 * Authenticate before decrypt if enabled
3065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003067 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003068 {
Hanno Becker992b6872017-11-09 18:57:39 +00003069 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003072
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003073 /* Update data_len in tandem with add_data.
3074 *
3075 * The subtraction is safe because of the previous check
3076 * data_len >= minlen + maclen + 1.
3077 *
3078 * Afterwards, we know that data + data_len is followed by at
3079 * least maclen Bytes, which justifies the call to
3080 * mbedtls_ssl_safer_memcmp() below.
3081 *
3082 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003083 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003084 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003085
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003086 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003087 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3088 add_data_len );
3089 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3090 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003091 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3092 data, rec->data_len );
3093 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3094 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003095
Hanno Becker4c6876b2017-12-27 21:28:58 +00003096 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3097 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003098 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003099 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003101 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3103 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003107 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003108 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003111
3112 /*
3113 * Check length sanity
3114 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003115
3116 /* We know from above that data_len > minlen >= 0,
3117 * so the following check in particular implies that
3118 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003119 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003122 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003124 }
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003127 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003129 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003130 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3131 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003132 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003133 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003134 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003135
Hanno Becker4c6876b2017-12-27 21:28:58 +00003136 data += transform->ivlen;
3137 rec->data_offset += transform->ivlen;
3138 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003141
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003142 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3143
Hanno Becker4c6876b2017-12-27 21:28:58 +00003144 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3145 transform->iv_dec, transform->ivlen,
3146 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003149 return( ret );
3150 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003151
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003152 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003157 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003160 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3161 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003162 {
3163 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003164 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3165 * records is equivalent to CBC decryption of the concatenation
3166 * of the records; in other words, IVs are maintained across
3167 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003168 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003169 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3170 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003172#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Hanno Becker4c6876b2017-12-27 21:28:58 +00003174 /* Safe since data_len >= minlen + maclen + 1, so after having
3175 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003176 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3177 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003178 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003179
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180 if( auth_done == 1 )
3181 {
3182 correct *= ( rec->data_len >= padlen + 1 );
3183 padlen *= ( rec->data_len >= padlen + 1 );
3184 }
3185 else
Paul Bakker45829992013-01-03 14:52:21 +01003186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 if( rec->data_len < transform->maclen + padlen + 1 )
3189 {
3190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3191 rec->data_len,
3192 transform->maclen,
3193 padlen + 1 ) );
3194 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003195#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003196
3197 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3198 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Hanno Becker4c6876b2017-12-27 21:28:58 +00003201 padlen++;
3202
3203 /* Regardless of the validity of the padding,
3204 * we have data_len >= padlen here. */
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003207 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3208 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003210 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#if defined(MBEDTLS_SSL_DEBUG_ALL)
3213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003214 "should be no more than %d",
3215 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003216#endif
Paul Bakker45829992013-01-03 14:52:21 +01003217 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219 }
3220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3222#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3223 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003224 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3225 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003227 /* The padding check involves a series of up to 256
3228 * consecutive memory reads at the end of the record
3229 * plaintext buffer. In order to hide the length and
3230 * validity of the padding, always perform exactly
3231 * `min(256,plaintext_len)` reads (but take into account
3232 * only the last `padlen` bytes for the padding check). */
3233 size_t pad_count = 0;
3234 size_t real_count = 0;
3235 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003236
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 /* Index of first padding byte; it has been ensured above
3238 * that the subtraction is safe. */
3239 size_t const padding_idx = rec->data_len - padlen;
3240 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3241 size_t const start_idx = rec->data_len - num_checks;
3242 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003243
Hanno Becker4c6876b2017-12-27 21:28:58 +00003244 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003245 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003246 real_count |= ( idx >= padding_idx );
3247 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003248 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003249 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003252 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003254#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003255 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3259 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003263 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003264
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 /* If the padding was found to be invalid, padlen == 0
3266 * and the subtraction is safe. If the padding was found valid,
3267 * padlen hasn't been changed and the previous assertion
3268 * data_len >= padlen still holds. */
3269 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003273 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003279#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003281 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003285 * Authenticate if not done yet.
3286 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003288#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003289 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003290 {
Hanno Becker992b6872017-11-09 18:57:39 +00003291 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003292
Hanno Becker4c6876b2017-12-27 21:28:58 +00003293 /* If the initial value of padlen was such that
3294 * data_len < maclen + padlen + 1, then padlen
3295 * got reset to 1, and the initial check
3296 * data_len >= minlen + maclen + 1
3297 * guarantees that at this point we still
3298 * have at least data_len >= maclen.
3299 *
3300 * If the initial value of padlen was such that
3301 * data_len >= maclen + padlen + 1, then we have
3302 * subtracted either padlen + 1 (if the padding was correct)
3303 * or 0 (if the padding was incorrect) since then,
3304 * hence data_len >= maclen in any case.
3305 */
3306 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003307 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003310 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3311 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003312 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003313 ssl_mac( &transform->md_ctx_dec,
3314 transform->mac_dec,
3315 data, rec->data_len,
3316 rec->ctr, rec->type,
3317 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003318 }
3319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3321#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3322 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003323 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3324 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003325 {
3326 /*
3327 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003328 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003329 *
3330 * Known timing attacks:
3331 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3332 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003333 * To compensate for different timings for the MAC calculation
3334 * depending on how much padding was removed (which is determined
3335 * by padlen), process extra_run more blocks through the hash
3336 * function.
3337 *
3338 * The formula in the paper is
3339 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3340 * where L1 is the size of the header plus the decrypted message
3341 * plus CBC padding and L2 is the size of the header plus the
3342 * decrypted message. This is for an underlying hash function
3343 * with 64-byte blocks.
3344 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3345 * correctly. We round down instead of up, so -56 is the correct
3346 * value for our calculations instead of -55.
3347 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003348 * Repeat the formula rather than defining a block_size variable.
3349 * This avoids requiring division by a variable at runtime
3350 * (which would be marginally less efficient and would require
3351 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003352 */
3353 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003354 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003355
3356 /*
3357 * The next two sizes are the minimum and maximum values of
3358 * in_msglen over all padlen values.
3359 *
3360 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003361 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003362 *
3363 * Note that max_len + maclen is never more than the buffer
3364 * length, as we previously did in_msglen -= maclen too.
3365 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003366 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003367 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3368
Hanno Becker4c6876b2017-12-27 21:28:58 +00003369 memset( tmp, 0, sizeof( tmp ) );
3370
3371 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003372 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003373#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3374 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003375 case MBEDTLS_MD_MD5:
3376 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003377 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003379 extra_run =
3380 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3381 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003382 break;
3383#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003384#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003385 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003386 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003387 extra_run =
3388 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3389 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003390 break;
3391#endif
3392 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3395 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003396
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003398
Hanno Beckere83efe62019-04-29 13:52:53 +01003399 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3400 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003401 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3402 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003403 /* Make sure we access everything even when padlen > 0. This
3404 * makes the synchronisation requirements for just-in-time
3405 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003406 ssl_read_memory( data + rec->data_len, padlen );
3407 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003408
3409 /* Call mbedtls_md_process at least once due to cache attacks
3410 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003411 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003413
Hanno Becker4c6876b2017-12-27 21:28:58 +00003414 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003415
3416 /* Make sure we access all the memory that could contain the MAC,
3417 * before we check it in the next code block. This makes the
3418 * synchronisation requirements for just-in-time Prime+Probe
3419 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003420 ssl_read_memory( data + min_len,
3421 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 }
3423 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3425 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003430
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003431#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003432 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3433 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
Hanno Becker4c6876b2017-12-27 21:28:58 +00003436 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3437 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_DEBUG_ALL)
3440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003441#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003442 correct = 0;
3443 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003444 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003445 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003446
3447 /*
3448 * Finally check the correct flag
3449 */
3450 if( correct == 0 )
3451 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003452#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003453
3454 /* Make extra sure authentication was performed, exactly once */
3455 if( auth_done != 1 )
3456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003460
Hanno Beckera5a2b082019-05-15 14:03:01 +01003461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003462 if( rec->cid_len != 0 )
3463 {
3464 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3465 &rec->type );
3466 if( ret != 0 )
3467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3468 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472
3473 return( 0 );
3474}
3475
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003476#undef MAC_NONE
3477#undef MAC_PLAINTEXT
3478#undef MAC_CIPHERTEXT
3479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481/*
3482 * Compression/decompression functions
3483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485{
3486 int ret;
3487 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003488 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003490 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003494 if( len_pre == 0 )
3495 return( 0 );
3496
Paul Bakker2770fbd2012-07-03 13:30:23 +00003497 memcpy( msg_pre, ssl->out_msg, len_pre );
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003500 ssl->out_msglen ) );
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503 ssl->out_msg, ssl->out_msglen );
3504
Paul Bakker48916f92012-09-16 19:57:18 +00003505 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3506 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3507 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003508 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003509
Paul Bakker48916f92012-09-16 19:57:18 +00003510 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 if( ret != Z_OK )
3512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3514 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003515 }
3516
Angus Grattond8213d02016-05-25 20:56:48 +10003517 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003518 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521 ssl->out_msglen ) );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 ssl->out_msg, ssl->out_msglen );
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003527
3528 return( 0 );
3529}
3530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532{
3533 int ret;
3534 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003535 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003536 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003537 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003541 if( len_pre == 0 )
3542 return( 0 );
3543
Paul Bakker2770fbd2012-07-03 13:30:23 +00003544 memcpy( msg_pre, ssl->in_msg, len_pre );
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 ssl->in_msglen ) );
3548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550 ssl->in_msg, ssl->in_msglen );
3551
Paul Bakker48916f92012-09-16 19:57:18 +00003552 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3553 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3554 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003555 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003556 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003557
Paul Bakker48916f92012-09-16 19:57:18 +00003558 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559 if( ret != Z_OK )
3560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3562 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003563 }
3564
Angus Grattond8213d02016-05-25 20:56:48 +10003565 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003566 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003569 ssl->in_msglen ) );
3570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572 ssl->in_msg, ssl->in_msglen );
3573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003575
3576 return( 0 );
3577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3581static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583#if defined(MBEDTLS_SSL_PROTO_DTLS)
3584static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003585{
3586 /* If renegotiation is not enforced, retransmit until we would reach max
3587 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003588 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003589 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003590 uint32_t ratio =
3591 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3592 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003593 unsigned char doublings = 1;
3594
3595 while( ratio != 0 )
3596 {
3597 ++doublings;
3598 ratio >>= 1;
3599 }
3600
3601 if( ++ssl->renego_records_seen > doublings )
3602 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003604 return( 0 );
3605 }
3606 }
3607
3608 return( ssl_write_hello_request( ssl ) );
3609}
3610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003612
Paul Bakker5121ce52009-01-03 21:22:43 +00003613/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003614 * Fill the input message buffer by appending data to it.
3615 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003616 *
3617 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3618 * available (from this read and/or a previous one). Otherwise, an error code
3619 * is returned (possibly EOF or WANT_READ).
3620 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003621 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3622 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3623 * since we always read a whole datagram at once.
3624 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003625 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003626 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
Paul Bakker23986e52011-04-24 08:57:21 +00003630 int ret;
3631 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634
Hanno Beckera58a8962019-06-13 16:11:15 +01003635 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3636 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003639 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003641 }
3642
Angus Grattond8213d02016-05-25 20:56:48 +10003643 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003647 }
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003652 uint32_t timeout;
3653
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003654 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003655 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3656 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003657 {
3658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3659 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3661 }
3662
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003663 /*
3664 * The point is, we need to always read a full datagram at once, so we
3665 * sometimes read more then requested, and handle the additional data.
3666 * It could be the rest of the current record (while fetching the
3667 * header) and/or some other records in the same datagram.
3668 */
3669
3670 /*
3671 * Move to the next record in the already read datagram if applicable
3672 */
3673 if( ssl->next_record_offset != 0 )
3674 {
3675 if( ssl->in_left < ssl->next_record_offset )
3676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 }
3680
3681 ssl->in_left -= ssl->next_record_offset;
3682
3683 if( ssl->in_left != 0 )
3684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003686 ssl->next_record_offset ) );
3687 memmove( ssl->in_hdr,
3688 ssl->in_hdr + ssl->next_record_offset,
3689 ssl->in_left );
3690 }
3691
3692 ssl->next_record_offset = 0;
3693 }
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697
3698 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003699 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003700 */
3701 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003704 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003705 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003706
3707 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003708 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003709 * are not at the beginning of a new record, the caller did something
3710 * wrong.
3711 */
3712 if( ssl->in_left != 0 )
3713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003716 }
3717
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003718 /*
3719 * Don't even try to read if time's out already.
3720 * This avoids by-passing the timer when repeatedly receiving messages
3721 * that will end up being dropped.
3722 */
3723 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003724 {
3725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003726 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003727 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003728 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003729 {
Angus Grattond8213d02016-05-25 20:56:48 +10003730 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003733 timeout = ssl->handshake->retransmit_timeout;
3734 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003735 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003738
Hanno Beckera58a8962019-06-13 16:11:15 +01003739 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3740 {
3741 ret = mbedtls_ssl_get_recv_timeout( ssl )
3742 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3743 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003745 {
3746 ret = mbedtls_ssl_get_recv( ssl )
3747 ( ssl->p_bio, ssl->in_hdr, len );
3748 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003751
3752 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003754 }
3755
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003756 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003759 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003762 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003763 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003766 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003767 }
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003772 return( ret );
3773 }
3774
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003775 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003778 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3779 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003781 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003782 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003785 return( ret );
3786 }
3787
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003788 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003791 }
3792
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 if( ret < 0 )
3794 return( ret );
3795
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796 ssl->in_left = ret;
3797 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003798 MBEDTLS_SSL_TRANSPORT_ELSE
3799#endif /* MBEDTLS_SSL_PROTO_DTLS */
3800#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003803 ssl->in_left, nb_want ) );
3804
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805 while( ssl->in_left < nb_want )
3806 {
3807 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003808
3809 if( ssl_check_timer( ssl ) != 0 )
3810 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3811 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003812 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003813 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003814 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003815 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3816 ssl->in_hdr + ssl->in_left, len,
3817 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003818 }
3819 else
3820 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003821 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003822 ssl->in_hdr + ssl->in_left, len );
3823 }
3824 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003827 ssl->in_left, nb_want ) );
3828 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003829
3830 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003832
3833 if( ret < 0 )
3834 return( ret );
3835
mohammad160352aecb92018-03-28 23:41:40 -07003836 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003837 {
Darryl Green11999bb2018-03-13 15:22:58 +00003838 MBEDTLS_SSL_DEBUG_MSG( 1,
3839 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003840 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3842 }
3843
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003844 ssl->in_left += ret;
3845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003847#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850
3851 return( 0 );
3852}
3853
3854/*
3855 * Flush any data not yet written
3856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003858{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003859 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003860 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003863
Hanno Beckera58a8962019-06-13 16:11:15 +01003864 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003867 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003869 }
3870
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003871 /* Avoid incrementing counter if data is flushed */
3872 if( ssl->out_left == 0 )
3873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003875 return( 0 );
3876 }
3877
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 while( ssl->out_left > 0 )
3879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003881 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882
Hanno Becker2b1e3542018-08-06 11:19:13 +01003883 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003884 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
3888 if( ret <= 0 )
3889 return( ret );
3890
mohammad160352aecb92018-03-28 23:41:40 -07003891 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003892 {
Darryl Green11999bb2018-03-13 15:22:58 +00003893 MBEDTLS_SSL_DEBUG_MSG( 1,
3894 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003895 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3897 }
3898
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 ssl->out_left -= ret;
3900 }
3901
Hanno Becker2b1e3542018-08-06 11:19:13 +01003902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003904 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003905 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003906 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003907 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003908#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003909#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003910 {
3911 ssl->out_hdr = ssl->out_buf + 8;
3912 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003913#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003914 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
3918 return( 0 );
3919}
3920
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003921/*
3922 * Functions to handle the DTLS retransmission state machine
3923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925/*
3926 * Append current handshake message to current outgoing flight
3927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3932 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3933 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934
3935 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003936 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941 }
3942
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003943 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003944 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 }
3949
3950 /* Copy current handshake message with headers */
3951 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3952 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003953 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954 msg->next = NULL;
3955
3956 /* Append to the current flight */
3957 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003958 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959 else
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962 while( cur->next != NULL )
3963 cur = cur->next;
3964 cur->next = msg;
3965 }
3966
Hanno Becker3b235902018-08-06 09:54:53 +01003967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 return( 0 );
3969}
3970
3971/*
3972 * Free the current flight of handshake messages
3973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 mbedtls_ssl_flight_item *cur = flight;
3977 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978
3979 while( cur != NULL )
3980 {
3981 next = cur->next;
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 mbedtls_free( cur->p );
3984 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003985
3986 cur = next;
3987 }
3988}
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3991static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003992#endif
3993
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003994/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003995 * Swap transform_out and out_ctr with the alternative ones
3996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004000 unsigned char tmp_out_ctr[8];
4001
4002 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004005 return;
4006 }
4007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004010 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 tmp_transform = ssl->transform_out;
4012 ssl->transform_out = ssl->handshake->alt_transform_out;
4013 ssl->handshake->alt_transform_out = tmp_transform;
4014
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004015 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004016 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4017 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004018 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004019
4020 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004021 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4024 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4029 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004030 }
4031 }
4032#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004033}
4034
4035/*
4036 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004037 */
4038int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4039{
4040 int ret = 0;
4041
4042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4043
4044 ret = mbedtls_ssl_flight_transmit( ssl );
4045
4046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4047
4048 return( ret );
4049}
4050
4051/*
4052 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053 *
4054 * Need to remember the current message in case flush_output returns
4055 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004056 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004060 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004064 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004066
4067 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004068 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004069 ssl_swap_epochs( ssl );
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004072 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
4074 while( ssl->handshake->cur_msg != NULL )
4075 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004076 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004077 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004078
Hanno Beckere1dcb032018-08-17 16:47:58 +01004079 int const is_finished =
4080 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4081 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4082
Hanno Becker04da1892018-08-14 13:22:10 +01004083 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4084 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4085
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004086 /* Swap epochs before sending Finished: we can't do it after
4087 * sending ChangeCipherSpec, in case write returns WANT_READ.
4088 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004089 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004090 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004092 ssl_swap_epochs( ssl );
4093 }
4094
Hanno Becker67bc7c32018-08-06 11:33:50 +01004095 ret = ssl_get_remaining_payload_in_datagram( ssl );
4096 if( ret < 0 )
4097 return( ret );
4098 max_frag_len = (size_t) ret;
4099
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004100 /* CCS is copied as is, while HS messages may need fragmentation */
4101 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4102 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004103 if( max_frag_len == 0 )
4104 {
4105 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4106 return( ret );
4107
4108 continue;
4109 }
4110
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004111 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004112 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004113 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004114
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004115 /* Update position inside current message */
4116 ssl->handshake->cur_msg_p += cur->len;
4117 }
4118 else
4119 {
4120 const unsigned char * const p = ssl->handshake->cur_msg_p;
4121 const size_t hs_len = cur->len - 12;
4122 const size_t frag_off = p - ( cur->p + 12 );
4123 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004125
Hanno Beckere1dcb032018-08-17 16:47:58 +01004126 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004127 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004128 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004129 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004130
Hanno Becker67bc7c32018-08-06 11:33:50 +01004131 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4132 return( ret );
4133
4134 continue;
4135 }
4136 max_hs_frag_len = max_frag_len - 12;
4137
4138 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4139 max_hs_frag_len : rem_len;
4140
4141 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004142 {
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004144 (unsigned) cur_hs_frag_len,
4145 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004146 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004147
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004148 /* Messages are stored with handshake headers as if not fragmented,
4149 * copy beginning of headers then fill fragmentation fields.
4150 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4151 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004153 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4154 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4155 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4156
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4158 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4159 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004160
4161 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4162
Hanno Becker3f7b9732018-08-28 09:53:25 +01004163 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004164 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4165 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004166 ssl->out_msgtype = cur->type;
4167
4168 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004169 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004170 }
4171
4172 /* If done with the current message move to the next one if any */
4173 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4174 {
4175 if( cur->next != NULL )
4176 {
4177 ssl->handshake->cur_msg = cur->next;
4178 ssl->handshake->cur_msg_p = cur->next->p + 12;
4179 }
4180 else
4181 {
4182 ssl->handshake->cur_msg = NULL;
4183 ssl->handshake->cur_msg_p = NULL;
4184 }
4185 }
4186
4187 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004188 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004191 return( ret );
4192 }
4193 }
4194
Hanno Becker67bc7c32018-08-06 11:33:50 +01004195 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4196 return( ret );
4197
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004198 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4200 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004201 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004204 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4205 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004206
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004208
4209 return( 0 );
4210}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004211
4212/*
4213 * To be called when the last message of an incoming flight is received.
4214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004216{
4217 /* We won't need to resend that one any more */
4218 ssl_flight_free( ssl->handshake->flight );
4219 ssl->handshake->flight = NULL;
4220 ssl->handshake->cur_msg = NULL;
4221
4222 /* The next incoming flight will start with this msg_seq */
4223 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4224
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004225 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004226 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004227
Hanno Becker0271f962018-08-16 13:23:47 +01004228 /* Clear future message buffering structure. */
4229 ssl_buffering_free( ssl );
4230
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004231 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004232 ssl_set_timer( ssl, 0 );
4233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4235 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004238 }
4239 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004241}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242
4243/*
4244 * To be called when the last message of an outgoing flight is send.
4245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004247{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004248 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004249 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4252 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004255 }
4256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004260
Paul Bakker5121ce52009-01-03 21:22:43 +00004261/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004262 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004264
4265/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004266 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267 *
4268 * - fill in handshake headers
4269 * - update handshake checksum
4270 * - DTLS: save message for resending
4271 * - then pass to the record layer
4272 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004273 * DTLS: except for HelloRequest, messages are only queued, and will only be
4274 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004275 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004276 * Inputs:
4277 * - ssl->out_msglen: 4 + actual handshake message len
4278 * (4 is the size of handshake headers for TLS)
4279 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4280 * - ssl->out_msg + 4: the handshake message body
4281 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004282 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004283 * - ssl->out_msglen: the length of the record contents
4284 * (including handshake headers but excluding record headers)
4285 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004286 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004287int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004288{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004289 int ret;
4290 const size_t hs_len = ssl->out_msglen - 4;
4291 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4294
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004295 /*
4296 * Sanity checks
4297 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004298 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004299 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4300 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004301 /* In SSLv3, the client might send a NoCertificate alert. */
4302#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004303 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004304 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004305 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4306 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004307#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4308 {
4309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4311 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004313
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004314 /* Whenever we send anything different from a
4315 * HelloRequest we should be in a handshake - double check. */
4316 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4317 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004318 ssl->handshake == NULL )
4319 {
4320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004325 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004326 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004328 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004331 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004332#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004333
Hanno Beckerb50a2532018-08-06 11:52:54 +01004334 /* Double-check that we did not exceed the bounds
4335 * of the outgoing record buffer.
4336 * This should never fail as the various message
4337 * writing functions must obey the bounds of the
4338 * outgoing record buffer, but better be safe.
4339 *
4340 * Note: We deliberately do not check for the MTU or MFL here.
4341 */
4342 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4343 {
4344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4345 "size %u, maximum %u",
4346 (unsigned) ssl->out_msglen,
4347 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4349 }
4350
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004351 /*
4352 * Fill handshake headers
4353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004356 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4357 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4358 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004359
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004360 /*
4361 * DTLS has additional fields in the Handshake layer,
4362 * between the length field and the actual payload:
4363 * uint16 message_seq;
4364 * uint24 fragment_offset;
4365 * uint24 fragment_length;
4366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004368 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004369 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004370 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004371 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004372 {
4373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4374 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004375 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004376 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4378 }
4379
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004381 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004382
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004383 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004384 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004385 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004386 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4387 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4388 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004389 }
4390 else
4391 {
4392 ssl->out_msg[4] = 0;
4393 ssl->out_msg[5] = 0;
4394 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004395
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004396 /* Handshake hashes are computed without fragmentation,
4397 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004398 memset( ssl->out_msg + 6, 0x00, 3 );
4399 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004402
Hanno Becker0207e532018-08-28 10:28:28 +01004403 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004404 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004405 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 }
4407
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004408 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004410 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004411 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4412 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004413 {
4414 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004417 return( ret );
4418 }
4419 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004420 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004421#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004422 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004423 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004424 {
4425 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4426 return( ret );
4427 }
4428 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004429
4430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4431
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004432 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004433}
4434
4435/*
4436 * Record layer functions
4437 */
4438
4439/*
4440 * Write current record.
4441 *
4442 * Uses:
4443 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4444 * - ssl->out_msglen: length of the record content (excl headers)
4445 * - ssl->out_msg: record content
4446 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004447int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004448{
4449 int ret, done = 0;
4450 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004451 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004452
4453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004456 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004458 {
4459 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004462 return( ret );
4463 }
4464
4465 len = ssl->out_msglen;
4466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4470 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 ret = mbedtls_ssl_hw_record_write( ssl );
4475 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4478 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004479 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004480
4481 if( ret == 0 )
4482 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004485 if( !done )
4486 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004487 unsigned i;
4488 size_t protected_record_size;
4489
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004490 /* Skip writing the record content type to after the encryption,
4491 * as it may change when using the CID extension. */
4492
Hanno Becker2881d802019-05-22 14:44:53 +01004493 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4494 mbedtls_ssl_get_minor_ver( ssl ),
4495 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004496
Hanno Becker19859472018-08-06 09:40:20 +01004497 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004498 ssl->out_len[0] = (unsigned char)( len >> 8 );
4499 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004500
Paul Bakker48916f92012-09-16 19:57:18 +00004501 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004502 {
Hanno Becker3307b532017-12-27 21:37:21 +00004503 mbedtls_record rec;
4504
4505 rec.buf = ssl->out_iv;
4506 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4507 ( ssl->out_iv - ssl->out_buf );
4508 rec.data_len = ssl->out_msglen;
4509 rec.data_offset = ssl->out_msg - rec.buf;
4510
4511 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004512 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4513 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004514 ssl->conf->transport, rec.ver );
4515 rec.type = ssl->out_msgtype;
4516
Hanno Beckera5a2b082019-05-15 14:03:01 +01004517#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004518 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004519 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004521
Hanno Becker611a83b2018-01-03 14:27:32 +00004522 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004523 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004524 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004527 return( ret );
4528 }
4529
Hanno Becker3307b532017-12-27 21:37:21 +00004530 if( rec.data_offset != 0 )
4531 {
4532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4534 }
4535
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004536 /* Update the record content type and CID. */
4537 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004538#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004539 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004540#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004541 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004542 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4543 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004544 }
4545
Hanno Becker43395762019-05-03 14:46:38 +01004546 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004547
4548#if defined(MBEDTLS_SSL_PROTO_DTLS)
4549 /* In case of DTLS, double-check that we don't exceed
4550 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004551 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004552 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004553 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004554 if( ret < 0 )
4555 return( ret );
4556
4557 if( protected_record_size > (size_t) ret )
4558 {
4559 /* Should never happen */
4560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4561 }
4562 }
4563#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004564
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004565 /* Now write the potentially updated record content type. */
4566 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004569 "version = [%d:%d], msglen = %d",
4570 ssl->out_hdr[0], ssl->out_hdr[1],
4571 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004574 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004575
4576 ssl->out_left += protected_record_size;
4577 ssl->out_hdr += protected_record_size;
4578 ssl_update_out_pointers( ssl, ssl->transform_out );
4579
Hanno Becker04484622018-08-06 09:49:38 +01004580 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4581 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4582 break;
4583
4584 /* The loop goes to its end iff the counter is wrapping */
4585 if( i == ssl_ep_len( ssl ) )
4586 {
4587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4588 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 }
4591
Hanno Becker67bc7c32018-08-06 11:33:50 +01004592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004593 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004594 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004596 size_t remaining;
4597 ret = ssl_get_remaining_payload_in_datagram( ssl );
4598 if( ret < 0 )
4599 {
4600 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4601 ret );
4602 return( ret );
4603 }
4604
4605 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004606 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004607 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004608 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004609 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004610 else
4611 {
Hanno Becker513815a2018-08-20 11:56:09 +01004612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004613 }
4614 }
4615#endif /* MBEDTLS_SSL_PROTO_DTLS */
4616
4617 if( ( flush == SSL_FORCE_FLUSH ) &&
4618 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 return( ret );
4622 }
4623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
4626 return( 0 );
4627}
4628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004630
4631static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4632{
4633 if( ssl->in_msglen < ssl->in_hslen ||
4634 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4635 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4636 {
4637 return( 1 );
4638 }
4639 return( 0 );
4640}
Hanno Becker44650b72018-08-16 12:51:11 +01004641
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004642static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004643{
4644 return( ( ssl->in_msg[9] << 16 ) |
4645 ( ssl->in_msg[10] << 8 ) |
4646 ssl->in_msg[11] );
4647}
4648
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004649static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004650{
4651 return( ( ssl->in_msg[6] << 16 ) |
4652 ( ssl->in_msg[7] << 8 ) |
4653 ssl->in_msg[8] );
4654}
4655
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004656static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004657{
4658 uint32_t msg_len, frag_off, frag_len;
4659
4660 msg_len = ssl_get_hs_total_len( ssl );
4661 frag_off = ssl_get_hs_frag_off( ssl );
4662 frag_len = ssl_get_hs_frag_len( ssl );
4663
4664 if( frag_off > msg_len )
4665 return( -1 );
4666
4667 if( frag_len > msg_len - frag_off )
4668 return( -1 );
4669
4670 if( frag_len + 12 > ssl->in_msglen )
4671 return( -1 );
4672
4673 return( 0 );
4674}
4675
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004676/*
4677 * Mark bits in bitmask (used for DTLS HS reassembly)
4678 */
4679static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4680{
4681 unsigned int start_bits, end_bits;
4682
4683 start_bits = 8 - ( offset % 8 );
4684 if( start_bits != 8 )
4685 {
4686 size_t first_byte_idx = offset / 8;
4687
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004688 /* Special case */
4689 if( len <= start_bits )
4690 {
4691 for( ; len != 0; len-- )
4692 mask[first_byte_idx] |= 1 << ( start_bits - len );
4693
4694 /* Avoid potential issues with offset or len becoming invalid */
4695 return;
4696 }
4697
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004698 offset += start_bits; /* Now offset % 8 == 0 */
4699 len -= start_bits;
4700
4701 for( ; start_bits != 0; start_bits-- )
4702 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4703 }
4704
4705 end_bits = len % 8;
4706 if( end_bits != 0 )
4707 {
4708 size_t last_byte_idx = ( offset + len ) / 8;
4709
4710 len -= end_bits; /* Now len % 8 == 0 */
4711
4712 for( ; end_bits != 0; end_bits-- )
4713 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4714 }
4715
4716 memset( mask + offset / 8, 0xFF, len / 8 );
4717}
4718
4719/*
4720 * Check that bitmask is full
4721 */
4722static int ssl_bitmask_check( unsigned char *mask, size_t len )
4723{
4724 size_t i;
4725
4726 for( i = 0; i < len / 8; i++ )
4727 if( mask[i] != 0xFF )
4728 return( -1 );
4729
4730 for( i = 0; i < len % 8; i++ )
4731 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4732 return( -1 );
4733
4734 return( 0 );
4735}
4736
Hanno Becker56e205e2018-08-16 09:06:12 +01004737/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004738static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004739 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004740{
Hanno Becker56e205e2018-08-16 09:06:12 +01004741 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004742
Hanno Becker56e205e2018-08-16 09:06:12 +01004743 alloc_len = 12; /* Handshake header */
4744 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004745
Hanno Beckerd07df862018-08-16 09:14:58 +01004746 if( add_bitmap )
4747 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004748
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004749 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004750}
Hanno Becker56e205e2018-08-16 09:06:12 +01004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004753
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004754static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004755{
4756 return( ( ssl->in_msg[1] << 16 ) |
4757 ( ssl->in_msg[2] << 8 ) |
4758 ssl->in_msg[3] );
4759}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004760
Simon Butcher99000142016-10-13 17:21:01 +01004761int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004766 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004768 }
4769
Hanno Becker12555c62018-08-16 12:47:53 +01004770 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004773 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004774 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004777 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004778 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004779 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004780 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004781
Hanno Becker44650b72018-08-16 12:51:11 +01004782 if( ssl_check_hs_header( ssl ) != 0 )
4783 {
4784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4785 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4786 }
4787
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004788 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004789 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4790 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4791 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4792 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004793 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004794 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4795 {
4796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4797 recv_msg_seq,
4798 ssl->handshake->in_msg_seq ) );
4799 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4800 }
4801
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004802 /* Retransmit only on last message from previous flight, to avoid
4803 * too many retransmissions.
4804 * Besides, No sane server ever retransmits HelloVerifyRequest */
4805 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004809 "message_seq = %d, start_of_flight = %d",
4810 recv_msg_seq,
4811 ssl->handshake->in_flight_start_seq ) );
4812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004816 return( ret );
4817 }
4818 }
4819 else
4820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004822 "message_seq = %d, expected = %d",
4823 recv_msg_seq,
4824 ssl->handshake->in_msg_seq ) );
4825 }
4826
Hanno Becker90333da2017-10-10 11:27:13 +01004827 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004828 }
4829 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004830
Hanno Becker6d97ef52018-08-16 13:09:04 +01004831 /* Message reassembly is handled alongside buffering of future
4832 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004833 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004834 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004835 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004838 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004839 }
4840 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004841 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004843#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004844 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004845 /* With TLS we don't handle fragmentation (for now) */
4846 if( ssl->in_msglen < ssl->in_hslen )
4847 {
4848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4849 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4850 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004852#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004853
Simon Butcher99000142016-10-13 17:21:01 +01004854 return( 0 );
4855}
4856
4857void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4858{
Hanno Becker0271f962018-08-16 13:23:47 +01004859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004860
Hanno Becker0271f962018-08-16 13:23:47 +01004861 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004862 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004863
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004864 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004866 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004867 ssl->handshake != NULL )
4868 {
Hanno Becker0271f962018-08-16 13:23:47 +01004869 unsigned offset;
4870 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004871
Hanno Becker0271f962018-08-16 13:23:47 +01004872 /* Increment handshake sequence number */
4873 hs->in_msg_seq++;
4874
4875 /*
4876 * Clear up handshake buffering and reassembly structure.
4877 */
4878
4879 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004880 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004881
4882 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004883 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4884 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004885 offset++, hs_buf++ )
4886 {
4887 *hs_buf = *(hs_buf + 1);
4888 }
4889
4890 /* Create a fresh last entry */
4891 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004892 }
4893#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004894}
4895
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004896/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004897 * DTLS anti-replay: RFC 6347 4.1.2.6
4898 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004899 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4900 * Bit n is set iff record number in_window_top - n has been seen.
4901 *
4902 * Usually, in_window_top is the last record number seen and the lsb of
4903 * in_window is set. The only exception is the initial state (record number 0
4904 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4907static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004908{
4909 ssl->in_window_top = 0;
4910 ssl->in_window = 0;
4911}
4912
4913static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4914{
4915 return( ( (uint64_t) buf[0] << 40 ) |
4916 ( (uint64_t) buf[1] << 32 ) |
4917 ( (uint64_t) buf[2] << 24 ) |
4918 ( (uint64_t) buf[3] << 16 ) |
4919 ( (uint64_t) buf[4] << 8 ) |
4920 ( (uint64_t) buf[5] ) );
4921}
4922
4923/*
4924 * Return 0 if sequence number is acceptable, -1 otherwise
4925 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004926int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004927{
4928 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4929 uint64_t bit;
4930
Hanno Becker7f376f42019-06-12 16:20:48 +01004931 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4932 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4933 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004934 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004935 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004936
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004937 if( rec_seqnum > ssl->in_window_top )
4938 return( 0 );
4939
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004940 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004941
4942 if( bit >= 64 )
4943 return( -1 );
4944
4945 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4946 return( -1 );
4947
4948 return( 0 );
4949}
4950
4951/*
4952 * Update replay window on new validated record
4953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004955{
4956 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4957
Hanno Becker7f376f42019-06-12 16:20:48 +01004958 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4959 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4960 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004961 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004962 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004963
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004964 if( rec_seqnum > ssl->in_window_top )
4965 {
4966 /* Update window_top and the contents of the window */
4967 uint64_t shift = rec_seqnum - ssl->in_window_top;
4968
4969 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004970 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004971 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004972 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004973 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004974 ssl->in_window |= 1;
4975 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004976
4977 ssl->in_window_top = rec_seqnum;
4978 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004979 else
4980 {
4981 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004982 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983
4984 if( bit < 64 ) /* Always true, but be extra sure */
4985 ssl->in_window |= (uint64_t) 1 << bit;
4986 }
4987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004989
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004990#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004991/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004992static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4993
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004994/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004995 * Without any SSL context, check if a datagram looks like a ClientHello with
4996 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004997 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004998 *
4999 * - if cookie is valid, return 0
5000 * - if ClientHello looks superficially valid but cookie is not,
5001 * fill obuf and set olen, then
5002 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5003 * - otherwise return a specific error code
5004 */
5005static int ssl_check_dtls_clihlo_cookie(
5006 mbedtls_ssl_cookie_write_t *f_cookie_write,
5007 mbedtls_ssl_cookie_check_t *f_cookie_check,
5008 void *p_cookie,
5009 const unsigned char *cli_id, size_t cli_id_len,
5010 const unsigned char *in, size_t in_len,
5011 unsigned char *obuf, size_t buf_len, size_t *olen )
5012{
5013 size_t sid_len, cookie_len;
5014 unsigned char *p;
5015
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005016 /*
5017 * Structure of ClientHello with record and handshake headers,
5018 * and expected values. We don't need to check a lot, more checks will be
5019 * done when actually parsing the ClientHello - skipping those checks
5020 * avoids code duplication and does not make cookie forging any easier.
5021 *
5022 * 0-0 ContentType type; copied, must be handshake
5023 * 1-2 ProtocolVersion version; copied
5024 * 3-4 uint16 epoch; copied, must be 0
5025 * 5-10 uint48 sequence_number; copied
5026 * 11-12 uint16 length; (ignored)
5027 *
5028 * 13-13 HandshakeType msg_type; (ignored)
5029 * 14-16 uint24 length; (ignored)
5030 * 17-18 uint16 message_seq; copied
5031 * 19-21 uint24 fragment_offset; copied, must be 0
5032 * 22-24 uint24 fragment_length; (ignored)
5033 *
5034 * 25-26 ProtocolVersion client_version; (ignored)
5035 * 27-58 Random random; (ignored)
5036 * 59-xx SessionID session_id; 1 byte len + sid_len content
5037 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5038 * ...
5039 *
5040 * Minimum length is 61 bytes.
5041 */
5042 if( in_len < 61 ||
5043 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5044 in[3] != 0 || in[4] != 0 ||
5045 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5046 {
5047 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5048 }
5049
5050 sid_len = in[59];
5051 if( sid_len > in_len - 61 )
5052 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5053
5054 cookie_len = in[60 + sid_len];
5055 if( cookie_len > in_len - 60 )
5056 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5057
5058 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5059 cli_id, cli_id_len ) == 0 )
5060 {
5061 /* Valid cookie */
5062 return( 0 );
5063 }
5064
5065 /*
5066 * If we get here, we've got an invalid cookie, let's prepare HVR.
5067 *
5068 * 0-0 ContentType type; copied
5069 * 1-2 ProtocolVersion version; copied
5070 * 3-4 uint16 epoch; copied
5071 * 5-10 uint48 sequence_number; copied
5072 * 11-12 uint16 length; olen - 13
5073 *
5074 * 13-13 HandshakeType msg_type; hello_verify_request
5075 * 14-16 uint24 length; olen - 25
5076 * 17-18 uint16 message_seq; copied
5077 * 19-21 uint24 fragment_offset; copied
5078 * 22-24 uint24 fragment_length; olen - 25
5079 *
5080 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5081 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5082 *
5083 * Minimum length is 28.
5084 */
5085 if( buf_len < 28 )
5086 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5087
5088 /* Copy most fields and adapt others */
5089 memcpy( obuf, in, 25 );
5090 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5091 obuf[25] = 0xfe;
5092 obuf[26] = 0xff;
5093
5094 /* Generate and write actual cookie */
5095 p = obuf + 28;
5096 if( f_cookie_write( p_cookie,
5097 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5098 {
5099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5100 }
5101
5102 *olen = p - obuf;
5103
5104 /* Go back and fill length fields */
5105 obuf[27] = (unsigned char)( *olen - 28 );
5106
5107 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5108 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5109 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5110
5111 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5112 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5113
5114 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5115}
5116
5117/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005118 * Handle possible client reconnect with the same UDP quadruplet
5119 * (RFC 6347 Section 4.2.8).
5120 *
5121 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5122 * that looks like a ClientHello.
5123 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005124 * - if the input looks like a ClientHello without cookies,
5125 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005126 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005127 * - if the input looks like a ClientHello with a valid cookie,
5128 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005129 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005130 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005131 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005132 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005133 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5134 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005135 */
5136static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5137{
5138 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005139 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140
Hanno Becker87b56262019-07-10 14:37:41 +01005141 if( ssl->conf->f_cookie_write == NULL ||
5142 ssl->conf->f_cookie_check == NULL )
5143 {
5144 /* If we can't use cookies to verify reachability of the peer,
5145 * drop the record. */
5146 return( 0 );
5147 }
5148
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005149 ret = ssl_check_dtls_clihlo_cookie(
5150 ssl->conf->f_cookie_write,
5151 ssl->conf->f_cookie_check,
5152 ssl->conf->p_cookie,
5153 ssl->cli_id, ssl->cli_id_len,
5154 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005155 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005156
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005157 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5158
5159 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005161 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005162 * If the error is permanent we'll catch it later,
5163 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005164 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005165 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005166 }
5167
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005168 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005169 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005170 /* Got a valid cookie, partially reset context */
5171 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5172 {
5173 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5174 return( ret );
5175 }
5176
5177 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005178 }
5179
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005180 return( ret );
5181}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005182#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005183
Hanno Becker46483f12019-05-03 13:25:54 +01005184static int ssl_check_record_type( uint8_t record_type )
5185{
5186 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5187 record_type != MBEDTLS_SSL_MSG_ALERT &&
5188 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5189 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5190 {
5191 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5192 }
5193
5194 return( 0 );
5195}
5196
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005197/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005198 * ContentType type;
5199 * ProtocolVersion version;
5200 * uint16 epoch; // DTLS only
5201 * uint48 sequence_number; // DTLS only
5202 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005203 *
5204 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005205 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005206 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5207 *
5208 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005209 * 1. proceed with the record if this function returns 0
5210 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5211 * 3. return CLIENT_RECONNECT if this function return that value
5212 * 4. drop the whole datagram if this function returns anything else.
5213 * Point 2 is needed when the peer is resending, and we have already received
5214 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005215 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005216static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 unsigned char *buf,
5218 size_t len,
5219 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005220{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005221 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005222
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005223 size_t const rec_hdr_type_offset = 0;
5224 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005225
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005226 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5227 rec_hdr_type_len;
5228 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005230 size_t const rec_hdr_ctr_len = 8;
5231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005232 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005233 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5234 rec_hdr_version_len;
5235
Hanno Beckera5a2b082019-05-15 14:03:01 +01005236#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005237 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5238 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005239 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005240#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
5243 size_t rec_hdr_len_offset; /* To be determined */
5244 size_t const rec_hdr_len_len = 2;
5245
5246 /*
5247 * Check minimum lengths for record header.
5248 */
5249
5250#if defined(MBEDTLS_SSL_PROTO_DTLS)
5251 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5252 {
5253 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5254 }
5255 MBEDTLS_SSL_TRANSPORT_ELSE
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257#if defined(MBEDTLS_SSL_PROTO_TLS)
5258 {
5259 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5260 }
5261#endif /* MBEDTLS_SSL_PROTO_DTLS */
5262
5263 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5264 {
5265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5266 (unsigned) len,
5267 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5269 }
5270
5271 /*
5272 * Parse and validate record content type
5273 */
5274
5275 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005276
5277 /* Check record content type */
5278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5279 rec->cid_len = 0;
5280
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005281 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005282 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5283 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005284 {
5285 /* Shift pointers to account for record header including CID
5286 * struct {
5287 * ContentType special_type = tls12_cid;
5288 * ProtocolVersion version;
5289 * uint16 epoch;
5290 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005291 * opaque cid[cid_length]; // Additional field compared to
5292 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005293 * uint16 length;
5294 * opaque enc_content[DTLSCiphertext.length];
5295 * } DTLSCiphertext;
5296 */
5297
5298 /* So far, we only support static CID lengths
5299 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005300 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5301 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005302
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005304 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5306 (unsigned) len,
5307 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005308 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005309 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005310
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005311 /* configured CID len is guaranteed at most 255, see
5312 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5313 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005314 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005315 }
5316 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005318 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319 if( ssl_check_record_type( rec->type ) )
5320 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5322 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005323 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5324 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005325 }
5326
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005327 /*
5328 * Parse and validate record version
5329 */
5330
Hanno Becker8061c6e2019-07-26 08:07:03 +01005331 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5332 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5334 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005335 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336
Hanno Becker2881d802019-05-22 14:44:53 +01005337 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5340 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341 }
5342
Hanno Beckere965bd32019-06-12 14:04:34 +01005343 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5346 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005347 }
5348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 /*
5350 * Parse/Copy record sequence number.
5351 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353#if defined(MBEDTLS_SSL_PROTO_DTLS)
5354 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5355 {
5356 /* Copy explicit record sequence number from input buffer. */
5357 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5358 rec_hdr_ctr_len );
5359 }
5360 MBEDTLS_SSL_TRANSPORT_ELSE
5361#endif /* MBEDTLS_SSL_PROTO_DTLS */
5362#if defined(MBEDTLS_SSL_PROTO_TLS)
5363 {
5364 /* Copy implicit record sequence number from SSL context structure. */
5365 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5366 }
5367#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005368
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005369 /*
5370 * Parse record length.
5371 */
5372
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005373 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005374 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5375 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005376 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5377
Hanno Becker8b09b732019-05-08 12:03:28 +01005378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005379 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005380 rec->type,
5381 major_ver, minor_ver, rec->data_len ) );
5382
5383 rec->buf = buf;
5384 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005385
Hanno Beckerec014082019-07-26 08:20:27 +01005386 if( rec->data_len == 0 )
5387 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5388
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005389 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005390 * DTLS-related tests.
5391 * Check epoch before checking length constraint because
5392 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5393 * message gets duplicated before the corresponding Finished message,
5394 * the second ChangeCipherSpec should be discarded because it belongs
5395 * to an old epoch, but not because its length is shorter than
5396 * the minimum record length for packets using the new record transform.
5397 * Note that these two kinds of failures are handled differently,
5398 * as an unexpected record is silently skipped but an invalid
5399 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005400 */
5401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005402 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005403 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005404 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005405
Hanno Beckere0452772019-07-10 17:12:07 +01005406 /* Check that the datagram is large enough to contain a record
5407 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005409 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5411 (unsigned) len,
5412 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5414 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005415
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005416 /* Records from other, non-matching epochs are silently discarded.
5417 * (The case of same-port Client reconnects must be considered in
5418 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005419 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005420 {
5421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5422 "expected %d, received %d",
5423 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005424
5425 /* Records from the next epoch are considered for buffering
5426 * (concretely: early Finished messages). */
5427 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5428 {
5429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5430 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5431 }
5432
Hanno Becker87b56262019-07-10 14:37:41 +01005433 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005434 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005435#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005436 /* For records from the correct epoch, check whether their
5437 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005438 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005439 {
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5441 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5442 }
5443#endif
5444 }
5445#endif /* MBEDTLS_SSL_PROTO_DTLS */
5446
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005447 return( 0 );
5448}
Paul Bakker5121ce52009-01-03 21:22:43 +00005449
Hanno Becker87b56262019-07-10 14:37:41 +01005450
5451#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5452static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5453{
5454 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5455
5456 /*
5457 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5458 * access the first byte of record content (handshake type), as we
5459 * have an active transform (possibly iv_len != 0), so use the
5460 * fact that the record header len is 13 instead.
5461 */
5462 if( rec_epoch == 0 &&
5463 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5464 MBEDTLS_SSL_IS_SERVER &&
5465 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5466 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5467 ssl->in_left > 13 &&
5468 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5469 {
5470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5471 "from the same port" ) );
5472 return( ssl_handle_possible_reconnect( ssl ) );
5473 }
5474
5475 return( 0 );
5476}
5477#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5478
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005479/*
5480 * If applicable, decrypt (and decompress) record content
5481 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005482static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5483 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005484{
5485 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005488 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5491 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 ret = mbedtls_ssl_hw_record_read( ssl );
5496 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5499 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005500 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005501
5502 if( ret == 0 )
5503 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005506 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 unsigned char const old_msg_type = rec->type;
5509
Hanno Becker611a83b2018-01-03 14:27:32 +00005510 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005511 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005514
Hanno Beckera5a2b082019-05-15 14:03:01 +01005515#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005516 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005517 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5518 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005519 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005521 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005522 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005523#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005524
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 return( ret );
5526 }
5527
Hanno Becker106f3da2019-07-12 09:35:58 +01005528 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005529 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005530 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005531 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Paul Bakker5121ce52009-01-03 21:22:43 +00005534 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005535 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Hanno Beckera5a2b082019-05-15 14:03:01 +01005537#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005538 /* We have already checked the record content type
5539 * in ssl_parse_record_header(), failing or silently
5540 * dropping the record in the case of an unknown type.
5541 *
5542 * Since with the use of CIDs, the record content type
5543 * might change during decryption, re-check the record
5544 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005545 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005546 {
5547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5548 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5549 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005551
Hanno Becker106f3da2019-07-12 09:35:58 +01005552 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005553 {
5554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005555 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005556 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005557 {
5558 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5561 }
5562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5563
5564 ssl->nb_zero++;
5565
5566 /*
5567 * Three or more empty messages may be a DoS attack
5568 * (excessive CPU consumption).
5569 */
5570 if( ssl->nb_zero > 3 )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005573 "messages, possible DoS attack" ) );
5574 /* Treat the records as if they were not properly authenticated,
5575 * thereby failing the connection if we see more than allowed
5576 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005577 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5578 }
5579 }
5580 else
5581 ssl->nb_zero = 0;
5582
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005583 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5584#if defined(MBEDTLS_SSL_PROTO_TLS)
5585 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005586 {
5587 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005588 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005589 if( ++ssl->in_ctr[i - 1] != 0 )
5590 break;
5591
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005592 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005593 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005594 {
5595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5596 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5597 }
5598 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005599#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005600
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 }
5602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005604 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005606 {
5607 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005610 return( ret );
5611 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005616 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005619 }
5620#endif
5621
Hanno Beckerf0242852019-07-09 17:30:02 +01005622 /* Check actual (decrypted) record content length against
5623 * configured maximum. */
5624 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5625 {
5626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5628 }
5629
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630 return( 0 );
5631}
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005634
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005635/*
5636 * Read a record.
5637 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005638 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5639 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5640 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005641 */
Hanno Becker1097b342018-08-15 14:09:41 +01005642
5643/* Helper functions for mbedtls_ssl_read_record(). */
5644static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005645static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5646static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005647
Hanno Becker327c93b2018-08-15 13:56:18 +01005648int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005649 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005650{
5651 int ret;
5652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005655 if( ssl->keep_current_message == 0 )
5656 {
5657 do {
Simon Butcher99000142016-10-13 17:21:01 +01005658
Hanno Becker26994592018-08-15 14:14:59 +01005659 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005660 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005661 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005662
Hanno Beckere74d5562018-08-15 14:26:08 +01005663 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005664 {
Hanno Becker40f50842018-08-15 14:48:01 +01005665#if defined(MBEDTLS_SSL_PROTO_DTLS)
5666 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005667
Hanno Becker40f50842018-08-15 14:48:01 +01005668 /* We only check for buffered messages if the
5669 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005670 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005671 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005672 {
Hanno Becker40f50842018-08-15 14:48:01 +01005673 if( ssl_load_buffered_message( ssl ) == 0 )
5674 have_buffered = 1;
5675 }
5676
5677 if( have_buffered == 0 )
5678#endif /* MBEDTLS_SSL_PROTO_DTLS */
5679 {
5680 ret = ssl_get_next_record( ssl );
5681 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5682 continue;
5683
5684 if( ret != 0 )
5685 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005686 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005687 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005688 return( ret );
5689 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005690 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005691 }
5692
5693 ret = mbedtls_ssl_handle_message_type( ssl );
5694
Hanno Becker40f50842018-08-15 14:48:01 +01005695#if defined(MBEDTLS_SSL_PROTO_DTLS)
5696 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5697 {
5698 /* Buffer future message */
5699 ret = ssl_buffer_message( ssl );
5700 if( ret != 0 )
5701 return( ret );
5702
5703 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5704 }
5705#endif /* MBEDTLS_SSL_PROTO_DTLS */
5706
Hanno Becker90333da2017-10-10 11:27:13 +01005707 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5708 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005709
5710 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005711 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005712 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005713 return( ret );
5714 }
5715
Hanno Becker327c93b2018-08-15 13:56:18 +01005716 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005717 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005718 {
5719 mbedtls_ssl_update_handshake_status( ssl );
5720 }
Simon Butcher99000142016-10-13 17:21:01 +01005721 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005722 else
Simon Butcher99000142016-10-13 17:21:01 +01005723 {
Hanno Becker02f59072018-08-15 14:00:24 +01005724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005725 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005726 }
5727
5728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5729
5730 return( 0 );
5731}
5732
Hanno Becker40f50842018-08-15 14:48:01 +01005733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005734static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005735{
Hanno Becker40f50842018-08-15 14:48:01 +01005736 if( ssl->in_left > ssl->next_record_offset )
5737 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005738
Hanno Becker40f50842018-08-15 14:48:01 +01005739 return( 0 );
5740}
5741
5742static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5743{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005744 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005745 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005746 int ret = 0;
5747
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005748 if( hs == NULL )
5749 return( -1 );
5750
Hanno Beckere00ae372018-08-20 09:39:42 +01005751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5752
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005753 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5754 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5755 {
5756 /* Check if we have seen a ChangeCipherSpec before.
5757 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005758 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005759 {
5760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5761 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005762 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005763 }
5764
Hanno Becker39b8bc92018-08-28 17:17:13 +01005765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005766 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5767 ssl->in_msglen = 1;
5768 ssl->in_msg[0] = 1;
5769
5770 /* As long as they are equal, the exact value doesn't matter. */
5771 ssl->in_left = 0;
5772 ssl->next_record_offset = 0;
5773
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005774 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005775 goto exit;
5776 }
Hanno Becker37f95322018-08-16 13:55:32 +01005777
Hanno Beckerb8f50142018-08-28 10:01:34 +01005778#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005779 /* Debug only */
5780 {
5781 unsigned offset;
5782 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5783 {
5784 hs_buf = &hs->buffering.hs[offset];
5785 if( hs_buf->is_valid == 1 )
5786 {
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5788 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005789 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005790 }
5791 }
5792 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005793#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005794
5795 /* Check if we have buffered and/or fully reassembled the
5796 * next handshake message. */
5797 hs_buf = &hs->buffering.hs[0];
5798 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5799 {
5800 /* Synthesize a record containing the buffered HS message. */
5801 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5802 ( hs_buf->data[2] << 8 ) |
5803 hs_buf->data[3];
5804
5805 /* Double-check that we haven't accidentally buffered
5806 * a message that doesn't fit into the input buffer. */
5807 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5808 {
5809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5811 }
5812
5813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5814 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5815 hs_buf->data, msg_len + 12 );
5816
5817 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5818 ssl->in_hslen = msg_len + 12;
5819 ssl->in_msglen = msg_len + 12;
5820 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5821
5822 ret = 0;
5823 goto exit;
5824 }
5825 else
5826 {
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5828 hs->in_msg_seq ) );
5829 }
5830
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005831 ret = -1;
5832
5833exit:
5834
5835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5836 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005837}
5838
Hanno Beckera02b0b42018-08-21 17:20:27 +01005839static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5840 size_t desired )
5841{
5842 int offset;
5843 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5845 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005846
Hanno Becker01315ea2018-08-21 17:22:17 +01005847 /* Get rid of future records epoch first, if such exist. */
5848 ssl_free_buffered_record( ssl );
5849
5850 /* Check if we have enough space available now. */
5851 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5852 hs->buffering.total_bytes_buffered ) )
5853 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005855 return( 0 );
5856 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005857
Hanno Becker4f432ad2018-08-28 10:02:32 +01005858 /* We don't have enough space to buffer the next expected handshake
5859 * message. Remove buffers used for future messages to gain space,
5860 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005861 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5862 offset >= 0; offset-- )
5863 {
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5865 offset ) );
5866
Hanno Beckerb309b922018-08-23 13:18:05 +01005867 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005868
5869 /* Check if we have enough space available now. */
5870 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5871 hs->buffering.total_bytes_buffered ) )
5872 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005874 return( 0 );
5875 }
5876 }
5877
5878 return( -1 );
5879}
5880
Hanno Becker40f50842018-08-15 14:48:01 +01005881static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5882{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005883 int ret = 0;
5884 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5885
5886 if( hs == NULL )
5887 return( 0 );
5888
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5890
5891 switch( ssl->in_msgtype )
5892 {
5893 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005895
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005896 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005897 break;
5898
5899 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005900 {
5901 unsigned recv_msg_seq_offset;
5902 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5903 mbedtls_ssl_hs_buffer *hs_buf;
5904 size_t msg_len = ssl->in_hslen - 12;
5905
5906 /* We should never receive an old handshake
5907 * message - double-check nonetheless. */
5908 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5909 {
5910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5912 }
5913
5914 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5915 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5916 {
5917 /* Silently ignore -- message too far in the future */
5918 MBEDTLS_SSL_DEBUG_MSG( 2,
5919 ( "Ignore future HS message with sequence number %u, "
5920 "buffering window %u - %u",
5921 recv_msg_seq, ssl->handshake->in_msg_seq,
5922 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5923
5924 goto exit;
5925 }
5926
5927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5928 recv_msg_seq, recv_msg_seq_offset ) );
5929
5930 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5931
5932 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005933 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005934 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005935 size_t reassembly_buf_sz;
5936
Hanno Becker37f95322018-08-16 13:55:32 +01005937 hs_buf->is_fragmented =
5938 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5939
5940 /* We copy the message back into the input buffer
5941 * after reassembly, so check that it's not too large.
5942 * This is an implementation-specific limitation
5943 * and not one from the standard, hence it is not
5944 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005945 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005946 {
5947 /* Ignore message */
5948 goto exit;
5949 }
5950
Hanno Beckere0b150f2018-08-21 15:51:03 +01005951 /* Check if we have enough space to buffer the message. */
5952 if( hs->buffering.total_bytes_buffered >
5953 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5954 {
5955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5957 }
5958
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005959 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5960 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005961
5962 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5963 hs->buffering.total_bytes_buffered ) )
5964 {
5965 if( recv_msg_seq_offset > 0 )
5966 {
5967 /* If we can't buffer a future message because
5968 * of space limitations -- ignore. */
5969 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",
5970 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5971 (unsigned) hs->buffering.total_bytes_buffered ) );
5972 goto exit;
5973 }
Hanno Beckere1801392018-08-21 16:51:05 +01005974 else
5975 {
5976 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",
5977 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5978 (unsigned) hs->buffering.total_bytes_buffered ) );
5979 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005980
Hanno Beckera02b0b42018-08-21 17:20:27 +01005981 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005982 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005983 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",
5984 (unsigned) msg_len,
5985 (unsigned) reassembly_buf_sz,
5986 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005987 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005988 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5989 goto exit;
5990 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005991 }
5992
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5994 msg_len ) );
5995
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005996 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5997 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005998 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005999 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006000 goto exit;
6001 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006002 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006003
6004 /* Prepare final header: copy msg_type, length and message_seq,
6005 * then add standardised fragment_offset and fragment_length */
6006 memcpy( hs_buf->data, ssl->in_msg, 6 );
6007 memset( hs_buf->data + 6, 0, 3 );
6008 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
6009
6010 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006011
6012 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006013 }
6014 else
6015 {
6016 /* Make sure msg_type and length are consistent */
6017 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
6018 {
6019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6020 /* Ignore */
6021 goto exit;
6022 }
6023 }
6024
Hanno Becker4422bbb2018-08-20 09:40:19 +01006025 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006026 {
6027 size_t frag_len, frag_off;
6028 unsigned char * const msg = hs_buf->data + 12;
6029
6030 /*
6031 * Check and copy current fragment
6032 */
6033
6034 /* Validation of header fields already done in
6035 * mbedtls_ssl_prepare_handshake_record(). */
6036 frag_off = ssl_get_hs_frag_off( ssl );
6037 frag_len = ssl_get_hs_frag_len( ssl );
6038
6039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6040 frag_off, frag_len ) );
6041 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6042
6043 if( hs_buf->is_fragmented )
6044 {
6045 unsigned char * const bitmask = msg + msg_len;
6046 ssl_bitmask_set( bitmask, frag_off, frag_len );
6047 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6048 msg_len ) == 0 );
6049 }
6050 else
6051 {
6052 hs_buf->is_complete = 1;
6053 }
6054
6055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6056 hs_buf->is_complete ? "" : "not yet " ) );
6057 }
6058
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006059 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006060 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006061
6062 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006063 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006064 break;
6065 }
6066
6067exit:
6068
6069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6070 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006071}
6072#endif /* MBEDTLS_SSL_PROTO_DTLS */
6073
Hanno Becker1097b342018-08-15 14:09:41 +01006074static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006075{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006076 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006077 * Consume last content-layer message and potentially
6078 * update in_msglen which keeps track of the contents'
6079 * consumption state.
6080 *
6081 * (1) Handshake messages:
6082 * Remove last handshake message, move content
6083 * and adapt in_msglen.
6084 *
6085 * (2) Alert messages:
6086 * Consume whole record content, in_msglen = 0.
6087 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006088 * (3) Change cipher spec:
6089 * Consume whole record content, in_msglen = 0.
6090 *
6091 * (4) Application data:
6092 * Don't do anything - the record layer provides
6093 * the application data as a stream transport
6094 * and consumes through mbedtls_ssl_read only.
6095 *
6096 */
6097
6098 /* Case (1): Handshake messages */
6099 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006100 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006101 /* Hard assertion to be sure that no application data
6102 * is in flight, as corrupting ssl->in_msglen during
6103 * ssl->in_offt != NULL is fatal. */
6104 if( ssl->in_offt != NULL )
6105 {
6106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6108 }
6109
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006110 /*
6111 * Get next Handshake message in the current record
6112 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006113
Hanno Becker4a810fb2017-05-24 16:27:30 +01006114 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006115 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006116 * current handshake content: If DTLS handshake
6117 * fragmentation is used, that's the fragment
6118 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006119 * size here is faulty and should be changed at
6120 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006121 * (2) While it doesn't seem to cause problems, one
6122 * has to be very careful not to assume that in_hslen
6123 * is always <= in_msglen in a sensible communication.
6124 * Again, it's wrong for DTLS handshake fragmentation.
6125 * The following check is therefore mandatory, and
6126 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006127 * Additionally, ssl->in_hslen might be arbitrarily out of
6128 * bounds after handling a DTLS message with an unexpected
6129 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006130 */
6131 if( ssl->in_hslen < ssl->in_msglen )
6132 {
6133 ssl->in_msglen -= ssl->in_hslen;
6134 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6135 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006136
Hanno Becker4a810fb2017-05-24 16:27:30 +01006137 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6138 ssl->in_msg, ssl->in_msglen );
6139 }
6140 else
6141 {
6142 ssl->in_msglen = 0;
6143 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006144
Hanno Becker4a810fb2017-05-24 16:27:30 +01006145 ssl->in_hslen = 0;
6146 }
6147 /* Case (4): Application data */
6148 else if( ssl->in_offt != NULL )
6149 {
6150 return( 0 );
6151 }
6152 /* Everything else (CCS & Alerts) */
6153 else
6154 {
6155 ssl->in_msglen = 0;
6156 }
6157
Hanno Becker1097b342018-08-15 14:09:41 +01006158 return( 0 );
6159}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006160
Hanno Beckere74d5562018-08-15 14:26:08 +01006161static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6162{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006163 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006164 return( 1 );
6165
6166 return( 0 );
6167}
6168
Hanno Becker5f066e72018-08-16 14:56:31 +01006169#if defined(MBEDTLS_SSL_PROTO_DTLS)
6170
6171static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6172{
6173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6174 if( hs == NULL )
6175 return;
6176
Hanno Becker01315ea2018-08-21 17:22:17 +01006177 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006178 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006179 hs->buffering.total_bytes_buffered -=
6180 hs->buffering.future_record.len;
6181
6182 mbedtls_free( hs->buffering.future_record.data );
6183 hs->buffering.future_record.data = NULL;
6184 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006185}
6186
6187static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6188{
6189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6190 unsigned char * rec;
6191 size_t rec_len;
6192 unsigned rec_epoch;
6193
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006194 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006195 return( 0 );
6196
6197 if( hs == NULL )
6198 return( 0 );
6199
Hanno Becker5f066e72018-08-16 14:56:31 +01006200 rec = hs->buffering.future_record.data;
6201 rec_len = hs->buffering.future_record.len;
6202 rec_epoch = hs->buffering.future_record.epoch;
6203
6204 if( rec == NULL )
6205 return( 0 );
6206
Hanno Becker4cb782d2018-08-20 11:19:05 +01006207 /* Only consider loading future records if the
6208 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006209 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006210 return( 0 );
6211
Hanno Becker5f066e72018-08-16 14:56:31 +01006212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6213
6214 if( rec_epoch != ssl->in_epoch )
6215 {
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6217 goto exit;
6218 }
6219
6220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6221
6222 /* Double-check that the record is not too large */
6223 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6224 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6228 }
6229
6230 memcpy( ssl->in_hdr, rec, rec_len );
6231 ssl->in_left = rec_len;
6232 ssl->next_record_offset = 0;
6233
6234 ssl_free_buffered_record( ssl );
6235
6236exit:
6237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6238 return( 0 );
6239}
6240
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006241static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6242 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006243{
6244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006245
6246 /* Don't buffer future records outside handshakes. */
6247 if( hs == NULL )
6248 return( 0 );
6249
6250 /* Only buffer handshake records (we are only interested
6251 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006253 return( 0 );
6254
6255 /* Don't buffer more than one future epoch record. */
6256 if( hs->buffering.future_record.data != NULL )
6257 return( 0 );
6258
Hanno Becker01315ea2018-08-21 17:22:17 +01006259 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006260 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006261 hs->buffering.total_bytes_buffered ) )
6262 {
6263 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 +01006264 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006265 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006266 return( 0 );
6267 }
6268
Hanno Becker5f066e72018-08-16 14:56:31 +01006269 /* Buffer record */
6270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6271 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006272 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006273
6274 /* ssl_parse_record_header() only considers records
6275 * of the next epoch as candidates for buffering. */
6276 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006277 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006278
6279 hs->buffering.future_record.data =
6280 mbedtls_calloc( 1, hs->buffering.future_record.len );
6281 if( hs->buffering.future_record.data == NULL )
6282 {
6283 /* If we run out of RAM trying to buffer a
6284 * record from the next epoch, just ignore. */
6285 return( 0 );
6286 }
6287
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006288 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006289
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006290 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006291 return( 0 );
6292}
6293
6294#endif /* MBEDTLS_SSL_PROTO_DTLS */
6295
Hanno Beckere74d5562018-08-15 14:26:08 +01006296static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006297{
6298 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006299 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006300
Hanno Becker5f066e72018-08-16 14:56:31 +01006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
6302 /* We might have buffered a future record; if so,
6303 * and if the epoch matches now, load it.
6304 * On success, this call will set ssl->in_left to
6305 * the length of the buffered record, so that
6306 * the calls to ssl_fetch_input() below will
6307 * essentially be no-ops. */
6308 ret = ssl_load_buffered_record( ssl );
6309 if( ret != 0 )
6310 return( ret );
6311#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006312
Hanno Becker8b09b732019-05-08 12:03:28 +01006313 /* Ensure that we have enough space available for the default form
6314 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6315 * with no space for CIDs counted in). */
6316 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6317 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006320 return( ret );
6321 }
6322
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006323 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6324 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006327 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006328 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006329 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6330 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006331 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 if( ret != 0 )
6333 return( ret );
6334
6335 /* Fall through to handling of unexpected records */
6336 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6337 }
6338
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006339 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6340 {
Hanno Becker87b56262019-07-10 14:37:41 +01006341#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006342 /* Reset in pointers to default state for TLS/DTLS records,
6343 * assuming no CID and no offset between record content and
6344 * record plaintext. */
6345 ssl_update_in_pointers( ssl );
6346
Hanno Becker69412452019-07-12 08:33:49 +01006347 /* Setup internal message pointers from record structure. */
6348 ssl->in_msgtype = rec.type;
6349#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6350 ssl->in_len = ssl->in_cid + rec.cid_len;
6351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006352 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006353 ssl->in_msglen = rec.data_len;
6354
Hanno Becker87b56262019-07-10 14:37:41 +01006355 ret = ssl_check_client_reconnect( ssl );
6356 if( ret != 0 )
6357 return( ret );
6358#endif
6359
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006360 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006361 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006362
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6364 "(header)" ) );
6365 }
6366 else
6367 {
6368 /* Skip invalid record and the rest of the datagram */
6369 ssl->next_record_offset = 0;
6370 ssl->in_left = 0;
6371
6372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6373 "(header)" ) );
6374 }
6375
6376 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006377 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006378 }
Hanno Becker87b56262019-07-10 14:37:41 +01006379 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006380#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006381 {
6382 return( ret );
6383 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006384 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006387 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006388 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006389 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006390 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006391 if( ssl->next_record_offset < ssl->in_left )
6392 {
6393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6394 }
6395 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006396 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006398#if defined(MBEDTLS_SSL_PROTO_TLS)
6399 {
Hanno Beckere0452772019-07-10 17:12:07 +01006400 /*
6401 * Fetch record contents from underlying transport.
6402 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006403 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006404 if( ret != 0 )
6405 {
6406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6407 return( ret );
6408 }
6409
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006410 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006411 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006412#endif /* MBEDTLS_SSL_PROTO_TLS */
6413
6414 /*
6415 * Decrypt record contents.
6416 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006417
Hanno Beckera89610a2019-07-11 13:07:45 +01006418 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006421 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006422 {
6423 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006424 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006425 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006426 /* Except when waiting for Finished as a bad mac here
6427 * probably means something went wrong in the handshake
6428 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6429 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6430 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6431 {
6432#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6433 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6434 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006435 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006436 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6437 }
6438#endif
6439 return( ret );
6440 }
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006443 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6444 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6447 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006448 }
6449#endif
6450
Hanno Becker4a810fb2017-05-24 16:27:30 +01006451 /* As above, invalid records cause
6452 * dismissal of the whole datagram. */
6453
6454 ssl->next_record_offset = 0;
6455 ssl->in_left = 0;
6456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006458 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006459 }
6460
6461 return( ret );
6462 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006463 MBEDTLS_SSL_TRANSPORT_ELSE
6464#endif /* MBEDTLS_SSL_PROTO_DTLS */
6465#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006466 {
6467 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6469 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006470 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006471 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006473 }
6474#endif
6475 return( ret );
6476 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006477#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006478 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006479
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006480
6481 /* Reset in pointers to default state for TLS/DTLS records,
6482 * assuming no CID and no offset between record content and
6483 * record plaintext. */
6484 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006485#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6486 ssl->in_len = ssl->in_cid + rec.cid_len;
6487#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006488 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006489
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006490 /* The record content type may change during decryption,
6491 * so re-read it. */
6492 ssl->in_msgtype = rec.type;
6493 /* Also update the input buffer, because unfortunately
6494 * the server-side ssl_parse_client_hello() reparses the
6495 * record header when receiving a ClientHello initiating
6496 * a renegotiation. */
6497 ssl->in_hdr[0] = rec.type;
6498 ssl->in_msg = rec.buf + rec.data_offset;
6499 ssl->in_msglen = rec.data_len;
6500 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6501 ssl->in_len[1] = (unsigned char)( rec.data_len );
6502
Simon Butcher99000142016-10-13 17:21:01 +01006503 return( 0 );
6504}
6505
6506int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6507{
6508 int ret;
6509
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006510 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006511 * Handle particular types of records
6512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 {
Simon Butcher99000142016-10-13 17:21:01 +01006515 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6516 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006517 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 }
6520
Hanno Beckere678eaa2018-08-21 14:57:46 +01006521 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006522 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006523 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006524 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6526 ssl->in_msglen ) );
6527 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006528 }
6529
Hanno Beckere678eaa2018-08-21 14:57:46 +01006530 if( ssl->in_msg[0] != 1 )
6531 {
6532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6533 ssl->in_msg[0] ) );
6534 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6535 }
6536
6537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006538 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006539 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6540 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6541 {
6542 if( ssl->handshake == NULL )
6543 {
6544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6545 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6546 }
6547
6548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6549 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6550 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006551#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006552 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006556 if( ssl->in_msglen != 2 )
6557 {
6558 /* Note: Standard allows for more than one 2 byte alert
6559 to be packed in a single message, but Mbed TLS doesn't
6560 currently support this. */
6561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6562 ssl->in_msglen ) );
6563 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6564 }
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 ssl->in_msg[0], ssl->in_msg[1] ) );
6568
6569 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006570 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006575 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 }
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6580 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6583 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006585
6586#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6587 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6588 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6589 {
Hanno Becker90333da2017-10-10 11:27:13 +01006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006591 /* Will be handled when trying to parse ServerHello */
6592 return( 0 );
6593 }
6594#endif
6595
6596#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006597 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006598 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6599 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006600 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6601 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6602 {
6603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6604 /* Will be handled in mbedtls_ssl_parse_certificate() */
6605 return( 0 );
6606 }
6607#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6608
6609 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006610 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 }
6612
Hanno Beckerc76c6192017-06-06 10:03:17 +01006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006615 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006616 /* Drop unexpected ApplicationData records,
6617 * except at the beginning of renegotiations */
6618 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6619 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6620#if defined(MBEDTLS_SSL_RENEGOTIATION)
6621 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6622 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006623#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006624 )
6625 {
6626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6627 return( MBEDTLS_ERR_SSL_NON_FATAL );
6628 }
6629
6630 if( ssl->handshake != NULL &&
6631 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6632 {
6633 ssl_handshake_wrapup_free_hs_transform( ssl );
6634 }
6635 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006637
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 return( 0 );
6639}
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006642{
6643 int ret;
6644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006648 {
6649 return( ret );
6650 }
6651
6652 return( 0 );
6653}
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006656 unsigned char level,
6657 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006658{
6659 int ret;
6660
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006661 if( ssl == NULL || ssl->conf == NULL )
6662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006668 ssl->out_msglen = 2;
6669 ssl->out_msg[0] = level;
6670 ssl->out_msg[1] = message;
6671
Hanno Becker67bc7c32018-08-06 11:33:50 +01006672 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006675 return( ret );
6676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006678
6679 return( 0 );
6680}
6681
Hanno Becker17572472019-02-08 07:19:04 +00006682#if defined(MBEDTLS_X509_CRT_PARSE_C)
6683static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6684{
6685#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6686 if( session->peer_cert != NULL )
6687 {
6688 mbedtls_x509_crt_free( session->peer_cert );
6689 mbedtls_free( session->peer_cert );
6690 session->peer_cert = NULL;
6691 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006692#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006693 if( session->peer_cert_digest != NULL )
6694 {
6695 /* Zeroization is not necessary. */
6696 mbedtls_free( session->peer_cert_digest );
6697 session->peer_cert_digest = NULL;
6698 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6699 session->peer_cert_digest_len = 0;
6700 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006701#else
6702 ((void) session);
6703#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006704}
6705#endif /* MBEDTLS_X509_CRT_PARSE_C */
6706
Paul Bakker5121ce52009-01-03 21:22:43 +00006707/*
6708 * Handshake functions
6709 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006710#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006711/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006713{
Hanno Beckerdf645962019-06-26 13:02:22 +01006714 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6715 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006718
Hanno Becker5097cba2019-02-05 13:36:46 +00006719 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006722 ssl->state++;
6723 return( 0 );
6724 }
6725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728}
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006731{
Hanno Beckerdf645962019-06-26 13:02:22 +01006732 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6733 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736
Hanno Becker5097cba2019-02-05 13:36:46 +00006737 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006740 ssl->state++;
6741 return( 0 );
6742 }
6743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006746}
Gilles Peskinef9828522017-05-03 12:28:43 +02006747
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006748#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006749/* Some certificate support -> implement write and parse */
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006754 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006756 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6757 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006760
Hanno Becker5097cba2019-02-05 13:36:46 +00006761 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006764 ssl->state++;
6765 return( 0 );
6766 }
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006769 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6770 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 {
6772 if( ssl->client_auth == 0 )
6773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 ssl->state++;
6776 return( 0 );
6777 }
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 /*
6781 * If using SSLv3 and got no cert, send an Alert message
6782 * (otherwise an empty Certificate message will be sent).
6783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006785 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 {
6787 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6789 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6790 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 goto write_msg;
6794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797#endif /* MBEDTLS_SSL_CLI_C */
6798#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006799 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6804 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
6811 /*
6812 * 0 . 0 handshake type
6813 * 1 . 3 handshake length
6814 * 4 . 6 length of all certs
6815 * 7 . 9 length of cert. 1
6816 * 10 . n-1 peer certificate
6817 * n . n+2 length of cert. 2
6818 * n+3 . ... upper level cert, etc.
6819 */
6820 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Paul Bakker29087132010-03-21 21:03:34 +00006823 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 {
6825 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006826 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006829 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 }
6832
6833 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6834 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6835 ssl->out_msg[i + 2] = (unsigned char)( n );
6836
6837 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6838 i += n; crt = crt->next;
6839 }
6840
6841 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6842 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6843 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6844
6845 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6847 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006849#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006850write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
6853 ssl->state++;
6854
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006855 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 return( ret );
6859 }
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Paul Bakkered27a042013-04-18 22:46:23 +02006863 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864}
6865
Hanno Becker285ff0c2019-01-31 07:44:03 +00006866#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006867
6868#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006869static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6870 unsigned char *crt_buf,
6871 size_t crt_buf_len )
6872{
6873 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6874
6875 if( peer_crt == NULL )
6876 return( -1 );
6877
6878 if( peer_crt->raw.len != crt_buf_len )
6879 return( -1 );
6880
Hanno Becker68b856d2019-02-08 14:00:04 +00006881 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006882}
Hanno Becker5882dd02019-06-06 16:25:57 +01006883#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006884static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6885 unsigned char *crt_buf,
6886 size_t crt_buf_len )
6887{
6888 int ret;
6889 unsigned char const * const peer_cert_digest =
6890 ssl->session->peer_cert_digest;
6891 mbedtls_md_type_t const peer_cert_digest_type =
6892 ssl->session->peer_cert_digest_type;
6893 mbedtls_md_info_t const * const digest_info =
6894 mbedtls_md_info_from_type( peer_cert_digest_type );
6895 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6896 size_t digest_len;
6897
6898 if( peer_cert_digest == NULL || digest_info == NULL )
6899 return( -1 );
6900
6901 digest_len = mbedtls_md_get_size( digest_info );
6902 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6903 return( -1 );
6904
6905 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6906 if( ret != 0 )
6907 return( -1 );
6908
6909 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6910}
Hanno Becker5882dd02019-06-06 16:25:57 +01006911#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006912#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006913
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006914/*
6915 * Once the certificate message is read, parse it into a cert chain and
6916 * perform basic checks, but leave actual verification to the caller
6917 */
Hanno Becker35e41772019-02-05 15:37:23 +00006918static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6919 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006920{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006921 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006922#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6923 int crt_cnt=0;
6924#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006925 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006926 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006931 mbedtls_ssl_pend_fatal_alert( ssl,
6932 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 }
6935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6937 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006940 mbedtls_ssl_pend_fatal_alert( ssl,
6941 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 }
6944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006946
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006950 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006951
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006952 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006956 mbedtls_ssl_pend_fatal_alert( ssl,
6957 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006959 }
6960
Hanno Becker33c3dc82019-01-30 14:46:46 +00006961 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6962 i += 3;
6963
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 while( i < ssl->in_hslen )
6966 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006967 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006968 if ( i + 3 > ssl->in_hslen ) {
6969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006970 mbedtls_ssl_pend_fatal_alert( ssl,
6971 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006972 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6973 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006974 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6975 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 if( ssl->in_msg[i] != 0 )
6977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006979 mbedtls_ssl_pend_fatal_alert( ssl,
6980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 }
6983
Hanno Becker33c3dc82019-01-30 14:46:46 +00006984 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6986 | (unsigned int) ssl->in_msg[i + 2];
6987 i += 3;
6988
6989 if( n < 128 || i + n > ssl->in_hslen )
6990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006992 mbedtls_ssl_pend_fatal_alert( ssl,
6993 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 }
6996
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006998#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6999 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007000 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7001 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007002 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007003 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007004 /* During client-side renegotiation, check that the server's
7005 * end-CRTs hasn't changed compared to the initial handshake,
7006 * mitigating the triple handshake attack. On success, reuse
7007 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7009 if( ssl_check_peer_crt_unchanged( ssl,
7010 &ssl->in_msg[i],
7011 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007012 {
Hanno Becker35e41772019-02-05 15:37:23 +00007013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007014 mbedtls_ssl_pend_fatal_alert( ssl,
7015 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007016 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007017 }
Hanno Becker35e41772019-02-05 15:37:23 +00007018
7019 /* Now we can safely free the original chain. */
7020 ssl_clear_peer_cert( ssl->session );
7021 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007022#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7023
Hanno Becker33c3dc82019-01-30 14:46:46 +00007024 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007025#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007026 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007027#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007028 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007029 * it in-place from the input buffer instead of making a copy. */
7030 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7031#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007032 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007033 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007034 case 0: /*ok*/
7035 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7036 /* Ignore certificate with an unknown algorithm: maybe a
7037 prior certificate was already trusted. */
7038 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007039
Hanno Becker33c3dc82019-01-30 14:46:46 +00007040 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7041 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7042 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007043
Hanno Becker33c3dc82019-01-30 14:46:46 +00007044 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7046 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007047
Hanno Becker33c3dc82019-01-30 14:46:46 +00007048 default:
7049 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7050 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007051 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007052 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7053 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054 }
7055
7056 i += n;
7057 }
7058
Hanno Becker35e41772019-02-05 15:37:23 +00007059 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007060 return( 0 );
7061}
7062
Hanno Beckerb8a08572019-02-05 12:49:06 +00007063#if defined(MBEDTLS_SSL_SRV_C)
7064static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7065{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007066 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007067 return( -1 );
7068
7069#if defined(MBEDTLS_SSL_PROTO_SSL3)
7070 /*
7071 * Check if the client sent an empty certificate
7072 */
Hanno Becker2881d802019-05-22 14:44:53 +01007073 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007074 {
7075 if( ssl->in_msglen == 2 &&
7076 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7077 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7078 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7079 {
7080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7081 return( 0 );
7082 }
7083
7084 return( -1 );
7085 }
7086#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7087
7088#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7089 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7090 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7091 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7092 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7093 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7094 {
7095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7096 return( 0 );
7097 }
7098
Hanno Beckerb8a08572019-02-05 12:49:06 +00007099#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7100 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007101
7102 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007103}
7104#endif /* MBEDTLS_SSL_SRV_C */
7105
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007106/* Check if a certificate message is expected.
7107 * Return either
7108 * - SSL_CERTIFICATE_EXPECTED, or
7109 * - SSL_CERTIFICATE_SKIP
7110 * indicating whether a Certificate message is expected or not.
7111 */
7112#define SSL_CERTIFICATE_EXPECTED 0
7113#define SSL_CERTIFICATE_SKIP 1
7114static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7115 int authmode )
7116{
Hanno Becker473f98f2019-06-26 10:27:32 +01007117 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007118 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007119
7120 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7121 return( SSL_CERTIFICATE_SKIP );
7122
7123#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007124 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007125 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7127 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7128 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007129 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007130 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007131
7132 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7133 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007134 ssl->session_negotiate->verify_result =
7135 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7136 return( SSL_CERTIFICATE_SKIP );
7137 }
7138 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007139#else
7140 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007141#endif /* MBEDTLS_SSL_SRV_C */
7142
7143 return( SSL_CERTIFICATE_EXPECTED );
7144}
7145
Hanno Becker3cf50612019-02-05 14:36:34 +00007146static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7147 int authmode,
7148 mbedtls_x509_crt *chain,
7149 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007150{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007151 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007152 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007153 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007154 mbedtls_x509_crt *ca_chain;
7155 mbedtls_x509_crl *ca_crl;
7156
7157 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7158 return( 0 );
7159
7160#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7161 if( ssl->handshake->sni_ca_chain != NULL )
7162 {
7163 ca_chain = ssl->handshake->sni_ca_chain;
7164 ca_crl = ssl->handshake->sni_ca_crl;
7165 }
7166 else
7167#endif
7168 {
7169 ca_chain = ssl->conf->ca_chain;
7170 ca_crl = ssl->conf->ca_crl;
7171 }
7172
7173 /*
7174 * Main check: verify certificate
7175 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007176 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007177 chain,
7178 ca_chain, ca_crl,
7179 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007180#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007181 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007182#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007183 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007184#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7185 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7186#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7187 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007188
Hanno Becker8c13ee62019-02-26 16:48:17 +00007189 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007190 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007192 }
7193
7194#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007195 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007196 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7197#endif
7198
7199 /*
7200 * Secondary checks: always done, but change 'ret' only if it was 0
7201 */
7202
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007203#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007204 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007205 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007206#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007207 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007208#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007209 mbedtls_pk_context *pk;
7210 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7211 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007212 {
7213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007214 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007215 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007216
7217 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007218 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007219 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007220 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007221 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007222
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007223 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007224#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007225
7226 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007227 {
7228 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7229
7230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007231 if( verify_ret == 0 )
7232 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007233 }
7234 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007235#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007236
7237 if( mbedtls_ssl_check_cert_usage( chain,
7238 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007239 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7240 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007241 &ssl->session_negotiate->verify_result ) != 0 )
7242 {
7243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007244 if( verify_ret == 0 )
7245 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007246 }
7247
7248 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7249 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7250 * with details encoded in the verification flags. All other kinds
7251 * of error codes, including those from the user provided f_vrfy
7252 * functions, are treated as fatal and lead to a failure of
7253 * ssl_parse_certificate even if verification was optional. */
7254 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007255 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7256 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007257 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007258 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007259 }
7260
7261 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7262 {
7263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007264 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007265 }
7266
Hanno Becker8c13ee62019-02-26 16:48:17 +00007267 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007268 {
7269 uint8_t alert;
7270
7271 /* The certificate may have been rejected for several reasons.
7272 Pick one and send the corresponding alert. Which alert to send
7273 may be a subject of debate in some cases. */
7274 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7275 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7276 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7277 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7278 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7279 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7280 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7281 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7282 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7283 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7284 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7285 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7286 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7287 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7288 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7289 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7290 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7291 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7292 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7293 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7294 else
7295 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007296 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007297 }
7298
7299#if defined(MBEDTLS_DEBUG_C)
7300 if( ssl->session_negotiate->verify_result != 0 )
7301 {
7302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7303 ssl->session_negotiate->verify_result ) );
7304 }
7305 else
7306 {
7307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7308 }
7309#endif /* MBEDTLS_DEBUG_C */
7310
Hanno Becker8c13ee62019-02-26 16:48:17 +00007311 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007312}
7313
Hanno Becker34106f62019-02-08 14:59:05 +00007314#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007315
7316#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007317static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7318 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007319{
7320 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007321 /* Remember digest of the peer's end-CRT. */
7322 ssl->session_negotiate->peer_cert_digest =
7323 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7324 if( ssl->session_negotiate->peer_cert_digest == NULL )
7325 {
7326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7327 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007328 mbedtls_ssl_pend_fatal_alert( ssl,
7329 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007330
7331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7332 }
7333
7334 ret = mbedtls_md( mbedtls_md_info_from_type(
7335 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7336 start, len,
7337 ssl->session_negotiate->peer_cert_digest );
7338
7339 ssl->session_negotiate->peer_cert_digest_type =
7340 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7341 ssl->session_negotiate->peer_cert_digest_len =
7342 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7343
7344 return( ret );
7345}
Hanno Becker5882dd02019-06-06 16:25:57 +01007346#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007347
7348static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7349 unsigned char *start, size_t len )
7350{
7351 unsigned char *end = start + len;
7352 int ret;
7353
7354 /* Make a copy of the peer's raw public key. */
7355 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7356 ret = mbedtls_pk_parse_subpubkey( &start, end,
7357 &ssl->handshake->peer_pubkey );
7358 if( ret != 0 )
7359 {
7360 /* We should have parsed the public key before. */
7361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7362 }
7363
7364 return( 0 );
7365}
7366#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7367
Hanno Becker3cf50612019-02-05 14:36:34 +00007368int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7369{
7370 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007371 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007372#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7373 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7374 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007375 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007376#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007377 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007379 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007380 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007381
7382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7383
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007384 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7385 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386 {
7387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007388 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007389 }
7390
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007391#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7392 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007393 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007394 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007395 chain = ssl->handshake->ecrs_peer_cert;
7396 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007397 goto crt_verify;
7398 }
7399#endif
7400
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007401 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007402 {
7403 /* mbedtls_ssl_read_record may have sent an alert already. We
7404 let it decide whether to alert. */
7405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007406 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007407 }
7408
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007409#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007410 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7411 {
7412 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007413
Hanno Beckerb8a08572019-02-05 12:49:06 +00007414 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007415 ret = 0;
7416 else
7417 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007418
Hanno Becker613d4902019-02-05 13:11:17 +00007419 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007420 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007421#endif /* MBEDTLS_SSL_SRV_C */
7422
Hanno Becker35e41772019-02-05 15:37:23 +00007423 /* Clear existing peer CRT structure in case we tried to
7424 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007425 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007426
7427 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7428 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007429 {
7430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7431 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007432 mbedtls_ssl_pend_fatal_alert( ssl,
7433 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007434
Hanno Beckere4aeb762019-02-05 17:19:52 +00007435 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7436 goto exit;
7437 }
7438 mbedtls_x509_crt_init( chain );
7439
7440 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007441 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007442 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007443
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007444#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7445 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007446 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007447
7448crt_verify:
7449 if( ssl->handshake->ecrs_enabled)
7450 rs_ctx = &ssl->handshake->ecrs_ctx;
7451#endif
7452
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007454 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007455 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007456 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007457
Hanno Becker3008d282019-02-05 17:02:28 +00007458#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007459 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007460 size_t pk_len;
7461 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007462
Hanno Becker34106f62019-02-08 14:59:05 +00007463 /* We parse the CRT chain without copying, so
7464 * these pointers point into the input buffer,
7465 * and are hence still valid after freeing the
7466 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker5882dd02019-06-06 16:25:57 +01007468#if defined(MBEDTLS_SSL_RENEGOTIATION)
7469 unsigned char *crt_start;
7470 size_t crt_len;
7471
Hanno Becker34106f62019-02-08 14:59:05 +00007472 crt_start = chain->raw.p;
7473 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007474#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007475
Hanno Becker8c13ee62019-02-26 16:48:17 +00007476 pk_start = chain->cache->pk_raw.p;
7477 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007478
7479 /* Free the CRT structures before computing
7480 * digest and copying the peer's public key. */
7481 mbedtls_x509_crt_free( chain );
7482 mbedtls_free( chain );
7483 chain = NULL;
7484
Hanno Becker5882dd02019-06-06 16:25:57 +01007485#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007486 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007487 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007488 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007489#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007490
Hanno Becker34106f62019-02-08 14:59:05 +00007491 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007492 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007493 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 }
Hanno Becker34106f62019-02-08 14:59:05 +00007495#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7496 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007497 ssl->session_negotiate->peer_cert = chain;
7498 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007499#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Hanno Becker613d4902019-02-05 13:11:17 +00007503exit:
7504
Hanno Beckere4aeb762019-02-05 17:19:52 +00007505 if( ret == 0 )
7506 ssl->state++;
7507
7508#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7509 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7510 {
7511 ssl->handshake->ecrs_peer_cert = chain;
7512 chain = NULL;
7513 }
7514#endif
7515
7516 if( chain != NULL )
7517 {
7518 mbedtls_x509_crt_free( chain );
7519 mbedtls_free( chain );
7520 }
7521
Paul Bakker5121ce52009-01-03 21:22:43 +00007522 return( ret );
7523}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007524#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527{
7528 int ret;
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 ssl->out_msglen = 1;
7534 ssl->out_msg[0] = 1;
7535
Paul Bakker5121ce52009-01-03 21:22:43 +00007536 ssl->state++;
7537
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007538 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007539 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541 return( ret );
7542 }
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007545
7546 return( 0 );
7547}
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007550{
7551 int ret;
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007554
Hanno Becker327c93b2018-08-15 13:56:18 +01007555 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007558 return( ret );
7559 }
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007564 mbedtls_ssl_pend_fatal_alert( ssl,
7565 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007567 }
7568
Hanno Beckere678eaa2018-08-21 14:57:46 +01007569 /* CCS records are only accepted if they have length 1 and content '1',
7570 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007571
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007572 /*
7573 * Switch to our negotiated transform and session parameters for inbound
7574 * data.
7575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007577 ssl->transform_in = ssl->transform_negotiate;
7578 ssl->session_in = ssl->session_negotiate;
7579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007581 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007584 ssl_dtls_replay_reset( ssl );
7585#endif
7586
7587 /* Increment epoch */
7588 if( ++ssl->in_epoch == 0 )
7589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007591 /* This is highly unlikely to happen for legitimate reasons, so
7592 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007594 }
7595 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007596 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007598#if defined(MBEDTLS_SSL_PROTO_TLS)
7599 {
7600 memset( ssl->in_ctr, 0, 8 );
7601 }
7602#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007603
Hanno Beckerf5970a02019-05-08 09:38:41 +01007604 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7607 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007612 mbedtls_ssl_pend_fatal_alert( ssl,
7613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007615 }
7616 }
7617#endif
7618
Paul Bakker5121ce52009-01-03 21:22:43 +00007619 ssl->state++;
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007622
7623 return( 0 );
7624}
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7629 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007630 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007631 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7634#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007635 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007638 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007639#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007641}
7642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007646
7647 /*
7648 * Free our handshake params
7649 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007650 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007652 ssl->handshake = NULL;
7653
7654 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007655 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007656 */
7657 if( ssl->transform )
7658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 mbedtls_ssl_transform_free( ssl->transform );
7660 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007661 }
7662 ssl->transform = ssl->transform_negotiate;
7663 ssl->transform_negotiate = NULL;
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007666}
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_SSL_RENEGOTIATION)
7673 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676 ssl->renego_records_seen = 0;
7677 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007678#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007679
7680 /*
7681 * Free the previous session and switch in the current one
7682 */
Paul Bakker0a597072012-09-25 21:55:46 +00007683 if( ssl->session )
7684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007686 /* RFC 7366 3.1: keep the EtM state */
7687 ssl->session_negotiate->encrypt_then_mac =
7688 ssl->session->encrypt_then_mac;
7689#endif
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 mbedtls_ssl_session_free( ssl->session );
7692 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007693 }
7694 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007695 ssl->session_negotiate = NULL;
7696
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007697#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007698 /*
7699 * Add cache entry
7700 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007701 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007702 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007703 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007704 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007705 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007707 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007708#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007711 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007712 ssl->handshake->flight != NULL )
7713 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007714 /* Cancel handshake timer */
7715 ssl_set_timer( ssl, 0 );
7716
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007717 /* Keep last flight around in case we need to resend it:
7718 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007720 }
7721 else
7722#endif
7723 ssl_handshake_wrapup_free_hs_transform( ssl );
7724
Paul Bakker48916f92012-09-16 19:57:18 +00007725 ssl->state++;
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007728}
7729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007731{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007732 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007736 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007737
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007738 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7739 mbedtls_ssl_suite_get_mac(
7740 mbedtls_ssl_ciphersuite_from_id(
7741 mbedtls_ssl_session_get_ciphersuite(
7742 ssl->session_negotiate ) ) ),
7743 ssl, ssl->out_msg + 4,
7744 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007745
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007746 /*
7747 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7748 * may define some other value. Currently (early 2016), no defined
7749 * ciphersuite does this (and this is unlikely to change as activity has
7750 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7751 */
Hanno Becker2881d802019-05-22 14:44:53 +01007752 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007755 ssl->verify_data_len = hash_len;
7756 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007757#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007758
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7761 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007762
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007763#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007764 /*
7765 * In case of session resuming, invert the client and server
7766 * ChangeCipherSpec messages order.
7767 */
Paul Bakker0a597072012-09-25 21:55:46 +00007768 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007771 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7772 MBEDTLS_SSL_IS_CLIENT )
7773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007775 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007778 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7779 MBEDTLS_SSL_IS_SERVER )
7780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007782 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007784 }
7785 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007786#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007787 ssl->state++;
7788
Paul Bakker48916f92012-09-16 19:57:18 +00007789 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007790 * Switch to our negotiated transform and session parameters for outbound
7791 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007796 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007797 {
7798 unsigned char i;
7799
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007800 /* Remember current epoch settings for resending */
7801 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007802 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007803
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007804 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007805 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007806
7807 /* Increment epoch */
7808 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007809 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007810 break;
7811
7812 /* The loop goes to its end iff the counter is wrapping */
7813 if( i == 0 )
7814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7816 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007817 }
7818 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007819 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007821#if defined(MBEDTLS_SSL_PROTO_TLS)
7822 {
7823 memset( ssl->cur_out_ctr, 0, 8 );
7824 }
7825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007826
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007827 ssl->transform_out = ssl->transform_negotiate;
7828 ssl->session_out = ssl->session_negotiate;
7829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7831 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7836 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007837 }
7838 }
7839#endif
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007842 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007844#endif
7845
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007846 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 return( ret );
7850 }
7851
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007853 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007854 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7855 {
7856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7857 return( ret );
7858 }
7859#endif
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007862
7863 return( 0 );
7864}
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007867#define SSL_MAX_HASH_LEN 36
7868#else
7869#define SSL_MAX_HASH_LEN 12
7870#endif
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007873{
Paul Bakker23986e52011-04-24 08:57:21 +00007874 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007875 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007876 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007879
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007880 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7881 mbedtls_ssl_suite_get_mac(
7882 mbedtls_ssl_ciphersuite_from_id(
7883 mbedtls_ssl_session_get_ciphersuite(
7884 ssl->session_negotiate ) ) ),
7885 ssl, buf,
7886 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007887
Hanno Becker327c93b2018-08-15 13:56:18 +01007888 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 return( ret );
7892 }
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007897 mbedtls_ssl_pend_fatal_alert( ssl,
7898 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007900 }
7901
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007902 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007904 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007905 hash_len = 36;
7906 else
7907#endif
7908 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7911 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007914 mbedtls_ssl_pend_fatal_alert( ssl,
7915 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007917 }
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007920 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007923 mbedtls_ssl_pend_fatal_alert( ssl,
7924 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007926 }
7927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007929 ssl->verify_data_len = hash_len;
7930 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007931#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007932
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007933#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007934 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007937 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007941 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007944 }
7945 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007946#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007947 ssl->state++;
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007950 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007952#endif
7953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007955
7956 return( 0 );
7957}
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7964 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7965 mbedtls_md5_init( &handshake->fin_md5 );
7966 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007967 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7968 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7971#if defined(MBEDTLS_SHA256_C)
7972 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007973 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SHA512_C)
7976 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007977 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980
Hanno Becker7e5437a2017-04-28 17:15:26 +01007981#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7982 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7983 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7984#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_DHM_C)
7987 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#if defined(MBEDTLS_ECDH_C)
7990 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007992#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007993 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007994#if defined(MBEDTLS_SSL_CLI_C)
7995 handshake->ecjpake_cache = NULL;
7996 handshake->ecjpake_cache_len = 0;
7997#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007998#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007999
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008000#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008001 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008002#endif
8003
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008004#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8005 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8006#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008007
8008#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8009 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8010 mbedtls_pk_init( &handshake->peer_pubkey );
8011#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012}
8013
Hanno Becker611a83b2018-01-03 14:27:32 +00008014void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8019 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008020
Hanno Becker92231322018-01-03 15:32:51 +00008021#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 mbedtls_md_init( &transform->md_ctx_enc );
8023 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008024#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008025}
8026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008030}
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008033{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008034 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008035 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008037 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008039 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008040 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008041
8042 /*
8043 * Either the pointers are now NULL or cleared properly and can be freed.
8044 * Now allocate missing structures.
8045 */
8046 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008047 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008048 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008049 }
Paul Bakker48916f92012-09-16 19:57:18 +00008050
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008051 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008052 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008053 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008054 }
Paul Bakker48916f92012-09-16 19:57:18 +00008055
Paul Bakker82788fb2014-10-20 13:59:19 +02008056 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008057 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008058 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008059 }
Paul Bakker48916f92012-09-16 19:57:18 +00008060
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008061 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008062 if( ssl->handshake == NULL ||
8063 ssl->transform_negotiate == NULL ||
8064 ssl->session_negotiate == NULL )
8065 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 mbedtls_free( ssl->handshake );
8069 mbedtls_free( ssl->transform_negotiate );
8070 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008071
8072 ssl->handshake = NULL;
8073 ssl->transform_negotiate = NULL;
8074 ssl->session_negotiate = NULL;
8075
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008076 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008077 }
8078
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008079 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008081 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008082 ssl_handshake_params_init( ssl->handshake );
8083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008085 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008086 {
8087 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008088
Hanno Becker2d9623f2019-06-13 12:07:05 +01008089 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8091 else
8092 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8093 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008094#endif
8095
Paul Bakker48916f92012-09-16 19:57:18 +00008096 return( 0 );
8097}
8098
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008099#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008100/* Dummy cookie callbacks for defaults */
8101static int ssl_cookie_write_dummy( void *ctx,
8102 unsigned char **p, unsigned char *end,
8103 const unsigned char *cli_id, size_t cli_id_len )
8104{
8105 ((void) ctx);
8106 ((void) p);
8107 ((void) end);
8108 ((void) cli_id);
8109 ((void) cli_id_len);
8110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008112}
8113
8114static int ssl_cookie_check_dummy( void *ctx,
8115 const unsigned char *cookie, size_t cookie_len,
8116 const unsigned char *cli_id, size_t cli_id_len )
8117{
8118 ((void) ctx);
8119 ((void) cookie);
8120 ((void) cookie_len);
8121 ((void) cli_id);
8122 ((void) cli_id_len);
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008125}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008126#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008127
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128/* Once ssl->out_hdr as the address of the beginning of the
8129 * next outgoing record is set, deduce the other pointers.
8130 *
8131 * Note: For TLS, we save the implicit record sequence number
8132 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8133 * and the caller has to make sure there's space for this.
8134 */
8135
8136static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8137 mbedtls_ssl_transform *transform )
8138{
8139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008140 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008141 {
8142 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008143#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008144 ssl->out_cid = ssl->out_ctr + 8;
8145 ssl->out_len = ssl->out_cid;
8146 if( transform != NULL )
8147 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008148#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008149 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008150#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008151 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008152 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008153 MBEDTLS_SSL_TRANSPORT_ELSE
8154#endif /* MBEDTLS_SSL_PROTO_DTLS */
8155#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008156 {
8157 ssl->out_ctr = ssl->out_hdr - 8;
8158 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008160 ssl->out_cid = ssl->out_len;
8161#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162 ssl->out_iv = ssl->out_hdr + 5;
8163 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008164#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008165
8166 /* Adjust out_msg to make space for explicit IV, if used. */
8167 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008168 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008169 {
8170 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8171 }
8172 else
8173 ssl->out_msg = ssl->out_iv;
8174}
8175
8176/* Once ssl->in_hdr as the address of the beginning of the
8177 * next incoming record is set, deduce the other pointers.
8178 *
8179 * Note: For TLS, we save the implicit record sequence number
8180 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8181 * and the caller has to make sure there's space for this.
8182 */
8183
Hanno Beckerf5970a02019-05-08 09:38:41 +01008184static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008185{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008186 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008187 * of unprotected TLS/DTLS records, with ssl->in_msg
8188 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008189 *
8190 * When decrypting a protected record, ssl->in_msg
8191 * will be shifted to point to the beginning of the
8192 * record plaintext.
8193 */
8194
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008196 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008197 {
Hanno Becker70e79282019-05-03 14:34:53 +01008198 /* This sets the header pointers to match records
8199 * without CID. When we receive a record containing
8200 * a CID, the fields are shifted accordingly in
8201 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008202 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008203#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008204 ssl->in_cid = ssl->in_ctr + 8;
8205 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008206#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008207 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008209 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008211 MBEDTLS_SSL_TRANSPORT_ELSE
8212#endif /* MBEDTLS_SSL_PROTO_DTLS */
8213#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008214 {
8215 ssl->in_ctr = ssl->in_hdr - 8;
8216 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008217#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008218 ssl->in_cid = ssl->in_len;
8219#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008220 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008221 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008222#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008223}
8224
Paul Bakker5121ce52009-01-03 21:22:43 +00008225/*
8226 * Initialize an SSL context
8227 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008228void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8229{
8230 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8231}
8232
8233/*
8234 * Setup an SSL context
8235 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008236
8237static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8238{
8239 /* Set the incoming and outgoing record pointers. */
8240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008241 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008242 {
8243 ssl->out_hdr = ssl->out_buf;
8244 ssl->in_hdr = ssl->in_buf;
8245 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008246 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008247#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008248#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008249 {
8250 ssl->out_hdr = ssl->out_buf + 8;
8251 ssl->in_hdr = ssl->in_buf + 8;
8252 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008253#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008254
8255 /* Derive other internal pointers. */
8256 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008257 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008258}
8259
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008260int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008261 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008262{
Paul Bakker48916f92012-09-16 19:57:18 +00008263 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008264
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008265 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008266
Hanno Beckeref982d52019-07-23 15:56:18 +01008267#if defined(MBEDTLS_USE_TINYCRYPT)
8268 uECC_set_rng( &uecc_rng_wrapper );
8269#endif
8270
Paul Bakker62f2dee2012-09-28 07:31:51 +00008271 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008272 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008273 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008274
8275 /* Set to NULL in case of an error condition */
8276 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008277
Angus Grattond8213d02016-05-25 20:56:48 +10008278 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8279 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008280 {
Angus Grattond8213d02016-05-25 20:56:48 +10008281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008282 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008283 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008284 }
8285
8286 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8287 if( ssl->out_buf == NULL )
8288 {
8289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008290 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008291 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008292 }
8293
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008294 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008295
Paul Bakker48916f92012-09-16 19:57:18 +00008296 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008297 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008298
Hanno Beckerc8f52992019-07-25 11:15:08 +01008299 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008300
Paul Bakker5121ce52009-01-03 21:22:43 +00008301 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008302
8303error:
8304 mbedtls_free( ssl->in_buf );
8305 mbedtls_free( ssl->out_buf );
8306
8307 ssl->conf = NULL;
8308
8309 ssl->in_buf = NULL;
8310 ssl->out_buf = NULL;
8311
8312 ssl->in_hdr = NULL;
8313 ssl->in_ctr = NULL;
8314 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008315 ssl->in_msg = NULL;
8316
8317 ssl->out_hdr = NULL;
8318 ssl->out_ctr = NULL;
8319 ssl->out_len = NULL;
8320 ssl->out_iv = NULL;
8321 ssl->out_msg = NULL;
8322
k-stachowiak9f7798e2018-07-31 16:52:32 +02008323 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008324}
8325
8326/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008327 * Reset an initialized and used SSL context for re-use while retaining
8328 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008329 *
8330 * If partial is non-zero, keep data in the input buffer and client ID.
8331 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008332 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008333static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008334{
Paul Bakker48916f92012-09-16 19:57:18 +00008335 int ret;
8336
Hanno Becker7e772132018-08-10 12:38:21 +01008337#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8338 !defined(MBEDTLS_SSL_SRV_C)
8339 ((void) partial);
8340#endif
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008343
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008344 /* Cancel any possibly running timer */
8345 ssl_set_timer( ssl, 0 );
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_RENEGOTIATION)
8348 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008349 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008350
8351 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8353 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008354#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008356
Paul Bakker7eb013f2011-10-06 12:37:39 +00008357 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008358 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008359
8360 ssl->in_msgtype = 0;
8361 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008363 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008364 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008367 ssl_dtls_replay_reset( ssl );
8368#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008369
8370 ssl->in_hslen = 0;
8371 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008372
8373 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008374
8375 ssl->out_msgtype = 0;
8376 ssl->out_msglen = 0;
8377 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8379 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008380 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008381#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008382
Hanno Becker19859472018-08-06 09:40:20 +01008383 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8384
Paul Bakker48916f92012-09-16 19:57:18 +00008385 ssl->transform_in = NULL;
8386 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008387
Hanno Becker78640902018-08-13 16:35:15 +01008388 ssl->session_in = NULL;
8389 ssl->session_out = NULL;
8390
Angus Grattond8213d02016-05-25 20:56:48 +10008391 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008392
8393#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008394 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008395#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8396 {
8397 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008398 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008399 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8402 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8405 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8408 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008409 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008410 }
8411#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008412
Paul Bakker48916f92012-09-16 19:57:18 +00008413 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415 mbedtls_ssl_transform_free( ssl->transform );
8416 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008417 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008418 }
Paul Bakker48916f92012-09-16 19:57:18 +00008419
Paul Bakkerc0463502013-02-14 11:19:38 +01008420 if( ssl->session )
8421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422 mbedtls_ssl_session_free( ssl->session );
8423 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008424 ssl->session = NULL;
8425 }
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008428 ssl->alpn_chosen = NULL;
8429#endif
8430
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008431#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008432#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008433 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008434#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008435 {
8436 mbedtls_free( ssl->cli_id );
8437 ssl->cli_id = NULL;
8438 ssl->cli_id_len = 0;
8439 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008440#endif
8441
Paul Bakker48916f92012-09-16 19:57:18 +00008442 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8443 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008444
8445 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008446}
8447
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008448/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008449 * Reset an initialized and used SSL context for re-use while retaining
8450 * all application-set variables, function pointers and data.
8451 */
8452int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8453{
8454 return( ssl_session_reset_int( ssl, 0 ) );
8455}
8456
8457/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008458 * SSL set accessors
8459 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008460#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008463 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008464}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008465#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008466
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008467void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008468{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008469 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008470}
8471
Hanno Becker7f376f42019-06-12 16:20:48 +01008472#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8473 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008474void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008475{
Hanno Becker7f376f42019-06-12 16:20:48 +01008476 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008477}
Hanno Becker7f376f42019-06-12 16:20:48 +01008478#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008479
Hanno Beckerde671542019-06-12 16:30:46 +01008480#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8481 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8482void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8483 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008484{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008485 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008486}
Hanno Beckerde671542019-06-12 16:30:46 +01008487#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008490
Hanno Becker1841b0a2018-08-24 11:13:57 +01008491void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8492 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008493{
8494 ssl->disable_datagram_packing = !allow_packing;
8495}
8496
Hanno Becker1f835fa2019-06-13 10:14:59 +01008497#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8498 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008499void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8500 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->hs_timeout_min = min;
8503 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008504}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008505#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8506 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8507void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8508 uint32_t min, uint32_t max )
8509{
8510 ((void) conf);
8511 ((void) min);
8512 ((void) max);
8513}
8514#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8515 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8516
8517#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008518
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008519void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008520{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008521#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8522 conf->authmode = authmode;
8523#else
8524 ((void) conf);
8525 ((void) authmode);
8526#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008527}
8528
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008529#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8530 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008531void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008532 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008533 void *p_vrfy )
8534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008535 conf->f_vrfy = f_vrfy;
8536 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008537}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008538#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008539
Hanno Beckerece325c2019-06-13 15:39:27 +01008540#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008541void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008542 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008543 void *p_rng )
8544{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008545 conf->f_rng = f_rng;
8546 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008547}
Hanno Beckerece325c2019-06-13 15:39:27 +01008548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008549
Hanno Becker14a4a442019-07-02 17:00:34 +01008550#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008551void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008552 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008553 void *p_dbg )
8554{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008555 conf->f_dbg = f_dbg;
8556 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008557}
Hanno Becker14a4a442019-07-02 17:00:34 +01008558#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008559
Hanno Beckera58a8962019-06-13 16:11:15 +01008560#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8561 !defined(MBEDTLS_SSL_CONF_SEND) && \
8562 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008564 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008565 mbedtls_ssl_send_t *f_send,
8566 mbedtls_ssl_recv_t *f_recv,
8567 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008568{
Hanno Beckera58a8962019-06-13 16:11:15 +01008569 ssl->p_bio = p_bio;
8570 ssl->f_send = f_send;
8571 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008572 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008573}
Hanno Beckera58a8962019-06-13 16:11:15 +01008574#else
8575void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8576 void *p_bio )
8577{
8578 ssl->p_bio = p_bio;
8579}
8580#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008581
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008582#if defined(MBEDTLS_SSL_PROTO_DTLS)
8583void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8584{
8585 ssl->mtu = mtu;
8586}
8587#endif
8588
Hanno Becker1f835fa2019-06-13 10:14:59 +01008589#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008591{
8592 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008593}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008594#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008595
Hanno Becker0ae6b242019-06-13 16:45:36 +01008596#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8597 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008598void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8599 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008600 mbedtls_ssl_set_timer_t *f_set_timer,
8601 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008602{
8603 ssl->p_timer = p_timer;
8604 ssl->f_set_timer = f_set_timer;
8605 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008606 /* Make sure we start with no timer running */
8607 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008608}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008609#else
8610void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8611 void *p_timer )
8612{
8613 ssl->p_timer = p_timer;
8614 /* Make sure we start with no timer running */
8615 ssl_set_timer( ssl, 0 );
8616}
8617#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008618
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008619#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008621 void *p_cache,
8622 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8623 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008624{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008625 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008627 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008628}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008629#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008630
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008631#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008633{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008634 int ret;
8635
8636 if( ssl == NULL ||
8637 session == NULL ||
8638 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008639 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008642 }
8643
Hanno Becker58fccf22019-02-06 14:30:46 +00008644 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8645 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008646 return( ret );
8647
Paul Bakker0a597072012-09-25 21:55:46 +00008648 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008649
8650 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008651}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008652#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008653
Hanno Becker73f4cb12019-06-27 13:51:07 +01008654#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008655void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008656 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008658 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8659 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8660 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8661 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008662}
8663
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008664void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008665 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008666 int major, int minor )
8667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008669 return;
8670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008672 return;
8673
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008674 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008675}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008676#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008679void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008680 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008681{
8682 conf->cert_profile = profile;
8683}
8684
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008685/* Append a new keycert entry to a (possibly empty) list */
8686static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8687 mbedtls_x509_crt *cert,
8688 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008689{
niisato8ee24222018-06-25 19:05:48 +09008690 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691
niisato8ee24222018-06-25 19:05:48 +09008692 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8693 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008694 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008695
niisato8ee24222018-06-25 19:05:48 +09008696 new_cert->cert = cert;
8697 new_cert->key = key;
8698 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008699
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008700 /* Update head is the list was null, else add to the end */
8701 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008702 {
niisato8ee24222018-06-25 19:05:48 +09008703 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008704 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008705 else
8706 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008707 mbedtls_ssl_key_cert *cur = *head;
8708 while( cur->next != NULL )
8709 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008710 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008711 }
8712
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008713 return( 0 );
8714}
8715
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008716int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008717 mbedtls_x509_crt *own_cert,
8718 mbedtls_pk_context *pk_key )
8719{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008720 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008721}
8722
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008723void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008724 mbedtls_x509_crt *ca_chain,
8725 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008726{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008727 conf->ca_chain = ca_chain;
8728 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008731
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008732#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8733int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8734 mbedtls_x509_crt *own_cert,
8735 mbedtls_pk_context *pk_key )
8736{
8737 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8738 own_cert, pk_key ) );
8739}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008740
8741void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8742 mbedtls_x509_crt *ca_chain,
8743 mbedtls_x509_crl *ca_crl )
8744{
8745 ssl->handshake->sni_ca_chain = ca_chain;
8746 ssl->handshake->sni_ca_crl = ca_crl;
8747}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008748
8749void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8750 int authmode )
8751{
8752 ssl->handshake->sni_authmode = authmode;
8753}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008754#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8755
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008756#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008757/*
8758 * Set EC J-PAKE password for current handshake
8759 */
8760int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8761 const unsigned char *pw,
8762 size_t pw_len )
8763{
8764 mbedtls_ecjpake_role role;
8765
Janos Follath8eb64132016-06-03 15:40:57 +01008766 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8768
Hanno Becker2d9623f2019-06-13 12:07:05 +01008769 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008770 role = MBEDTLS_ECJPAKE_SERVER;
8771 else
8772 role = MBEDTLS_ECJPAKE_CLIENT;
8773
8774 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8775 role,
8776 MBEDTLS_MD_SHA256,
8777 MBEDTLS_ECP_DP_SECP256R1,
8778 pw, pw_len ) );
8779}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008780#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008783int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008784 const unsigned char *psk, size_t psk_len,
8785 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008786{
Paul Bakker6db455e2013-09-18 17:29:31 +02008787 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008792
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008793 /* Identity len will be encoded on two bytes */
8794 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008795 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008796 {
8797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8798 }
8799
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008800 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008801 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008802 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008803
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008804 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008805 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008806 conf->psk_len = 0;
8807 }
8808 if( conf->psk_identity != NULL )
8809 {
8810 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008811 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008812 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008813 }
8814
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008815 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8816 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008817 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008818 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008819 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008820 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008821 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008823 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008824
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008825 conf->psk_len = psk_len;
8826 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008827
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008828 memcpy( conf->psk, psk, conf->psk_len );
8829 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008830
8831 return( 0 );
8832}
8833
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008834int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8835 const unsigned char *psk, size_t psk_len )
8836{
8837 if( psk == NULL || ssl->handshake == NULL )
8838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8839
8840 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8842
8843 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008844 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008845 mbedtls_platform_zeroize( ssl->handshake->psk,
8846 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008847 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008848 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008849 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008850
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008851 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008852 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008853
8854 ssl->handshake->psk_len = psk_len;
8855 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8856
8857 return( 0 );
8858}
8859
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008860void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008862 size_t),
8863 void *p_psk )
8864{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008865 conf->f_psk = f_psk;
8866 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008869
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008871
8872#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008874{
8875 int ret;
8876
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008877 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8878 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8879 {
8880 mbedtls_mpi_free( &conf->dhm_P );
8881 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008882 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008884
8885 return( 0 );
8886}
Hanno Becker470a8c42017-10-04 15:28:46 +01008887#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008888
Hanno Beckera90658f2017-10-04 15:29:08 +01008889int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8890 const unsigned char *dhm_P, size_t P_len,
8891 const unsigned char *dhm_G, size_t G_len )
8892{
8893 int ret;
8894
8895 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8896 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8897 {
8898 mbedtls_mpi_free( &conf->dhm_P );
8899 mbedtls_mpi_free( &conf->dhm_G );
8900 return( ret );
8901 }
8902
8903 return( 0 );
8904}
Paul Bakker5121ce52009-01-03 21:22:43 +00008905
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008906int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008907{
8908 int ret;
8909
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008910 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8911 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8912 {
8913 mbedtls_mpi_free( &conf->dhm_P );
8914 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008915 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008916 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008917
8918 return( 0 );
8919}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008920#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008921
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008922#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8923/*
8924 * Set the minimum length for Diffie-Hellman parameters
8925 */
8926void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8927 unsigned int bitlen )
8928{
8929 conf->dhm_min_bitlen = bitlen;
8930}
8931#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8932
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008933#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008934/*
8935 * Set allowed/preferred hashes for handshake signatures
8936 */
8937void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8938 const int *hashes )
8939{
Hanno Becker56595f42019-06-19 16:31:38 +01008940#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008941 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008942#else
8943 ((void) conf);
8944 ((void) hashes);
8945#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008946}
Hanno Becker947194e2017-04-07 13:25:49 +01008947#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008948
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008949#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008950#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951/*
8952 * Set the allowed elliptic curves
8953 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008954void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008955 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008957 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008958}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008959#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008960#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008961
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008962#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008964{
Hanno Becker947194e2017-04-07 13:25:49 +01008965 /* Initialize to suppress unnecessary compiler warning */
8966 size_t hostname_len = 0;
8967
8968 /* Check if new hostname is valid before
8969 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008970 if( hostname != NULL )
8971 {
8972 hostname_len = strlen( hostname );
8973
8974 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8976 }
8977
8978 /* Now it's clear that we will overwrite the old hostname,
8979 * so we can free it safely */
8980
8981 if( ssl->hostname != NULL )
8982 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008983 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008984 mbedtls_free( ssl->hostname );
8985 }
8986
8987 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008988
Paul Bakker5121ce52009-01-03 21:22:43 +00008989 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008990 {
8991 ssl->hostname = NULL;
8992 }
8993 else
8994 {
8995 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008996 if( ssl->hostname == NULL )
8997 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008998
Hanno Becker947194e2017-04-07 13:25:49 +01008999 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009000
Hanno Becker947194e2017-04-07 13:25:49 +01009001 ssl->hostname[hostname_len] = '\0';
9002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009003
9004 return( 0 );
9005}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009006#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009007
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009008#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009009void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009011 const unsigned char *, size_t),
9012 void *p_sni )
9013{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009014 conf->f_sni = f_sni;
9015 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009020int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009021{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009022 size_t cur_len, tot_len;
9023 const char **p;
9024
9025 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009026 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9027 * MUST NOT be truncated."
9028 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009029 */
9030 tot_len = 0;
9031 for( p = protos; *p != NULL; p++ )
9032 {
9033 cur_len = strlen( *p );
9034 tot_len += cur_len;
9035
9036 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009038 }
9039
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009040 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009041
9042 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009043}
9044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009046{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009047 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009050
Hanno Becker33b9b252019-07-05 11:23:25 +01009051#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9052 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9053void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9054 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->max_major_ver = major;
9057 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009058}
Hanno Becker33b9b252019-07-05 11:23:25 +01009059#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9060 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009061
Hanno Becker33b9b252019-07-05 11:23:25 +01009062#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9063 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9064void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9065 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009066{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009067 conf->min_major_ver = major;
9068 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009069}
Hanno Becker33b9b252019-07-05 11:23:25 +01009070#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9071 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009074void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009075{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009076 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009077}
9078#endif
9079
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009080#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009081void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9082 char cert_req_ca_list )
9083{
9084 conf->cert_req_ca_list = cert_req_ca_list;
9085}
9086#endif
9087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009089void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009091 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009092}
9093#endif
9094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009096#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009097void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009099 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009100}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009101#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9102#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009103void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009104 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009105{
9106 conf->enforce_extended_master_secret = ems_enf;
9107}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009108#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009109#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009110
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009111#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009114 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009115}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009116#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009122 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009125 }
9126
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009127 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009128
9129 return( 0 );
9130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009134void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009136 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009141void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009142{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009143 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009144}
9145#endif
9146
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009147#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009148void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009149{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009150 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009151}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009152#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009155void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009156{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009157 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009158}
9159
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009160void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009162 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009163}
9164
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009165void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009166 const unsigned char period[8] )
9167{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009168 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009173#if defined(MBEDTLS_SSL_CLI_C)
9174void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009175{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009176 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009177}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009178#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009179
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009180#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009181void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9182 mbedtls_ssl_ticket_write_t *f_ticket_write,
9183 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9184 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009185{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009186 conf->f_ticket_write = f_ticket_write;
9187 conf->f_ticket_parse = f_ticket_parse;
9188 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009189}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009191#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009192
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009193#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9194void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9195 mbedtls_ssl_export_keys_t *f_export_keys,
9196 void *p_export_keys )
9197{
9198 conf->f_export_keys = f_export_keys;
9199 conf->p_export_keys = p_export_keys;
9200}
9201#endif
9202
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009203#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204void mbedtls_ssl_conf_async_private_cb(
9205 mbedtls_ssl_config *conf,
9206 mbedtls_ssl_async_sign_t *f_async_sign,
9207 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9208 mbedtls_ssl_async_resume_t *f_async_resume,
9209 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009210 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009211{
9212 conf->f_async_sign_start = f_async_sign;
9213 conf->f_async_decrypt_start = f_async_decrypt;
9214 conf->f_async_resume = f_async_resume;
9215 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009216 conf->p_async_config_data = async_config_data;
9217}
9218
Gilles Peskine8f97af72018-04-26 11:46:10 +02009219void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9220{
9221 return( conf->p_async_config_data );
9222}
9223
Gilles Peskine1febfef2018-04-30 11:54:39 +02009224void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009225{
9226 if( ssl->handshake == NULL )
9227 return( NULL );
9228 else
9229 return( ssl->handshake->user_async_ctx );
9230}
9231
Gilles Peskine1febfef2018-04-30 11:54:39 +02009232void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009233 void *ctx )
9234{
9235 if( ssl->handshake != NULL )
9236 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009237}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009238#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009239
Paul Bakker5121ce52009-01-03 21:22:43 +00009240/*
9241 * SSL get accessors
9242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009244{
9245 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9246}
9247
Hanno Becker8b170a02017-10-10 11:51:19 +01009248int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9249{
9250 /*
9251 * Case A: We're currently holding back
9252 * a message for further processing.
9253 */
9254
9255 if( ssl->keep_current_message == 1 )
9256 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009258 return( 1 );
9259 }
9260
9261 /*
9262 * Case B: Further records are pending in the current datagram.
9263 */
9264
9265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009266 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009267 ssl->in_left > ssl->next_record_offset )
9268 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009270 return( 1 );
9271 }
9272#endif /* MBEDTLS_SSL_PROTO_DTLS */
9273
9274 /*
9275 * Case C: A handshake message is being processed.
9276 */
9277
Hanno Becker8b170a02017-10-10 11:51:19 +01009278 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9279 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009281 return( 1 );
9282 }
9283
9284 /*
9285 * Case D: An application data message is being processed
9286 */
9287 if( ssl->in_offt != NULL )
9288 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009290 return( 1 );
9291 }
9292
9293 /*
9294 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009295 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009296 * we implement support for multiple alerts in single records.
9297 */
9298
9299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9300 return( 0 );
9301}
9302
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009303uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009304{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009305 if( ssl->session != NULL )
9306 return( ssl->session->verify_result );
9307
9308 if( ssl->session_negotiate != NULL )
9309 return( ssl->session_negotiate->verify_result );
9310
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009311 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009312}
9313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009315{
Hanno Beckere02758c2019-06-26 15:31:31 +01009316 int suite;
9317
Paul Bakker926c8e42013-03-06 10:23:34 +01009318 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009319 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009320
Hanno Beckere02758c2019-06-26 15:31:31 +01009321 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9322 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009323}
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009328 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009329 {
Hanno Becker2881d802019-05-22 14:44:53 +01009330 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009333 return( "DTLSv1.0" );
9334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009336 return( "DTLSv1.2" );
9337
9338 default:
9339 return( "unknown (DTLS)" );
9340 }
9341 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009342 MBEDTLS_SSL_TRANSPORT_ELSE
9343#endif /* MBEDTLS_SSL_PROTO_DTLS */
9344#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009345 {
Hanno Becker2881d802019-05-22 14:44:53 +01009346 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009347 {
9348 case MBEDTLS_SSL_MINOR_VERSION_0:
9349 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009350
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009351 case MBEDTLS_SSL_MINOR_VERSION_1:
9352 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009353
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009354 case MBEDTLS_SSL_MINOR_VERSION_2:
9355 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009356
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009357 case MBEDTLS_SSL_MINOR_VERSION_3:
9358 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009359
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009360 default:
9361 return( "unknown" );
9362 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009363 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009364#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009365}
9366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368{
Hanno Becker3136ede2018-08-17 15:28:19 +01009369 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009371
Hanno Becker43395762019-05-03 14:46:38 +01009372 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9373
Hanno Becker78640902018-08-13 16:35:15 +01009374 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009375 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_ZLIB_SUPPORT)
9378 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9379 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009380#endif
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009383 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009384#if defined(MBEDTLS_GCM_C) || \
9385 defined(MBEDTLS_CCM_C) || \
9386 defined(MBEDTLS_CHACHAPOLY_C)
9387#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009389#endif
9390#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009392#endif
9393#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009394 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009395#endif
9396 transform_expansion =
9397 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009398 break;
9399
Hanno Beckera9d5c452019-07-25 16:47:12 +01009400#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9401 MBEDTLS_CHACHAPOLY_C */
9402
9403#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9404 case MBEDTLS_MODE_STREAM:
9405 transform_expansion = transform->maclen;
9406 break;
9407#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9408
9409#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009411 {
9412 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009413
9414 block_size = mbedtls_cipher_get_block_size(
9415 &transform->cipher_ctx_enc );
9416
Hanno Becker3136ede2018-08-17 15:28:19 +01009417 /* Expansion due to the addition of the MAC. */
9418 transform_expansion += transform->maclen;
9419
9420 /* Expansion due to the addition of CBC padding;
9421 * Theoretically up to 256 bytes, but we never use
9422 * more than the block size of the underlying cipher. */
9423 transform_expansion += block_size;
9424
9425 /* For TLS 1.1 or higher, an explicit IV is added
9426 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009427#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009428 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009429 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009430#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009431
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009432 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009433 }
9434#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009435
9436 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009439 }
9440
Hanno Beckera5a2b082019-05-15 14:03:01 +01009441#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009442 if( transform->out_cid_len != 0 )
9443 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009444#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009445
Hanno Becker43395762019-05-03 14:46:38 +01009446 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009447}
9448
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009449#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9450size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9451{
9452 size_t max_len;
9453
9454 /*
9455 * Assume mfl_code is correct since it was checked when set
9456 */
Angus Grattond8213d02016-05-25 20:56:48 +10009457 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009458
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009459 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009460 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009461 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009462 {
Angus Grattond8213d02016-05-25 20:56:48 +10009463 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009464 }
9465
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009466 /* During a handshake, use the value being negotiated */
9467 if( ssl->session_negotiate != NULL &&
9468 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9469 {
9470 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9471 }
9472
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009473 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009474}
9475#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9476
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009477#if defined(MBEDTLS_SSL_PROTO_DTLS)
9478static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9479{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009480 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009481 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009482 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9483 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9484 return ( 0 );
9485
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009486 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9487 return( ssl->mtu );
9488
9489 if( ssl->mtu == 0 )
9490 return( ssl->handshake->mtu );
9491
9492 return( ssl->mtu < ssl->handshake->mtu ?
9493 ssl->mtu : ssl->handshake->mtu );
9494}
9495#endif /* MBEDTLS_SSL_PROTO_DTLS */
9496
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009497int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9498{
9499 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9500
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009501#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9502 !defined(MBEDTLS_SSL_PROTO_DTLS)
9503 (void) ssl;
9504#endif
9505
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009506#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9507 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9508
9509 if( max_len > mfl )
9510 max_len = mfl;
9511#endif
9512
9513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009514 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009515 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009516 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009517 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9518 const size_t overhead = (size_t) ret;
9519
9520 if( ret < 0 )
9521 return( ret );
9522
9523 if( mtu <= overhead )
9524 {
9525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9526 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9527 }
9528
9529 if( max_len > mtu - overhead )
9530 max_len = mtu - overhead;
9531 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009532#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009533
Hanno Becker0defedb2018-08-10 12:35:02 +01009534#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9535 !defined(MBEDTLS_SSL_PROTO_DTLS)
9536 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009537#endif
9538
9539 return( (int) max_len );
9540}
9541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#if defined(MBEDTLS_X509_CRT_PARSE_C)
9543const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009544{
9545 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009546 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009547
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009548#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009549 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009550#else
9551 return( NULL );
9552#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009557int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9558 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009559{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009560 if( ssl == NULL ||
9561 dst == NULL ||
9562 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009563 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009566 }
9567
Hanno Becker58fccf22019-02-06 14:30:46 +00009568 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009571
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009572const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9573{
9574 if( ssl == NULL )
9575 return( NULL );
9576
9577 return( ssl->session );
9578}
9579
Paul Bakker5121ce52009-01-03 21:22:43 +00009580/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009581 * Define ticket header determining Mbed TLS version
9582 * and structure of the ticket.
9583 */
9584
Hanno Becker41527622019-05-16 12:50:45 +01009585/*
Hanno Becker26829e92019-05-28 14:30:45 +01009586 * Define bitflag determining compile-time settings influencing
9587 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009588 */
9589
Hanno Becker26829e92019-05-28 14:30:45 +01009590#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009591#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009592#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009593#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009594#endif /* MBEDTLS_HAVE_TIME */
9595
9596#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009597#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009598#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009599#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009600#endif /* MBEDTLS_X509_CRT_PARSE_C */
9601
9602#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009603#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009604#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009605#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009606#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9607
9608#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009609#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009610#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009611#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009612#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9613
9614#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009615#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009616#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009617#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009618#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9619
9620#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009621#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009622#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009623#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009624#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9625
Hanno Becker41527622019-05-16 12:50:45 +01009626#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9627#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9628#else
9629#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9630#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9631
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009632#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9633#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9634#else
9635#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9636#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9637
Hanno Becker88440552019-07-03 14:16:13 +01009638#if defined(MBEDTLS_ZLIB_SUPPORT)
9639#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9640#else
9641#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9642#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9643
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009644#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9645#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9646#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9647#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9648#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9649#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9650#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009651#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009652#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009653
Hanno Becker26829e92019-05-28 14:30:45 +01009654#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009655 ( (uint16_t) ( \
9656 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9657 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9658 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9659 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9660 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9661 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009662 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009663 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009664 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009665
Hanno Becker557fe9f2019-05-16 12:41:07 +01009666static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009667 MBEDTLS_VERSION_MAJOR,
9668 MBEDTLS_VERSION_MINOR,
9669 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009670 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9671 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009672};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009673
9674/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009675 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009676 * (in the presentation language of TLS, RFC 8446 section 3)
9677 *
Hanno Becker26829e92019-05-28 14:30:45 +01009678 * opaque mbedtls_version[3]; // major, minor, patch
9679 * opaque session_format[2]; // version-specific 16-bit field determining
9680 * // the format of the remaining
9681 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009682 *
9683 * Note: When updating the format, remember to keep
9684 * these version+format bytes.
9685 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009686 * // In this version, `session_format` determines
9687 * // the setting of those compile-time
9688 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009689 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009690 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009691 * uint8 ciphersuite[2]; // defined by the standard
9692 * uint8 compression; // 0 or 1
9693 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009694 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009695 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009696 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009697 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9698 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9699 * case disabled: uint8_t peer_cert_digest_type;
9700 * opaque peer_cert_digest<0..2^8-1>;
9701 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009702 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009703 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009704 * uint8 mfl_code; // up to 255 according to standard
9705 * uint8 trunc_hmac; // 0 or 1
9706 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009707 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009708 * The order is the same as in the definition of the structure, except
9709 * verify_result is put before peer_cert so that all mandatory fields come
9710 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009711 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009712static int ssl_session_save( const mbedtls_ssl_session *session,
9713 unsigned char omit_header,
9714 unsigned char *buf,
9715 size_t buf_len,
9716 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009717{
9718 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009719 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009720#if defined(MBEDTLS_HAVE_TIME)
9721 uint64_t start;
9722#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009723#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009724#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009725 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009726#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009727#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009728
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009729 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009730 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009731 /*
9732 * Add version identifier
9733 */
9734
9735 used += sizeof( ssl_serialized_session_header );
9736
9737 if( used <= buf_len )
9738 {
9739 memcpy( p, ssl_serialized_session_header,
9740 sizeof( ssl_serialized_session_header ) );
9741 p += sizeof( ssl_serialized_session_header );
9742 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009743 }
9744
9745 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009746 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009747 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009748#if defined(MBEDTLS_HAVE_TIME)
9749 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009750
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009751 if( used <= buf_len )
9752 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009753 start = (uint64_t) session->start;
9754
9755 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9756 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9757 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9758 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9759 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9760 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9761 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9762 *p++ = (unsigned char)( ( start ) & 0xFF );
9763 }
9764#endif /* MBEDTLS_HAVE_TIME */
9765
9766 /*
9767 * Basic mandatory fields
9768 */
Hanno Becker88440552019-07-03 14:16:13 +01009769 {
9770 size_t const ciphersuite_len = 2;
9771#if defined(MBEDTLS_ZLIB_SUPPORT)
9772 size_t const compression_len = 1;
9773#else
9774 size_t const compression_len = 0;
9775#endif
9776 size_t const id_len_len = 1;
9777 size_t const id_len = 32;
9778 size_t const master_len = 48;
9779 size_t const verif_result_len = 4;
9780
9781 size_t const basic_len =
9782 ciphersuite_len +
9783 compression_len +
9784 id_len_len +
9785 id_len +
9786 master_len +
9787 verif_result_len;
9788
9789 used += basic_len;
9790 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009791
9792 if( used <= buf_len )
9793 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009794 const int ciphersuite =
9795 mbedtls_ssl_session_get_ciphersuite( session );
9796 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9797 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009798
Hanno Becker88440552019-07-03 14:16:13 +01009799#if defined(MBEDTLS_ZLIB_SUPPORT)
9800 *p++ = (unsigned char)(
9801 mbedtls_ssl_session_get_compression( session ) );
9802#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009803
9804 *p++ = (unsigned char)( session->id_len & 0xFF );
9805 memcpy( p, session->id, 32 );
9806 p += 32;
9807
9808 memcpy( p, session->master, 48 );
9809 p += 48;
9810
9811 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9812 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9813 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9814 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009815 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009816
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009817 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009818 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009819 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009820#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009821#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009822 if( session->peer_cert == NULL )
9823 cert_len = 0;
9824 else
9825 cert_len = session->peer_cert->raw.len;
9826
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009827 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009828
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009829 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009830 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009831 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9832 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9833 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9834
9835 if( session->peer_cert != NULL )
9836 {
9837 memcpy( p, session->peer_cert->raw.p, cert_len );
9838 p += cert_len;
9839 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009840 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009841
Hanno Becker5882dd02019-06-06 16:25:57 +01009842#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009843 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009844 if( session->peer_cert_digest != NULL )
9845 {
9846 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9847 if( used <= buf_len )
9848 {
9849 *p++ = (unsigned char) session->peer_cert_digest_type;
9850 *p++ = (unsigned char) session->peer_cert_digest_len;
9851 memcpy( p, session->peer_cert_digest,
9852 session->peer_cert_digest_len );
9853 p += session->peer_cert_digest_len;
9854 }
9855 }
9856 else
9857 {
9858 used += 2;
9859 if( used <= buf_len )
9860 {
9861 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9862 *p++ = 0;
9863 }
9864 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009865#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009866#endif /* MBEDTLS_X509_CRT_PARSE_C */
9867
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009868 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009869 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009870 */
9871#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009872 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009873
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009874 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009875 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009876 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9877 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9878 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9879
9880 if( session->ticket != NULL )
9881 {
9882 memcpy( p, session->ticket, session->ticket_len );
9883 p += session->ticket_len;
9884 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009885
9886 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9887 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9888 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9889 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009890 }
9891#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9892
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009893 /*
9894 * Misc extension-related info
9895 */
9896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9897 used += 1;
9898
9899 if( used <= buf_len )
9900 *p++ = session->mfl_code;
9901#endif
9902
9903#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9904 used += 1;
9905
9906 if( used <= buf_len )
9907 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9908#endif
9909
9910#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9911 used += 1;
9912
9913 if( used <= buf_len )
9914 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9915#endif
9916
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009917 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009918 *olen = used;
9919
9920 if( used > buf_len )
9921 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009922
9923 return( 0 );
9924}
9925
9926/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009927 * Public wrapper for ssl_session_save()
9928 */
9929int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9930 unsigned char *buf,
9931 size_t buf_len,
9932 size_t *olen )
9933{
9934 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9935}
9936
9937/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009938 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009939 *
9940 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009941 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009942 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009943static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009944 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009945 const unsigned char *buf,
9946 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009947{
9948 const unsigned char *p = buf;
9949 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009950 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009951#if defined(MBEDTLS_HAVE_TIME)
9952 uint64_t start;
9953#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009954#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009955#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009956 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009957#endif
9958#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009959
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009960 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009961 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009962 /*
9963 * Check version identifier
9964 */
9965
9966 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9968
9969 if( memcmp( p, ssl_serialized_session_header,
9970 sizeof( ssl_serialized_session_header ) ) != 0 )
9971 {
9972 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9973 }
9974 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009975 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009976
9977 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009978 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009979 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009980#if defined(MBEDTLS_HAVE_TIME)
9981 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9983
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009984 start = ( (uint64_t) p[0] << 56 ) |
9985 ( (uint64_t) p[1] << 48 ) |
9986 ( (uint64_t) p[2] << 40 ) |
9987 ( (uint64_t) p[3] << 32 ) |
9988 ( (uint64_t) p[4] << 24 ) |
9989 ( (uint64_t) p[5] << 16 ) |
9990 ( (uint64_t) p[6] << 8 ) |
9991 ( (uint64_t) p[7] );
9992 p += 8;
9993
9994 session->start = (time_t) start;
9995#endif /* MBEDTLS_HAVE_TIME */
9996
9997 /*
9998 * Basic mandatory fields
9999 */
Hanno Becker88440552019-07-03 14:16:13 +010010000 {
10001 size_t const ciphersuite_len = 2;
10002#if defined(MBEDTLS_ZLIB_SUPPORT)
10003 size_t const compression_len = 1;
10004#else
10005 size_t const compression_len = 0;
10006#endif
10007 size_t const id_len_len = 1;
10008 size_t const id_len = 32;
10009 size_t const master_len = 48;
10010 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010011
Hanno Becker88440552019-07-03 14:16:13 +010010012 size_t const basic_len =
10013 ciphersuite_len +
10014 compression_len +
10015 id_len_len +
10016 id_len +
10017 master_len +
10018 verif_result_len;
10019
10020 if( basic_len > (size_t)( end - p ) )
10021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10022 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010023
Hanno Beckere02758c2019-06-26 15:31:31 +010010024 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010025 p += 2;
10026
Hanno Becker73f4cb12019-06-27 13:51:07 +010010027#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010028 session->ciphersuite = ciphersuite;
10029#else
10030 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010031 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010032 {
10033 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10034 }
10035#endif
10036
Hanno Becker88440552019-07-03 14:16:13 +010010037#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010038 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010039#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010040
10041 session->id_len = *p++;
10042 memcpy( session->id, p, 32 );
10043 p += 32;
10044
10045 memcpy( session->master, p, 48 );
10046 p += 48;
10047
10048 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10049 ( (uint32_t) p[1] << 16 ) |
10050 ( (uint32_t) p[2] << 8 ) |
10051 ( (uint32_t) p[3] );
10052 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010053
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010054 /* Immediately clear invalid pointer values that have been read, in case
10055 * we exit early before we replaced them with valid ones. */
10056#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010057#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010058 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010059#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010060 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010061#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010062#endif
10063#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10064 session->ticket = NULL;
10065#endif
10066
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010067 /*
10068 * Peer certificate
10069 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010070#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010071#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010072 if( 3 > (size_t)( end - p ) )
10073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10074
10075 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10076 p += 3;
10077
10078 if( cert_len == 0 )
10079 {
10080 session->peer_cert = NULL;
10081 }
10082 else
10083 {
10084 int ret;
10085
10086 if( cert_len > (size_t)( end - p ) )
10087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10088
10089 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10090
10091 if( session->peer_cert == NULL )
10092 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10093
10094 mbedtls_x509_crt_init( session->peer_cert );
10095
10096 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10097 p, cert_len ) ) != 0 )
10098 {
10099 mbedtls_x509_crt_free( session->peer_cert );
10100 mbedtls_free( session->peer_cert );
10101 session->peer_cert = NULL;
10102 return( ret );
10103 }
10104
10105 p += cert_len;
10106 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010107#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010108 /* Deserialize CRT digest from the end of the ticket. */
10109 if( 2 > (size_t)( end - p ) )
10110 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10111
10112 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10113 session->peer_cert_digest_len = (size_t) *p++;
10114
10115 if( session->peer_cert_digest_len != 0 )
10116 {
Hanno Becker2326d202019-06-06 14:54:55 +010010117 const mbedtls_md_info_t *md_info =
10118 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10119 if( md_info == NULL )
10120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10121 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10123
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010124 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10126
10127 session->peer_cert_digest =
10128 mbedtls_calloc( 1, session->peer_cert_digest_len );
10129 if( session->peer_cert_digest == NULL )
10130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10131
10132 memcpy( session->peer_cert_digest, p,
10133 session->peer_cert_digest_len );
10134 p += session->peer_cert_digest_len;
10135 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010136#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010137#endif /* MBEDTLS_X509_CRT_PARSE_C */
10138
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010139 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010140 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010141 */
10142#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10143 if( 3 > (size_t)( end - p ) )
10144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10145
10146 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10147 p += 3;
10148
10149 if( session->ticket_len != 0 )
10150 {
10151 if( session->ticket_len > (size_t)( end - p ) )
10152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10153
10154 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10155 if( session->ticket == NULL )
10156 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10157
10158 memcpy( session->ticket, p, session->ticket_len );
10159 p += session->ticket_len;
10160 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010161
10162 if( 4 > (size_t)( end - p ) )
10163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10164
10165 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10166 ( (uint32_t) p[1] << 16 ) |
10167 ( (uint32_t) p[2] << 8 ) |
10168 ( (uint32_t) p[3] );
10169 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010170#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10171
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010172 /*
10173 * Misc extension-related info
10174 */
10175#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10176 if( 1 > (size_t)( end - p ) )
10177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10178
10179 session->mfl_code = *p++;
10180#endif
10181
10182#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10183 if( 1 > (size_t)( end - p ) )
10184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10185
10186 session->trunc_hmac = *p++;
10187#endif
10188
10189#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10190 if( 1 > (size_t)( end - p ) )
10191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10192
10193 session->encrypt_then_mac = *p++;
10194#endif
10195
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010196 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010197 if( p != end )
10198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10199
10200 return( 0 );
10201}
10202
10203/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010204 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010205 */
10206int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10207 const unsigned char *buf,
10208 size_t len )
10209{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010210 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010211
10212 if( ret != 0 )
10213 mbedtls_ssl_session_free( session );
10214
10215 return( ret );
10216}
10217
10218/*
Paul Bakker1961b702013-01-25 14:49:24 +010010219 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010224
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010225 if( ssl == NULL || ssl->conf == NULL )
10226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010229 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010233 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010235#endif
10236
Hanno Beckerb82350b2019-07-26 07:24:05 +010010237 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010238 return( ret );
10239}
10240
10241/*
10242 * Perform the SSL handshake
10243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010245{
10246 int ret = 0;
10247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010248 if( ssl == NULL || ssl->conf == NULL )
10249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010256
10257 if( ret != 0 )
10258 break;
10259 }
10260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010262
10263 return( ret );
10264}
10265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266#if defined(MBEDTLS_SSL_RENEGOTIATION)
10267#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010268/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010269 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010272{
10273 int ret;
10274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010276
10277 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10279 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010280
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010281 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010282 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010284 return( ret );
10285 }
10286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010288
10289 return( 0 );
10290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010292
10293/*
10294 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 * - any side: calling mbedtls_ssl_renegotiate(),
10296 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10297 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010298 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010299 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010303{
10304 int ret;
10305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010307
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010308 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10309 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010310
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010311 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10312 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010314 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010316 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010317 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10318 MBEDTLS_SSL_IS_SERVER )
10319 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010320 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010321 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010322 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010323 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010324 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010325 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010326 }
10327#endif
10328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10330 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010335 return( ret );
10336 }
10337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010339
10340 return( 0 );
10341}
10342
10343/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010344 * Renegotiate current connection on client,
10345 * or request renegotiation on server
10346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010350
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010351 if( ssl == NULL || ssl->conf == NULL )
10352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010355 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010356 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010362
10363 /* Did we already try/start sending HelloRequest? */
10364 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010366
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010367 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010372 /*
10373 * On client, either start the renegotiation process or,
10374 * if already in progress, continue the handshake
10375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010380
10381 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010384 return( ret );
10385 }
10386 }
10387 else
10388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010392 return( ret );
10393 }
10394 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010396
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010397 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010398}
10399
10400/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010401 * Check record counters and renegotiate if they're above the limit.
10402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010404{
Andres AG2196c7f2016-12-15 17:01:16 +000010405 size_t ep_len = ssl_ep_len( ssl );
10406 int in_ctr_cmp;
10407 int out_ctr_cmp;
10408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10410 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010411 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010412 {
10413 return( 0 );
10414 }
10415
Andres AG2196c7f2016-12-15 17:01:16 +000010416 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10417 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010418 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010419 ssl->conf->renego_period + ep_len, 8 - ep_len );
10420
10421 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010422 {
10423 return( 0 );
10424 }
10425
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010430
10431/*
10432 * Receive application data decrypted from the SSL layer
10433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010435{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010436 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010437 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010438
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010439 if( ssl == NULL || ssl->conf == NULL )
10440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010445 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010448 return( ret );
10449
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010450 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010452 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010453 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010454 return( ret );
10455 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010456 }
10457#endif
10458
Hanno Becker4a810fb2017-05-24 16:27:30 +010010459 /*
10460 * Check if renegotiation is necessary and/or handshake is
10461 * in process. If yes, perform/continue, and fall through
10462 * if an unexpected packet is received while the client
10463 * is waiting for the ServerHello.
10464 *
10465 * (There is no equivalent to the last condition on
10466 * the server-side as it is not treated as within
10467 * a handshake while waiting for the ClientHello
10468 * after a renegotiation request.)
10469 */
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010472 ret = ssl_check_ctr_renegotiate( ssl );
10473 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10474 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010477 return( ret );
10478 }
10479#endif
10480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010484 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10485 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488 return( ret );
10489 }
10490 }
10491
Hanno Beckere41158b2017-10-23 13:30:32 +010010492 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010493 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010495 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010496 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10497 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010498 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010499 ssl_set_timer( ssl,
10500 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010501 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010502
Hanno Becker327c93b2018-08-15 13:56:18 +010010503 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010504 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010505 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10506 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010507
Hanno Becker4a810fb2017-05-24 16:27:30 +010010508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10509 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010510 }
10511
10512 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010514 {
10515 /*
10516 * OpenSSL sends empty messages to randomize the IV
10517 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010518 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010521 return( 0 );
10522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010524 return( ret );
10525 }
10526 }
10527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010531
Hanno Becker4a810fb2017-05-24 16:27:30 +010010532 /*
10533 * - For client-side, expect SERVER_HELLO_REQUEST.
10534 * - For server-side, expect CLIENT_HELLO.
10535 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10536 */
10537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010539 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10540 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010541 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010542 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010545
10546 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010548 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010549 {
10550 continue;
10551 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010552 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010553#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010554#if defined(MBEDTLS_SSL_PROTO_TLS)
10555 {
10556 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10557 }
10558#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010559 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010560#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010561
Hanno Becker4a810fb2017-05-24 16:27:30 +010010562#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010563 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10564 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010568
10569 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010570#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010571 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010572 {
10573 continue;
10574 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010575 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010576#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010577#if defined(MBEDTLS_SSL_PROTO_TLS)
10578 {
10579 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10580 }
10581#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010582 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010583#endif /* MBEDTLS_SSL_SRV_C */
10584
Hanno Becker21df7f92017-10-17 11:03:26 +010010585#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010586 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010587 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10588 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010589 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010590 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10591 {
10592 /*
10593 * Accept renegotiation request
10594 */
Paul Bakker48916f92012-09-16 19:57:18 +000010595
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010596 /* DTLS clients need to know renego is server-initiated */
10597#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010598 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010599 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10600 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010601 {
10602 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10603 }
10604#endif
10605 ret = ssl_start_renegotiation( ssl );
10606 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10607 ret != 0 )
10608 {
10609 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10610 return( ret );
10611 }
10612 }
10613 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010614#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010615 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010616 /*
10617 * Refuse renegotiation
10618 */
10619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010623 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010624 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010625 /* SSLv3 does not have a "no_renegotiation" warning, so
10626 we send a fatal alert and abort the connection. */
10627 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10628 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10629 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010630 }
10631 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10633#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10634 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010635 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010636 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010637 ret = mbedtls_ssl_send_alert_message( ssl,
10638 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10639 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10640 if( ret != 0 )
10641 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010642 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10645 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10648 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010649 }
Paul Bakker48916f92012-09-16 19:57:18 +000010650 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010651
Hanno Becker90333da2017-10-10 11:27:13 +010010652 /* At this point, we don't know whether the renegotiation has been
10653 * completed or not. The cases to consider are the following:
10654 * 1) The renegotiation is complete. In this case, no new record
10655 * has been read yet.
10656 * 2) The renegotiation is incomplete because the client received
10657 * an application data record while awaiting the ServerHello.
10658 * 3) The renegotiation is incomplete because the client received
10659 * a non-handshake, non-application data message while awaiting
10660 * the ServerHello.
10661 * In each of these case, looping will be the proper action:
10662 * - For 1), the next iteration will read a new record and check
10663 * if it's application data.
10664 * - For 2), the loop condition isn't satisfied as application data
10665 * is present, hence continue is the same as break
10666 * - For 3), the loop condition is satisfied and read_record
10667 * will re-deliver the message that was held back by the client
10668 * when expecting the ServerHello.
10669 */
10670 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010671 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010672#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010674 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010675 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010676 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010677 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010680 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010682 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010683 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10688 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010691 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010692 }
10693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10697 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010698 }
10699
10700 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010701
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010702 /* We're going to return something now, cancel timer,
10703 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010705 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010706
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010708 /* If we requested renego but received AppData, resend HelloRequest.
10709 * Do it now, after setting in_offt, to avoid taking this branch
10710 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010712 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10713 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010715 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010716 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010719 return( ret );
10720 }
10721 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010723#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010724 }
10725
10726 n = ( len < ssl->in_msglen )
10727 ? len : ssl->in_msglen;
10728
10729 memcpy( buf, ssl->in_offt, n );
10730 ssl->in_msglen -= n;
10731
10732 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010733 {
10734 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010735 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010736 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010737 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010738 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010739 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010740 /* more data available */
10741 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010742 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010745
Paul Bakker23986e52011-04-24 08:57:21 +000010746 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010747}
10748
10749/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010750 * Send application data to be encrypted by the SSL layer, taking care of max
10751 * fragment length and buffer size.
10752 *
10753 * According to RFC 5246 Section 6.2.1:
10754 *
10755 * Zero-length fragments of Application data MAY be sent as they are
10756 * potentially useful as a traffic analysis countermeasure.
10757 *
10758 * Therefore, it is possible that the input message length is 0 and the
10759 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010760 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010761static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010762 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010763{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010764 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10765 const size_t max_len = (size_t) ret;
10766
10767 if( ret < 0 )
10768 {
10769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10770 return( ret );
10771 }
10772
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010773 if( len > max_len )
10774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010776 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010779 "maximum fragment length: %d > %d",
10780 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010782 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010783 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010784#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010785#if defined(MBEDTLS_SSL_PROTO_TLS)
10786 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010787 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010788 }
10789#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010790 }
Paul Bakker887bd502011-06-08 13:10:54 +000010791
Paul Bakker5121ce52009-01-03 21:22:43 +000010792 if( ssl->out_left != 0 )
10793 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010794 /*
10795 * The user has previously tried to send the data and
10796 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10797 * written. In this case, we expect the high-level write function
10798 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010803 return( ret );
10804 }
10805 }
Paul Bakker887bd502011-06-08 13:10:54 +000010806 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010807 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010808 /*
10809 * The user is trying to send a message the first time, so we need to
10810 * copy the data into the internal buffers and setup the data structure
10811 * to keep track of partial writes
10812 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010813 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010815 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010816
Hanno Becker67bc7c32018-08-06 11:33:50 +010010817 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010820 return( ret );
10821 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010822 }
10823
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010824 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010825}
10826
10827/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010828 * Write application data, doing 1/n-1 splitting if necessary.
10829 *
10830 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010831 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010832 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010834#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010835static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010836 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010837{
10838 int ret;
10839
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010840 if( ssl->conf->cbc_record_splitting ==
10841 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010842 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010843 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10845 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010846 {
10847 return( ssl_write_real( ssl, buf, len ) );
10848 }
10849
10850 if( ssl->split_done == 0 )
10851 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010852 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010853 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010854 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010855 }
10856
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010857 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10858 return( ret );
10859 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010860
10861 return( ret + 1 );
10862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010864
10865/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010866 * Write application data (public-facing wrapper)
10867 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010868int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010869{
10870 int ret;
10871
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010873
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010874 if( ssl == NULL || ssl->conf == NULL )
10875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10876
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010877#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010878 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10879 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010880 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010881 return( ret );
10882 }
10883#endif
10884
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010885 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010886 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010887 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010888 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010890 return( ret );
10891 }
10892 }
10893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010894#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010895 ret = ssl_write_split( ssl, buf, len );
10896#else
10897 ret = ssl_write_real( ssl, buf, len );
10898#endif
10899
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010901
10902 return( ret );
10903}
10904
10905/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010906 * Notify the peer that the connection is being closed
10907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010909{
10910 int ret;
10911
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010912 if( ssl == NULL || ssl->conf == NULL )
10913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010916
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010917 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10923 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10924 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010927 return( ret );
10928 }
10929 }
10930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010932
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010933 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010934}
10935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010937{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010938 if( transform == NULL )
10939 return;
10940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010942 deflateEnd( &transform->ctx_deflate );
10943 inflateEnd( &transform->ctx_inflate );
10944#endif
10945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10947 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010948
Hanno Becker92231322018-01-03 15:32:51 +000010949#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950 mbedtls_md_free( &transform->md_ctx_enc );
10951 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010952#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010953
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010954 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010955}
10956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957#if defined(MBEDTLS_X509_CRT_PARSE_C)
10958static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010961
10962 while( cur != NULL )
10963 {
10964 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010966 cur = next;
10967 }
10968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010970
Hanno Becker0271f962018-08-16 13:23:47 +010010971#if defined(MBEDTLS_SSL_PROTO_DTLS)
10972
10973static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10974{
10975 unsigned offset;
10976 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10977
10978 if( hs == NULL )
10979 return;
10980
Hanno Becker283f5ef2018-08-24 09:34:47 +010010981 ssl_free_buffered_record( ssl );
10982
Hanno Becker0271f962018-08-16 13:23:47 +010010983 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010984 ssl_buffering_free_slot( ssl, offset );
10985}
10986
10987static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10988 uint8_t slot )
10989{
10990 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10991 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010992
10993 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10994 return;
10995
Hanno Beckere605b192018-08-21 15:59:07 +010010996 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010997 {
Hanno Beckere605b192018-08-21 15:59:07 +010010998 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010999 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011000 mbedtls_free( hs_buf->data );
11001 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011002 }
11003}
11004
11005#endif /* MBEDTLS_SSL_PROTO_DTLS */
11006
Gilles Peskine9b562d52018-04-25 20:32:43 +020011007void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011008{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011009 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11010
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011011 if( handshake == NULL )
11012 return;
11013
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011014#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11015 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11016 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011017 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011018 handshake->async_in_progress = 0;
11019 }
11020#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11021
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011022#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11023 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11024 mbedtls_md5_free( &handshake->fin_md5 );
11025 mbedtls_sha1_free( &handshake->fin_sha1 );
11026#endif
11027#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11028#if defined(MBEDTLS_SHA256_C)
11029 mbedtls_sha256_free( &handshake->fin_sha256 );
11030#endif
11031#if defined(MBEDTLS_SHA512_C)
11032 mbedtls_sha512_free( &handshake->fin_sha512 );
11033#endif
11034#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#if defined(MBEDTLS_DHM_C)
11037 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039#if defined(MBEDTLS_ECDH_C)
11040 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011041#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011042#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011043 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011044#if defined(MBEDTLS_SSL_CLI_C)
11045 mbedtls_free( handshake->ecjpake_cache );
11046 handshake->ecjpake_cache = NULL;
11047 handshake->ecjpake_cache_len = 0;
11048#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011049#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011050
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011051#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11052 if( handshake->psk != NULL )
11053 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011054 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011055 mbedtls_free( handshake->psk );
11056 }
11057#endif
11058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11060 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011061 /*
11062 * Free only the linked list wrapper, not the keys themselves
11063 * since the belong to the SNI callback
11064 */
11065 if( handshake->sni_key_cert != NULL )
11066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011067 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011068
11069 while( cur != NULL )
11070 {
11071 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011073 cur = next;
11074 }
11075 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011077
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011078#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011079 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011080 if( handshake->ecrs_peer_cert != NULL )
11081 {
11082 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11083 mbedtls_free( handshake->ecrs_peer_cert );
11084 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011085#endif
11086
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011087#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11088 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11089 mbedtls_pk_free( &handshake->peer_pubkey );
11090#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011092#if defined(MBEDTLS_SSL_PROTO_DTLS)
11093 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011094 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011095 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011096#endif
11097
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011098 mbedtls_platform_zeroize( handshake,
11099 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011100}
11101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011103{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011104 if( session == NULL )
11105 return;
11106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011108 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011109#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011110
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011111#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011113#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011114
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011115 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011116}
11117
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011118#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011119
11120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11121#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11122#else
11123#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11124#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11125
11126#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11127#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11128#else
11129#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11130#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11131
11132#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11133#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11134#else
11135#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11136#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11137
11138#if defined(MBEDTLS_SSL_ALPN)
11139#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11140#else
11141#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11142#endif /* MBEDTLS_SSL_ALPN */
11143
11144#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11145#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11146#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11147#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11148
11149#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11150 ( (uint32_t) ( \
11151 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11152 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11153 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11154 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11155 0u ) )
11156
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011157static unsigned char ssl_serialized_context_header[] = {
11158 MBEDTLS_VERSION_MAJOR,
11159 MBEDTLS_VERSION_MINOR,
11160 MBEDTLS_VERSION_PATCH,
11161 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11162 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011163 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11164 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11165 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011166};
11167
Paul Bakker5121ce52009-01-03 21:22:43 +000011168/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011169 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011170 *
11171 * The format of the serialized data is:
11172 * (in the presentation language of TLS, RFC 8446 section 3)
11173 *
11174 * // header
11175 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011176 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011177 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011178 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011179 * Note: When updating the format, remember to keep these
11180 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011181 *
11182 * // session sub-structure
11183 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11184 * // transform sub-structure
11185 * uint8 random[64]; // ServerHello.random+ClientHello.random
11186 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11187 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11188 * // fields from ssl_context
11189 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11190 * uint64 in_window_top; // DTLS: last validated record seq_num
11191 * uint64 in_window; // DTLS: bitmask for replay protection
11192 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11193 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11194 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11195 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11196 *
11197 * Note that many fields of the ssl_context or sub-structures are not
11198 * serialized, as they fall in one of the following categories:
11199 *
11200 * 1. forced value (eg in_left must be 0)
11201 * 2. pointer to dynamically-allocated memory (eg session, transform)
11202 * 3. value can be re-derived from other data (eg session keys from MS)
11203 * 4. value was temporary (eg content of input buffer)
11204 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011205 */
11206int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11207 unsigned char *buf,
11208 size_t buf_len,
11209 size_t *olen )
11210{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011211 unsigned char *p = buf;
11212 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011213 size_t session_len;
11214 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011215
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011216 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011217 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11218 * this function's documentation.
11219 *
11220 * These are due to assumptions/limitations in the implementation. Some of
11221 * them are likely to stay (no handshake in progress) some might go away
11222 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011223 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011224 /* The initial handshake must be over */
11225 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011227 if( ssl->handshake != NULL )
11228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11229 /* Double-check that sub-structures are indeed ready */
11230 if( ssl->transform == NULL || ssl->session == NULL )
11231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11232 /* There must be no pending incoming or outgoing data */
11233 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11235 if( ssl->out_left != 0 )
11236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11237 /* Protocol must be DLTS, not TLS */
11238 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11240 /* Version must be 1.2 */
11241 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11243 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11245 /* We must be using an AEAD ciphersuite */
11246 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11248 /* Renegotiation must not be enabled */
11249 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011251
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011252 /*
11253 * Version and format identifier
11254 */
11255 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011256
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011257 if( used <= buf_len )
11258 {
11259 memcpy( p, ssl_serialized_context_header,
11260 sizeof( ssl_serialized_context_header ) );
11261 p += sizeof( ssl_serialized_context_header );
11262 }
11263
11264 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011265 * Session (length + data)
11266 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011267 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011268 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11269 return( ret );
11270
11271 used += 4 + session_len;
11272 if( used <= buf_len )
11273 {
11274 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11275 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11276 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11277 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11278
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011279 ret = ssl_session_save( ssl->session, 1,
11280 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011281 if( ret != 0 )
11282 return( ret );
11283
11284 p += session_len;
11285 }
11286
11287 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011288 * Transform
11289 */
11290 used += sizeof( ssl->transform->randbytes );
11291 if( used <= buf_len )
11292 {
11293 memcpy( p, ssl->transform->randbytes,
11294 sizeof( ssl->transform->randbytes ) );
11295 p += sizeof( ssl->transform->randbytes );
11296 }
11297
11298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11299 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11300 if( used <= buf_len )
11301 {
11302 *p++ = ssl->transform->in_cid_len;
11303 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11304 p += ssl->transform->in_cid_len;
11305
11306 *p++ = ssl->transform->out_cid_len;
11307 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11308 p += ssl->transform->out_cid_len;
11309 }
11310#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11311
11312 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011313 * Saved fields from top-level ssl_context structure
11314 */
11315#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11316 used += 4;
11317 if( used <= buf_len )
11318 {
11319 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11320 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11321 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11322 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11323 }
11324#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11325
11326#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11327 used += 16;
11328 if( used <= buf_len )
11329 {
11330 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11331 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11332 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11333 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11334 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11335 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11336 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11337 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11338
11339 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11340 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11341 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11342 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11343 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11344 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11345 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11346 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11347 }
11348#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11349
11350#if defined(MBEDTLS_SSL_PROTO_DTLS)
11351 used += 1;
11352 if( used <= buf_len )
11353 {
11354 *p++ = ssl->disable_datagram_packing;
11355 }
11356#endif /* MBEDTLS_SSL_PROTO_DTLS */
11357
11358 used += 8;
11359 if( used <= buf_len )
11360 {
11361 memcpy( p, ssl->cur_out_ctr, 8 );
11362 p += 8;
11363 }
11364
11365#if defined(MBEDTLS_SSL_PROTO_DTLS)
11366 used += 2;
11367 if( used <= buf_len )
11368 {
11369 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11370 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11371 }
11372#endif /* MBEDTLS_SSL_PROTO_DTLS */
11373
11374#if defined(MBEDTLS_SSL_ALPN)
11375 {
11376 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011377 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011378 : 0;
11379
11380 used += 1 + alpn_len;
11381 if( used <= buf_len )
11382 {
11383 *p++ = alpn_len;
11384
11385 if( ssl->alpn_chosen != NULL )
11386 {
11387 memcpy( p, ssl->alpn_chosen, alpn_len );
11388 p += alpn_len;
11389 }
11390 }
11391 }
11392#endif /* MBEDTLS_SSL_ALPN */
11393
11394 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011395 * Done
11396 */
11397 *olen = used;
11398
11399 if( used > buf_len )
11400 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011401
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011402 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11403
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011404 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011405}
11406
11407/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011408 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011409 *
11410 * This internal version is wrapped by a public function that cleans up in
11411 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011412 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011413static int ssl_context_load( mbedtls_ssl_context *ssl,
11414 const unsigned char *buf,
11415 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011416{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011417 const unsigned char *p = buf;
11418 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011419 size_t session_len;
11420 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011421
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011422 /*
11423 * The context should have been freshly setup or reset.
11424 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011425 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011426 * renegotiating, or if the user mistakenly loaded a session first.)
11427 */
11428 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11429 ssl->session != NULL )
11430 {
11431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11432 }
11433
11434 /*
11435 * We can't check that the config matches the initial one, but we can at
11436 * least check it matches the requirements for serializing.
11437 */
11438 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011439 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11440 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11441 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11442 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11443 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11444 MBEDTLS_SSL_MINOR_VERSION_3 ||
11445 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11446 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011447 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011448 {
11449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11450 }
11451
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011452 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11453
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011454 /*
11455 * Check version identifier
11456 */
11457 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11459
11460 if( memcmp( p, ssl_serialized_context_header,
11461 sizeof( ssl_serialized_context_header ) ) != 0 )
11462 {
11463 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11464 }
11465 p += sizeof( ssl_serialized_context_header );
11466
11467 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011468 * Session
11469 */
11470 if( (size_t)( end - p ) < 4 )
11471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11472
11473 session_len = ( (size_t) p[0] << 24 ) |
11474 ( (size_t) p[1] << 16 ) |
11475 ( (size_t) p[2] << 8 ) |
11476 ( (size_t) p[3] );
11477 p += 4;
11478
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011479 /* This has been allocated by ssl_handshake_init(), called by
11480 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11481 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011482 ssl->session_in = ssl->session;
11483 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011484 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011485
11486 if( (size_t)( end - p ) < session_len )
11487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11488
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011489 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011490 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011491 {
11492 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011493 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011494 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011495
11496 p += session_len;
11497
11498 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011499 * Transform
11500 */
11501
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011502 /* This has been allocated by ssl_handshake_init(), called by
11503 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11504 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011505 ssl->transform_in = ssl->transform;
11506 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011507 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011508
11509 /* Read random bytes and populate structure */
11510 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11512
11513 ret = ssl_populate_transform( ssl->transform,
11514 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11515 ssl->session->master,
11516#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11517#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11518 ssl->session->encrypt_then_mac,
11519#endif
11520#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11521 ssl->session->trunc_hmac,
11522#endif
11523#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11524#if defined(MBEDTLS_ZLIB_SUPPORT)
11525 ssl->session->compression,
11526#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011527 p, /* currently pointing to randbytes */
11528 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11529 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11530 ssl );
11531 if( ret != 0 )
11532 return( ret );
11533
11534 p += sizeof( ssl->transform->randbytes );
11535
11536#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11537 /* Read connection IDs and store them */
11538 if( (size_t)( end - p ) < 1 )
11539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11540
11541 ssl->transform->in_cid_len = *p++;
11542
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011543 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11545
11546 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11547 p += ssl->transform->in_cid_len;
11548
11549 ssl->transform->out_cid_len = *p++;
11550
11551 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11553
11554 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11555 p += ssl->transform->out_cid_len;
11556#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11557
11558 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011559 * Saved fields from top-level ssl_context structure
11560 */
11561#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11562 if( (size_t)( end - p ) < 4 )
11563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11564
11565 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11566 ( (uint32_t) p[1] << 16 ) |
11567 ( (uint32_t) p[2] << 8 ) |
11568 ( (uint32_t) p[3] );
11569 p += 4;
11570#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11571
11572#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11573 if( (size_t)( end - p ) < 16 )
11574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11575
11576 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11577 ( (uint64_t) p[1] << 48 ) |
11578 ( (uint64_t) p[2] << 40 ) |
11579 ( (uint64_t) p[3] << 32 ) |
11580 ( (uint64_t) p[4] << 24 ) |
11581 ( (uint64_t) p[5] << 16 ) |
11582 ( (uint64_t) p[6] << 8 ) |
11583 ( (uint64_t) p[7] );
11584 p += 8;
11585
11586 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11587 ( (uint64_t) p[1] << 48 ) |
11588 ( (uint64_t) p[2] << 40 ) |
11589 ( (uint64_t) p[3] << 32 ) |
11590 ( (uint64_t) p[4] << 24 ) |
11591 ( (uint64_t) p[5] << 16 ) |
11592 ( (uint64_t) p[6] << 8 ) |
11593 ( (uint64_t) p[7] );
11594 p += 8;
11595#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11596
11597#if defined(MBEDTLS_SSL_PROTO_DTLS)
11598 if( (size_t)( end - p ) < 1 )
11599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11600
11601 ssl->disable_datagram_packing = *p++;
11602#endif /* MBEDTLS_SSL_PROTO_DTLS */
11603
11604 if( (size_t)( end - p ) < 8 )
11605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11606
11607 memcpy( ssl->cur_out_ctr, p, 8 );
11608 p += 8;
11609
11610#if defined(MBEDTLS_SSL_PROTO_DTLS)
11611 if( (size_t)( end - p ) < 2 )
11612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11613
11614 ssl->mtu = ( p[0] << 8 ) | p[1];
11615 p += 2;
11616#endif /* MBEDTLS_SSL_PROTO_DTLS */
11617
11618#if defined(MBEDTLS_SSL_ALPN)
11619 {
11620 uint8_t alpn_len;
11621 const char **cur;
11622
11623 if( (size_t)( end - p ) < 1 )
11624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11625
11626 alpn_len = *p++;
11627
11628 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11629 {
11630 /* alpn_chosen should point to an item in the configured list */
11631 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11632 {
11633 if( strlen( *cur ) == alpn_len &&
11634 memcmp( p, cur, alpn_len ) == 0 )
11635 {
11636 ssl->alpn_chosen = *cur;
11637 break;
11638 }
11639 }
11640 }
11641
11642 /* can only happen on conf mismatch */
11643 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11645
11646 p += alpn_len;
11647 }
11648#endif /* MBEDTLS_SSL_ALPN */
11649
11650 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011651 * Forced fields from top-level ssl_context structure
11652 *
11653 * Most of them already set to the correct value by mbedtls_ssl_init() and
11654 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11655 */
11656 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11657
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011658#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011659 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011660#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11661#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011662 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011663#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011664
Hanno Becker83985822019-08-30 10:42:49 +010011665 /* Adjust pointers for header fields of outgoing records to
11666 * the given transform, accounting for explicit IV and CID. */
11667 ssl_update_out_pointers( ssl, ssl->transform );
11668
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011669#if defined(MBEDTLS_SSL_PROTO_DTLS)
11670 ssl->in_epoch = 1;
11671#endif
11672
11673 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11674 * which we don't want - otherwise we'd end up freeing the wrong transform
11675 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11676 if( ssl->handshake != NULL )
11677 {
11678 mbedtls_ssl_handshake_free( ssl );
11679 mbedtls_free( ssl->handshake );
11680 ssl->handshake = NULL;
11681 }
11682
11683 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011684 * Done - should have consumed entire buffer
11685 */
11686 if( p != end )
11687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011688
11689 return( 0 );
11690}
11691
11692/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011693 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011694 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011695int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011696 const unsigned char *buf,
11697 size_t len )
11698{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011699 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011700
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011701 if( ret != 0 )
11702 mbedtls_ssl_free( context );
11703
11704 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011705}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011706#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011707
11708/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011709 * Free an SSL context
11710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011711void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011712{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011713 if( ssl == NULL )
11714 return;
11715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011717
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011718 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011719 {
Angus Grattond8213d02016-05-25 20:56:48 +100011720 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011721 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011722 }
11723
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011724 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011725 {
Angus Grattond8213d02016-05-25 20:56:48 +100011726 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011727 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011728 }
11729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011731 if( ssl->compress_buf != NULL )
11732 {
Angus Grattond8213d02016-05-25 20:56:48 +100011733 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011734 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011735 }
11736#endif
11737
Paul Bakker48916f92012-09-16 19:57:18 +000011738 if( ssl->transform )
11739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011740 mbedtls_ssl_transform_free( ssl->transform );
11741 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011742 }
11743
11744 if( ssl->handshake )
11745 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011746 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011747 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11748 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011750 mbedtls_free( ssl->handshake );
11751 mbedtls_free( ssl->transform_negotiate );
11752 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011753 }
11754
Paul Bakkerc0463502013-02-14 11:19:38 +010011755 if( ssl->session )
11756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011757 mbedtls_ssl_session_free( ssl->session );
11758 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011759 }
11760
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011761#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011762 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011763 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011764 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011765 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011766 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011767#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011769#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11770 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11773 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011774 }
11775#endif
11776
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011777#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011778 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011779#endif
11780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011782
Paul Bakker86f04f42013-02-14 11:20:09 +010011783 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011784 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011785}
11786
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011787/*
11788 * Initialze mbedtls_ssl_config
11789 */
11790void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11791{
11792 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011793
11794#if !defined(MBEDTLS_SSL_PROTO_TLS)
11795 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11796#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011797}
11798
Simon Butcherc97b6972015-12-27 23:48:17 +000011799#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011800#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011801static int ssl_preset_default_hashes[] = {
11802#if defined(MBEDTLS_SHA512_C)
11803 MBEDTLS_MD_SHA512,
11804 MBEDTLS_MD_SHA384,
11805#endif
11806#if defined(MBEDTLS_SHA256_C)
11807 MBEDTLS_MD_SHA256,
11808 MBEDTLS_MD_SHA224,
11809#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011810#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011811 MBEDTLS_MD_SHA1,
11812#endif
11813 MBEDTLS_MD_NONE
11814};
Simon Butcherc97b6972015-12-27 23:48:17 +000011815#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011816#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011817
Hanno Becker73f4cb12019-06-27 13:51:07 +010011818#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011819static int ssl_preset_suiteb_ciphersuites[] = {
11820 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11821 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11822 0
11823};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011824#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011825
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011826#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011827#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011828static int ssl_preset_suiteb_hashes[] = {
11829 MBEDTLS_MD_SHA256,
11830 MBEDTLS_MD_SHA384,
11831 MBEDTLS_MD_NONE
11832};
11833#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011834#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011835
Hanno Beckerc1096e72019-06-19 12:30:41 +010011836#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011837static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011838#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011839 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011840#endif
11841#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011842 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011843#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011844 MBEDTLS_ECP_DP_NONE
11845};
11846#endif
11847
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011848/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011849 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011850 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011851int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011852 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011853{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011854#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011855 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011856#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011857
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011858 /* Use the functions here so that they are covered in tests,
11859 * but otherwise access member directly for efficiency */
11860 mbedtls_ssl_conf_endpoint( conf, endpoint );
11861 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011862
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011863 /*
11864 * Things that are common to all presets
11865 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011866#if defined(MBEDTLS_SSL_CLI_C)
11867 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11868 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011869#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011870 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011871#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011872#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11873 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11874#endif
11875 }
11876#endif
11877
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011878#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011879 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011880#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011881
11882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11883 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11884#endif
11885
11886#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011887#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011888 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011889#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11890#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011891 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011892 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011893#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011894#endif
11895
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011896#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11897 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11898#endif
11899
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011900#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011901 conf->f_cookie_write = ssl_cookie_write_dummy;
11902 conf->f_cookie_check = ssl_cookie_check_dummy;
11903#endif
11904
Hanno Becker7f376f42019-06-12 16:20:48 +010011905#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11906 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011907 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11908#endif
11909
Janos Follath088ce432017-04-10 12:42:31 +010011910#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011911#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011912 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011913#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11914#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011915
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011917#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011918 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011919#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11920#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011921 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011922#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11923#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011924
11925#if defined(MBEDTLS_SSL_RENEGOTIATION)
11926 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011927 memset( conf->renego_period, 0x00, 2 );
11928 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011929#endif
11930
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011931#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11932 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11933 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011934 const unsigned char dhm_p[] =
11935 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11936 const unsigned char dhm_g[] =
11937 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11938
Hanno Beckera90658f2017-10-04 15:29:08 +010011939 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11940 dhm_p, sizeof( dhm_p ),
11941 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011942 {
11943 return( ret );
11944 }
11945 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011946#endif
11947
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011948 /*
11949 * Preset-specific defaults
11950 */
11951 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011952 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011953 /*
11954 * NSA Suite B
11955 */
11956 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011957#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011958 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011959#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11960#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011961 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011962#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11963#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011964 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011965#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11966#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011967 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011968#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011969
Hanno Becker73f4cb12019-06-27 13:51:07 +010011970#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011971 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11972 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11973 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11974 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11975 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011976#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011977
11978#if defined(MBEDTLS_X509_CRT_PARSE_C)
11979 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011980#endif
11981
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011982#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011983#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011984 conf->sig_hashes = ssl_preset_suiteb_hashes;
11985#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011986#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011987
11988#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011989#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011990 conf->curve_list = ssl_preset_suiteb_curves;
11991#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011992#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011993 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011994
11995 /*
11996 * Default
11997 */
11998 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011999#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012000 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12001 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12002 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12003 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012004#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12005#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012006 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12007 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12008 MBEDTLS_SSL_MIN_MINOR_VERSION :
12009 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012011 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012012 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12013#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012014#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12015#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12016 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12017#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12018#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12019 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12020#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012021
Hanno Becker73f4cb12019-06-27 13:51:07 +010012022#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012023 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
12024 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
12025 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
12026 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
12027 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012028#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012029
12030#if defined(MBEDTLS_X509_CRT_PARSE_C)
12031 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12032#endif
12033
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012034#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012035#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012036 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012037#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012038#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012039
12040#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012041#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012042 conf->curve_list = mbedtls_ecp_grp_id_list();
12043#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012044#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012045
12046#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12047 conf->dhm_min_bitlen = 1024;
12048#endif
12049 }
12050
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012051 return( 0 );
12052}
12053
12054/*
12055 * Free mbedtls_ssl_config
12056 */
12057void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12058{
12059#if defined(MBEDTLS_DHM_C)
12060 mbedtls_mpi_free( &conf->dhm_P );
12061 mbedtls_mpi_free( &conf->dhm_G );
12062#endif
12063
12064#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12065 if( conf->psk != NULL )
12066 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012067 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012068 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012069 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012070 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012071 }
12072
12073 if( conf->psk_identity != NULL )
12074 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012075 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012076 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012077 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012078 conf->psk_identity_len = 0;
12079 }
12080#endif
12081
12082#if defined(MBEDTLS_X509_CRT_PARSE_C)
12083 ssl_key_cert_free( conf->key_cert );
12084#endif
12085
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012086 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012087}
12088
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012089#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012090 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12091 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012092/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012093 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012095unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012097#if defined(MBEDTLS_RSA_C)
12098 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12099 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012100#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012101#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012102 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12103 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012105 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012106}
12107
Hanno Becker7e5437a2017-04-28 17:15:26 +010012108unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12109{
12110 switch( type ) {
12111 case MBEDTLS_PK_RSA:
12112 return( MBEDTLS_SSL_SIG_RSA );
12113 case MBEDTLS_PK_ECDSA:
12114 case MBEDTLS_PK_ECKEY:
12115 return( MBEDTLS_SSL_SIG_ECDSA );
12116 default:
12117 return( MBEDTLS_SSL_SIG_ANON );
12118 }
12119}
12120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012121mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012122{
12123 switch( sig )
12124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012125#if defined(MBEDTLS_RSA_C)
12126 case MBEDTLS_SSL_SIG_RSA:
12127 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012128#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012129#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012130 case MBEDTLS_SSL_SIG_ECDSA:
12131 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012132#endif
12133 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012134 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012135 }
12136}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012137#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012138
Hanno Becker7e5437a2017-04-28 17:15:26 +010012139#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12140 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12141
12142/* Find an entry in a signature-hash set matching a given hash algorithm. */
12143mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12144 mbedtls_pk_type_t sig_alg )
12145{
12146 switch( sig_alg )
12147 {
12148 case MBEDTLS_PK_RSA:
12149 return( set->rsa );
12150 case MBEDTLS_PK_ECDSA:
12151 return( set->ecdsa );
12152 default:
12153 return( MBEDTLS_MD_NONE );
12154 }
12155}
12156
12157/* Add a signature-hash-pair to a signature-hash set */
12158void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12159 mbedtls_pk_type_t sig_alg,
12160 mbedtls_md_type_t md_alg )
12161{
12162 switch( sig_alg )
12163 {
12164 case MBEDTLS_PK_RSA:
12165 if( set->rsa == MBEDTLS_MD_NONE )
12166 set->rsa = md_alg;
12167 break;
12168
12169 case MBEDTLS_PK_ECDSA:
12170 if( set->ecdsa == MBEDTLS_MD_NONE )
12171 set->ecdsa = md_alg;
12172 break;
12173
12174 default:
12175 break;
12176 }
12177}
12178
12179/* Allow exactly one hash algorithm for each signature. */
12180void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12181 mbedtls_md_type_t md_alg )
12182{
12183 set->rsa = md_alg;
12184 set->ecdsa = md_alg;
12185}
12186
12187#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12188 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12189
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012190/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012191 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012193mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012194{
12195 switch( hash )
12196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012197#if defined(MBEDTLS_MD5_C)
12198 case MBEDTLS_SSL_HASH_MD5:
12199 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012201#if defined(MBEDTLS_SHA1_C)
12202 case MBEDTLS_SSL_HASH_SHA1:
12203 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012205#if defined(MBEDTLS_SHA256_C)
12206 case MBEDTLS_SSL_HASH_SHA224:
12207 return( MBEDTLS_MD_SHA224 );
12208 case MBEDTLS_SSL_HASH_SHA256:
12209 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012211#if defined(MBEDTLS_SHA512_C)
12212 case MBEDTLS_SSL_HASH_SHA384:
12213 return( MBEDTLS_MD_SHA384 );
12214 case MBEDTLS_SSL_HASH_SHA512:
12215 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012216#endif
12217 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012218 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012219 }
12220}
12221
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012222/*
12223 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12224 */
12225unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12226{
12227 switch( md )
12228 {
12229#if defined(MBEDTLS_MD5_C)
12230 case MBEDTLS_MD_MD5:
12231 return( MBEDTLS_SSL_HASH_MD5 );
12232#endif
12233#if defined(MBEDTLS_SHA1_C)
12234 case MBEDTLS_MD_SHA1:
12235 return( MBEDTLS_SSL_HASH_SHA1 );
12236#endif
12237#if defined(MBEDTLS_SHA256_C)
12238 case MBEDTLS_MD_SHA224:
12239 return( MBEDTLS_SSL_HASH_SHA224 );
12240 case MBEDTLS_MD_SHA256:
12241 return( MBEDTLS_SSL_HASH_SHA256 );
12242#endif
12243#if defined(MBEDTLS_SHA512_C)
12244 case MBEDTLS_MD_SHA384:
12245 return( MBEDTLS_SSL_HASH_SHA384 );
12246 case MBEDTLS_MD_SHA512:
12247 return( MBEDTLS_SSL_HASH_SHA512 );
12248#endif
12249 default:
12250 return( MBEDTLS_SSL_HASH_NONE );
12251 }
12252}
12253
Hanno Beckeree902df2019-08-23 13:47:47 +010012254#if defined(MBEDTLS_USE_TINYCRYPT)
12255/*
12256 * Check if a curve proposed by the peer is in our list.
12257 * Return 0 if we're willing to use it, -1 otherwise.
12258 */
12259int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12260 mbedtls_uecc_group_id grp_id )
12261{
12262 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12263 if( own_ec_id == grp_id )
12264 return( 0 );
12265 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12266
12267 return( -1 );
12268}
12269#endif /* MBEDTLS_USE_TINYCRYPT */
12270
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012271#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012272/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012273 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012274 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012275 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012276int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12277 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012278{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012279 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12280 if( own_ec_id == grp_id )
12281 return( 0 );
12282 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012283
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012284 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012285}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012286#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012287
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012288#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012289/*
12290 * Check if a hash proposed by the peer is in our list.
12291 * Return 0 if we're willing to use it, -1 otherwise.
12292 */
12293int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12294 mbedtls_md_type_t md )
12295{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012296 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12297 if( md_alg == md )
12298 return( 0 );
12299 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012300
12301 return( -1 );
12302}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012303#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012305#if defined(MBEDTLS_X509_CRT_PARSE_C)
12306int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012307 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012308 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012309 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012310{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012311 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012312#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012313 int usage = 0;
12314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012315#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012316 const char *ext_oid;
12317 size_t ext_len;
12318#endif
12319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012320#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12321 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012322 ((void) cert);
12323 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012324 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012325#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012327#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12328 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012329 {
12330 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012331 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012333 case MBEDTLS_KEY_EXCHANGE_RSA:
12334 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012335 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012336 break;
12337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012338 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12339 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12340 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12341 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012342 break;
12343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012344 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12345 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012346 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012347 break;
12348
12349 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012350 case MBEDTLS_KEY_EXCHANGE_NONE:
12351 case MBEDTLS_KEY_EXCHANGE_PSK:
12352 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12353 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012354 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012355 usage = 0;
12356 }
12357 }
12358 else
12359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012360 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12361 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012362 }
12363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012364 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012365 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012366 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012367 ret = -1;
12368 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012369#else
12370 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012371#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012373#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12374 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012376 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12377 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012378 }
12379 else
12380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012381 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12382 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012383 }
12384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012385 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012386 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012387 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012388 ret = -1;
12389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012390#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012391
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012392 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012394#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012395
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012396#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12397 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12398int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12399 unsigned char *output,
12400 unsigned char *data, size_t data_len )
12401{
12402 int ret = 0;
12403 mbedtls_md5_context mbedtls_md5;
12404 mbedtls_sha1_context mbedtls_sha1;
12405
12406 mbedtls_md5_init( &mbedtls_md5 );
12407 mbedtls_sha1_init( &mbedtls_sha1 );
12408
12409 /*
12410 * digitally-signed struct {
12411 * opaque md5_hash[16];
12412 * opaque sha_hash[20];
12413 * };
12414 *
12415 * md5_hash
12416 * MD5(ClientHello.random + ServerHello.random
12417 * + ServerParams);
12418 * sha_hash
12419 * SHA(ClientHello.random + ServerHello.random
12420 * + ServerParams);
12421 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012422 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012423 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012425 goto exit;
12426 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012427 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012428 ssl->handshake->randbytes, 64 ) ) != 0 )
12429 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012431 goto exit;
12432 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012433 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012434 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012436 goto exit;
12437 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012438 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012439 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012441 goto exit;
12442 }
12443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012444 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012445 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012447 goto exit;
12448 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012449 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012450 ssl->handshake->randbytes, 64 ) ) != 0 )
12451 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012453 goto exit;
12454 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012455 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012456 data_len ) ) != 0 )
12457 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012459 goto exit;
12460 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012461 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012462 output + 16 ) ) != 0 )
12463 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012465 goto exit;
12466 }
12467
12468exit:
12469 mbedtls_md5_free( &mbedtls_md5 );
12470 mbedtls_sha1_free( &mbedtls_sha1 );
12471
12472 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012473 mbedtls_ssl_pend_fatal_alert( ssl,
12474 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012475
12476 return( ret );
12477
12478}
12479#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12480 MBEDTLS_SSL_PROTO_TLS1_1 */
12481
12482#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12483 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12484int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012485 unsigned char *hash, size_t *hashlen,
12486 unsigned char *data, size_t data_len,
12487 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012488{
12489 int ret = 0;
12490 mbedtls_md_context_t ctx;
12491 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012492 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012493
12494 mbedtls_md_init( &ctx );
12495
12496 /*
12497 * digitally-signed struct {
12498 * opaque client_random[32];
12499 * opaque server_random[32];
12500 * ServerDHParams params;
12501 * };
12502 */
12503 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12504 {
12505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12506 goto exit;
12507 }
12508 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12509 {
12510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12511 goto exit;
12512 }
12513 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12514 {
12515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12516 goto exit;
12517 }
12518 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12519 {
12520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12521 goto exit;
12522 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012523 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012524 {
12525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12526 goto exit;
12527 }
12528
12529exit:
12530 mbedtls_md_free( &ctx );
12531
12532 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012533 mbedtls_ssl_pend_fatal_alert( ssl,
12534 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012535
12536 return( ret );
12537}
12538#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12539 MBEDTLS_SSL_PROTO_TLS1_2 */
12540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012541#endif /* MBEDTLS_SSL_TLS_C */