blob: 7f689afbf4fa42f81401bf041f9b33ab4220feda [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:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300500 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000501 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:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300510 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000511 }
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
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002105 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002106
2107 if( end < p || (size_t)( end - p ) < psk_len )
2108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2109
2110 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002111 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002112 }
2113 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2115#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2116 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002117 {
2118 /*
2119 * other_secret already set by the ClientKeyExchange message,
2120 * and is 48 bytes long
2121 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002122 if( end - p < 2 )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002125 *p++ = 0;
2126 *p++ = 48;
2127 p += 48;
2128 }
2129 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2131#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2132 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002134 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002135 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002136
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002137 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002139 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002140 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002141 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002144 return( ret );
2145 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002146 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002147 p += len;
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002150 }
2151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2153#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2154 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002156 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 size_t zlen;
2158
Hanno Becker982da7e2019-09-02 09:47:39 +01002159#if defined(MBEDTLS_USE_TINYCRYPT)
2160 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2161 ((void) ret);
2162
2163 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2164 ssl->handshake->ecdh_privkey,
2165 p + 2,
2166 uecc_curve ) )
2167 {
2168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2169 }
2170
2171 zlen = NUM_ECC_BYTES;
2172#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002174 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002175 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002176 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179 return( ret );
2180 }
2181
Hanno Becker982da7e2019-09-02 09:47:39 +01002182 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2183 MBEDTLS_DEBUG_ECDH_Z );
2184#endif /* MBEDTLS_USE_TINYCRYPT */
2185
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002186 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002187 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 }
2189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002194 }
2195
2196 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002197 if( end - p < 2 )
2198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002199
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002200 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002201
2202 if( end < p || (size_t)( end - p ) < psk_len )
2203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2204
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002205 memcpy( p, psk, psk_len );
2206 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002207
2208 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2209
2210 return( 0 );
2211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002215/*
2216 * SSLv3.0 MAC functions
2217 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002218#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002219static void ssl_mac( mbedtls_md_context_t *md_ctx,
2220 const unsigned char *secret,
2221 const unsigned char *buf, size_t len,
2222 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002223 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002224{
2225 unsigned char header[11];
2226 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002227 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2229 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002230
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002231 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002233 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002234 else
Paul Bakker68884e32013-01-07 18:20:04 +01002235 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002238 header[8] = (unsigned char) type;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002239 mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Paul Bakker68884e32013-01-07 18:20:04 +01002241 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_md_starts( md_ctx );
2243 mbedtls_md_update( md_ctx, secret, md_size );
2244 mbedtls_md_update( md_ctx, padding, padlen );
2245 mbedtls_md_update( md_ctx, header, 11 );
2246 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002247 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Paul Bakker68884e32013-01-07 18:20:04 +01002249 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_md_starts( md_ctx );
2251 mbedtls_md_update( md_ctx, secret, md_size );
2252 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002253 mbedtls_md_update( md_ctx, out, md_size );
2254 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002257
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002258/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002259 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002260#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002261 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2262 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2263 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2264/* This function makes sure every byte in the memory region is accessed
2265 * (in ascending addresses order) */
2266static void ssl_read_memory( unsigned char *p, size_t len )
2267{
2268 unsigned char acc = 0;
2269 volatile unsigned char force;
2270
2271 for( ; len != 0; p++, len-- )
2272 acc ^= *p;
2273
2274 force = acc;
2275 (void) force;
2276}
2277#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2278
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002279/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002281 */
Hanno Becker3307b532017-12-27 21:37:21 +00002282
Hanno Beckera5a2b082019-05-15 14:03:01 +01002283#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002284/* This functions transforms a DTLS plaintext fragment and a record content
2285 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002286 *
2287 * struct {
2288 * opaque content[DTLSPlaintext.length];
2289 * ContentType real_type;
2290 * uint8 zeros[length_of_padding];
2291 * } DTLSInnerPlaintext;
2292 *
2293 * Input:
2294 * - `content`: The beginning of the buffer holding the
2295 * plaintext to be wrapped.
2296 * - `*content_size`: The length of the plaintext in Bytes.
2297 * - `max_len`: The number of Bytes available starting from
2298 * `content`. This must be `>= *content_size`.
2299 * - `rec_type`: The desired record content type.
2300 *
2301 * Output:
2302 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2303 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2304 *
2305 * Returns:
2306 * - `0` on success.
2307 * - A negative error code if `max_len` didn't offer enough space
2308 * for the expansion.
2309 */
2310static int ssl_cid_build_inner_plaintext( unsigned char *content,
2311 size_t *content_size,
2312 size_t remaining,
2313 uint8_t rec_type )
2314{
2315 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002316 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2317 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2318 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002319
2320 /* Write real content type */
2321 if( remaining == 0 )
2322 return( -1 );
2323 content[ len ] = rec_type;
2324 len++;
2325 remaining--;
2326
2327 if( remaining < pad )
2328 return( -1 );
2329 memset( content + len, 0, pad );
2330 len += pad;
2331 remaining -= pad;
2332
2333 *content_size = len;
2334 return( 0 );
2335}
2336
Hanno Becker7dc25772019-05-20 15:08:01 +01002337/* This function parses a DTLSInnerPlaintext structure.
2338 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002339static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2340 size_t *content_size,
2341 uint8_t *rec_type )
2342{
2343 size_t remaining = *content_size;
2344
2345 /* Determine length of padding by skipping zeroes from the back. */
2346 do
2347 {
2348 if( remaining == 0 )
2349 return( -1 );
2350 remaining--;
2351 } while( content[ remaining ] == 0 );
2352
2353 *content_size = remaining;
2354 *rec_type = content[ remaining ];
2355
2356 return( 0 );
2357}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002358#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002359
Hanno Becker99abf512019-05-20 14:50:53 +01002360/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002361 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002362static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002363 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002364 mbedtls_record *rec )
2365{
Hanno Becker99abf512019-05-20 14:50:53 +01002366 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002367 *
2368 * additional_data = seq_num + TLSCompressed.type +
2369 * TLSCompressed.version + TLSCompressed.length;
2370 *
Hanno Becker99abf512019-05-20 14:50:53 +01002371 * For the CID extension, this is extended as follows
2372 * (quoting draft-ietf-tls-dtls-connection-id-05,
2373 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002374 *
2375 * additional_data = seq_num + DTLSPlaintext.type +
2376 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002377 * cid +
2378 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002379 * length_of_DTLSInnerPlaintext;
2380 */
2381
Hanno Becker3307b532017-12-27 21:37:21 +00002382 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2383 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002384 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002385
Hanno Beckera5a2b082019-05-15 14:03:01 +01002386#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002387 if( rec->cid_len != 0 )
2388 {
2389 memcpy( add_data + 11, rec->cid, rec->cid_len );
2390 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002391 mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002392 *add_data_len = 13 + 1 + rec->cid_len;
2393 }
2394 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002395#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002396 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002397 mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002398 *add_data_len = 13;
2399 }
Hanno Becker3307b532017-12-27 21:37:21 +00002400}
2401
Hanno Becker611a83b2018-01-03 14:27:32 +00002402int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2403 mbedtls_ssl_transform *transform,
2404 mbedtls_record *rec,
2405 int (*f_rng)(void *, unsigned char *, size_t),
2406 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002409 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002410 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002411 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002412 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002413 size_t post_avail;
2414
2415 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002416#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002417 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002418 ((void) ssl);
2419#endif
2420
2421 /* The PRNG is used for dynamic IV generation that's used
2422 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2423#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2424 ( defined(MBEDTLS_AES_C) || \
2425 defined(MBEDTLS_ARIA_C) || \
2426 defined(MBEDTLS_CAMELLIA_C) ) && \
2427 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2428 ((void) f_rng);
2429 ((void) p_rng);
2430#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Hanno Becker3307b532017-12-27 21:37:21 +00002434 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002435 {
Hanno Becker3307b532017-12-27 21:37:21 +00002436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2438 }
Hanno Becker505089d2019-05-01 09:45:57 +01002439 if( rec == NULL
2440 || rec->buf == NULL
2441 || rec->buf_len < rec->data_offset
2442 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002443#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002444 || rec->cid_len != 0
2445#endif
2446 )
Hanno Becker3307b532017-12-27 21:37:21 +00002447 {
2448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002450 }
2451
Hanno Becker3307b532017-12-27 21:37:21 +00002452 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002453 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002455 data, rec->data_len );
2456
2457 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2458
2459 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2460 {
2461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2462 (unsigned) rec->data_len,
2463 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2465 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002466
Hanno Beckera5a2b082019-05-15 14:03:01 +01002467#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002468 /*
2469 * Add CID information
2470 */
2471 rec->cid_len = transform->out_cid_len;
2472 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2473 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002474
2475 if( rec->cid_len != 0 )
2476 {
2477 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002478 * Wrap plaintext into DTLSInnerPlaintext structure.
2479 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002480 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002481 * Note that this changes `rec->data_len`, and hence
2482 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002483 */
2484 if( ssl_cid_build_inner_plaintext( data,
2485 &rec->data_len,
2486 post_avail,
2487 rec->type ) != 0 )
2488 {
2489 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2490 }
2491
2492 rec->type = MBEDTLS_SSL_MSG_CID;
2493 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002494#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002495
Hanno Becker92c930f2019-04-29 17:31:37 +01002496 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002499 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002501#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 if( mode == MBEDTLS_MODE_STREAM ||
2503 ( mode == MBEDTLS_MODE_CBC
2504#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002505 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506#endif
2507 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 {
Hanno Becker3307b532017-12-27 21:37:21 +00002509 if( post_avail < transform->maclen )
2510 {
2511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2513 }
2514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002516 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2517 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002518 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002519 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002520 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2521 data, rec->data_len, rec->ctr, rec->type, mac );
2522 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002523 }
2524 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2527 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002528 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2529 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002530 {
Hanno Becker992b6872017-11-09 18:57:39 +00002531 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2532
Hanno Beckere83efe62019-04-29 13:52:53 +01002533 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002534
Hanno Becker3307b532017-12-27 21:37:21 +00002535 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002536 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002537 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2538 data, rec->data_len );
2539 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2540 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2541
2542 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002543 }
2544 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002545#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002549 }
2550
Hanno Becker3307b532017-12-27 21:37:21 +00002551 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2552 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002553
Hanno Becker3307b532017-12-27 21:37:21 +00002554 rec->data_len += transform->maclen;
2555 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002556 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002557 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002558#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002560 /*
2561 * Encrypt
2562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2564 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002566 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002567 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002569 "including %d bytes of padding",
2570 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Hanno Becker3307b532017-12-27 21:37:21 +00002572 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2573 transform->iv_enc, transform->ivlen,
2574 data, rec->data_len,
2575 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002578 return( ret );
2579 }
2580
Hanno Becker3307b532017-12-27 21:37:21 +00002581 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
Paul Bakker68884e32013-01-07 18:20:04 +01002587 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002590#if defined(MBEDTLS_GCM_C) || \
2591 defined(MBEDTLS_CCM_C) || \
2592 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002594 mode == MBEDTLS_MODE_CCM ||
2595 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002596 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002597 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002598 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002599 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002600
Hanno Becker3307b532017-12-27 21:37:21 +00002601 /* Check that there's space for both the authentication tag
2602 * and the explicit IV before and after the record content. */
2603 if( post_avail < transform->taglen ||
2604 rec->data_offset < explicit_iv_len )
2605 {
2606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2607 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2608 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002609
Paul Bakker68884e32013-01-07 18:20:04 +01002610 /*
2611 * Generate IV
2612 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2614 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002615 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002617 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2618 explicit_iv_len );
2619 /* Prefix record content with explicit IV. */
2620 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 }
2622 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2623 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002624 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625 unsigned char i;
2626
2627 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2628
2629 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002630 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002631 }
2632 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002633 {
2634 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002637 }
2638
Hanno Beckere83efe62019-04-29 13:52:53 +01002639 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002640
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002641 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2642 iv, transform->ivlen );
2643 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002644 data - explicit_iv_len, explicit_iv_len );
2645 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002646 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002648 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002649 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002650
Paul Bakker68884e32013-01-07 18:20:04 +01002651 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002652 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002653 */
Hanno Becker3307b532017-12-27 21:37:21 +00002654
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002655 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002656 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002657 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002658 data, rec->data_len, /* source */
2659 data, &rec->data_len, /* destination */
2660 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002663 return( ret );
2664 }
2665
Hanno Becker3307b532017-12-27 21:37:21 +00002666 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2667 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002668
Hanno Becker3307b532017-12-27 21:37:21 +00002669 rec->data_len += transform->taglen + explicit_iv_len;
2670 rec->data_offset -= explicit_iv_len;
2671 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002672 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2676#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002677 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002680 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002681 size_t padlen, i;
2682 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002683
Hanno Becker3307b532017-12-27 21:37:21 +00002684 /* Currently we're always using minimal padding
2685 * (up to 255 bytes would be allowed). */
2686 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2687 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 padlen = 0;
2689
Hanno Becker3307b532017-12-27 21:37:21 +00002690 /* Check there's enough space in the buffer for the padding. */
2691 if( post_avail < padlen + 1 )
2692 {
2693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2694 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2695 }
2696
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002698 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Hanno Becker3307b532017-12-27 21:37:21 +00002700 rec->data_len += padlen + 1;
2701 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002704 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002705 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2706 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002707 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002708 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2709 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002710 {
Hanno Becker3307b532017-12-27 21:37:21 +00002711 if( f_rng == NULL )
2712 {
2713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2715 }
2716
2717 if( rec->data_offset < transform->ivlen )
2718 {
2719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2720 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2721 }
2722
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002723 /*
2724 * Generate IV
2725 */
Hanno Becker3307b532017-12-27 21:37:21 +00002726 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002727 if( ret != 0 )
2728 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002729
Hanno Becker3307b532017-12-27 21:37:21 +00002730 memcpy( data - transform->ivlen, transform->iv_enc,
2731 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002732
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002737 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002738 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002739 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Hanno Becker3307b532017-12-27 21:37:21 +00002741 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2742 transform->iv_enc,
2743 transform->ivlen,
2744 data, rec->data_len,
2745 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002748 return( ret );
2749 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002750
Hanno Becker3307b532017-12-27 21:37:21 +00002751 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002755 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002758 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2759 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002760 {
2761 /*
2762 * Save IV in SSL3 and TLS1
2763 */
Hanno Becker3307b532017-12-27 21:37:21 +00002764 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2765 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 }
Hanno Becker3307b532017-12-27 21:37:21 +00002767 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002768#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002769 {
2770 data -= transform->ivlen;
2771 rec->data_offset -= transform->ivlen;
2772 rec->data_len += transform->ivlen;
2773 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002776 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002778 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2779
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780 /*
2781 * MAC(MAC_write_key, seq_num +
2782 * TLSCipherText.type +
2783 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002784 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 * IV + // except for TLS 1.0
2786 * ENC(content + padding + padding_length));
2787 */
Hanno Becker3307b532017-12-27 21:37:21 +00002788
2789 if( post_avail < transform->maclen)
2790 {
2791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2792 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2793 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002794
Hanno Beckere83efe62019-04-29 13:52:53 +01002795 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002798 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002799 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002800
Hanno Becker3307b532017-12-27 21:37:21 +00002801 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002802 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002803 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2804 data, rec->data_len );
2805 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2806 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002807
Hanno Becker3307b532017-12-27 21:37:21 +00002808 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002809
Hanno Becker3307b532017-12-27 21:37:21 +00002810 rec->data_len += transform->maclen;
2811 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002812 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002816 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002818 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002822 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002824 /* Make extra sure authentication was performed, exactly once */
2825 if( auth_done != 1 )
2826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002829 }
2830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
2833 return( 0 );
2834}
2835
Hanno Becker40478be2019-07-12 08:23:59 +01002836int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002837 mbedtls_ssl_transform *transform,
2838 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002839{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002842 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002843#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002844 size_t padlen = 0, correct = 1;
2845#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002846 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002847 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002848 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002849
Hanno Becker611a83b2018-01-03 14:27:32 +00002850#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002851 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002852 ((void) ssl);
2853#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002856 if( rec == NULL ||
2857 rec->buf == NULL ||
2858 rec->buf_len < rec->data_offset ||
2859 rec->buf_len - rec->data_offset < rec->data_len )
2860 {
2861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002863 }
2864
Hanno Becker4c6876b2017-12-27 21:28:58 +00002865 data = rec->buf + rec->data_offset;
2866 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Hanno Beckera5a2b082019-05-15 14:03:01 +01002868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002869 /*
2870 * Match record's CID with incoming CID.
2871 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002872 if( rec->cid_len != transform->in_cid_len ||
2873 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2874 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002875 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002876 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002877#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2880 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002881 {
2882 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002883 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2884 transform->iv_dec,
2885 transform->ivlen,
2886 data, rec->data_len,
2887 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002890 return( ret );
2891 }
2892
Hanno Becker4c6876b2017-12-27 21:28:58 +00002893 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002897 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
Paul Bakker68884e32013-01-07 18:20:04 +01002899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002901#if defined(MBEDTLS_GCM_C) || \
2902 defined(MBEDTLS_CCM_C) || \
2903 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002905 mode == MBEDTLS_MODE_CCM ||
2906 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002907 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002908 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002910
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002911 /*
2912 * Prepare IV from explicit and implicit data.
2913 */
2914
2915 /* Check that there's enough space for the explicit IV
2916 * (at the beginning of the record) and the MAC (at the
2917 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002918 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002921 "+ taglen (%d)", rec->data_len,
2922 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002924 }
Paul Bakker68884e32013-01-07 18:20:04 +01002925
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002926#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002927 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2928 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002929 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002930
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002931 /* Fixed */
2932 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2933 /* Explicit */
2934 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002935 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002936 else
2937#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2938#if defined(MBEDTLS_CHACHAPOLY_C)
2939 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002940 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002941 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002942 unsigned char i;
2943
2944 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2945
2946 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002948 }
2949 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002950#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002951 {
2952 /* Reminder if we ever add an AEAD mode with a different size */
2953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2955 }
2956
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002957 /* Group changes to data, data_len, and add_data, because
2958 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002959 data += explicit_iv_len;
2960 rec->data_offset += explicit_iv_len;
2961 rec->data_len -= explicit_iv_len + transform->taglen;
2962
Hanno Beckere83efe62019-04-29 13:52:53 +01002963 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002964 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002965 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002967 /* Because of the check above, we know that there are
2968 * explicit_iv_len Bytes preceeding data, and taglen
2969 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002970 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002971 * mbedtls_cipher_auth_decrypt() below. */
2972
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002973 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002974 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002975 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002976
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002977 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002978 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002979 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002980 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2981 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002982 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002983 data, rec->data_len,
2984 data, &olen,
2985 data + rec->data_len,
2986 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2991 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002992
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002993 return( ret );
2994 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002995 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002997 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002998 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003002 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3006#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003007 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003010 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003011
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 /*
Paul Bakker45829992013-01-03 14:52:21 +01003013 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003016 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3017 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003018 {
3019 /* The ciphertext is prefixed with the CBC IV. */
3020 minlen += transform->ivlen;
3021 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003022#endif
Paul Bakker45829992013-01-03 14:52:21 +01003023
Hanno Becker4c6876b2017-12-27 21:28:58 +00003024 /* Size considerations:
3025 *
3026 * - The CBC cipher text must not be empty and hence
3027 * at least of size transform->ivlen.
3028 *
3029 * Together with the potential IV-prefix, this explains
3030 * the first of the two checks below.
3031 *
3032 * - The record must contain a MAC, either in plain or
3033 * encrypted, depending on whether Encrypt-then-MAC
3034 * is used or not.
3035 * - If it is, the message contains the IV-prefix,
3036 * the CBC ciphertext, and the MAC.
3037 * - If it is not, the padded plaintext, and hence
3038 * the CBC ciphertext, has at least length maclen + 1
3039 * because there is at least the padding length byte.
3040 *
3041 * As the CBC ciphertext is not empty, both cases give the
3042 * lower bound minlen + maclen + 1 on the record size, which
3043 * we test for in the second check below.
3044 */
3045 if( rec->data_len < minlen + transform->ivlen ||
3046 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049 "+ 1 ) ( + expl IV )", rec->data_len,
3050 transform->ivlen,
3051 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003053 }
3054
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003055 /*
3056 * Authenticate before decrypt if enabled
3057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003059 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003060 {
Hanno Becker992b6872017-11-09 18:57:39 +00003061 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003064
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003065 /* Update data_len in tandem with add_data.
3066 *
3067 * The subtraction is safe because of the previous check
3068 * data_len >= minlen + maclen + 1.
3069 *
3070 * Afterwards, we know that data + data_len is followed by at
3071 * least maclen Bytes, which justifies the call to
3072 * mbedtls_ssl_safer_memcmp() below.
3073 *
3074 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003076 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003077
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003078 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003079 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3080 add_data_len );
3081 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3082 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003083 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3084 data, rec->data_len );
3085 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3086 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003087
Hanno Becker4c6876b2017-12-27 21:28:58 +00003088 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3089 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003090 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003091 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003092
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003093 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003094 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3095 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003099 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003100 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003101 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003103
3104 /*
3105 * Check length sanity
3106 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003107
3108 /* We know from above that data_len > minlen >= 0,
3109 * so the following check in particular implies that
3110 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003111 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003114 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003116 }
3117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003119 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003121 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003122 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3123 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003124 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003125 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003126 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003127
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 data += transform->ivlen;
3129 rec->data_offset += transform->ivlen;
3130 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003131 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003133
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003134 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3135
Hanno Becker4c6876b2017-12-27 21:28:58 +00003136 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3137 transform->iv_dec, transform->ivlen,
3138 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003141 return( ret );
3142 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003143
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003144 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003145 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003149 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003152 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3153 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003154 {
3155 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003156 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3157 * records is equivalent to CBC decryption of the concatenation
3158 * of the records; in other words, IVs are maintained across
3159 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003160 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003161 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3162 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
Hanno Becker4c6876b2017-12-27 21:28:58 +00003166 /* Safe since data_len >= minlen + maclen + 1, so after having
3167 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003168 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3169 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003170 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003171
Hanno Becker4c6876b2017-12-27 21:28:58 +00003172 if( auth_done == 1 )
3173 {
3174 correct *= ( rec->data_len >= padlen + 1 );
3175 padlen *= ( rec->data_len >= padlen + 1 );
3176 }
3177 else
Paul Bakker45829992013-01-03 14:52:21 +01003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180 if( rec->data_len < transform->maclen + padlen + 1 )
3181 {
3182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3183 rec->data_len,
3184 transform->maclen,
3185 padlen + 1 ) );
3186 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003187#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188
3189 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3190 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Hanno Becker4c6876b2017-12-27 21:28:58 +00003193 padlen++;
3194
3195 /* Regardless of the validity of the padding,
3196 * we have data_len >= padlen here. */
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003199 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3200 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003202 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#if defined(MBEDTLS_SSL_DEBUG_ALL)
3205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003206 "should be no more than %d",
3207 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003208#endif
Paul Bakker45829992013-01-03 14:52:21 +01003209 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 }
3211 }
3212 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3214#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3215 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003216 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3217 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003219 /* The padding check involves a series of up to 256
3220 * consecutive memory reads at the end of the record
3221 * plaintext buffer. In order to hide the length and
3222 * validity of the padding, always perform exactly
3223 * `min(256,plaintext_len)` reads (but take into account
3224 * only the last `padlen` bytes for the padding check). */
3225 size_t pad_count = 0;
3226 size_t real_count = 0;
3227 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003228
Hanno Becker4c6876b2017-12-27 21:28:58 +00003229 /* Index of first padding byte; it has been ensured above
3230 * that the subtraction is safe. */
3231 size_t const padding_idx = rec->data_len - padlen;
3232 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3233 size_t const start_idx = rec->data_len - num_checks;
3234 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003235
Hanno Becker4c6876b2017-12-27 21:28:58 +00003236 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003237 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003238 real_count |= ( idx >= padding_idx );
3239 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003240 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003241 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003244 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003246#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003247 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3251 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003255 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003256
Hanno Becker4c6876b2017-12-27 21:28:58 +00003257 /* If the padding was found to be invalid, padlen == 0
3258 * and the subtraction is safe. If the padding was found valid,
3259 * padlen hasn't been changed and the previous assertion
3260 * data_len >= padlen still holds. */
3261 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003265 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003270
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003271#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003273 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003275
3276 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003277 * Authenticate if not done yet.
3278 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003280#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003281 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003282 {
Hanno Becker992b6872017-11-09 18:57:39 +00003283 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003284
Hanno Becker4c6876b2017-12-27 21:28:58 +00003285 /* If the initial value of padlen was such that
3286 * data_len < maclen + padlen + 1, then padlen
3287 * got reset to 1, and the initial check
3288 * data_len >= minlen + maclen + 1
3289 * guarantees that at this point we still
3290 * have at least data_len >= maclen.
3291 *
3292 * If the initial value of padlen was such that
3293 * data_len >= maclen + padlen + 1, then we have
3294 * subtracted either padlen + 1 (if the padding was correct)
3295 * or 0 (if the padding was incorrect) since then,
3296 * hence data_len >= maclen in any case.
3297 */
3298 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003299 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003302 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3303 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003304 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003305 ssl_mac( &transform->md_ctx_dec,
3306 transform->mac_dec,
3307 data, rec->data_len,
3308 rec->ctr, rec->type,
3309 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003310 }
3311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3313#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3314 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003315 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3316 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003317 {
3318 /*
3319 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003320 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003321 *
3322 * Known timing attacks:
3323 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3324 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003325 * To compensate for different timings for the MAC calculation
3326 * depending on how much padding was removed (which is determined
3327 * by padlen), process extra_run more blocks through the hash
3328 * function.
3329 *
3330 * The formula in the paper is
3331 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3332 * where L1 is the size of the header plus the decrypted message
3333 * plus CBC padding and L2 is the size of the header plus the
3334 * decrypted message. This is for an underlying hash function
3335 * with 64-byte blocks.
3336 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3337 * correctly. We round down instead of up, so -56 is the correct
3338 * value for our calculations instead of -55.
3339 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003340 * Repeat the formula rather than defining a block_size variable.
3341 * This avoids requiring division by a variable at runtime
3342 * (which would be marginally less efficient and would require
3343 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003344 */
3345 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003346 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003347
3348 /*
3349 * The next two sizes are the minimum and maximum values of
3350 * in_msglen over all padlen values.
3351 *
3352 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003353 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003354 *
3355 * Note that max_len + maclen is never more than the buffer
3356 * length, as we previously did in_msglen -= maclen too.
3357 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003358 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003359 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3360
Hanno Becker4c6876b2017-12-27 21:28:58 +00003361 memset( tmp, 0, sizeof( tmp ) );
3362
3363 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003364 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003365#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3366 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003367 case MBEDTLS_MD_MD5:
3368 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003369 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003371 extra_run =
3372 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3373 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003374 break;
3375#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003376#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003377 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003379 extra_run =
3380 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3381 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003382 break;
3383#endif
3384 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3387 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003388
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003389 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003390
Hanno Beckere83efe62019-04-29 13:52:53 +01003391 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3392 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003393 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3394 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003395 /* Make sure we access everything even when padlen > 0. This
3396 * makes the synchronisation requirements for just-in-time
3397 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003398 ssl_read_memory( data + rec->data_len, padlen );
3399 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003400
3401 /* Call mbedtls_md_process at least once due to cache attacks
3402 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003403 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003404 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003405
Hanno Becker4c6876b2017-12-27 21:28:58 +00003406 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003407
3408 /* Make sure we access all the memory that could contain the MAC,
3409 * before we check it in the next code block. This makes the
3410 * synchronisation requirements for just-in-time Prime+Probe
3411 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 ssl_read_memory( data + min_len,
3413 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003414 }
3415 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3417 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003422
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003423#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003424 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3425 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003426#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003427
Hanno Becker4c6876b2017-12-27 21:28:58 +00003428 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3429 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431#if defined(MBEDTLS_SSL_DEBUG_ALL)
3432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003433#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003434 correct = 0;
3435 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003436 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003437 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003438
3439 /*
3440 * Finally check the correct flag
3441 */
3442 if( correct == 0 )
3443 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003444#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003445
3446 /* Make extra sure authentication was performed, exactly once */
3447 if( auth_done != 1 )
3448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
Hanno Beckera5a2b082019-05-15 14:03:01 +01003453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003454 if( rec->cid_len != 0 )
3455 {
3456 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3457 &rec->type );
3458 if( ret != 0 )
3459 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3460 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003461#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464
3465 return( 0 );
3466}
3467
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003468#undef MAC_NONE
3469#undef MAC_PLAINTEXT
3470#undef MAC_CIPHERTEXT
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473/*
3474 * Compression/decompression functions
3475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477{
3478 int ret;
3479 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003480 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003482 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003486 if( len_pre == 0 )
3487 return( 0 );
3488
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489 memcpy( msg_pre, ssl->out_msg, len_pre );
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003492 ssl->out_msglen ) );
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 ssl->out_msg, ssl->out_msglen );
3496
Paul Bakker48916f92012-09-16 19:57:18 +00003497 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3498 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3499 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003500 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501
Paul Bakker48916f92012-09-16 19:57:18 +00003502 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503 if( ret != Z_OK )
3504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3506 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507 }
3508
Angus Grattond8213d02016-05-25 20:56:48 +10003509 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003510 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003513 ssl->out_msglen ) );
3514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 ssl->out_msg, ssl->out_msglen );
3517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003519
3520 return( 0 );
3521}
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524{
3525 int ret;
3526 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003527 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003528 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003529 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003533 if( len_pre == 0 )
3534 return( 0 );
3535
Paul Bakker2770fbd2012-07-03 13:30:23 +00003536 memcpy( msg_pre, ssl->in_msg, len_pre );
3537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539 ssl->in_msglen ) );
3540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003542 ssl->in_msg, ssl->in_msglen );
3543
Paul Bakker48916f92012-09-16 19:57:18 +00003544 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3545 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3546 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003547 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003548 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549
Paul Bakker48916f92012-09-16 19:57:18 +00003550 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003551 if( ret != Z_OK )
3552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3554 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555 }
3556
Angus Grattond8213d02016-05-25 20:56:48 +10003557 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003558 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 ssl->in_msglen ) );
3562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003564 ssl->in_msg, ssl->in_msglen );
3565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
3568 return( 0 );
3569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3573static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_PROTO_DTLS)
3576static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003577{
3578 /* If renegotiation is not enforced, retransmit until we would reach max
3579 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003580 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003581 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003582 uint32_t ratio =
3583 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3584 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003585 unsigned char doublings = 1;
3586
3587 while( ratio != 0 )
3588 {
3589 ++doublings;
3590 ratio >>= 1;
3591 }
3592
3593 if( ++ssl->renego_records_seen > doublings )
3594 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003596 return( 0 );
3597 }
3598 }
3599
3600 return( ssl_write_hello_request( ssl ) );
3601}
3602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003604
Paul Bakker5121ce52009-01-03 21:22:43 +00003605/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003606 * Fill the input message buffer by appending data to it.
3607 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003608 *
3609 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3610 * available (from this read and/or a previous one). Otherwise, an error code
3611 * is returned (possibly EOF or WANT_READ).
3612 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003613 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3614 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3615 * since we always read a whole datagram at once.
3616 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003617 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003618 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621{
Paul Bakker23986e52011-04-24 08:57:21 +00003622 int ret;
3623 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Hanno Beckera58a8962019-06-13 16:11:15 +01003627 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3628 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003631 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003633 }
3634
Angus Grattond8213d02016-05-25 20:56:48 +10003635 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003639 }
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003642 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003644 uint32_t timeout;
3645
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003646 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003647 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3648 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003649 {
3650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3651 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3653 }
3654
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003655 /*
3656 * The point is, we need to always read a full datagram at once, so we
3657 * sometimes read more then requested, and handle the additional data.
3658 * It could be the rest of the current record (while fetching the
3659 * header) and/or some other records in the same datagram.
3660 */
3661
3662 /*
3663 * Move to the next record in the already read datagram if applicable
3664 */
3665 if( ssl->next_record_offset != 0 )
3666 {
3667 if( ssl->in_left < ssl->next_record_offset )
3668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003671 }
3672
3673 ssl->in_left -= ssl->next_record_offset;
3674
3675 if( ssl->in_left != 0 )
3676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003678 ssl->next_record_offset ) );
3679 memmove( ssl->in_hdr,
3680 ssl->in_hdr + ssl->next_record_offset,
3681 ssl->in_left );
3682 }
3683
3684 ssl->next_record_offset = 0;
3685 }
3686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003689
3690 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003691 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003692 */
3693 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003696 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003697 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003698
3699 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003700 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003701 * are not at the beginning of a new record, the caller did something
3702 * wrong.
3703 */
3704 if( ssl->in_left != 0 )
3705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003708 }
3709
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003710 /*
3711 * Don't even try to read if time's out already.
3712 * This avoids by-passing the timer when repeatedly receiving messages
3713 * that will end up being dropped.
3714 */
3715 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003718 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003719 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003720 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003721 {
Angus Grattond8213d02016-05-25 20:56:48 +10003722 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003725 timeout = ssl->handshake->retransmit_timeout;
3726 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003727 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003730
Hanno Beckera58a8962019-06-13 16:11:15 +01003731 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3732 {
3733 ret = mbedtls_ssl_get_recv_timeout( ssl )
3734 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3735 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003736 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003737 {
3738 ret = mbedtls_ssl_get_recv( ssl )
3739 ( ssl->p_bio, ssl->in_hdr, len );
3740 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003743
3744 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003746 }
3747
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003748 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003751 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003754 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003755 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003758 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003759 }
3760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003764 return( ret );
3765 }
3766
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003767 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003770 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3771 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003773 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003774 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003777 return( ret );
3778 }
3779
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003780 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003783 }
3784
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 if( ret < 0 )
3786 return( ret );
3787
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003788 ssl->in_left = ret;
3789 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003790 MBEDTLS_SSL_TRANSPORT_ELSE
3791#endif /* MBEDTLS_SSL_PROTO_DTLS */
3792#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003795 ssl->in_left, nb_want ) );
3796
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 while( ssl->in_left < nb_want )
3798 {
3799 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003800
3801 if( ssl_check_timer( ssl ) != 0 )
3802 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3803 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003804 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003805 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003806 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003807 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3808 ssl->in_hdr + ssl->in_left, len,
3809 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003810 }
3811 else
3812 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003813 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003814 ssl->in_hdr + ssl->in_left, len );
3815 }
3816 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003819 ssl->in_left, nb_want ) );
3820 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003821
3822 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003824
3825 if( ret < 0 )
3826 return( ret );
3827
mohammad160352aecb92018-03-28 23:41:40 -07003828 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003829 {
Darryl Green11999bb2018-03-13 15:22:58 +00003830 MBEDTLS_SSL_DEBUG_MSG( 1,
3831 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003832 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3834 }
3835
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003836 ssl->in_left += ret;
3837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003839#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843 return( 0 );
3844}
3845
3846/*
3847 * Flush any data not yet written
3848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003850{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003851 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003852 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003855
Hanno Beckera58a8962019-06-13 16:11:15 +01003856 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003859 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003861 }
3862
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003863 /* Avoid incrementing counter if data is flushed */
3864 if( ssl->out_left == 0 )
3865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003867 return( 0 );
3868 }
3869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 while( ssl->out_left > 0 )
3871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003873 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003874
Hanno Becker2b1e3542018-08-06 11:19:13 +01003875 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003876 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003879
3880 if( ret <= 0 )
3881 return( ret );
3882
mohammad160352aecb92018-03-28 23:41:40 -07003883 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003884 {
Darryl Green11999bb2018-03-13 15:22:58 +00003885 MBEDTLS_SSL_DEBUG_MSG( 1,
3886 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003887 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3889 }
3890
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 ssl->out_left -= ret;
3892 }
3893
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003895 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003896 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003897 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003898 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003899 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003900#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003901#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003902 {
3903 ssl->out_hdr = ssl->out_buf + 8;
3904 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003905#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003906 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
3910 return( 0 );
3911}
3912
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003913/*
3914 * Functions to handle the DTLS retransmission state machine
3915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003917/*
3918 * Append current handshake message to current outgoing flight
3919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3924 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3925 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003926
3927 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003928 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003932 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003933 }
3934
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003935 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003939 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003940 }
3941
3942 /* Copy current handshake message with headers */
3943 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3944 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003945 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003946 msg->next = NULL;
3947
3948 /* Append to the current flight */
3949 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003950 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951 else
3952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954 while( cur->next != NULL )
3955 cur = cur->next;
3956 cur->next = msg;
3957 }
3958
Hanno Becker3b235902018-08-06 09:54:53 +01003959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003960 return( 0 );
3961}
3962
3963/*
3964 * Free the current flight of handshake messages
3965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 mbedtls_ssl_flight_item *cur = flight;
3969 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970
3971 while( cur != NULL )
3972 {
3973 next = cur->next;
3974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 mbedtls_free( cur->p );
3976 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977
3978 cur = next;
3979 }
3980}
3981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3983static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003984#endif
3985
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003986/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003987 * Swap transform_out and out_ctr with the alternative ones
3988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003992 unsigned char tmp_out_ctr[8];
3993
3994 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003997 return;
3998 }
3999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004001
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004002 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004003 tmp_transform = ssl->transform_out;
4004 ssl->transform_out = ssl->handshake->alt_transform_out;
4005 ssl->handshake->alt_transform_out = tmp_transform;
4006
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004007 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004008 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4009 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004010 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004011
4012 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004013 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4016 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4021 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004022 }
4023 }
4024#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004025}
4026
4027/*
4028 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004029 */
4030int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4031{
4032 int ret = 0;
4033
4034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4035
4036 ret = mbedtls_ssl_flight_transmit( ssl );
4037
4038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4039
4040 return( ret );
4041}
4042
4043/*
4044 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045 *
4046 * Need to remember the current message in case flush_output returns
4047 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004049 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004050int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004052 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004056 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004058
4059 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004060 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004061 ssl_swap_epochs( ssl );
4062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004064 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004065
4066 while( ssl->handshake->cur_msg != NULL )
4067 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004068 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004069 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004070
Hanno Beckere1dcb032018-08-17 16:47:58 +01004071 int const is_finished =
4072 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4073 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4074
Hanno Becker04da1892018-08-14 13:22:10 +01004075 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4076 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4077
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004078 /* Swap epochs before sending Finished: we can't do it after
4079 * sending ChangeCipherSpec, in case write returns WANT_READ.
4080 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004081 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004082 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004084 ssl_swap_epochs( ssl );
4085 }
4086
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087 ret = ssl_get_remaining_payload_in_datagram( ssl );
4088 if( ret < 0 )
4089 return( ret );
4090 max_frag_len = (size_t) ret;
4091
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004092 /* CCS is copied as is, while HS messages may need fragmentation */
4093 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4094 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004095 if( max_frag_len == 0 )
4096 {
4097 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4098 return( ret );
4099
4100 continue;
4101 }
4102
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004103 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004104 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004105 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004106
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004107 /* Update position inside current message */
4108 ssl->handshake->cur_msg_p += cur->len;
4109 }
4110 else
4111 {
4112 const unsigned char * const p = ssl->handshake->cur_msg_p;
4113 const size_t hs_len = cur->len - 12;
4114 const size_t frag_off = p - ( cur->p + 12 );
4115 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004116 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004117
Hanno Beckere1dcb032018-08-17 16:47:58 +01004118 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004119 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004120 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004121 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004122
Hanno Becker67bc7c32018-08-06 11:33:50 +01004123 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4124 return( ret );
4125
4126 continue;
4127 }
4128 max_hs_frag_len = max_frag_len - 12;
4129
4130 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4131 max_hs_frag_len : rem_len;
4132
4133 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004134 {
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004136 (unsigned) cur_hs_frag_len,
4137 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004138 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004139
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004140 /* Messages are stored with handshake headers as if not fragmented,
4141 * copy beginning of headers then fill fragmentation fields.
4142 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4143 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004144
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004145 mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4146 mbedtls_platform_put_uint24_be( &ssl->out_msg[9], cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004147
4148 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4149
Hanno Becker3f7b9732018-08-28 09:53:25 +01004150 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004151 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4152 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004153 ssl->out_msgtype = cur->type;
4154
4155 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004156 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004157 }
4158
4159 /* If done with the current message move to the next one if any */
4160 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4161 {
4162 if( cur->next != NULL )
4163 {
4164 ssl->handshake->cur_msg = cur->next;
4165 ssl->handshake->cur_msg_p = cur->next->p + 12;
4166 }
4167 else
4168 {
4169 ssl->handshake->cur_msg = NULL;
4170 ssl->handshake->cur_msg_p = NULL;
4171 }
4172 }
4173
4174 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004175 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004178 return( ret );
4179 }
4180 }
4181
Hanno Becker67bc7c32018-08-06 11:33:50 +01004182 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4183 return( ret );
4184
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004185 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4187 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004188 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004191 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4192 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004193
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004195
4196 return( 0 );
4197}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004198
4199/*
4200 * To be called when the last message of an incoming flight is received.
4201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004203{
4204 /* We won't need to resend that one any more */
4205 ssl_flight_free( ssl->handshake->flight );
4206 ssl->handshake->flight = NULL;
4207 ssl->handshake->cur_msg = NULL;
4208
4209 /* The next incoming flight will start with this msg_seq */
4210 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4211
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004212 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004213 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004214
Hanno Becker0271f962018-08-16 13:23:47 +01004215 /* Clear future message buffering structure. */
4216 ssl_buffering_free( ssl );
4217
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004218 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004219 ssl_set_timer( ssl, 0 );
4220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4222 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004225 }
4226 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004228}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004229
4230/*
4231 * To be called when the last message of an outgoing flight is send.
4232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004234{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004235 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004236 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4239 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242 }
4243 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004247
Paul Bakker5121ce52009-01-03 21:22:43 +00004248/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004249 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004251
4252/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004253 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004254 *
4255 * - fill in handshake headers
4256 * - update handshake checksum
4257 * - DTLS: save message for resending
4258 * - then pass to the record layer
4259 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004260 * DTLS: except for HelloRequest, messages are only queued, and will only be
4261 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004262 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004263 * Inputs:
4264 * - ssl->out_msglen: 4 + actual handshake message len
4265 * (4 is the size of handshake headers for TLS)
4266 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4267 * - ssl->out_msg + 4: the handshake message body
4268 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004269 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004270 * - ssl->out_msglen: the length of the record contents
4271 * (including handshake headers but excluding record headers)
4272 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004273 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004274int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004275{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004276 int ret;
4277 const size_t hs_len = ssl->out_msglen - 4;
4278 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4281
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004282 /*
4283 * Sanity checks
4284 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004285 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004286 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4287 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004288 /* In SSLv3, the client might send a NoCertificate alert. */
4289#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004290 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004291 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004292 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4293 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004294#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4295 {
4296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4298 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004301 /* Whenever we send anything different from a
4302 * HelloRequest we should be in a handshake - double check. */
4303 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4304 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004305 ssl->handshake == NULL )
4306 {
4307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004312 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004313 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004315 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004318 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004319#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004320
Hanno Beckerb50a2532018-08-06 11:52:54 +01004321 /* Double-check that we did not exceed the bounds
4322 * of the outgoing record buffer.
4323 * This should never fail as the various message
4324 * writing functions must obey the bounds of the
4325 * outgoing record buffer, but better be safe.
4326 *
4327 * Note: We deliberately do not check for the MTU or MFL here.
4328 */
4329 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4330 {
4331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4332 "size %u, maximum %u",
4333 (unsigned) ssl->out_msglen,
4334 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4335 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4336 }
4337
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004338 /*
4339 * Fill handshake headers
4340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004343 mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004344
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004345 /*
4346 * DTLS has additional fields in the Handshake layer,
4347 * between the length field and the actual payload:
4348 * uint16 message_seq;
4349 * uint24 fragment_offset;
4350 * uint24 fragment_length;
4351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004353 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004354 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004355 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004356 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004357 {
4358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4359 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004360 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004361 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4363 }
4364
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004365 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004366 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004367
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004368 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004369 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004370 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004371 mbedtls_platform_put_uint16_be( &ssl->out_msg[4], ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004372 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004373 }
4374 else
4375 {
4376 ssl->out_msg[4] = 0;
4377 ssl->out_msg[5] = 0;
4378 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004379
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 /* Handshake hashes are computed without fragmentation,
4381 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004382 memset( ssl->out_msg + 6, 0x00, 3 );
4383 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004384 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004386
Hanno Becker0207e532018-08-28 10:28:28 +01004387 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004388 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004389 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
4391
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004392 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004394 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004395 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4396 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004397 {
4398 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004401 return( ret );
4402 }
4403 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004404 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004405#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004406 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004407 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004408 {
4409 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4410 return( ret );
4411 }
4412 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004413
4414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4415
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004416 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004417}
4418
4419/*
4420 * Record layer functions
4421 */
4422
4423/*
4424 * Write current record.
4425 *
4426 * Uses:
4427 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4428 * - ssl->out_msglen: length of the record content (excl headers)
4429 * - ssl->out_msg: record content
4430 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004432{
4433 int ret, done = 0;
4434 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004435 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004440 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004442 {
4443 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004446 return( ret );
4447 }
4448
4449 len = ssl->out_msglen;
4450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4454 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 ret = mbedtls_ssl_hw_record_write( ssl );
4459 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4462 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004463 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004464
4465 if( ret == 0 )
4466 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004469 if( !done )
4470 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004471 unsigned i;
4472 size_t protected_record_size;
4473
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004474 /* Skip writing the record content type to after the encryption,
4475 * as it may change when using the CID extension. */
4476
Hanno Becker2881d802019-05-22 14:44:53 +01004477 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4478 mbedtls_ssl_get_minor_ver( ssl ),
4479 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004480
Hanno Becker19859472018-08-06 09:40:20 +01004481 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004482 mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004483
Paul Bakker48916f92012-09-16 19:57:18 +00004484 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004485 {
Hanno Becker3307b532017-12-27 21:37:21 +00004486 mbedtls_record rec;
4487
4488 rec.buf = ssl->out_iv;
4489 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4490 ( ssl->out_iv - ssl->out_buf );
4491 rec.data_len = ssl->out_msglen;
4492 rec.data_offset = ssl->out_msg - rec.buf;
4493
4494 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004495 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4496 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004497 ssl->conf->transport, rec.ver );
4498 rec.type = ssl->out_msgtype;
4499
Hanno Beckera5a2b082019-05-15 14:03:01 +01004500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004501 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004502 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004503#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004504
Hanno Becker611a83b2018-01-03 14:27:32 +00004505 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004506 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004507 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004510 return( ret );
4511 }
4512
Hanno Becker3307b532017-12-27 21:37:21 +00004513 if( rec.data_offset != 0 )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4517 }
4518
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004519 /* Update the record content type and CID. */
4520 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004521#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004522 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004523#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004524 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004525 mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004526 }
4527
Hanno Becker43395762019-05-03 14:46:38 +01004528 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004529
4530#if defined(MBEDTLS_SSL_PROTO_DTLS)
4531 /* In case of DTLS, double-check that we don't exceed
4532 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004533 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004534 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004535 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004536 if( ret < 0 )
4537 return( ret );
4538
4539 if( protected_record_size > (size_t) ret )
4540 {
4541 /* Should never happen */
4542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4543 }
4544 }
4545#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004546
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004547 /* Now write the potentially updated record content type. */
4548 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004551 "version = [%d:%d], msglen = %d",
4552 ssl->out_hdr[0], ssl->out_hdr[1],
4553 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004556 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004557
4558 ssl->out_left += protected_record_size;
4559 ssl->out_hdr += protected_record_size;
4560 ssl_update_out_pointers( ssl, ssl->transform_out );
4561
Hanno Becker04484622018-08-06 09:49:38 +01004562 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4563 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4564 break;
4565
4566 /* The loop goes to its end iff the counter is wrapping */
4567 if( i == ssl_ep_len( ssl ) )
4568 {
4569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4570 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 }
4573
Hanno Becker67bc7c32018-08-06 11:33:50 +01004574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004575 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004576 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004577 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004578 size_t remaining;
4579 ret = ssl_get_remaining_payload_in_datagram( ssl );
4580 if( ret < 0 )
4581 {
4582 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4583 ret );
4584 return( ret );
4585 }
4586
4587 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004588 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004589 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004590 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004591 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004592 else
4593 {
Hanno Becker513815a2018-08-20 11:56:09 +01004594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595 }
4596 }
4597#endif /* MBEDTLS_SSL_PROTO_DTLS */
4598
4599 if( ( flush == SSL_FORCE_FLUSH ) &&
4600 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 return( ret );
4604 }
4605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004607
4608 return( 0 );
4609}
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004612
4613static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4614{
4615 if( ssl->in_msglen < ssl->in_hslen ||
4616 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4617 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4618 {
4619 return( 1 );
4620 }
4621 return( 0 );
4622}
Hanno Becker44650b72018-08-16 12:51:11 +01004623
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004624static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004625{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004626 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004627}
4628
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004629static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004630{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004631 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004632}
4633
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004634static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004635{
4636 uint32_t msg_len, frag_off, frag_len;
4637
4638 msg_len = ssl_get_hs_total_len( ssl );
4639 frag_off = ssl_get_hs_frag_off( ssl );
4640 frag_len = ssl_get_hs_frag_len( ssl );
4641
4642 if( frag_off > msg_len )
4643 return( -1 );
4644
4645 if( frag_len > msg_len - frag_off )
4646 return( -1 );
4647
4648 if( frag_len + 12 > ssl->in_msglen )
4649 return( -1 );
4650
4651 return( 0 );
4652}
4653
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004654/*
4655 * Mark bits in bitmask (used for DTLS HS reassembly)
4656 */
4657static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4658{
4659 unsigned int start_bits, end_bits;
4660
4661 start_bits = 8 - ( offset % 8 );
4662 if( start_bits != 8 )
4663 {
4664 size_t first_byte_idx = offset / 8;
4665
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004666 /* Special case */
4667 if( len <= start_bits )
4668 {
4669 for( ; len != 0; len-- )
4670 mask[first_byte_idx] |= 1 << ( start_bits - len );
4671
4672 /* Avoid potential issues with offset or len becoming invalid */
4673 return;
4674 }
4675
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004676 offset += start_bits; /* Now offset % 8 == 0 */
4677 len -= start_bits;
4678
4679 for( ; start_bits != 0; start_bits-- )
4680 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4681 }
4682
4683 end_bits = len % 8;
4684 if( end_bits != 0 )
4685 {
4686 size_t last_byte_idx = ( offset + len ) / 8;
4687
4688 len -= end_bits; /* Now len % 8 == 0 */
4689
4690 for( ; end_bits != 0; end_bits-- )
4691 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4692 }
4693
4694 memset( mask + offset / 8, 0xFF, len / 8 );
4695}
4696
4697/*
4698 * Check that bitmask is full
4699 */
4700static int ssl_bitmask_check( unsigned char *mask, size_t len )
4701{
4702 size_t i;
4703
4704 for( i = 0; i < len / 8; i++ )
4705 if( mask[i] != 0xFF )
4706 return( -1 );
4707
4708 for( i = 0; i < len % 8; i++ )
4709 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4710 return( -1 );
4711
4712 return( 0 );
4713}
4714
Hanno Becker56e205e2018-08-16 09:06:12 +01004715/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004716static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004717 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004718{
Hanno Becker56e205e2018-08-16 09:06:12 +01004719 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004720
Hanno Becker56e205e2018-08-16 09:06:12 +01004721 alloc_len = 12; /* Handshake header */
4722 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004723
Hanno Beckerd07df862018-08-16 09:14:58 +01004724 if( add_bitmap )
4725 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004726
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004727 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004728}
Hanno Becker56e205e2018-08-16 09:06:12 +01004729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004731
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004732static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004733{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004734 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004735}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004736
Simon Butcher99000142016-10-13 17:21:01 +01004737int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004742 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004744 }
4745
Hanno Becker12555c62018-08-16 12:47:53 +01004746 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004749 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004750 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004753 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004754 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004755 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004756 unsigned int recv_msg_seq = (unsigned int)
4757 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004758
Hanno Becker44650b72018-08-16 12:51:11 +01004759 if( ssl_check_hs_header( ssl ) != 0 )
4760 {
4761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4762 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4763 }
4764
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004765 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004766 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4767 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4768 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4769 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004770 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004771 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4774 recv_msg_seq,
4775 ssl->handshake->in_msg_seq ) );
4776 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4777 }
4778
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004779 /* Retransmit only on last message from previous flight, to avoid
4780 * too many retransmissions.
4781 * Besides, No sane server ever retransmits HelloVerifyRequest */
4782 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004786 "message_seq = %d, start_of_flight = %d",
4787 recv_msg_seq,
4788 ssl->handshake->in_flight_start_seq ) );
4789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004793 return( ret );
4794 }
4795 }
4796 else
4797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004799 "message_seq = %d, expected = %d",
4800 recv_msg_seq,
4801 ssl->handshake->in_msg_seq ) );
4802 }
4803
Hanno Becker90333da2017-10-10 11:27:13 +01004804 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004805 }
4806 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004807
Hanno Becker6d97ef52018-08-16 13:09:04 +01004808 /* Message reassembly is handled alongside buffering of future
4809 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004810 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004811 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004812 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004815 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004816 }
4817 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004818 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004820#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004821 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004822 /* With TLS we don't handle fragmentation (for now) */
4823 if( ssl->in_msglen < ssl->in_hslen )
4824 {
4825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4826 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4827 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004828 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004829#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004830
Simon Butcher99000142016-10-13 17:21:01 +01004831 return( 0 );
4832}
4833
4834void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4835{
Hanno Becker0271f962018-08-16 13:23:47 +01004836 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004837
Hanno Becker0271f962018-08-16 13:23:47 +01004838 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004839 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004840
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004841 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004844 ssl->handshake != NULL )
4845 {
Hanno Becker0271f962018-08-16 13:23:47 +01004846 unsigned offset;
4847 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004848
Hanno Becker0271f962018-08-16 13:23:47 +01004849 /* Increment handshake sequence number */
4850 hs->in_msg_seq++;
4851
4852 /*
4853 * Clear up handshake buffering and reassembly structure.
4854 */
4855
4856 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004857 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004858
4859 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004860 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4861 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004862 offset++, hs_buf++ )
4863 {
4864 *hs_buf = *(hs_buf + 1);
4865 }
4866
4867 /* Create a fresh last entry */
4868 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004869 }
4870#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004871}
4872
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004873/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004874 * DTLS anti-replay: RFC 6347 4.1.2.6
4875 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004876 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4877 * Bit n is set iff record number in_window_top - n has been seen.
4878 *
4879 * Usually, in_window_top is the last record number seen and the lsb of
4880 * in_window is set. The only exception is the initial state (record number 0
4881 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4884static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004885{
4886 ssl->in_window_top = 0;
4887 ssl->in_window = 0;
4888}
4889
4890static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4891{
4892 return( ( (uint64_t) buf[0] << 40 ) |
4893 ( (uint64_t) buf[1] << 32 ) |
4894 ( (uint64_t) buf[2] << 24 ) |
4895 ( (uint64_t) buf[3] << 16 ) |
4896 ( (uint64_t) buf[4] << 8 ) |
4897 ( (uint64_t) buf[5] ) );
4898}
4899
4900/*
4901 * Return 0 if sequence number is acceptable, -1 otherwise
4902 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004903int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004904{
4905 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4906 uint64_t bit;
4907
Hanno Becker7f376f42019-06-12 16:20:48 +01004908 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4909 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4910 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004911 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004912 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004913
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004914 if( rec_seqnum > ssl->in_window_top )
4915 return( 0 );
4916
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004917 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004918
4919 if( bit >= 64 )
4920 return( -1 );
4921
4922 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4923 return( -1 );
4924
4925 return( 0 );
4926}
4927
4928/*
4929 * Update replay window on new validated record
4930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004932{
4933 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4934
Hanno Becker7f376f42019-06-12 16:20:48 +01004935 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4936 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4937 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004938 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004939 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004940
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004941 if( rec_seqnum > ssl->in_window_top )
4942 {
4943 /* Update window_top and the contents of the window */
4944 uint64_t shift = rec_seqnum - ssl->in_window_top;
4945
4946 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004947 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004948 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004949 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004950 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004951 ssl->in_window |= 1;
4952 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953
4954 ssl->in_window_top = rec_seqnum;
4955 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004956 else
4957 {
4958 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004959 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004960
4961 if( bit < 64 ) /* Always true, but be extra sure */
4962 ssl->in_window |= (uint64_t) 1 << bit;
4963 }
4964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004966
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004967#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004968/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004969static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4970
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004971/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004972 * Without any SSL context, check if a datagram looks like a ClientHello with
4973 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004974 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 *
4976 * - if cookie is valid, return 0
4977 * - if ClientHello looks superficially valid but cookie is not,
4978 * fill obuf and set olen, then
4979 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4980 * - otherwise return a specific error code
4981 */
4982static int ssl_check_dtls_clihlo_cookie(
4983 mbedtls_ssl_cookie_write_t *f_cookie_write,
4984 mbedtls_ssl_cookie_check_t *f_cookie_check,
4985 void *p_cookie,
4986 const unsigned char *cli_id, size_t cli_id_len,
4987 const unsigned char *in, size_t in_len,
4988 unsigned char *obuf, size_t buf_len, size_t *olen )
4989{
4990 size_t sid_len, cookie_len;
4991 unsigned char *p;
4992
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004993 /*
4994 * Structure of ClientHello with record and handshake headers,
4995 * and expected values. We don't need to check a lot, more checks will be
4996 * done when actually parsing the ClientHello - skipping those checks
4997 * avoids code duplication and does not make cookie forging any easier.
4998 *
4999 * 0-0 ContentType type; copied, must be handshake
5000 * 1-2 ProtocolVersion version; copied
5001 * 3-4 uint16 epoch; copied, must be 0
5002 * 5-10 uint48 sequence_number; copied
5003 * 11-12 uint16 length; (ignored)
5004 *
5005 * 13-13 HandshakeType msg_type; (ignored)
5006 * 14-16 uint24 length; (ignored)
5007 * 17-18 uint16 message_seq; copied
5008 * 19-21 uint24 fragment_offset; copied, must be 0
5009 * 22-24 uint24 fragment_length; (ignored)
5010 *
5011 * 25-26 ProtocolVersion client_version; (ignored)
5012 * 27-58 Random random; (ignored)
5013 * 59-xx SessionID session_id; 1 byte len + sid_len content
5014 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5015 * ...
5016 *
5017 * Minimum length is 61 bytes.
5018 */
5019 if( in_len < 61 ||
5020 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5021 in[3] != 0 || in[4] != 0 ||
5022 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5023 {
5024 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5025 }
5026
5027 sid_len = in[59];
5028 if( sid_len > in_len - 61 )
5029 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5030
5031 cookie_len = in[60 + sid_len];
5032 if( cookie_len > in_len - 60 )
5033 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5034
5035 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5036 cli_id, cli_id_len ) == 0 )
5037 {
5038 /* Valid cookie */
5039 return( 0 );
5040 }
5041
5042 /*
5043 * If we get here, we've got an invalid cookie, let's prepare HVR.
5044 *
5045 * 0-0 ContentType type; copied
5046 * 1-2 ProtocolVersion version; copied
5047 * 3-4 uint16 epoch; copied
5048 * 5-10 uint48 sequence_number; copied
5049 * 11-12 uint16 length; olen - 13
5050 *
5051 * 13-13 HandshakeType msg_type; hello_verify_request
5052 * 14-16 uint24 length; olen - 25
5053 * 17-18 uint16 message_seq; copied
5054 * 19-21 uint24 fragment_offset; copied
5055 * 22-24 uint24 fragment_length; olen - 25
5056 *
5057 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5058 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5059 *
5060 * Minimum length is 28.
5061 */
5062 if( buf_len < 28 )
5063 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5064
5065 /* Copy most fields and adapt others */
5066 memcpy( obuf, in, 25 );
5067 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5068 obuf[25] = 0xfe;
5069 obuf[26] = 0xff;
5070
5071 /* Generate and write actual cookie */
5072 p = obuf + 28;
5073 if( f_cookie_write( p_cookie,
5074 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5075 {
5076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5077 }
5078
5079 *olen = p - obuf;
5080
5081 /* Go back and fill length fields */
5082 obuf[27] = (unsigned char)( *olen - 28 );
5083
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005084 mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
5085 obuf[22] = obuf[14];
5086 obuf[23] = obuf[15];
5087 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005088
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005089 mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005090
5091 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5092}
5093
5094/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005095 * Handle possible client reconnect with the same UDP quadruplet
5096 * (RFC 6347 Section 4.2.8).
5097 *
5098 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5099 * that looks like a ClientHello.
5100 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005101 * - if the input looks like a ClientHello without cookies,
5102 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005103 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005104 * - if the input looks like a ClientHello with a valid cookie,
5105 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005106 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005107 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005108 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005109 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005110 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5111 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005112 */
5113static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5114{
5115 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005116 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005117
Hanno Becker87b56262019-07-10 14:37:41 +01005118 if( ssl->conf->f_cookie_write == NULL ||
5119 ssl->conf->f_cookie_check == NULL )
5120 {
5121 /* If we can't use cookies to verify reachability of the peer,
5122 * drop the record. */
5123 return( 0 );
5124 }
5125
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005126 ret = ssl_check_dtls_clihlo_cookie(
5127 ssl->conf->f_cookie_write,
5128 ssl->conf->f_cookie_check,
5129 ssl->conf->p_cookie,
5130 ssl->cli_id, ssl->cli_id_len,
5131 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005132 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005133
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005134 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5135
5136 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005137 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005138 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005139 * If the error is permanent we'll catch it later,
5140 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005141 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005142 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005143 }
5144
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005145 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005146 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005147 /* Got a valid cookie, partially reset context */
5148 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5149 {
5150 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5151 return( ret );
5152 }
5153
5154 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005155 }
5156
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005157 return( ret );
5158}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005159#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160
Hanno Becker46483f12019-05-03 13:25:54 +01005161static int ssl_check_record_type( uint8_t record_type )
5162{
5163 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5164 record_type != MBEDTLS_SSL_MSG_ALERT &&
5165 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5166 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5167 {
5168 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5169 }
5170
5171 return( 0 );
5172}
5173
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005174/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005175 * ContentType type;
5176 * ProtocolVersion version;
5177 * uint16 epoch; // DTLS only
5178 * uint48 sequence_number; // DTLS only
5179 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005180 *
5181 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005182 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005183 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5184 *
5185 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005186 * 1. proceed with the record if this function returns 0
5187 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5188 * 3. return CLIENT_RECONNECT if this function return that value
5189 * 4. drop the whole datagram if this function returns anything else.
5190 * Point 2 is needed when the peer is resending, and we have already received
5191 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005192 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005193static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005194 unsigned char *buf,
5195 size_t len,
5196 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005197{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005198 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005199
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005200 size_t const rec_hdr_type_offset = 0;
5201 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005202
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005203 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5204 rec_hdr_type_len;
5205 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005206
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005207 size_t const rec_hdr_ctr_len = 8;
5208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005209 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005210 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5211 rec_hdr_version_len;
5212
Hanno Beckera5a2b082019-05-15 14:03:01 +01005213#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005214 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5215 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005216 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5218#endif /* MBEDTLS_SSL_PROTO_DTLS */
5219
5220 size_t rec_hdr_len_offset; /* To be determined */
5221 size_t const rec_hdr_len_len = 2;
5222
5223 /*
5224 * Check minimum lengths for record header.
5225 */
5226
5227#if defined(MBEDTLS_SSL_PROTO_DTLS)
5228 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5229 {
5230 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5231 }
5232 MBEDTLS_SSL_TRANSPORT_ELSE
5233#endif /* MBEDTLS_SSL_PROTO_DTLS */
5234#if defined(MBEDTLS_SSL_PROTO_TLS)
5235 {
5236 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5237 }
5238#endif /* MBEDTLS_SSL_PROTO_DTLS */
5239
5240 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5243 (unsigned) len,
5244 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5245 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5246 }
5247
5248 /*
5249 * Parse and validate record content type
5250 */
5251
5252 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005253
5254 /* Check record content type */
5255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5256 rec->cid_len = 0;
5257
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005258 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005259 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5260 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005261 {
5262 /* Shift pointers to account for record header including CID
5263 * struct {
5264 * ContentType special_type = tls12_cid;
5265 * ProtocolVersion version;
5266 * uint16 epoch;
5267 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005268 * opaque cid[cid_length]; // Additional field compared to
5269 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005270 * uint16 length;
5271 * opaque enc_content[DTLSCiphertext.length];
5272 * } DTLSCiphertext;
5273 */
5274
5275 /* So far, we only support static CID lengths
5276 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005277 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5278 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005279
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005281 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5283 (unsigned) len,
5284 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005285 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005286 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005287
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005288 /* configured CID len is guaranteed at most 255, see
5289 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5290 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005291 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005292 }
5293 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005294#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005295 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005296 if( ssl_check_record_type( rec->type ) )
5297 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5299 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005300 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5301 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005302 }
5303
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005304 /*
5305 * Parse and validate record version
5306 */
5307
Hanno Becker8061c6e2019-07-26 08:07:03 +01005308 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5309 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005310 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5311 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005312 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313
Hanno Becker2881d802019-05-22 14:44:53 +01005314 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5317 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 }
5319
Hanno Beckere965bd32019-06-12 14:04:34 +01005320 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5323 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 }
5325
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005326 /*
5327 * Parse/Copy record sequence number.
5328 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005329
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005330#if defined(MBEDTLS_SSL_PROTO_DTLS)
5331 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5332 {
5333 /* Copy explicit record sequence number from input buffer. */
5334 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5335 rec_hdr_ctr_len );
5336 }
5337 MBEDTLS_SSL_TRANSPORT_ELSE
5338#endif /* MBEDTLS_SSL_PROTO_DTLS */
5339#if defined(MBEDTLS_SSL_PROTO_TLS)
5340 {
5341 /* Copy implicit record sequence number from SSL context structure. */
5342 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5343 }
5344#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005345
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005346 /*
5347 * Parse record length.
5348 */
5349
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005350 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005351 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5353
Hanno Becker8b09b732019-05-08 12:03:28 +01005354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005355 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 rec->type,
5357 major_ver, minor_ver, rec->data_len ) );
5358
5359 rec->buf = buf;
5360 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005361
Hanno Beckerec014082019-07-26 08:20:27 +01005362 if( rec->data_len == 0 )
5363 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5364
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005365 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005366 * DTLS-related tests.
5367 * Check epoch before checking length constraint because
5368 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5369 * message gets duplicated before the corresponding Finished message,
5370 * the second ChangeCipherSpec should be discarded because it belongs
5371 * to an old epoch, but not because its length is shorter than
5372 * the minimum record length for packets using the new record transform.
5373 * Note that these two kinds of failures are handled differently,
5374 * as an unexpected record is silently skipped but an invalid
5375 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005376 */
5377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005378 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005379 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005380 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005381
Hanno Beckere0452772019-07-10 17:12:07 +01005382 /* Check that the datagram is large enough to contain a record
5383 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005384 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005385 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5387 (unsigned) len,
5388 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005389 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5390 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005391
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005392 /* Records from other, non-matching epochs are silently discarded.
5393 * (The case of same-port Client reconnects must be considered in
5394 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005395 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005396 {
5397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5398 "expected %d, received %d",
5399 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005400
5401 /* Records from the next epoch are considered for buffering
5402 * (concretely: early Finished messages). */
5403 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5404 {
5405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5406 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5407 }
5408
Hanno Becker87b56262019-07-10 14:37:41 +01005409 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005410 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005411#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005412 /* For records from the correct epoch, check whether their
5413 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005414 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005415 {
5416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5417 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5418 }
5419#endif
5420 }
5421#endif /* MBEDTLS_SSL_PROTO_DTLS */
5422
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005423 return( 0 );
5424}
Paul Bakker5121ce52009-01-03 21:22:43 +00005425
Hanno Becker87b56262019-07-10 14:37:41 +01005426
5427#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5428static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5429{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005430 unsigned int rec_epoch = (unsigned int)
5431 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005432
5433 /*
5434 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5435 * access the first byte of record content (handshake type), as we
5436 * have an active transform (possibly iv_len != 0), so use the
5437 * fact that the record header len is 13 instead.
5438 */
5439 if( rec_epoch == 0 &&
5440 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5441 MBEDTLS_SSL_IS_SERVER &&
5442 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5443 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5444 ssl->in_left > 13 &&
5445 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5446 {
5447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5448 "from the same port" ) );
5449 return( ssl_handle_possible_reconnect( ssl ) );
5450 }
5451
5452 return( 0 );
5453}
5454#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005456/*
5457 * If applicable, decrypt (and decompress) record content
5458 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005459static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5460 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005461{
5462 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005465 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5468 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 ret = mbedtls_ssl_hw_record_read( ssl );
5473 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5476 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005477 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005478
5479 if( ret == 0 )
5480 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005483 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005485 unsigned char const old_msg_type = rec->type;
5486
Hanno Becker611a83b2018-01-03 14:27:32 +00005487 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005488 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005491
Hanno Beckera5a2b082019-05-15 14:03:01 +01005492#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005493 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005494 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5495 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005496 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005498 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005499 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005500#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005501
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 return( ret );
5503 }
5504
Hanno Becker106f3da2019-07-12 09:35:58 +01005505 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005506 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005507 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005512 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Hanno Beckera5a2b082019-05-15 14:03:01 +01005514#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005515 /* We have already checked the record content type
5516 * in ssl_parse_record_header(), failing or silently
5517 * dropping the record in the case of an unknown type.
5518 *
5519 * Since with the use of CIDs, the record content type
5520 * might change during decryption, re-check the record
5521 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005522 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005523 {
5524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5526 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005527#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005528
Hanno Becker106f3da2019-07-12 09:35:58 +01005529 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005530 {
5531#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005532 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005533 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005534 {
5535 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5537 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5538 }
5539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5540
5541 ssl->nb_zero++;
5542
5543 /*
5544 * Three or more empty messages may be a DoS attack
5545 * (excessive CPU consumption).
5546 */
5547 if( ssl->nb_zero > 3 )
5548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005550 "messages, possible DoS attack" ) );
5551 /* Treat the records as if they were not properly authenticated,
5552 * thereby failing the connection if we see more than allowed
5553 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005554 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5555 }
5556 }
5557 else
5558 ssl->nb_zero = 0;
5559
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005560 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5561#if defined(MBEDTLS_SSL_PROTO_TLS)
5562 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005563 {
5564 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005565 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005566 if( ++ssl->in_ctr[i - 1] != 0 )
5567 break;
5568
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005569 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005570 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5573 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5574 }
5575 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005576#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005577
Paul Bakker5121ce52009-01-03 21:22:43 +00005578 }
5579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005581 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005583 {
5584 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005587 return( ret );
5588 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005589 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005593 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005596 }
5597#endif
5598
Hanno Beckerf0242852019-07-09 17:30:02 +01005599 /* Check actual (decrypted) record content length against
5600 * configured maximum. */
5601 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5602 {
5603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5604 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5605 }
5606
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005607 return( 0 );
5608}
5609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005611
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005612/*
5613 * Read a record.
5614 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005615 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5616 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5617 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005618 */
Hanno Becker1097b342018-08-15 14:09:41 +01005619
5620/* Helper functions for mbedtls_ssl_read_record(). */
5621static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005622static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5623static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005624
Hanno Becker327c93b2018-08-15 13:56:18 +01005625int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005626 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005627{
5628 int ret;
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005631
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005632 if( ssl->keep_current_message == 0 )
5633 {
5634 do {
Simon Butcher99000142016-10-13 17:21:01 +01005635
Hanno Becker26994592018-08-15 14:14:59 +01005636 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005637 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005638 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005639
Hanno Beckere74d5562018-08-15 14:26:08 +01005640 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005641 {
Hanno Becker40f50842018-08-15 14:48:01 +01005642#if defined(MBEDTLS_SSL_PROTO_DTLS)
5643 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005644
Hanno Becker40f50842018-08-15 14:48:01 +01005645 /* We only check for buffered messages if the
5646 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005647 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005648 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005649 {
Hanno Becker40f50842018-08-15 14:48:01 +01005650 if( ssl_load_buffered_message( ssl ) == 0 )
5651 have_buffered = 1;
5652 }
5653
5654 if( have_buffered == 0 )
5655#endif /* MBEDTLS_SSL_PROTO_DTLS */
5656 {
5657 ret = ssl_get_next_record( ssl );
5658 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5659 continue;
5660
5661 if( ret != 0 )
5662 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005663 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005664 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005665 return( ret );
5666 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005667 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005668 }
5669
5670 ret = mbedtls_ssl_handle_message_type( ssl );
5671
Hanno Becker40f50842018-08-15 14:48:01 +01005672#if defined(MBEDTLS_SSL_PROTO_DTLS)
5673 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5674 {
5675 /* Buffer future message */
5676 ret = ssl_buffer_message( ssl );
5677 if( ret != 0 )
5678 return( ret );
5679
5680 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5681 }
5682#endif /* MBEDTLS_SSL_PROTO_DTLS */
5683
Hanno Becker90333da2017-10-10 11:27:13 +01005684 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5685 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005686
5687 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005688 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005689 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005690 return( ret );
5691 }
5692
Hanno Becker327c93b2018-08-15 13:56:18 +01005693 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005694 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005695 {
5696 mbedtls_ssl_update_handshake_status( ssl );
5697 }
Simon Butcher99000142016-10-13 17:21:01 +01005698 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005699 else
Simon Butcher99000142016-10-13 17:21:01 +01005700 {
Hanno Becker02f59072018-08-15 14:00:24 +01005701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005702 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005703 }
5704
5705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5706
5707 return( 0 );
5708}
5709
Hanno Becker40f50842018-08-15 14:48:01 +01005710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005711static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005712{
Hanno Becker40f50842018-08-15 14:48:01 +01005713 if( ssl->in_left > ssl->next_record_offset )
5714 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005715
Hanno Becker40f50842018-08-15 14:48:01 +01005716 return( 0 );
5717}
5718
5719static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5720{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005721 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005722 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005723 int ret = 0;
5724
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005725 if( hs == NULL )
5726 return( -1 );
5727
Hanno Beckere00ae372018-08-20 09:39:42 +01005728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5729
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005730 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5731 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5732 {
5733 /* Check if we have seen a ChangeCipherSpec before.
5734 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005735 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005736 {
5737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5738 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005739 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005740 }
5741
Hanno Becker39b8bc92018-08-28 17:17:13 +01005742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5744 ssl->in_msglen = 1;
5745 ssl->in_msg[0] = 1;
5746
5747 /* As long as they are equal, the exact value doesn't matter. */
5748 ssl->in_left = 0;
5749 ssl->next_record_offset = 0;
5750
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005751 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005752 goto exit;
5753 }
Hanno Becker37f95322018-08-16 13:55:32 +01005754
Hanno Beckerb8f50142018-08-28 10:01:34 +01005755#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005756 /* Debug only */
5757 {
5758 unsigned offset;
5759 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5760 {
5761 hs_buf = &hs->buffering.hs[offset];
5762 if( hs_buf->is_valid == 1 )
5763 {
5764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5765 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005766 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005767 }
5768 }
5769 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005770#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005771
5772 /* Check if we have buffered and/or fully reassembled the
5773 * next handshake message. */
5774 hs_buf = &hs->buffering.hs[0];
5775 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5776 {
5777 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005778 uint32_t msg_len = (uint32_t)mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005779
5780 /* Double-check that we haven't accidentally buffered
5781 * a message that doesn't fit into the input buffer. */
5782 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5783 {
5784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5786 }
5787
5788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5789 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5790 hs_buf->data, msg_len + 12 );
5791
5792 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5793 ssl->in_hslen = msg_len + 12;
5794 ssl->in_msglen = msg_len + 12;
5795 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5796
5797 ret = 0;
5798 goto exit;
5799 }
5800 else
5801 {
5802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5803 hs->in_msg_seq ) );
5804 }
5805
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005806 ret = -1;
5807
5808exit:
5809
5810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5811 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005812}
5813
Hanno Beckera02b0b42018-08-21 17:20:27 +01005814static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5815 size_t desired )
5816{
5817 int offset;
5818 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5820 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005821
Hanno Becker01315ea2018-08-21 17:22:17 +01005822 /* Get rid of future records epoch first, if such exist. */
5823 ssl_free_buffered_record( ssl );
5824
5825 /* Check if we have enough space available now. */
5826 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5827 hs->buffering.total_bytes_buffered ) )
5828 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005830 return( 0 );
5831 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005832
Hanno Becker4f432ad2018-08-28 10:02:32 +01005833 /* We don't have enough space to buffer the next expected handshake
5834 * message. Remove buffers used for future messages to gain space,
5835 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005836 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5837 offset >= 0; offset-- )
5838 {
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5840 offset ) );
5841
Hanno Beckerb309b922018-08-23 13:18:05 +01005842 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005843
5844 /* Check if we have enough space available now. */
5845 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5846 hs->buffering.total_bytes_buffered ) )
5847 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005849 return( 0 );
5850 }
5851 }
5852
5853 return( -1 );
5854}
5855
Hanno Becker40f50842018-08-15 14:48:01 +01005856static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5857{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 int ret = 0;
5859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5860
5861 if( hs == NULL )
5862 return( 0 );
5863
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5865
5866 switch( ssl->in_msgtype )
5867 {
5868 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005870
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005871 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872 break;
5873
5874 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005875 {
5876 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005877 unsigned recv_msg_seq = (unsigned)
5878 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005879
Hanno Becker37f95322018-08-16 13:55:32 +01005880 mbedtls_ssl_hs_buffer *hs_buf;
5881 size_t msg_len = ssl->in_hslen - 12;
5882
5883 /* We should never receive an old handshake
5884 * message - double-check nonetheless. */
5885 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5886 {
5887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5889 }
5890
5891 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5892 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5893 {
5894 /* Silently ignore -- message too far in the future */
5895 MBEDTLS_SSL_DEBUG_MSG( 2,
5896 ( "Ignore future HS message with sequence number %u, "
5897 "buffering window %u - %u",
5898 recv_msg_seq, ssl->handshake->in_msg_seq,
5899 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5900
5901 goto exit;
5902 }
5903
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5905 recv_msg_seq, recv_msg_seq_offset ) );
5906
5907 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5908
5909 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005910 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005911 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005912 size_t reassembly_buf_sz;
5913
Hanno Becker37f95322018-08-16 13:55:32 +01005914 hs_buf->is_fragmented =
5915 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5916
5917 /* We copy the message back into the input buffer
5918 * after reassembly, so check that it's not too large.
5919 * This is an implementation-specific limitation
5920 * and not one from the standard, hence it is not
5921 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005922 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005923 {
5924 /* Ignore message */
5925 goto exit;
5926 }
5927
Hanno Beckere0b150f2018-08-21 15:51:03 +01005928 /* Check if we have enough space to buffer the message. */
5929 if( hs->buffering.total_bytes_buffered >
5930 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5931 {
5932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5934 }
5935
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005936 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5937 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005938
5939 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5940 hs->buffering.total_bytes_buffered ) )
5941 {
5942 if( recv_msg_seq_offset > 0 )
5943 {
5944 /* If we can't buffer a future message because
5945 * of space limitations -- ignore. */
5946 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",
5947 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5948 (unsigned) hs->buffering.total_bytes_buffered ) );
5949 goto exit;
5950 }
Hanno Beckere1801392018-08-21 16:51:05 +01005951 else
5952 {
5953 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",
5954 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5955 (unsigned) hs->buffering.total_bytes_buffered ) );
5956 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005957
Hanno Beckera02b0b42018-08-21 17:20:27 +01005958 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005959 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005960 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",
5961 (unsigned) msg_len,
5962 (unsigned) reassembly_buf_sz,
5963 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005964 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005965 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5966 goto exit;
5967 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005968 }
5969
5970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5971 msg_len ) );
5972
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005973 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5974 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005975 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005976 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005977 goto exit;
5978 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005980
5981 /* Prepare final header: copy msg_type, length and message_seq,
5982 * then add standardised fragment_offset and fragment_length */
5983 memcpy( hs_buf->data, ssl->in_msg, 6 );
5984 memset( hs_buf->data + 6, 0, 3 );
5985 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5986
5987 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005988
5989 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005990 }
5991 else
5992 {
5993 /* Make sure msg_type and length are consistent */
5994 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5995 {
5996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5997 /* Ignore */
5998 goto exit;
5999 }
6000 }
6001
Hanno Becker4422bbb2018-08-20 09:40:19 +01006002 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006003 {
6004 size_t frag_len, frag_off;
6005 unsigned char * const msg = hs_buf->data + 12;
6006
6007 /*
6008 * Check and copy current fragment
6009 */
6010
6011 /* Validation of header fields already done in
6012 * mbedtls_ssl_prepare_handshake_record(). */
6013 frag_off = ssl_get_hs_frag_off( ssl );
6014 frag_len = ssl_get_hs_frag_len( ssl );
6015
6016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6017 frag_off, frag_len ) );
6018 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6019
6020 if( hs_buf->is_fragmented )
6021 {
6022 unsigned char * const bitmask = msg + msg_len;
6023 ssl_bitmask_set( bitmask, frag_off, frag_len );
6024 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6025 msg_len ) == 0 );
6026 }
6027 else
6028 {
6029 hs_buf->is_complete = 1;
6030 }
6031
6032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6033 hs_buf->is_complete ? "" : "not yet " ) );
6034 }
6035
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006036 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006037 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006038
6039 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006040 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006041 break;
6042 }
6043
6044exit:
6045
6046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6047 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006048}
6049#endif /* MBEDTLS_SSL_PROTO_DTLS */
6050
Hanno Becker1097b342018-08-15 14:09:41 +01006051static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006052{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006053 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006054 * Consume last content-layer message and potentially
6055 * update in_msglen which keeps track of the contents'
6056 * consumption state.
6057 *
6058 * (1) Handshake messages:
6059 * Remove last handshake message, move content
6060 * and adapt in_msglen.
6061 *
6062 * (2) Alert messages:
6063 * Consume whole record content, in_msglen = 0.
6064 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006065 * (3) Change cipher spec:
6066 * Consume whole record content, in_msglen = 0.
6067 *
6068 * (4) Application data:
6069 * Don't do anything - the record layer provides
6070 * the application data as a stream transport
6071 * and consumes through mbedtls_ssl_read only.
6072 *
6073 */
6074
6075 /* Case (1): Handshake messages */
6076 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006077 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006078 /* Hard assertion to be sure that no application data
6079 * is in flight, as corrupting ssl->in_msglen during
6080 * ssl->in_offt != NULL is fatal. */
6081 if( ssl->in_offt != NULL )
6082 {
6083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6085 }
6086
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006087 /*
6088 * Get next Handshake message in the current record
6089 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006090
Hanno Becker4a810fb2017-05-24 16:27:30 +01006091 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006092 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006093 * current handshake content: If DTLS handshake
6094 * fragmentation is used, that's the fragment
6095 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006096 * size here is faulty and should be changed at
6097 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006098 * (2) While it doesn't seem to cause problems, one
6099 * has to be very careful not to assume that in_hslen
6100 * is always <= in_msglen in a sensible communication.
6101 * Again, it's wrong for DTLS handshake fragmentation.
6102 * The following check is therefore mandatory, and
6103 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006104 * Additionally, ssl->in_hslen might be arbitrarily out of
6105 * bounds after handling a DTLS message with an unexpected
6106 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006107 */
6108 if( ssl->in_hslen < ssl->in_msglen )
6109 {
6110 ssl->in_msglen -= ssl->in_hslen;
6111 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6112 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006113
Hanno Becker4a810fb2017-05-24 16:27:30 +01006114 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6115 ssl->in_msg, ssl->in_msglen );
6116 }
6117 else
6118 {
6119 ssl->in_msglen = 0;
6120 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006121
Hanno Becker4a810fb2017-05-24 16:27:30 +01006122 ssl->in_hslen = 0;
6123 }
6124 /* Case (4): Application data */
6125 else if( ssl->in_offt != NULL )
6126 {
6127 return( 0 );
6128 }
6129 /* Everything else (CCS & Alerts) */
6130 else
6131 {
6132 ssl->in_msglen = 0;
6133 }
6134
Hanno Becker1097b342018-08-15 14:09:41 +01006135 return( 0 );
6136}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006137
Hanno Beckere74d5562018-08-15 14:26:08 +01006138static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6139{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006140 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006141 return( 1 );
6142
6143 return( 0 );
6144}
6145
Hanno Becker5f066e72018-08-16 14:56:31 +01006146#if defined(MBEDTLS_SSL_PROTO_DTLS)
6147
6148static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6149{
6150 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6151 if( hs == NULL )
6152 return;
6153
Hanno Becker01315ea2018-08-21 17:22:17 +01006154 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006155 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006156 hs->buffering.total_bytes_buffered -=
6157 hs->buffering.future_record.len;
6158
6159 mbedtls_free( hs->buffering.future_record.data );
6160 hs->buffering.future_record.data = NULL;
6161 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006162}
6163
6164static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6165{
6166 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6167 unsigned char * rec;
6168 size_t rec_len;
6169 unsigned rec_epoch;
6170
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006171 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006172 return( 0 );
6173
6174 if( hs == NULL )
6175 return( 0 );
6176
Hanno Becker5f066e72018-08-16 14:56:31 +01006177 rec = hs->buffering.future_record.data;
6178 rec_len = hs->buffering.future_record.len;
6179 rec_epoch = hs->buffering.future_record.epoch;
6180
6181 if( rec == NULL )
6182 return( 0 );
6183
Hanno Becker4cb782d2018-08-20 11:19:05 +01006184 /* Only consider loading future records if the
6185 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006186 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006187 return( 0 );
6188
Hanno Becker5f066e72018-08-16 14:56:31 +01006189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6190
6191 if( rec_epoch != ssl->in_epoch )
6192 {
6193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6194 goto exit;
6195 }
6196
6197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6198
6199 /* Double-check that the record is not too large */
6200 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6201 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6202 {
6203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6205 }
6206
6207 memcpy( ssl->in_hdr, rec, rec_len );
6208 ssl->in_left = rec_len;
6209 ssl->next_record_offset = 0;
6210
6211 ssl_free_buffered_record( ssl );
6212
6213exit:
6214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6215 return( 0 );
6216}
6217
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6219 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006220{
6221 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006222
6223 /* Don't buffer future records outside handshakes. */
6224 if( hs == NULL )
6225 return( 0 );
6226
6227 /* Only buffer handshake records (we are only interested
6228 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006229 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006230 return( 0 );
6231
6232 /* Don't buffer more than one future epoch record. */
6233 if( hs->buffering.future_record.data != NULL )
6234 return( 0 );
6235
Hanno Becker01315ea2018-08-21 17:22:17 +01006236 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006237 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006238 hs->buffering.total_bytes_buffered ) )
6239 {
6240 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 +01006241 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006242 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006243 return( 0 );
6244 }
6245
Hanno Becker5f066e72018-08-16 14:56:31 +01006246 /* Buffer record */
6247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6248 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006249 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006250
6251 /* ssl_parse_record_header() only considers records
6252 * of the next epoch as candidates for buffering. */
6253 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006254 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006255
6256 hs->buffering.future_record.data =
6257 mbedtls_calloc( 1, hs->buffering.future_record.len );
6258 if( hs->buffering.future_record.data == NULL )
6259 {
6260 /* If we run out of RAM trying to buffer a
6261 * record from the next epoch, just ignore. */
6262 return( 0 );
6263 }
6264
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006265 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006266
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006267 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006268 return( 0 );
6269}
6270
6271#endif /* MBEDTLS_SSL_PROTO_DTLS */
6272
Hanno Beckere74d5562018-08-15 14:26:08 +01006273static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006274{
6275 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006276 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006277
Hanno Becker5f066e72018-08-16 14:56:31 +01006278#if defined(MBEDTLS_SSL_PROTO_DTLS)
6279 /* We might have buffered a future record; if so,
6280 * and if the epoch matches now, load it.
6281 * On success, this call will set ssl->in_left to
6282 * the length of the buffered record, so that
6283 * the calls to ssl_fetch_input() below will
6284 * essentially be no-ops. */
6285 ret = ssl_load_buffered_record( ssl );
6286 if( ret != 0 )
6287 return( ret );
6288#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006289
Hanno Becker8b09b732019-05-08 12:03:28 +01006290 /* Ensure that we have enough space available for the default form
6291 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6292 * with no space for CIDs counted in). */
6293 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6294 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006297 return( ret );
6298 }
6299
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006300 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6301 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006304 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006305 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006306 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6307 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006308 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006309 if( ret != 0 )
6310 return( ret );
6311
6312 /* Fall through to handling of unexpected records */
6313 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6314 }
6315
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006316 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6317 {
Hanno Becker87b56262019-07-10 14:37:41 +01006318#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006319 /* Reset in pointers to default state for TLS/DTLS records,
6320 * assuming no CID and no offset between record content and
6321 * record plaintext. */
6322 ssl_update_in_pointers( ssl );
6323
Hanno Becker69412452019-07-12 08:33:49 +01006324 /* Setup internal message pointers from record structure. */
6325 ssl->in_msgtype = rec.type;
6326#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6327 ssl->in_len = ssl->in_cid + rec.cid_len;
6328#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006329 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006330 ssl->in_msglen = rec.data_len;
6331
Hanno Becker87b56262019-07-10 14:37:41 +01006332 ret = ssl_check_client_reconnect( ssl );
6333 if( ret != 0 )
6334 return( ret );
6335#endif
6336
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006337 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006338 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006339
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6341 "(header)" ) );
6342 }
6343 else
6344 {
6345 /* Skip invalid record and the rest of the datagram */
6346 ssl->next_record_offset = 0;
6347 ssl->in_left = 0;
6348
6349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6350 "(header)" ) );
6351 }
6352
6353 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006354 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006355 }
Hanno Becker87b56262019-07-10 14:37:41 +01006356 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006357#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006358 {
6359 return( ret );
6360 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006361 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006364 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006365 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006366 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006367 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006368 if( ssl->next_record_offset < ssl->in_left )
6369 {
6370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6371 }
6372 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006373 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006374#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006375#if defined(MBEDTLS_SSL_PROTO_TLS)
6376 {
Hanno Beckere0452772019-07-10 17:12:07 +01006377 /*
6378 * Fetch record contents from underlying transport.
6379 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006380 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006381 if( ret != 0 )
6382 {
6383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6384 return( ret );
6385 }
6386
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006387 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006388 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006389#endif /* MBEDTLS_SSL_PROTO_TLS */
6390
6391 /*
6392 * Decrypt record contents.
6393 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006394
Hanno Beckera89610a2019-07-11 13:07:45 +01006395 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006398 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006399 {
6400 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006401 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006402 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006403 /* Except when waiting for Finished as a bad mac here
6404 * probably means something went wrong in the handshake
6405 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6406 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6407 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6408 {
6409#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6410 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6411 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006412 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006413 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6414 }
6415#endif
6416 return( ret );
6417 }
6418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006420 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6421 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006425 }
6426#endif
6427
Hanno Becker4a810fb2017-05-24 16:27:30 +01006428 /* As above, invalid records cause
6429 * dismissal of the whole datagram. */
6430
6431 ssl->next_record_offset = 0;
6432 ssl->in_left = 0;
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006435 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006436 }
6437
6438 return( ret );
6439 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006440 MBEDTLS_SSL_TRANSPORT_ELSE
6441#endif /* MBEDTLS_SSL_PROTO_DTLS */
6442#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006443 {
6444 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6446 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006447 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006448 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006450 }
6451#endif
6452 return( ret );
6453 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006454#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006455 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006456
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006457
6458 /* Reset in pointers to default state for TLS/DTLS records,
6459 * assuming no CID and no offset between record content and
6460 * record plaintext. */
6461 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006462#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6463 ssl->in_len = ssl->in_cid + rec.cid_len;
6464#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006465 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006466
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006467 /* The record content type may change during decryption,
6468 * so re-read it. */
6469 ssl->in_msgtype = rec.type;
6470 /* Also update the input buffer, because unfortunately
6471 * the server-side ssl_parse_client_hello() reparses the
6472 * record header when receiving a ClientHello initiating
6473 * a renegotiation. */
6474 ssl->in_hdr[0] = rec.type;
6475 ssl->in_msg = rec.buf + rec.data_offset;
6476 ssl->in_msglen = rec.data_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006477 mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006478
Simon Butcher99000142016-10-13 17:21:01 +01006479 return( 0 );
6480}
6481
6482int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6483{
6484 int ret;
6485
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006486 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006487 * Handle particular types of records
6488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006490 {
Simon Butcher99000142016-10-13 17:21:01 +01006491 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6492 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006493 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 }
6496
Hanno Beckere678eaa2018-08-21 14:57:46 +01006497 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006498 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006499 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006500 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6502 ssl->in_msglen ) );
6503 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006504 }
6505
Hanno Beckere678eaa2018-08-21 14:57:46 +01006506 if( ssl->in_msg[0] != 1 )
6507 {
6508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6509 ssl->in_msg[0] ) );
6510 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6511 }
6512
6513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006514 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006515 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6516 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6517 {
6518 if( ssl->handshake == NULL )
6519 {
6520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6521 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6522 }
6523
6524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6525 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6526 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006527#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006528 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006531 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006532 if( ssl->in_msglen != 2 )
6533 {
6534 /* Note: Standard allows for more than one 2 byte alert
6535 to be packed in a single message, but Mbed TLS doesn't
6536 currently support this. */
6537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6538 ssl->in_msglen ) );
6539 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6540 }
6541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 ssl->in_msg[0], ssl->in_msg[1] ) );
6544
6545 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006546 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006551 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 }
6554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6556 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6559 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006561
6562#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6563 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6564 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6565 {
Hanno Becker90333da2017-10-10 11:27:13 +01006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006567 /* Will be handled when trying to parse ServerHello */
6568 return( 0 );
6569 }
6570#endif
6571
6572#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006573 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006574 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6575 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006576 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6577 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6578 {
6579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6580 /* Will be handled in mbedtls_ssl_parse_certificate() */
6581 return( 0 );
6582 }
6583#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6584
6585 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006586 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 }
6588
Hanno Beckerc76c6192017-06-06 10:03:17 +01006589#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006591 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006592 /* Drop unexpected ApplicationData records,
6593 * except at the beginning of renegotiations */
6594 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6595 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6596#if defined(MBEDTLS_SSL_RENEGOTIATION)
6597 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6598 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006599#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006600 )
6601 {
6602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6603 return( MBEDTLS_ERR_SSL_NON_FATAL );
6604 }
6605
6606 if( ssl->handshake != NULL &&
6607 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6608 {
6609 ssl_handshake_wrapup_free_hs_transform( ssl );
6610 }
6611 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006612#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006613
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 return( 0 );
6615}
6616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006618{
6619 int ret;
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6622 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6623 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006624 {
6625 return( ret );
6626 }
6627
6628 return( 0 );
6629}
6630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006632 unsigned char level,
6633 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006634{
6635 int ret;
6636
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006637 if( ssl == NULL || ssl->conf == NULL )
6638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006644 ssl->out_msglen = 2;
6645 ssl->out_msg[0] = level;
6646 ssl->out_msg[1] = message;
6647
Hanno Becker67bc7c32018-08-06 11:33:50 +01006648 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006651 return( ret );
6652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006654
6655 return( 0 );
6656}
6657
Hanno Becker17572472019-02-08 07:19:04 +00006658#if defined(MBEDTLS_X509_CRT_PARSE_C)
6659static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6660{
6661#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6662 if( session->peer_cert != NULL )
6663 {
6664 mbedtls_x509_crt_free( session->peer_cert );
6665 mbedtls_free( session->peer_cert );
6666 session->peer_cert = NULL;
6667 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006668#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006669 if( session->peer_cert_digest != NULL )
6670 {
6671 /* Zeroization is not necessary. */
6672 mbedtls_free( session->peer_cert_digest );
6673 session->peer_cert_digest = NULL;
6674 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6675 session->peer_cert_digest_len = 0;
6676 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006677#else
6678 ((void) session);
6679#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006680}
6681#endif /* MBEDTLS_X509_CRT_PARSE_C */
6682
Paul Bakker5121ce52009-01-03 21:22:43 +00006683/*
6684 * Handshake functions
6685 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006686#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006687/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006689{
Hanno Beckerdf645962019-06-26 13:02:22 +01006690 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6691 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694
Hanno Becker5097cba2019-02-05 13:36:46 +00006695 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006698 ssl->state++;
6699 return( 0 );
6700 }
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006704}
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006707{
Hanno Beckerdf645962019-06-26 13:02:22 +01006708 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6709 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006712
Hanno Becker5097cba2019-02-05 13:36:46 +00006713 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006716 ssl->state++;
6717 return( 0 );
6718 }
6719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006722}
Gilles Peskinef9828522017-05-03 12:28:43 +02006723
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006724#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006725/* Some certificate support -> implement write and parse */
6726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006730 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 const mbedtls_x509_crt *crt;
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, ( "=> write 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 write 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#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006745 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6746 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006747 {
6748 if( ssl->client_auth == 0 )
6749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 ssl->state++;
6752 return( 0 );
6753 }
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 /*
6757 * If using SSLv3 and got no cert, send an Alert message
6758 * (otherwise an empty Certificate message will be sent).
6759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006761 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 {
6763 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6765 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6766 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 goto write_msg;
6770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773#endif /* MBEDTLS_SSL_CLI_C */
6774#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006775 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6780 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 }
6782 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006783#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
6787 /*
6788 * 0 . 0 handshake type
6789 * 1 . 3 handshake length
6790 * 4 . 6 length of all certs
6791 * 7 . 9 length of cert. 1
6792 * 10 . n-1 peer certificate
6793 * n . n+2 length of cert. 2
6794 * n+3 . ... upper level cert, etc.
6795 */
6796 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006798
Paul Bakker29087132010-03-21 21:03:34 +00006799 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
6801 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006802 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006805 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 }
6808
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006809 mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
6811 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6812 i += n; crt = crt->next;
6813 }
6814
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006815 mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006816
6817 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6819 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006821#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006822write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006824
6825 ssl->state++;
6826
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006827 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 return( ret );
6831 }
6832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Paul Bakkered27a042013-04-18 22:46:23 +02006835 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006836}
6837
Hanno Becker285ff0c2019-01-31 07:44:03 +00006838#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006839
6840#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006841static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6842 unsigned char *crt_buf,
6843 size_t crt_buf_len )
6844{
6845 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6846
6847 if( peer_crt == NULL )
6848 return( -1 );
6849
6850 if( peer_crt->raw.len != crt_buf_len )
6851 return( -1 );
6852
Hanno Becker68b856d2019-02-08 14:00:04 +00006853 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006854}
Hanno Becker5882dd02019-06-06 16:25:57 +01006855#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006856static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6857 unsigned char *crt_buf,
6858 size_t crt_buf_len )
6859{
6860 int ret;
6861 unsigned char const * const peer_cert_digest =
6862 ssl->session->peer_cert_digest;
6863 mbedtls_md_type_t const peer_cert_digest_type =
6864 ssl->session->peer_cert_digest_type;
6865 mbedtls_md_info_t const * const digest_info =
6866 mbedtls_md_info_from_type( peer_cert_digest_type );
6867 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6868 size_t digest_len;
6869
6870 if( peer_cert_digest == NULL || digest_info == NULL )
6871 return( -1 );
6872
6873 digest_len = mbedtls_md_get_size( digest_info );
6874 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6875 return( -1 );
6876
6877 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6878 if( ret != 0 )
6879 return( -1 );
6880
6881 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6882}
Hanno Becker5882dd02019-06-06 16:25:57 +01006883#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006884#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006885
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006886/*
6887 * Once the certificate message is read, parse it into a cert chain and
6888 * perform basic checks, but leave actual verification to the caller
6889 */
Hanno Becker35e41772019-02-05 15:37:23 +00006890static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6891 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006892{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006893 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006894#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6895 int crt_cnt=0;
6896#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006897 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006898 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006903 mbedtls_ssl_pend_fatal_alert( ssl,
6904 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006906 }
6907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6909 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006912 mbedtls_ssl_pend_fatal_alert( ssl,
6913 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006915 }
6916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006918
Paul Bakker5121ce52009-01-03 21:22:43 +00006919 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006921 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006922 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006923
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006924 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006928 mbedtls_ssl_pend_fatal_alert( ssl,
6929 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 }
6932
Hanno Becker33c3dc82019-01-30 14:46:46 +00006933 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6934 i += 3;
6935
Hanno Becker33c3dc82019-01-30 14:46:46 +00006936 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006937 while( i < ssl->in_hslen )
6938 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006939 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006940 if ( i + 3 > ssl->in_hslen ) {
6941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006942 mbedtls_ssl_pend_fatal_alert( ssl,
6943 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006944 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6945 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006946 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6947 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006948 if( ssl->in_msg[i] != 0 )
6949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006951 mbedtls_ssl_pend_fatal_alert( ssl,
6952 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 }
6955
Hanno Becker33c3dc82019-01-30 14:46:46 +00006956 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006957 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 i += 3;
6959
6960 if( n < 128 || i + n > ssl->in_hslen )
6961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006963 mbedtls_ssl_pend_fatal_alert( ssl,
6964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 }
6967
Hanno Becker33c3dc82019-01-30 14:46:46 +00006968 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006969#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6970 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006971 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6972 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006973 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006974 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006975 /* During client-side renegotiation, check that the server's
6976 * end-CRTs hasn't changed compared to the initial handshake,
6977 * mitigating the triple handshake attack. On success, reuse
6978 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6980 if( ssl_check_peer_crt_unchanged( ssl,
6981 &ssl->in_msg[i],
6982 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006983 {
Hanno Becker35e41772019-02-05 15:37:23 +00006984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006985 mbedtls_ssl_pend_fatal_alert( ssl,
6986 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006987 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006988 }
Hanno Becker35e41772019-02-05 15:37:23 +00006989
6990 /* Now we can safely free the original chain. */
6991 ssl_clear_peer_cert( ssl->session );
6992 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006993#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6994
Hanno Becker33c3dc82019-01-30 14:46:46 +00006995 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006996#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006997 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006998#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006999 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007000 * it in-place from the input buffer instead of making a copy. */
7001 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7002#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007003 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007004 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007005 case 0: /*ok*/
7006 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7007 /* Ignore certificate with an unknown algorithm: maybe a
7008 prior certificate was already trusted. */
7009 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007010
Hanno Becker33c3dc82019-01-30 14:46:46 +00007011 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7012 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7013 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007014
Hanno Becker33c3dc82019-01-30 14:46:46 +00007015 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7016 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7017 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007018
Hanno Becker33c3dc82019-01-30 14:46:46 +00007019 default:
7020 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7021 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007022 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007023 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7024 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007025 }
7026
7027 i += n;
7028 }
7029
Hanno Becker35e41772019-02-05 15:37:23 +00007030 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007031 return( 0 );
7032}
7033
Hanno Beckerb8a08572019-02-05 12:49:06 +00007034#if defined(MBEDTLS_SSL_SRV_C)
7035static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7036{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007037 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007038 return( -1 );
7039
7040#if defined(MBEDTLS_SSL_PROTO_SSL3)
7041 /*
7042 * Check if the client sent an empty certificate
7043 */
Hanno Becker2881d802019-05-22 14:44:53 +01007044 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007045 {
7046 if( ssl->in_msglen == 2 &&
7047 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7048 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7049 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7050 {
7051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7052 return( 0 );
7053 }
7054
7055 return( -1 );
7056 }
7057#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7058
7059#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7060 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7061 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7062 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7063 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7064 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7065 {
7066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7067 return( 0 );
7068 }
7069
Hanno Beckerb8a08572019-02-05 12:49:06 +00007070#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7071 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007072
7073 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007074}
7075#endif /* MBEDTLS_SSL_SRV_C */
7076
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007077/* Check if a certificate message is expected.
7078 * Return either
7079 * - SSL_CERTIFICATE_EXPECTED, or
7080 * - SSL_CERTIFICATE_SKIP
7081 * indicating whether a Certificate message is expected or not.
7082 */
7083#define SSL_CERTIFICATE_EXPECTED 0
7084#define SSL_CERTIFICATE_SKIP 1
7085static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7086 int authmode )
7087{
Hanno Becker473f98f2019-06-26 10:27:32 +01007088 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007089 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007090
7091 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7092 return( SSL_CERTIFICATE_SKIP );
7093
7094#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007095 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007096 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007097 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7098 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7099 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007100 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007101 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007102
7103 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7104 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007105 ssl->session_negotiate->verify_result =
7106 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7107 return( SSL_CERTIFICATE_SKIP );
7108 }
7109 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007110#else
7111 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007112#endif /* MBEDTLS_SSL_SRV_C */
7113
7114 return( SSL_CERTIFICATE_EXPECTED );
7115}
7116
Hanno Becker3cf50612019-02-05 14:36:34 +00007117static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7118 int authmode,
7119 mbedtls_x509_crt *chain,
7120 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007121{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007122 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007123 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007124 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007125 mbedtls_x509_crt *ca_chain;
7126 mbedtls_x509_crl *ca_crl;
7127
7128 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7129 return( 0 );
7130
7131#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7132 if( ssl->handshake->sni_ca_chain != NULL )
7133 {
7134 ca_chain = ssl->handshake->sni_ca_chain;
7135 ca_crl = ssl->handshake->sni_ca_crl;
7136 }
7137 else
7138#endif
7139 {
7140 ca_chain = ssl->conf->ca_chain;
7141 ca_crl = ssl->conf->ca_crl;
7142 }
7143
7144 /*
7145 * Main check: verify certificate
7146 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007147 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007148 chain,
7149 ca_chain, ca_crl,
7150 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007151#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007152 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007153#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007154 &ssl->session_negotiate->verify_result,
7155 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7156
Hanno Becker8c13ee62019-02-26 16:48:17 +00007157 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007158 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007159 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007160 }
7161
7162#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007163 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007164 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7165#endif
7166
7167 /*
7168 * Secondary checks: always done, but change 'ret' only if it was 0
7169 */
7170
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007171#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007172 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007173 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007174#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007175 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007176#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007177 mbedtls_pk_context *pk;
7178 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7179 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007180 {
7181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007182 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007183 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007184
7185 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007186 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007187 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007188 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007189 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007190
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007191 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007192#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007193
7194 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007195 {
7196 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7197
7198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007199 if( verify_ret == 0 )
7200 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007201 }
7202 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007203#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007204
7205 if( mbedtls_ssl_check_cert_usage( chain,
7206 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007207 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7208 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007209 &ssl->session_negotiate->verify_result ) != 0 )
7210 {
7211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007212 if( verify_ret == 0 )
7213 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007214 }
7215
7216 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7217 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7218 * with details encoded in the verification flags. All other kinds
7219 * of error codes, including those from the user provided f_vrfy
7220 * functions, are treated as fatal and lead to a failure of
7221 * ssl_parse_certificate even if verification was optional. */
7222 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007223 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7224 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007225 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007226 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007227 }
7228
7229 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7230 {
7231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007232 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007233 }
7234
Hanno Becker8c13ee62019-02-26 16:48:17 +00007235 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007236 {
7237 uint8_t alert;
7238
7239 /* The certificate may have been rejected for several reasons.
7240 Pick one and send the corresponding alert. Which alert to send
7241 may be a subject of debate in some cases. */
7242 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7243 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7244 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7245 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7246 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7247 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7248 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7249 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7250 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7251 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7252 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7253 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7254 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7255 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7256 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7257 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7258 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7259 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7260 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7261 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7262 else
7263 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007264 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007265 }
7266
7267#if defined(MBEDTLS_DEBUG_C)
7268 if( ssl->session_negotiate->verify_result != 0 )
7269 {
7270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7271 ssl->session_negotiate->verify_result ) );
7272 }
7273 else
7274 {
7275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7276 }
7277#endif /* MBEDTLS_DEBUG_C */
7278
Hanno Becker8c13ee62019-02-26 16:48:17 +00007279 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007280}
7281
Hanno Becker34106f62019-02-08 14:59:05 +00007282#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007283
7284#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007285static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7286 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007287{
7288 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007289 /* Remember digest of the peer's end-CRT. */
7290 ssl->session_negotiate->peer_cert_digest =
7291 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7292 if( ssl->session_negotiate->peer_cert_digest == NULL )
7293 {
7294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7295 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007296 mbedtls_ssl_pend_fatal_alert( ssl,
7297 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007298
7299 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7300 }
7301
7302 ret = mbedtls_md( mbedtls_md_info_from_type(
7303 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7304 start, len,
7305 ssl->session_negotiate->peer_cert_digest );
7306
7307 ssl->session_negotiate->peer_cert_digest_type =
7308 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7309 ssl->session_negotiate->peer_cert_digest_len =
7310 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7311
7312 return( ret );
7313}
Hanno Becker5882dd02019-06-06 16:25:57 +01007314#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007315
7316static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7317 unsigned char *start, size_t len )
7318{
7319 unsigned char *end = start + len;
7320 int ret;
7321
7322 /* Make a copy of the peer's raw public key. */
7323 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7324 ret = mbedtls_pk_parse_subpubkey( &start, end,
7325 &ssl->handshake->peer_pubkey );
7326 if( ret != 0 )
7327 {
7328 /* We should have parsed the public key before. */
7329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7330 }
7331
7332 return( 0 );
7333}
7334#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7335
Hanno Becker3cf50612019-02-05 14:36:34 +00007336int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7337{
7338 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007339 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007340#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7341 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7342 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007343 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007344#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007345 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007346#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007347 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007348 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007349
7350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7351
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007352 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7353 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007354 {
7355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007356 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007357 }
7358
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007359#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7360 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007361 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007362 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007363 chain = ssl->handshake->ecrs_peer_cert;
7364 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007365 goto crt_verify;
7366 }
7367#endif
7368
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007369 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007370 {
7371 /* mbedtls_ssl_read_record may have sent an alert already. We
7372 let it decide whether to alert. */
7373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007374 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007375 }
7376
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007377#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007378 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7379 {
7380 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007381
Hanno Beckerb8a08572019-02-05 12:49:06 +00007382 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007383 ret = 0;
7384 else
7385 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007386
Hanno Becker613d4902019-02-05 13:11:17 +00007387 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007388 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007389#endif /* MBEDTLS_SSL_SRV_C */
7390
Hanno Becker35e41772019-02-05 15:37:23 +00007391 /* Clear existing peer CRT structure in case we tried to
7392 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007393 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007394
7395 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7396 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007397 {
7398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7399 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007400 mbedtls_ssl_pend_fatal_alert( ssl,
7401 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007402
Hanno Beckere4aeb762019-02-05 17:19:52 +00007403 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7404 goto exit;
7405 }
7406 mbedtls_x509_crt_init( chain );
7407
7408 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007409 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007410 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007411
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007412#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7413 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007414 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007415
7416crt_verify:
7417 if( ssl->handshake->ecrs_enabled)
7418 rs_ctx = &ssl->handshake->ecrs_ctx;
7419#endif
7420
Hanno Becker3cf50612019-02-05 14:36:34 +00007421 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007422 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007423 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007424 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007425
Hanno Becker3008d282019-02-05 17:02:28 +00007426#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007427 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007428 size_t pk_len;
7429 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007430
Hanno Becker34106f62019-02-08 14:59:05 +00007431 /* We parse the CRT chain without copying, so
7432 * these pointers point into the input buffer,
7433 * and are hence still valid after freeing the
7434 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007435
Hanno Becker5882dd02019-06-06 16:25:57 +01007436#if defined(MBEDTLS_SSL_RENEGOTIATION)
7437 unsigned char *crt_start;
7438 size_t crt_len;
7439
Hanno Becker34106f62019-02-08 14:59:05 +00007440 crt_start = chain->raw.p;
7441 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007442#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007443
Hanno Becker8c13ee62019-02-26 16:48:17 +00007444 pk_start = chain->cache->pk_raw.p;
7445 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007446
7447 /* Free the CRT structures before computing
7448 * digest and copying the peer's public key. */
7449 mbedtls_x509_crt_free( chain );
7450 mbedtls_free( chain );
7451 chain = NULL;
7452
Hanno Becker5882dd02019-06-06 16:25:57 +01007453#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007454 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007455 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007456 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007457#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007458
Hanno Becker34106f62019-02-08 14:59:05 +00007459 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007460 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007461 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007462 }
Hanno Becker34106f62019-02-08 14:59:05 +00007463#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7464 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007465 ssl->session_negotiate->peer_cert = chain;
7466 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007467#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007470
Hanno Becker613d4902019-02-05 13:11:17 +00007471exit:
7472
Hanno Beckere4aeb762019-02-05 17:19:52 +00007473 if( ret == 0 )
7474 ssl->state++;
7475
7476#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7477 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7478 {
7479 ssl->handshake->ecrs_peer_cert = chain;
7480 chain = NULL;
7481 }
7482#endif
7483
7484 if( chain != NULL )
7485 {
7486 mbedtls_x509_crt_free( chain );
7487 mbedtls_free( chain );
7488 }
7489
Paul Bakker5121ce52009-01-03 21:22:43 +00007490 return( ret );
7491}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007492#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007495{
7496 int ret;
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 ssl->out_msglen = 1;
7502 ssl->out_msg[0] = 1;
7503
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 ssl->state++;
7505
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007506 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007509 return( ret );
7510 }
7511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
7514 return( 0 );
7515}
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007518{
7519 int ret;
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
Hanno Becker327c93b2018-08-15 13:56:18 +01007523 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007526 return( ret );
7527 }
7528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007532 mbedtls_ssl_pend_fatal_alert( ssl,
7533 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007535 }
7536
Hanno Beckere678eaa2018-08-21 14:57:46 +01007537 /* CCS records are only accepted if they have length 1 and content '1',
7538 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007539
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007540 /*
7541 * Switch to our negotiated transform and session parameters for inbound
7542 * data.
7543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007545 ssl->transform_in = ssl->transform_negotiate;
7546 ssl->session_in = ssl->session_negotiate;
7547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007549 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007552 ssl_dtls_replay_reset( ssl );
7553#endif
7554
7555 /* Increment epoch */
7556 if( ++ssl->in_epoch == 0 )
7557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007559 /* This is highly unlikely to happen for legitimate reasons, so
7560 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007562 }
7563 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007564 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007566#if defined(MBEDTLS_SSL_PROTO_TLS)
7567 {
7568 memset( ssl->in_ctr, 0, 8 );
7569 }
7570#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007571
Hanno Beckerf5970a02019-05-08 09:38:41 +01007572 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7575 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007580 mbedtls_ssl_pend_fatal_alert( ssl,
7581 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007583 }
7584 }
7585#endif
7586
Paul Bakker5121ce52009-01-03 21:22:43 +00007587 ssl->state++;
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007590
7591 return( 0 );
7592}
7593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7597 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007598 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007599 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7602#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007603 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007606 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007609}
7610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007614
7615 /*
7616 * Free our handshake params
7617 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007618 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007620 ssl->handshake = NULL;
7621
7622 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007624 */
7625 if( ssl->transform )
7626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 mbedtls_ssl_transform_free( ssl->transform );
7628 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007629 }
7630 ssl->transform = ssl->transform_negotiate;
7631 ssl->transform_negotiate = NULL;
7632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#if defined(MBEDTLS_SSL_RENEGOTIATION)
7641 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007644 ssl->renego_records_seen = 0;
7645 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007646#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647
7648 /*
7649 * Free the previous session and switch in the current one
7650 */
Paul Bakker0a597072012-09-25 21:55:46 +00007651 if( ssl->session )
7652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007654 /* RFC 7366 3.1: keep the EtM state */
7655 ssl->session_negotiate->encrypt_then_mac =
7656 ssl->session->encrypt_then_mac;
7657#endif
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 mbedtls_ssl_session_free( ssl->session );
7660 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007661 }
7662 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007663 ssl->session_negotiate = NULL;
7664
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007665#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007666 /*
7667 * Add cache entry
7668 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007669 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007670 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007671 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007672 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007673 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007675 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007676#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007679 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007680 ssl->handshake->flight != NULL )
7681 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007682 /* Cancel handshake timer */
7683 ssl_set_timer( ssl, 0 );
7684
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007685 /* Keep last flight around in case we need to resend it:
7686 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007688 }
7689 else
7690#endif
7691 ssl_handshake_wrapup_free_hs_transform( ssl );
7692
Paul Bakker48916f92012-09-16 19:57:18 +00007693 ssl->state++;
7694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007696}
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007700 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007704 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007705
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007706 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7707 mbedtls_ssl_suite_get_mac(
7708 mbedtls_ssl_ciphersuite_from_id(
7709 mbedtls_ssl_session_get_ciphersuite(
7710 ssl->session_negotiate ) ) ),
7711 ssl, ssl->out_msg + 4,
7712 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007713
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007714 /*
7715 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7716 * may define some other value. Currently (early 2016), no defined
7717 * ciphersuite does this (and this is unlikely to change as activity has
7718 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7719 */
Hanno Becker2881d802019-05-22 14:44:53 +01007720 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007723 ssl->verify_data_len = hash_len;
7724 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007725#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007726
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7729 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007731#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007732 /*
7733 * In case of session resuming, invert the client and server
7734 * ChangeCipherSpec messages order.
7735 */
Paul Bakker0a597072012-09-25 21:55:46 +00007736 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007739 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7740 MBEDTLS_SSL_IS_CLIENT )
7741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007743 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007746 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7747 MBEDTLS_SSL_IS_SERVER )
7748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007750 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 }
7753 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007754#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 ssl->state++;
7756
Paul Bakker48916f92012-09-16 19:57:18 +00007757 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007758 * Switch to our negotiated transform and session parameters for outbound
7759 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007764 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007765 {
7766 unsigned char i;
7767
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007768 /* Remember current epoch settings for resending */
7769 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007770 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007771
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007772 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007773 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007774
7775 /* Increment epoch */
7776 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007777 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007778 break;
7779
7780 /* The loop goes to its end iff the counter is wrapping */
7781 if( i == 0 )
7782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7784 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007785 }
7786 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007787 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007789#if defined(MBEDTLS_SSL_PROTO_TLS)
7790 {
7791 memset( ssl->cur_out_ctr, 0, 8 );
7792 }
7793#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007794
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007795 ssl->transform_out = ssl->transform_negotiate;
7796 ssl->session_out = ssl->session_negotiate;
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7799 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7804 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007805 }
7806 }
7807#endif
7808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007810 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007812#endif
7813
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007814 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 return( ret );
7818 }
7819
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007821 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007822 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7823 {
7824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7825 return( ret );
7826 }
7827#endif
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
7831 return( 0 );
7832}
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007835#define SSL_MAX_HASH_LEN 36
7836#else
7837#define SSL_MAX_HASH_LEN 12
7838#endif
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007841{
Paul Bakker23986e52011-04-24 08:57:21 +00007842 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007843 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007844 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007847
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007848 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7849 mbedtls_ssl_suite_get_mac(
7850 mbedtls_ssl_ciphersuite_from_id(
7851 mbedtls_ssl_session_get_ciphersuite(
7852 ssl->session_negotiate ) ) ),
7853 ssl, buf,
7854 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007855
Hanno Becker327c93b2018-08-15 13:56:18 +01007856 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 return( ret );
7860 }
7861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007865 mbedtls_ssl_pend_fatal_alert( ssl,
7866 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 }
7869
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007870 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007872 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007873 hash_len = 36;
7874 else
7875#endif
7876 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7879 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007882 mbedtls_ssl_pend_fatal_alert( ssl,
7883 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007885 }
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007888 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007891 mbedtls_ssl_pend_fatal_alert( ssl,
7892 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007894 }
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007897 ssl->verify_data_len = hash_len;
7898 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007899#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007900
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007901#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007902 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007905 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007909 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 }
7913 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007914#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007915 ssl->state++;
7916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007920#endif
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007923
7924 return( 0 );
7925}
7926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7932 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7933 mbedtls_md5_init( &handshake->fin_md5 );
7934 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007935 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7936 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7939#if defined(MBEDTLS_SHA256_C)
7940 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007941 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SHA512_C)
7944 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007945 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007948
Hanno Becker7e5437a2017-04-28 17:15:26 +01007949#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7950 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7951 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7952#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_DHM_C)
7955 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_ECDH_C)
7958 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007960#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007961 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007962#if defined(MBEDTLS_SSL_CLI_C)
7963 handshake->ecjpake_cache = NULL;
7964 handshake->ecjpake_cache_len = 0;
7965#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007966#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007967
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007968#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007969 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007970#endif
7971
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007972#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7973 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7974#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007975
7976#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7977 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7978 mbedtls_pk_init( &handshake->peer_pubkey );
7979#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980}
7981
Hanno Becker611a83b2018-01-03 14:27:32 +00007982void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7987 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007988
Hanno Becker92231322018-01-03 15:32:51 +00007989#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 mbedtls_md_init( &transform->md_ctx_enc );
7991 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007992#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007993}
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998}
7999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008001{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008003 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008007 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008008 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009
8010 /*
8011 * Either the pointers are now NULL or cleared properly and can be freed.
8012 * Now allocate missing structures.
8013 */
8014 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008015 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008016 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008017 }
Paul Bakker48916f92012-09-16 19:57:18 +00008018
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008019 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008020 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008021 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008022 }
Paul Bakker48916f92012-09-16 19:57:18 +00008023
Paul Bakker82788fb2014-10-20 13:59:19 +02008024 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008025 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008026 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008027 }
Paul Bakker48916f92012-09-16 19:57:18 +00008028
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008029 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008030 if( ssl->handshake == NULL ||
8031 ssl->transform_negotiate == NULL ||
8032 ssl->session_negotiate == NULL )
8033 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 mbedtls_free( ssl->handshake );
8037 mbedtls_free( ssl->transform_negotiate );
8038 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008039
8040 ssl->handshake = NULL;
8041 ssl->transform_negotiate = NULL;
8042 ssl->session_negotiate = NULL;
8043
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008044 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008045 }
8046
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008047 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008049 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008050 ssl_handshake_params_init( ssl->handshake );
8051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008053 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008054 {
8055 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008056
Hanno Becker2d9623f2019-06-13 12:07:05 +01008057 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008058 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8059 else
8060 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8061 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008062#endif
8063
Paul Bakker48916f92012-09-16 19:57:18 +00008064 return( 0 );
8065}
8066
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008067#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008068/* Dummy cookie callbacks for defaults */
8069static int ssl_cookie_write_dummy( void *ctx,
8070 unsigned char **p, unsigned char *end,
8071 const unsigned char *cli_id, size_t cli_id_len )
8072{
8073 ((void) ctx);
8074 ((void) p);
8075 ((void) end);
8076 ((void) cli_id);
8077 ((void) cli_id_len);
8078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008080}
8081
8082static int ssl_cookie_check_dummy( void *ctx,
8083 const unsigned char *cookie, size_t cookie_len,
8084 const unsigned char *cli_id, size_t cli_id_len )
8085{
8086 ((void) ctx);
8087 ((void) cookie);
8088 ((void) cookie_len);
8089 ((void) cli_id);
8090 ((void) cli_id_len);
8091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008093}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008094#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008095
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008096/* Once ssl->out_hdr as the address of the beginning of the
8097 * next outgoing record is set, deduce the other pointers.
8098 *
8099 * Note: For TLS, we save the implicit record sequence number
8100 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8101 * and the caller has to make sure there's space for this.
8102 */
8103
8104static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8105 mbedtls_ssl_transform *transform )
8106{
8107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008108 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008109 {
8110 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008111#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008112 ssl->out_cid = ssl->out_ctr + 8;
8113 ssl->out_len = ssl->out_cid;
8114 if( transform != NULL )
8115 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008116#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008117 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008118#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008119 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008120 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008121 MBEDTLS_SSL_TRANSPORT_ELSE
8122#endif /* MBEDTLS_SSL_PROTO_DTLS */
8123#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008124 {
8125 ssl->out_ctr = ssl->out_hdr - 8;
8126 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008127#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008128 ssl->out_cid = ssl->out_len;
8129#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008130 ssl->out_iv = ssl->out_hdr + 5;
8131 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008132#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133
8134 /* Adjust out_msg to make space for explicit IV, if used. */
8135 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008136 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008137 {
8138 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8139 }
8140 else
8141 ssl->out_msg = ssl->out_iv;
8142}
8143
8144/* Once ssl->in_hdr as the address of the beginning of the
8145 * next incoming record is set, deduce the other pointers.
8146 *
8147 * Note: For TLS, we save the implicit record sequence number
8148 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8149 * and the caller has to make sure there's space for this.
8150 */
8151
Hanno Beckerf5970a02019-05-08 09:38:41 +01008152static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008154 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008155 * of unprotected TLS/DTLS records, with ssl->in_msg
8156 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008157 *
8158 * When decrypting a protected record, ssl->in_msg
8159 * will be shifted to point to the beginning of the
8160 * record plaintext.
8161 */
8162
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008164 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008165 {
Hanno Becker70e79282019-05-03 14:34:53 +01008166 /* This sets the header pointers to match records
8167 * without CID. When we receive a record containing
8168 * a CID, the fields are shifted accordingly in
8169 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008170 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008172 ssl->in_cid = ssl->in_ctr + 8;
8173 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008175 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008176#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008177 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008178 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008179 MBEDTLS_SSL_TRANSPORT_ELSE
8180#endif /* MBEDTLS_SSL_PROTO_DTLS */
8181#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008182 {
8183 ssl->in_ctr = ssl->in_hdr - 8;
8184 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008186 ssl->in_cid = ssl->in_len;
8187#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008188 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008189 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008190#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008191}
8192
Paul Bakker5121ce52009-01-03 21:22:43 +00008193/*
8194 * Initialize an SSL context
8195 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008196void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8197{
8198 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8199}
8200
8201/*
8202 * Setup an SSL context
8203 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008204
8205static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8206{
8207 /* Set the incoming and outgoing record pointers. */
8208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008209 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008210 {
8211 ssl->out_hdr = ssl->out_buf;
8212 ssl->in_hdr = ssl->in_buf;
8213 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008214 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008216#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008217 {
8218 ssl->out_hdr = ssl->out_buf + 8;
8219 ssl->in_hdr = ssl->in_buf + 8;
8220 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008221#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008222
8223 /* Derive other internal pointers. */
8224 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008225 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008226}
8227
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008228int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008229 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008230{
Paul Bakker48916f92012-09-16 19:57:18 +00008231 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008232
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008233 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008234
Hanno Beckeref982d52019-07-23 15:56:18 +01008235#if defined(MBEDTLS_USE_TINYCRYPT)
8236 uECC_set_rng( &uecc_rng_wrapper );
8237#endif
8238
Paul Bakker62f2dee2012-09-28 07:31:51 +00008239 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008240 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008241 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008242
8243 /* Set to NULL in case of an error condition */
8244 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008245
Angus Grattond8213d02016-05-25 20:56:48 +10008246 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8247 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008248 {
Angus Grattond8213d02016-05-25 20:56:48 +10008249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008250 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008251 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008252 }
8253
8254 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8255 if( ssl->out_buf == NULL )
8256 {
8257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008258 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008259 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008260 }
8261
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008262 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008263
Paul Bakker48916f92012-09-16 19:57:18 +00008264 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008265 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008266
Hanno Beckerc8f52992019-07-25 11:15:08 +01008267 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008268
Paul Bakker5121ce52009-01-03 21:22:43 +00008269 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008270
8271error:
8272 mbedtls_free( ssl->in_buf );
8273 mbedtls_free( ssl->out_buf );
8274
8275 ssl->conf = NULL;
8276
8277 ssl->in_buf = NULL;
8278 ssl->out_buf = NULL;
8279
8280 ssl->in_hdr = NULL;
8281 ssl->in_ctr = NULL;
8282 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008283 ssl->in_msg = NULL;
8284
8285 ssl->out_hdr = NULL;
8286 ssl->out_ctr = NULL;
8287 ssl->out_len = NULL;
8288 ssl->out_iv = NULL;
8289 ssl->out_msg = NULL;
8290
k-stachowiak9f7798e2018-07-31 16:52:32 +02008291 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008292}
8293
8294/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008295 * Reset an initialized and used SSL context for re-use while retaining
8296 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008297 *
8298 * If partial is non-zero, keep data in the input buffer and client ID.
8299 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008300 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008301static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008302{
Paul Bakker48916f92012-09-16 19:57:18 +00008303 int ret;
8304
Hanno Becker7e772132018-08-10 12:38:21 +01008305#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8306 !defined(MBEDTLS_SSL_SRV_C)
8307 ((void) partial);
8308#endif
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008311
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008312 /* Cancel any possibly running timer */
8313 ssl_set_timer( ssl, 0 );
8314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315#if defined(MBEDTLS_SSL_RENEGOTIATION)
8316 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008317 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008318
8319 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8321 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008324
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008326 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008327
8328 ssl->in_msgtype = 0;
8329 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008331 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008332 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008335 ssl_dtls_replay_reset( ssl );
8336#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008337
8338 ssl->in_hslen = 0;
8339 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008340
8341 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008342
8343 ssl->out_msgtype = 0;
8344 ssl->out_msglen = 0;
8345 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8347 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008348 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008349#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008350
Hanno Becker19859472018-08-06 09:40:20 +01008351 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8352
Paul Bakker48916f92012-09-16 19:57:18 +00008353 ssl->transform_in = NULL;
8354 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008355
Hanno Becker78640902018-08-13 16:35:15 +01008356 ssl->session_in = NULL;
8357 ssl->session_out = NULL;
8358
Angus Grattond8213d02016-05-25 20:56:48 +10008359 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008360
8361#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008362 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008363#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8364 {
8365 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008366 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008367 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8370 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8373 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8376 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008377 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008378 }
8379#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380
Paul Bakker48916f92012-09-16 19:57:18 +00008381 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 mbedtls_ssl_transform_free( ssl->transform );
8384 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008385 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008386 }
Paul Bakker48916f92012-09-16 19:57:18 +00008387
Paul Bakkerc0463502013-02-14 11:19:38 +01008388 if( ssl->session )
8389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 mbedtls_ssl_session_free( ssl->session );
8391 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008392 ssl->session = NULL;
8393 }
8394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008396 ssl->alpn_chosen = NULL;
8397#endif
8398
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008399#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008400#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008401 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008402#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008403 {
8404 mbedtls_free( ssl->cli_id );
8405 ssl->cli_id = NULL;
8406 ssl->cli_id_len = 0;
8407 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008408#endif
8409
Paul Bakker48916f92012-09-16 19:57:18 +00008410 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8411 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008412
8413 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008414}
8415
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008416/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008417 * Reset an initialized and used SSL context for re-use while retaining
8418 * all application-set variables, function pointers and data.
8419 */
8420int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8421{
8422 return( ssl_session_reset_int( ssl, 0 ) );
8423}
8424
8425/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008426 * SSL set accessors
8427 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008428#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008429void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008431 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008432}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008433#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008434
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008435void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008436{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008437 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008438}
8439
Hanno Becker7f376f42019-06-12 16:20:48 +01008440#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8441 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008442void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008443{
Hanno Becker7f376f42019-06-12 16:20:48 +01008444 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008445}
Hanno Becker7f376f42019-06-12 16:20:48 +01008446#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008447
Hanno Beckerde671542019-06-12 16:30:46 +01008448#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8449 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8450void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8451 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008453 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008454}
Hanno Beckerde671542019-06-12 16:30:46 +01008455#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008458
Hanno Becker1841b0a2018-08-24 11:13:57 +01008459void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8460 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008461{
8462 ssl->disable_datagram_packing = !allow_packing;
8463}
8464
Hanno Becker1f835fa2019-06-13 10:14:59 +01008465#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8466 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008467void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8468 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008470 conf->hs_timeout_min = min;
8471 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008472}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008473#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8474 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8475void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8476 uint32_t min, uint32_t max )
8477{
8478 ((void) conf);
8479 ((void) min);
8480 ((void) max);
8481}
8482#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8483 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8484
8485#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008487void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008488{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008489#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8490 conf->authmode = authmode;
8491#else
8492 ((void) conf);
8493 ((void) authmode);
8494#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008495}
8496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008498void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008499 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008500 void *p_vrfy )
8501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->f_vrfy = f_vrfy;
8503 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008506
Hanno Beckerece325c2019-06-13 15:39:27 +01008507#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008508void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008509 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008510 void *p_rng )
8511{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008512 conf->f_rng = f_rng;
8513 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008514}
Hanno Beckerece325c2019-06-13 15:39:27 +01008515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008516
Hanno Becker14a4a442019-07-02 17:00:34 +01008517#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008518void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008519 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008520 void *p_dbg )
8521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008522 conf->f_dbg = f_dbg;
8523 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008524}
Hanno Becker14a4a442019-07-02 17:00:34 +01008525#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008526
Hanno Beckera58a8962019-06-13 16:11:15 +01008527#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8528 !defined(MBEDTLS_SSL_CONF_SEND) && \
8529 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008531 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008532 mbedtls_ssl_send_t *f_send,
8533 mbedtls_ssl_recv_t *f_recv,
8534 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008535{
Hanno Beckera58a8962019-06-13 16:11:15 +01008536 ssl->p_bio = p_bio;
8537 ssl->f_send = f_send;
8538 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008539 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008540}
Hanno Beckera58a8962019-06-13 16:11:15 +01008541#else
8542void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8543 void *p_bio )
8544{
8545 ssl->p_bio = p_bio;
8546}
8547#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008548
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008549#if defined(MBEDTLS_SSL_PROTO_DTLS)
8550void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8551{
8552 ssl->mtu = mtu;
8553}
8554#endif
8555
Hanno Becker1f835fa2019-06-13 10:14:59 +01008556#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008557void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008558{
8559 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008560}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008561#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008562
Hanno Becker0ae6b242019-06-13 16:45:36 +01008563#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8564 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008565void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8566 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008567 mbedtls_ssl_set_timer_t *f_set_timer,
8568 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008569{
8570 ssl->p_timer = p_timer;
8571 ssl->f_set_timer = f_set_timer;
8572 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008573 /* Make sure we start with no timer running */
8574 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008575}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008576#else
8577void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8578 void *p_timer )
8579{
8580 ssl->p_timer = p_timer;
8581 /* Make sure we start with no timer running */
8582 ssl_set_timer( ssl, 0 );
8583}
8584#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008585
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008586#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008587void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008588 void *p_cache,
8589 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8590 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008591{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008592 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008593 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008594 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008595}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008596#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008597
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008598#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008600{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008601 int ret;
8602
8603 if( ssl == NULL ||
8604 session == NULL ||
8605 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008606 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008609 }
8610
Hanno Becker58fccf22019-02-06 14:30:46 +00008611 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8612 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008613 return( ret );
8614
Paul Bakker0a597072012-09-25 21:55:46 +00008615 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008616
8617 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008618}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008619#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008620
Hanno Becker73f4cb12019-06-27 13:51:07 +01008621#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008622void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008624{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008625 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8626 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8627 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008629}
8630
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008631void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008632 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008633 int major, int minor )
8634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008636 return;
8637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008639 return;
8640
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008641 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008642}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008643#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008646void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008647 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008648{
8649 conf->cert_profile = profile;
8650}
8651
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008652/* Append a new keycert entry to a (possibly empty) list */
8653static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8654 mbedtls_x509_crt *cert,
8655 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008656{
niisato8ee24222018-06-25 19:05:48 +09008657 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008658
niisato8ee24222018-06-25 19:05:48 +09008659 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8660 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008661 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008662
niisato8ee24222018-06-25 19:05:48 +09008663 new_cert->cert = cert;
8664 new_cert->key = key;
8665 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008666
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008667 /* Update head is the list was null, else add to the end */
8668 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008669 {
niisato8ee24222018-06-25 19:05:48 +09008670 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008671 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008672 else
8673 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008674 mbedtls_ssl_key_cert *cur = *head;
8675 while( cur->next != NULL )
8676 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008677 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008678 }
8679
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008680 return( 0 );
8681}
8682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008683int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008684 mbedtls_x509_crt *own_cert,
8685 mbedtls_pk_context *pk_key )
8686{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008687 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008688}
8689
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008690void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008691 mbedtls_x509_crt *ca_chain,
8692 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008693{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008694 conf->ca_chain = ca_chain;
8695 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008698
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008699#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8700int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8701 mbedtls_x509_crt *own_cert,
8702 mbedtls_pk_context *pk_key )
8703{
8704 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8705 own_cert, pk_key ) );
8706}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008707
8708void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8709 mbedtls_x509_crt *ca_chain,
8710 mbedtls_x509_crl *ca_crl )
8711{
8712 ssl->handshake->sni_ca_chain = ca_chain;
8713 ssl->handshake->sni_ca_crl = ca_crl;
8714}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008715
8716void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8717 int authmode )
8718{
8719 ssl->handshake->sni_authmode = authmode;
8720}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008721#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8722
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008723#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008724/*
8725 * Set EC J-PAKE password for current handshake
8726 */
8727int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8728 const unsigned char *pw,
8729 size_t pw_len )
8730{
8731 mbedtls_ecjpake_role role;
8732
Janos Follath8eb64132016-06-03 15:40:57 +01008733 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8735
Hanno Becker2d9623f2019-06-13 12:07:05 +01008736 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008737 role = MBEDTLS_ECJPAKE_SERVER;
8738 else
8739 role = MBEDTLS_ECJPAKE_CLIENT;
8740
8741 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8742 role,
8743 MBEDTLS_MD_SHA256,
8744 MBEDTLS_ECP_DP_SECP256R1,
8745 pw, pw_len ) );
8746}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008747#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008750int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008751 const unsigned char *psk, size_t psk_len,
8752 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008753{
Paul Bakker6db455e2013-09-18 17:29:31 +02008754 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008759
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008760 /* Identity len will be encoded on two bytes */
8761 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008762 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008763 {
8764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8765 }
8766
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008767 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008768 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008769 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008770
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008771 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008772 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008773 conf->psk_len = 0;
8774 }
8775 if( conf->psk_identity != NULL )
8776 {
8777 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008778 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008779 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008780 }
8781
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008782 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8783 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008784 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008785 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008786 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008787 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008788 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008789 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008790 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008791
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008792 conf->psk_len = psk_len;
8793 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008794
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008795 memcpy( conf->psk, psk, conf->psk_len );
8796 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008797
8798 return( 0 );
8799}
8800
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008801int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8802 const unsigned char *psk, size_t psk_len )
8803{
8804 if( psk == NULL || ssl->handshake == NULL )
8805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8806
8807 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809
8810 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008811 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008812 mbedtls_platform_zeroize( ssl->handshake->psk,
8813 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008814 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008815 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008816 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008817
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008818 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008819 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008820
8821 ssl->handshake->psk_len = psk_len;
8822 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8823
8824 return( 0 );
8825}
8826
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008827void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008829 size_t),
8830 void *p_psk )
8831{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008832 conf->f_psk = f_psk;
8833 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008836
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008837#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008838
8839#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008840int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008841{
8842 int ret;
8843
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008844 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8845 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8846 {
8847 mbedtls_mpi_free( &conf->dhm_P );
8848 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008851
8852 return( 0 );
8853}
Hanno Becker470a8c42017-10-04 15:28:46 +01008854#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008855
Hanno Beckera90658f2017-10-04 15:29:08 +01008856int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8857 const unsigned char *dhm_P, size_t P_len,
8858 const unsigned char *dhm_G, size_t G_len )
8859{
8860 int ret;
8861
8862 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8863 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8864 {
8865 mbedtls_mpi_free( &conf->dhm_P );
8866 mbedtls_mpi_free( &conf->dhm_G );
8867 return( ret );
8868 }
8869
8870 return( 0 );
8871}
Paul Bakker5121ce52009-01-03 21:22:43 +00008872
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008874{
8875 int ret;
8876
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008877 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8878 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8879 {
8880 mbedtls_mpi_free( &conf->dhm_P );
8881 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008882 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008883 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008884
8885 return( 0 );
8886}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008887#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008888
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008889#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8890/*
8891 * Set the minimum length for Diffie-Hellman parameters
8892 */
8893void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8894 unsigned int bitlen )
8895{
8896 conf->dhm_min_bitlen = bitlen;
8897}
8898#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8899
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008900#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008901/*
8902 * Set allowed/preferred hashes for handshake signatures
8903 */
8904void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8905 const int *hashes )
8906{
Hanno Becker56595f42019-06-19 16:31:38 +01008907#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008908 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008909#else
8910 ((void) conf);
8911 ((void) hashes);
8912#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008913}
Hanno Becker947194e2017-04-07 13:25:49 +01008914#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008915
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008916#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008917#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008918/*
8919 * Set the allowed elliptic curves
8920 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008921void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008922 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008924 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008925}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008926#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008927#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008928
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008929#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008931{
Hanno Becker947194e2017-04-07 13:25:49 +01008932 /* Initialize to suppress unnecessary compiler warning */
8933 size_t hostname_len = 0;
8934
8935 /* Check if new hostname is valid before
8936 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008937 if( hostname != NULL )
8938 {
8939 hostname_len = strlen( hostname );
8940
8941 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8943 }
8944
8945 /* Now it's clear that we will overwrite the old hostname,
8946 * so we can free it safely */
8947
8948 if( ssl->hostname != NULL )
8949 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008950 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008951 mbedtls_free( ssl->hostname );
8952 }
8953
8954 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008955
Paul Bakker5121ce52009-01-03 21:22:43 +00008956 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008957 {
8958 ssl->hostname = NULL;
8959 }
8960 else
8961 {
8962 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008963 if( ssl->hostname == NULL )
8964 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008965
Hanno Becker947194e2017-04-07 13:25:49 +01008966 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008967
Hanno Becker947194e2017-04-07 13:25:49 +01008968 ssl->hostname[hostname_len] = '\0';
8969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008970
8971 return( 0 );
8972}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008973#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008974
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008975#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008976void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008978 const unsigned char *, size_t),
8979 void *p_sni )
8980{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008981 conf->f_sni = f_sni;
8982 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008987int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008988{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008989 size_t cur_len, tot_len;
8990 const char **p;
8991
8992 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008993 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8994 * MUST NOT be truncated."
8995 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008996 */
8997 tot_len = 0;
8998 for( p = protos; *p != NULL; p++ )
8999 {
9000 cur_len = strlen( *p );
9001 tot_len += cur_len;
9002
9003 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009005 }
9006
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009007 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009008
9009 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009010}
9011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009014 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009017
Hanno Becker33b9b252019-07-05 11:23:25 +01009018#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9019 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9020void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9021 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009022{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009023 conf->max_major_ver = major;
9024 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009025}
Hanno Becker33b9b252019-07-05 11:23:25 +01009026#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9027 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009028
Hanno Becker33b9b252019-07-05 11:23:25 +01009029#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9030 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9031void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9032 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009033{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009034 conf->min_major_ver = major;
9035 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009036}
Hanno Becker33b9b252019-07-05 11:23:25 +01009037#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9038 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009041void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009042{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009043 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009044}
9045#endif
9046
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009047#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009048void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9049 char cert_req_ca_list )
9050{
9051 conf->cert_req_ca_list = cert_req_ca_list;
9052}
9053#endif
9054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009059}
9060#endif
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009063#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009064void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009066 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009067}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009068#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9069#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009070void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009071 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009072{
9073 conf->enforce_extended_master_secret = ems_enf;
9074}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009075#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009076#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009077
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009078#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009079void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009080{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009081 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009082}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009083#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009086int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009089 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009092 }
9093
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009094 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009095
9096 return( 0 );
9097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009101void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009103 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009108void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009109{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009110 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009111}
9112#endif
9113
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009114#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009118}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009119#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009122void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009124 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009125}
9126
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009127void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009129 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009130}
9131
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009132void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009133 const unsigned char period[8] )
9134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009135 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009137#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009140#if defined(MBEDTLS_SSL_CLI_C)
9141void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009142{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009143 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009144}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009145#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009146
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009147#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009148void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9149 mbedtls_ssl_ticket_write_t *f_ticket_write,
9150 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9151 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009152{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009153 conf->f_ticket_write = f_ticket_write;
9154 conf->f_ticket_parse = f_ticket_parse;
9155 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009156}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009158#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009159
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009160#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9161void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9162 mbedtls_ssl_export_keys_t *f_export_keys,
9163 void *p_export_keys )
9164{
9165 conf->f_export_keys = f_export_keys;
9166 conf->p_export_keys = p_export_keys;
9167}
9168#endif
9169
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009170#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009171void mbedtls_ssl_conf_async_private_cb(
9172 mbedtls_ssl_config *conf,
9173 mbedtls_ssl_async_sign_t *f_async_sign,
9174 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9175 mbedtls_ssl_async_resume_t *f_async_resume,
9176 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009177 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009178{
9179 conf->f_async_sign_start = f_async_sign;
9180 conf->f_async_decrypt_start = f_async_decrypt;
9181 conf->f_async_resume = f_async_resume;
9182 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009183 conf->p_async_config_data = async_config_data;
9184}
9185
Gilles Peskine8f97af72018-04-26 11:46:10 +02009186void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9187{
9188 return( conf->p_async_config_data );
9189}
9190
Gilles Peskine1febfef2018-04-30 11:54:39 +02009191void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009192{
9193 if( ssl->handshake == NULL )
9194 return( NULL );
9195 else
9196 return( ssl->handshake->user_async_ctx );
9197}
9198
Gilles Peskine1febfef2018-04-30 11:54:39 +02009199void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009200 void *ctx )
9201{
9202 if( ssl->handshake != NULL )
9203 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009205#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009206
Paul Bakker5121ce52009-01-03 21:22:43 +00009207/*
9208 * SSL get accessors
9209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009211{
9212 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9213}
9214
Hanno Becker8b170a02017-10-10 11:51:19 +01009215int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9216{
9217 /*
9218 * Case A: We're currently holding back
9219 * a message for further processing.
9220 */
9221
9222 if( ssl->keep_current_message == 1 )
9223 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009225 return( 1 );
9226 }
9227
9228 /*
9229 * Case B: Further records are pending in the current datagram.
9230 */
9231
9232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009233 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009234 ssl->in_left > ssl->next_record_offset )
9235 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 return( 1 );
9238 }
9239#endif /* MBEDTLS_SSL_PROTO_DTLS */
9240
9241 /*
9242 * Case C: A handshake message is being processed.
9243 */
9244
Hanno Becker8b170a02017-10-10 11:51:19 +01009245 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9246 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009248 return( 1 );
9249 }
9250
9251 /*
9252 * Case D: An application data message is being processed
9253 */
9254 if( ssl->in_offt != NULL )
9255 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 return( 1 );
9258 }
9259
9260 /*
9261 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009262 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009263 * we implement support for multiple alerts in single records.
9264 */
9265
9266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9267 return( 0 );
9268}
9269
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009270uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009271{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009272 if( ssl->session != NULL )
9273 return( ssl->session->verify_result );
9274
9275 if( ssl->session_negotiate != NULL )
9276 return( ssl->session_negotiate->verify_result );
9277
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009278 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009279}
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009282{
Hanno Beckere02758c2019-06-26 15:31:31 +01009283 int suite;
9284
Paul Bakker926c8e42013-03-06 10:23:34 +01009285 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009286 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009287
Hanno Beckere02758c2019-06-26 15:31:31 +01009288 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9289 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009290}
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009295 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009296 {
Hanno Becker2881d802019-05-22 14:44:53 +01009297 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009300 return( "DTLSv1.0" );
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009303 return( "DTLSv1.2" );
9304
9305 default:
9306 return( "unknown (DTLS)" );
9307 }
9308 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009309 MBEDTLS_SSL_TRANSPORT_ELSE
9310#endif /* MBEDTLS_SSL_PROTO_DTLS */
9311#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009312 {
Hanno Becker2881d802019-05-22 14:44:53 +01009313 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009314 {
9315 case MBEDTLS_SSL_MINOR_VERSION_0:
9316 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009317
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009318 case MBEDTLS_SSL_MINOR_VERSION_1:
9319 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009321 case MBEDTLS_SSL_MINOR_VERSION_2:
9322 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009324 case MBEDTLS_SSL_MINOR_VERSION_3:
9325 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009326
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009327 default:
9328 return( "unknown" );
9329 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009331#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009335{
Hanno Becker3136ede2018-08-17 15:28:19 +01009336 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009338
Hanno Becker43395762019-05-03 14:46:38 +01009339 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9340
Hanno Becker78640902018-08-13 16:35:15 +01009341 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009342 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_ZLIB_SUPPORT)
9345 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9346 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009347#endif
9348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009350 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009351#if defined(MBEDTLS_GCM_C) || \
9352 defined(MBEDTLS_CCM_C) || \
9353 defined(MBEDTLS_CHACHAPOLY_C)
9354#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009356#endif
9357#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009359#endif
9360#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009361 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009362#endif
9363 transform_expansion =
9364 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009365 break;
9366
Hanno Beckera9d5c452019-07-25 16:47:12 +01009367#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9368 MBEDTLS_CHACHAPOLY_C */
9369
9370#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9371 case MBEDTLS_MODE_STREAM:
9372 transform_expansion = transform->maclen;
9373 break;
9374#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9375
9376#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009378 {
9379 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009380
9381 block_size = mbedtls_cipher_get_block_size(
9382 &transform->cipher_ctx_enc );
9383
Hanno Becker3136ede2018-08-17 15:28:19 +01009384 /* Expansion due to the addition of the MAC. */
9385 transform_expansion += transform->maclen;
9386
9387 /* Expansion due to the addition of CBC padding;
9388 * Theoretically up to 256 bytes, but we never use
9389 * more than the block size of the underlying cipher. */
9390 transform_expansion += block_size;
9391
9392 /* For TLS 1.1 or higher, an explicit IV is added
9393 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009394#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009395 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009396 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009397#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009398
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009399 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009400 }
9401#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009402
9403 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009406 }
9407
Hanno Beckera5a2b082019-05-15 14:03:01 +01009408#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009409 if( transform->out_cid_len != 0 )
9410 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009411#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009412
Hanno Becker43395762019-05-03 14:46:38 +01009413 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009414}
9415
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009416#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9417size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9418{
9419 size_t max_len;
9420
9421 /*
9422 * Assume mfl_code is correct since it was checked when set
9423 */
Angus Grattond8213d02016-05-25 20:56:48 +10009424 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009425
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009426 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009427 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009428 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009429 {
Angus Grattond8213d02016-05-25 20:56:48 +10009430 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009431 }
9432
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009433 /* During a handshake, use the value being negotiated */
9434 if( ssl->session_negotiate != NULL &&
9435 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9436 {
9437 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9438 }
9439
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009440 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009441}
9442#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9443
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009444#if defined(MBEDTLS_SSL_PROTO_DTLS)
9445static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9446{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009447 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009448 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009449 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9450 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009451 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009452
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009453 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9454 return( ssl->mtu );
9455
9456 if( ssl->mtu == 0 )
9457 return( ssl->handshake->mtu );
9458
9459 return( ssl->mtu < ssl->handshake->mtu ?
9460 ssl->mtu : ssl->handshake->mtu );
9461}
9462#endif /* MBEDTLS_SSL_PROTO_DTLS */
9463
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009464int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9465{
9466 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9467
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009468#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9469 !defined(MBEDTLS_SSL_PROTO_DTLS)
9470 (void) ssl;
9471#endif
9472
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009473#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9474 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9475
9476 if( max_len > mfl )
9477 max_len = mfl;
9478#endif
9479
9480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009481 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009483 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009484 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9485 const size_t overhead = (size_t) ret;
9486
9487 if( ret < 0 )
9488 return( ret );
9489
9490 if( mtu <= overhead )
9491 {
9492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9493 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9494 }
9495
9496 if( max_len > mtu - overhead )
9497 max_len = mtu - overhead;
9498 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009499#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009500
Hanno Becker0defedb2018-08-10 12:35:02 +01009501#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9502 !defined(MBEDTLS_SSL_PROTO_DTLS)
9503 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009504#endif
9505
9506 return( (int) max_len );
9507}
9508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509#if defined(MBEDTLS_X509_CRT_PARSE_C)
9510const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009511{
9512 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009513 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009514
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009515#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009516 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009517#else
9518 return( NULL );
9519#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009524int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9525 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009526{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009527 if( ssl == NULL ||
9528 dst == NULL ||
9529 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009530 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009533 }
9534
Hanno Becker58fccf22019-02-06 14:30:46 +00009535 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009538
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009539const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9540{
9541 if( ssl == NULL )
9542 return( NULL );
9543
9544 return( ssl->session );
9545}
9546
Paul Bakker5121ce52009-01-03 21:22:43 +00009547/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009548 * Define ticket header determining Mbed TLS version
9549 * and structure of the ticket.
9550 */
9551
Hanno Becker41527622019-05-16 12:50:45 +01009552/*
Hanno Becker26829e92019-05-28 14:30:45 +01009553 * Define bitflag determining compile-time settings influencing
9554 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009555 */
9556
Hanno Becker26829e92019-05-28 14:30:45 +01009557#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009558#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009559#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009560#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009561#endif /* MBEDTLS_HAVE_TIME */
9562
9563#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009564#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009565#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009566#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009567#endif /* MBEDTLS_X509_CRT_PARSE_C */
9568
9569#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009570#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009571#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009572#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009573#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9574
9575#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009576#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009577#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009578#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009579#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9580
9581#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009582#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009583#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009584#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009585#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9586
9587#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009588#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009589#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009590#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009591#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9592
Hanno Becker41527622019-05-16 12:50:45 +01009593#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9594#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9595#else
9596#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9597#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9598
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009599#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9600#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9601#else
9602#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9603#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9604
Hanno Becker88440552019-07-03 14:16:13 +01009605#if defined(MBEDTLS_ZLIB_SUPPORT)
9606#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9607#else
9608#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9609#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9610
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009611#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9612#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9613#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9614#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9615#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9616#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9617#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009618#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009619#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009620
Hanno Becker26829e92019-05-28 14:30:45 +01009621#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009622 ( (uint16_t) ( \
9623 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9624 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9625 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9626 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9627 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9628 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009629 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009630 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009631 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009632
Hanno Becker557fe9f2019-05-16 12:41:07 +01009633static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009634 MBEDTLS_VERSION_MAJOR,
9635 MBEDTLS_VERSION_MINOR,
9636 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009637 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9638 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009639};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009640
9641/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009642 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009643 * (in the presentation language of TLS, RFC 8446 section 3)
9644 *
Hanno Becker26829e92019-05-28 14:30:45 +01009645 * opaque mbedtls_version[3]; // major, minor, patch
9646 * opaque session_format[2]; // version-specific 16-bit field determining
9647 * // the format of the remaining
9648 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009649 *
9650 * Note: When updating the format, remember to keep
9651 * these version+format bytes.
9652 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009653 * // In this version, `session_format` determines
9654 * // the setting of those compile-time
9655 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009656 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009657 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009658 * uint8 ciphersuite[2]; // defined by the standard
9659 * uint8 compression; // 0 or 1
9660 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009661 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009662 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009663 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009664 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9665 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9666 * case disabled: uint8_t peer_cert_digest_type;
9667 * opaque peer_cert_digest<0..2^8-1>;
9668 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009669 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009670 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009671 * uint8 mfl_code; // up to 255 according to standard
9672 * uint8 trunc_hmac; // 0 or 1
9673 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009674 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009675 * The order is the same as in the definition of the structure, except
9676 * verify_result is put before peer_cert so that all mandatory fields come
9677 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009678 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009679static int ssl_session_save( const mbedtls_ssl_session *session,
9680 unsigned char omit_header,
9681 unsigned char *buf,
9682 size_t buf_len,
9683 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009684{
9685 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009686 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009687#if defined(MBEDTLS_HAVE_TIME)
9688 uint64_t start;
9689#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009690#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009691#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009692 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009693#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009694#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009695
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009696 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009697 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009698 /*
9699 * Add version identifier
9700 */
9701
9702 used += sizeof( ssl_serialized_session_header );
9703
9704 if( used <= buf_len )
9705 {
9706 memcpy( p, ssl_serialized_session_header,
9707 sizeof( ssl_serialized_session_header ) );
9708 p += sizeof( ssl_serialized_session_header );
9709 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009710 }
9711
9712 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009713 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009714 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009715#if defined(MBEDTLS_HAVE_TIME)
9716 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009717
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009718 if( used <= buf_len )
9719 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009720 start = (uint64_t) session->start;
9721
9722 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9723 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9724 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9725 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9727 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9728 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9729 *p++ = (unsigned char)( ( start ) & 0xFF );
9730 }
9731#endif /* MBEDTLS_HAVE_TIME */
9732
9733 /*
9734 * Basic mandatory fields
9735 */
Hanno Becker88440552019-07-03 14:16:13 +01009736 {
9737 size_t const ciphersuite_len = 2;
9738#if defined(MBEDTLS_ZLIB_SUPPORT)
9739 size_t const compression_len = 1;
9740#else
9741 size_t const compression_len = 0;
9742#endif
9743 size_t const id_len_len = 1;
9744 size_t const id_len = 32;
9745 size_t const master_len = 48;
9746 size_t const verif_result_len = 4;
9747
9748 size_t const basic_len =
9749 ciphersuite_len +
9750 compression_len +
9751 id_len_len +
9752 id_len +
9753 master_len +
9754 verif_result_len;
9755
9756 used += basic_len;
9757 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009758
9759 if( used <= buf_len )
9760 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009761 const int ciphersuite =
9762 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009763 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009764
Hanno Becker88440552019-07-03 14:16:13 +01009765#if defined(MBEDTLS_ZLIB_SUPPORT)
9766 *p++ = (unsigned char)(
9767 mbedtls_ssl_session_get_compression( session ) );
9768#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009769
9770 *p++ = (unsigned char)( session->id_len & 0xFF );
9771 memcpy( p, session->id, 32 );
9772 p += 32;
9773
9774 memcpy( p, session->master, 48 );
9775 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009776 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009777 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009778
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009779 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009780 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009781 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009782#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009783#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009784 if( session->peer_cert == NULL )
9785 cert_len = 0;
9786 else
9787 cert_len = session->peer_cert->raw.len;
9788
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009789 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009790
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009791 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009792 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009793 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009794
9795 if( session->peer_cert != NULL )
9796 {
9797 memcpy( p, session->peer_cert->raw.p, cert_len );
9798 p += cert_len;
9799 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009800 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009801
Hanno Becker5882dd02019-06-06 16:25:57 +01009802#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009803 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009804 if( session->peer_cert_digest != NULL )
9805 {
9806 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9807 if( used <= buf_len )
9808 {
9809 *p++ = (unsigned char) session->peer_cert_digest_type;
9810 *p++ = (unsigned char) session->peer_cert_digest_len;
9811 memcpy( p, session->peer_cert_digest,
9812 session->peer_cert_digest_len );
9813 p += session->peer_cert_digest_len;
9814 }
9815 }
9816 else
9817 {
9818 used += 2;
9819 if( used <= buf_len )
9820 {
9821 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9822 *p++ = 0;
9823 }
9824 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009825#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009826#endif /* MBEDTLS_X509_CRT_PARSE_C */
9827
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009828 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009829 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009830 */
9831#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009832 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009833
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009834 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009835 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009836 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009837
9838 if( session->ticket != NULL )
9839 {
9840 memcpy( p, session->ticket, session->ticket_len );
9841 p += session->ticket_len;
9842 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009843
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009844 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009845 }
9846#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9847
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009848 /*
9849 * Misc extension-related info
9850 */
9851#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9852 used += 1;
9853
9854 if( used <= buf_len )
9855 *p++ = session->mfl_code;
9856#endif
9857
9858#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9859 used += 1;
9860
9861 if( used <= buf_len )
9862 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9863#endif
9864
9865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9866 used += 1;
9867
9868 if( used <= buf_len )
9869 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9870#endif
9871
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009872 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009873 *olen = used;
9874
9875 if( used > buf_len )
9876 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009877
9878 return( 0 );
9879}
9880
9881/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009882 * Public wrapper for ssl_session_save()
9883 */
9884int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9885 unsigned char *buf,
9886 size_t buf_len,
9887 size_t *olen )
9888{
9889 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9890}
9891
9892/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009893 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009894 *
9895 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009896 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009897 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009898static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009899 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009900 const unsigned char *buf,
9901 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009902{
9903 const unsigned char *p = buf;
9904 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009905 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009906#if defined(MBEDTLS_HAVE_TIME)
9907 uint64_t start;
9908#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009909#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009910#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009911 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009912#endif
9913#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009914
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009915 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009916 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009917 /*
9918 * Check version identifier
9919 */
9920
9921 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9923
9924 if( memcmp( p, ssl_serialized_session_header,
9925 sizeof( ssl_serialized_session_header ) ) != 0 )
9926 {
9927 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9928 }
9929 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009930 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009931
9932 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009933 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009934 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009935#if defined(MBEDTLS_HAVE_TIME)
9936 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9938
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009939 start = ( (uint64_t) p[0] << 56 ) |
9940 ( (uint64_t) p[1] << 48 ) |
9941 ( (uint64_t) p[2] << 40 ) |
9942 ( (uint64_t) p[3] << 32 ) |
9943 ( (uint64_t) p[4] << 24 ) |
9944 ( (uint64_t) p[5] << 16 ) |
9945 ( (uint64_t) p[6] << 8 ) |
9946 ( (uint64_t) p[7] );
9947 p += 8;
9948
9949 session->start = (time_t) start;
9950#endif /* MBEDTLS_HAVE_TIME */
9951
9952 /*
9953 * Basic mandatory fields
9954 */
Hanno Becker88440552019-07-03 14:16:13 +01009955 {
9956 size_t const ciphersuite_len = 2;
9957#if defined(MBEDTLS_ZLIB_SUPPORT)
9958 size_t const compression_len = 1;
9959#else
9960 size_t const compression_len = 0;
9961#endif
9962 size_t const id_len_len = 1;
9963 size_t const id_len = 32;
9964 size_t const master_len = 48;
9965 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009966
Hanno Becker88440552019-07-03 14:16:13 +01009967 size_t const basic_len =
9968 ciphersuite_len +
9969 compression_len +
9970 id_len_len +
9971 id_len +
9972 master_len +
9973 verif_result_len;
9974
9975 if( basic_len > (size_t)( end - p ) )
9976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9977 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009978
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009979 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009980 p += 2;
9981
Hanno Becker73f4cb12019-06-27 13:51:07 +01009982#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009983 session->ciphersuite = ciphersuite;
9984#else
9985 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009986 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009987 {
9988 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9989 }
9990#endif
9991
Hanno Becker88440552019-07-03 14:16:13 +01009992#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009993 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +01009994#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009995
9996 session->id_len = *p++;
9997 memcpy( session->id, p, 32 );
9998 p += 32;
9999
10000 memcpy( session->master, p, 48 );
10001 p += 48;
10002
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010003 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010004 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010005
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010006 /* Immediately clear invalid pointer values that have been read, in case
10007 * we exit early before we replaced them with valid ones. */
10008#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010009#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010010 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010011#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010012 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010013#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010014#endif
10015#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10016 session->ticket = NULL;
10017#endif
10018
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010019 /*
10020 * Peer certificate
10021 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010022#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010023#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010024 if( 3 > (size_t)( end - p ) )
10025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10026
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010027 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10028
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010029 p += 3;
10030
10031 if( cert_len == 0 )
10032 {
10033 session->peer_cert = NULL;
10034 }
10035 else
10036 {
10037 int ret;
10038
10039 if( cert_len > (size_t)( end - p ) )
10040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10041
10042 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10043
10044 if( session->peer_cert == NULL )
10045 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10046
10047 mbedtls_x509_crt_init( session->peer_cert );
10048
10049 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10050 p, cert_len ) ) != 0 )
10051 {
10052 mbedtls_x509_crt_free( session->peer_cert );
10053 mbedtls_free( session->peer_cert );
10054 session->peer_cert = NULL;
10055 return( ret );
10056 }
10057
10058 p += cert_len;
10059 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010060#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010061 /* Deserialize CRT digest from the end of the ticket. */
10062 if( 2 > (size_t)( end - p ) )
10063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10064
10065 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10066 session->peer_cert_digest_len = (size_t) *p++;
10067
10068 if( session->peer_cert_digest_len != 0 )
10069 {
Hanno Becker2326d202019-06-06 14:54:55 +010010070 const mbedtls_md_info_t *md_info =
10071 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10072 if( md_info == NULL )
10073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10074 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10076
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010077 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10079
10080 session->peer_cert_digest =
10081 mbedtls_calloc( 1, session->peer_cert_digest_len );
10082 if( session->peer_cert_digest == NULL )
10083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10084
10085 memcpy( session->peer_cert_digest, p,
10086 session->peer_cert_digest_len );
10087 p += session->peer_cert_digest_len;
10088 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010089#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010090#endif /* MBEDTLS_X509_CRT_PARSE_C */
10091
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010092 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010093 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010094 */
10095#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10096 if( 3 > (size_t)( end - p ) )
10097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10098
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010099 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010100 p += 3;
10101
10102 if( session->ticket_len != 0 )
10103 {
10104 if( session->ticket_len > (size_t)( end - p ) )
10105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10106
10107 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10108 if( session->ticket == NULL )
10109 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10110
10111 memcpy( session->ticket, p, session->ticket_len );
10112 p += session->ticket_len;
10113 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010114
10115 if( 4 > (size_t)( end - p ) )
10116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10117
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010118 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010119 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010120#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10121
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010122 /*
10123 * Misc extension-related info
10124 */
10125#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10126 if( 1 > (size_t)( end - p ) )
10127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10128
10129 session->mfl_code = *p++;
10130#endif
10131
10132#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10133 if( 1 > (size_t)( end - p ) )
10134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10135
10136 session->trunc_hmac = *p++;
10137#endif
10138
10139#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10140 if( 1 > (size_t)( end - p ) )
10141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10142
10143 session->encrypt_then_mac = *p++;
10144#endif
10145
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010146 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010147 if( p != end )
10148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10149
10150 return( 0 );
10151}
10152
10153/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010154 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010155 */
10156int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10157 const unsigned char *buf,
10158 size_t len )
10159{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010160 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010161
10162 if( ret != 0 )
10163 mbedtls_ssl_session_free( session );
10164
10165 return( ret );
10166}
10167
10168/*
Paul Bakker1961b702013-01-25 14:49:24 +010010169 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010174
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010175 if( ssl == NULL || ssl->conf == NULL )
10176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010179 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010183 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010185#endif
10186
Hanno Beckerb82350b2019-07-26 07:24:05 +010010187 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010188 return( ret );
10189}
10190
10191/*
10192 * Perform the SSL handshake
10193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010195{
10196 int ret = 0;
10197
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010198 if( ssl == NULL || ssl->conf == NULL )
10199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010206
10207 if( ret != 0 )
10208 break;
10209 }
10210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010212
10213 return( ret );
10214}
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_SSL_RENEGOTIATION)
10217#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010218/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010219 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010222{
10223 int ret;
10224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010226
10227 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10229 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010230
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010231 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010232 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010234 return( ret );
10235 }
10236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010238
10239 return( 0 );
10240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010242
10243/*
10244 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 * - any side: calling mbedtls_ssl_renegotiate(),
10246 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10247 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010248 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010249 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010253{
10254 int ret;
10255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010257
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010258 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10259 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010260
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010261 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10262 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010264 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010266 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010267 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10268 MBEDTLS_SSL_IS_SERVER )
10269 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010270 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010271 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010272 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010273 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010274 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010275 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010276 }
10277#endif
10278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10280 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010285 return( ret );
10286 }
10287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010289
10290 return( 0 );
10291}
10292
10293/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010294 * Renegotiate current connection on client,
10295 * or request renegotiation on server
10296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010300
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010301 if( ssl == NULL || ssl->conf == NULL )
10302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010305 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010306 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010312
10313 /* Did we already try/start sending HelloRequest? */
10314 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010316
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010317 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010322 /*
10323 * On client, either start the renegotiation process or,
10324 * if already in progress, continue the handshake
10325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010330
10331 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010334 return( ret );
10335 }
10336 }
10337 else
10338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010342 return( ret );
10343 }
10344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010346
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010347 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010348}
10349
10350/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010351 * Check record counters and renegotiate if they're above the limit.
10352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010354{
Andres AG2196c7f2016-12-15 17:01:16 +000010355 size_t ep_len = ssl_ep_len( ssl );
10356 int in_ctr_cmp;
10357 int out_ctr_cmp;
10358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10360 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010361 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010362 {
10363 return( 0 );
10364 }
10365
Andres AG2196c7f2016-12-15 17:01:16 +000010366 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10367 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010368 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010369 ssl->conf->renego_period + ep_len, 8 - ep_len );
10370
10371 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010372 {
10373 return( 0 );
10374 }
10375
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010380
10381/*
10382 * Receive application data decrypted from the SSL layer
10383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010385{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010386 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010387 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010388
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010389 if( ssl == NULL || ssl->conf == NULL )
10390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010395 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010398 return( ret );
10399
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010400 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010402 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010403 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010404 return( ret );
10405 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010406 }
10407#endif
10408
Hanno Becker4a810fb2017-05-24 16:27:30 +010010409 /*
10410 * Check if renegotiation is necessary and/or handshake is
10411 * in process. If yes, perform/continue, and fall through
10412 * if an unexpected packet is received while the client
10413 * is waiting for the ServerHello.
10414 *
10415 * (There is no equivalent to the last condition on
10416 * the server-side as it is not treated as within
10417 * a handshake while waiting for the ClientHello
10418 * after a renegotiation request.)
10419 */
10420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010422 ret = ssl_check_ctr_renegotiate( ssl );
10423 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10424 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010427 return( ret );
10428 }
10429#endif
10430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010434 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10435 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010438 return( ret );
10439 }
10440 }
10441
Hanno Beckere41158b2017-10-23 13:30:32 +010010442 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010443 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010444 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010445 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010446 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10447 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010448 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010449 ssl_set_timer( ssl,
10450 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010451 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010452
Hanno Becker327c93b2018-08-15 13:56:18 +010010453 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010454 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010455 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10456 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010457
Hanno Becker4a810fb2017-05-24 16:27:30 +010010458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10459 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 }
10461
10462 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010464 {
10465 /*
10466 * OpenSSL sends empty messages to randomize the IV
10467 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010468 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010471 return( 0 );
10472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 return( ret );
10475 }
10476 }
10477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010481
Hanno Becker4a810fb2017-05-24 16:27:30 +010010482 /*
10483 * - For client-side, expect SERVER_HELLO_REQUEST.
10484 * - For server-side, expect CLIENT_HELLO.
10485 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10486 */
10487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010489 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10490 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010492 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010495
10496 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010498 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010499 {
10500 continue;
10501 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010502 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010503#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010504#if defined(MBEDTLS_SSL_PROTO_TLS)
10505 {
10506 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10507 }
10508#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010509 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010510#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010511
Hanno Becker4a810fb2017-05-24 16:27:30 +010010512#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010513 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10514 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010515 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010518
10519 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010521 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010522 {
10523 continue;
10524 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010525 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010526#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010527#if defined(MBEDTLS_SSL_PROTO_TLS)
10528 {
10529 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10530 }
10531#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010532 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010533#endif /* MBEDTLS_SSL_SRV_C */
10534
Hanno Becker21df7f92017-10-17 11:03:26 +010010535#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010536 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010537 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10538 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010539 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010540 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10541 {
10542 /*
10543 * Accept renegotiation request
10544 */
Paul Bakker48916f92012-09-16 19:57:18 +000010545
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010546 /* DTLS clients need to know renego is server-initiated */
10547#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 Becker2d9623f2019-06-13 12:07:05 +010010549 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10550 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010551 {
10552 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10553 }
10554#endif
10555 ret = ssl_start_renegotiation( ssl );
10556 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10557 ret != 0 )
10558 {
10559 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10560 return( ret );
10561 }
10562 }
10563 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010564#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010565 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010566 /*
10567 * Refuse renegotiation
10568 */
10569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010573 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010574 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010575 /* SSLv3 does not have a "no_renegotiation" warning, so
10576 we send a fatal alert and abort the connection. */
10577 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10578 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10579 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010580 }
10581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010582#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10583#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10584 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010585 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010586 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010587 ret = mbedtls_ssl_send_alert_message( ssl,
10588 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10589 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10590 if( ret != 0 )
10591 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010592 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10595 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10598 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010599 }
Paul Bakker48916f92012-09-16 19:57:18 +000010600 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010601
Hanno Becker90333da2017-10-10 11:27:13 +010010602 /* At this point, we don't know whether the renegotiation has been
10603 * completed or not. The cases to consider are the following:
10604 * 1) The renegotiation is complete. In this case, no new record
10605 * has been read yet.
10606 * 2) The renegotiation is incomplete because the client received
10607 * an application data record while awaiting the ServerHello.
10608 * 3) The renegotiation is incomplete because the client received
10609 * a non-handshake, non-application data message while awaiting
10610 * the ServerHello.
10611 * In each of these case, looping will be the proper action:
10612 * - For 1), the next iteration will read a new record and check
10613 * if it's application data.
10614 * - For 2), the loop condition isn't satisfied as application data
10615 * is present, hence continue is the same as break
10616 * - For 3), the loop condition is satisfied and read_record
10617 * will re-deliver the message that was held back by the client
10618 * when expecting the ServerHello.
10619 */
10620 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010621 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010622#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010624 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010625 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010626 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010627 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010630 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010632 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010633 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10638 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010641 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010642 }
10643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10647 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010648 }
10649
10650 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010651
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010652 /* We're going to return something now, cancel timer,
10653 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010655 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010656
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010658 /* If we requested renego but received AppData, resend HelloRequest.
10659 * Do it now, after setting in_offt, to avoid taking this branch
10660 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010662 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10663 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010665 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010666 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010669 return( ret );
10670 }
10671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010672#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010673#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010674 }
10675
10676 n = ( len < ssl->in_msglen )
10677 ? len : ssl->in_msglen;
10678
10679 memcpy( buf, ssl->in_offt, n );
10680 ssl->in_msglen -= n;
10681
10682 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010683 {
10684 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010685 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010686 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010687 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010688 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010689 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010690 /* more data available */
10691 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010692 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010695
Paul Bakker23986e52011-04-24 08:57:21 +000010696 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010697}
10698
10699/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010700 * Send application data to be encrypted by the SSL layer, taking care of max
10701 * fragment length and buffer size.
10702 *
10703 * According to RFC 5246 Section 6.2.1:
10704 *
10705 * Zero-length fragments of Application data MAY be sent as they are
10706 * potentially useful as a traffic analysis countermeasure.
10707 *
10708 * Therefore, it is possible that the input message length is 0 and the
10709 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010710 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010711static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010712 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010713{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010714 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10715 const size_t max_len = (size_t) ret;
10716
10717 if( ret < 0 )
10718 {
10719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10720 return( ret );
10721 }
10722
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010723 if( len > max_len )
10724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010726 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010729 "maximum fragment length: %d > %d",
10730 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010732 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010733 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010734#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010735#if defined(MBEDTLS_SSL_PROTO_TLS)
10736 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010737 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010738 }
10739#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010740 }
Paul Bakker887bd502011-06-08 13:10:54 +000010741
Paul Bakker5121ce52009-01-03 21:22:43 +000010742 if( ssl->out_left != 0 )
10743 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010744 /*
10745 * The user has previously tried to send the data and
10746 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10747 * written. In this case, we expect the high-level write function
10748 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010753 return( ret );
10754 }
10755 }
Paul Bakker887bd502011-06-08 13:10:54 +000010756 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010757 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010758 /*
10759 * The user is trying to send a message the first time, so we need to
10760 * copy the data into the internal buffers and setup the data structure
10761 * to keep track of partial writes
10762 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010763 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010765 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010766
Hanno Becker67bc7c32018-08-06 11:33:50 +010010767 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010770 return( ret );
10771 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010772 }
10773
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010774 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010775}
10776
10777/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010778 * Write application data, doing 1/n-1 splitting if necessary.
10779 *
10780 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010781 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010782 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010785static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010786 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010787{
10788 int ret;
10789
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010790 if( ssl->conf->cbc_record_splitting ==
10791 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010792 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010793 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10795 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010796 {
10797 return( ssl_write_real( ssl, buf, len ) );
10798 }
10799
10800 if( ssl->split_done == 0 )
10801 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010802 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010803 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010804 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010805 }
10806
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010807 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10808 return( ret );
10809 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010810
10811 return( ret + 1 );
10812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010814
10815/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010816 * Write application data (public-facing wrapper)
10817 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010818int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010819{
10820 int ret;
10821
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010823
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010824 if( ssl == NULL || ssl->conf == NULL )
10825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10826
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010827#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010828 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10829 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010830 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010831 return( ret );
10832 }
10833#endif
10834
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010835 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010836 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010837 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010838 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010840 return( ret );
10841 }
10842 }
10843
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010844#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010845 ret = ssl_write_split( ssl, buf, len );
10846#else
10847 ret = ssl_write_real( ssl, buf, len );
10848#endif
10849
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010851
10852 return( ret );
10853}
10854
10855/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010856 * Notify the peer that the connection is being closed
10857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010859{
10860 int ret;
10861
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010862 if( ssl == NULL || ssl->conf == NULL )
10863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010866
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010867 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10873 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10874 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010877 return( ret );
10878 }
10879 }
10880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010882
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010883 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010884}
10885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010887{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010888 if( transform == NULL )
10889 return;
10890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010892 deflateEnd( &transform->ctx_deflate );
10893 inflateEnd( &transform->ctx_inflate );
10894#endif
10895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10897 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010898
Hanno Becker92231322018-01-03 15:32:51 +000010899#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 mbedtls_md_free( &transform->md_ctx_enc );
10901 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010902#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010903
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010904 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010905}
10906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907#if defined(MBEDTLS_X509_CRT_PARSE_C)
10908static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010911
10912 while( cur != NULL )
10913 {
10914 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010916 cur = next;
10917 }
10918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010920
Hanno Becker0271f962018-08-16 13:23:47 +010010921#if defined(MBEDTLS_SSL_PROTO_DTLS)
10922
10923static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10924{
10925 unsigned offset;
10926 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10927
10928 if( hs == NULL )
10929 return;
10930
Hanno Becker283f5ef2018-08-24 09:34:47 +010010931 ssl_free_buffered_record( ssl );
10932
Hanno Becker0271f962018-08-16 13:23:47 +010010933 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010934 ssl_buffering_free_slot( ssl, offset );
10935}
10936
10937static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10938 uint8_t slot )
10939{
10940 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10941 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010942
10943 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10944 return;
10945
Hanno Beckere605b192018-08-21 15:59:07 +010010946 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010947 {
Hanno Beckere605b192018-08-21 15:59:07 +010010948 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010949 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010950 mbedtls_free( hs_buf->data );
10951 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010952 }
10953}
10954
10955#endif /* MBEDTLS_SSL_PROTO_DTLS */
10956
Gilles Peskine9b562d52018-04-25 20:32:43 +020010957void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010958{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010959 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10960
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010961 if( handshake == NULL )
10962 return;
10963
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010964#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10965 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10966 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010967 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010968 handshake->async_in_progress = 0;
10969 }
10970#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10971
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010972#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10973 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10974 mbedtls_md5_free( &handshake->fin_md5 );
10975 mbedtls_sha1_free( &handshake->fin_sha1 );
10976#endif
10977#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10978#if defined(MBEDTLS_SHA256_C)
10979 mbedtls_sha256_free( &handshake->fin_sha256 );
10980#endif
10981#if defined(MBEDTLS_SHA512_C)
10982 mbedtls_sha512_free( &handshake->fin_sha512 );
10983#endif
10984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986#if defined(MBEDTLS_DHM_C)
10987 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989#if defined(MBEDTLS_ECDH_C)
10990 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010991#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010992#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010993 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010994#if defined(MBEDTLS_SSL_CLI_C)
10995 mbedtls_free( handshake->ecjpake_cache );
10996 handshake->ecjpake_cache = NULL;
10997 handshake->ecjpake_cache_len = 0;
10998#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010999#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011000
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011001#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11002 if( handshake->psk != NULL )
11003 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011004 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011005 mbedtls_free( handshake->psk );
11006 }
11007#endif
11008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11010 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011011 /*
11012 * Free only the linked list wrapper, not the keys themselves
11013 * since the belong to the SNI callback
11014 */
11015 if( handshake->sni_key_cert != NULL )
11016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011018
11019 while( cur != NULL )
11020 {
11021 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011023 cur = next;
11024 }
11025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011027
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011028#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011029 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011030 if( handshake->ecrs_peer_cert != NULL )
11031 {
11032 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11033 mbedtls_free( handshake->ecrs_peer_cert );
11034 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011035#endif
11036
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011037#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11038 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11039 mbedtls_pk_free( &handshake->peer_pubkey );
11040#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042#if defined(MBEDTLS_SSL_PROTO_DTLS)
11043 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011044 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011045 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011046#endif
11047
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011048 mbedtls_platform_zeroize( handshake,
11049 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011050}
11051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011053{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011054 if( session == NULL )
11055 return;
11056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011058 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011059#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011060
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011061#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011063#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011064
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011065 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011066}
11067
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011068#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011069
11070#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11071#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11072#else
11073#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11074#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11075
11076#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11077#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11078#else
11079#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11080#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11081
11082#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11083#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11084#else
11085#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11086#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11087
11088#if defined(MBEDTLS_SSL_ALPN)
11089#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11090#else
11091#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11092#endif /* MBEDTLS_SSL_ALPN */
11093
11094#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11095#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11096#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11098
11099#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11100 ( (uint32_t) ( \
11101 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11102 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11103 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11104 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11105 0u ) )
11106
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011107static unsigned char ssl_serialized_context_header[] = {
11108 MBEDTLS_VERSION_MAJOR,
11109 MBEDTLS_VERSION_MINOR,
11110 MBEDTLS_VERSION_PATCH,
11111 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11112 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011113 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11114 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11115 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011116};
11117
Paul Bakker5121ce52009-01-03 21:22:43 +000011118/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011119 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011120 *
11121 * The format of the serialized data is:
11122 * (in the presentation language of TLS, RFC 8446 section 3)
11123 *
11124 * // header
11125 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011126 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011127 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011128 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011129 * Note: When updating the format, remember to keep these
11130 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011131 *
11132 * // session sub-structure
11133 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11134 * // transform sub-structure
11135 * uint8 random[64]; // ServerHello.random+ClientHello.random
11136 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11137 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11138 * // fields from ssl_context
11139 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11140 * uint64 in_window_top; // DTLS: last validated record seq_num
11141 * uint64 in_window; // DTLS: bitmask for replay protection
11142 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11143 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11144 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11145 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11146 *
11147 * Note that many fields of the ssl_context or sub-structures are not
11148 * serialized, as they fall in one of the following categories:
11149 *
11150 * 1. forced value (eg in_left must be 0)
11151 * 2. pointer to dynamically-allocated memory (eg session, transform)
11152 * 3. value can be re-derived from other data (eg session keys from MS)
11153 * 4. value was temporary (eg content of input buffer)
11154 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011155 */
11156int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11157 unsigned char *buf,
11158 size_t buf_len,
11159 size_t *olen )
11160{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011161 unsigned char *p = buf;
11162 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011163 size_t session_len;
11164 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011165
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011166 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011167 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11168 * this function's documentation.
11169 *
11170 * These are due to assumptions/limitations in the implementation. Some of
11171 * them are likely to stay (no handshake in progress) some might go away
11172 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011173 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011174 /* The initial handshake must be over */
11175 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011177 if( ssl->handshake != NULL )
11178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11179 /* Double-check that sub-structures are indeed ready */
11180 if( ssl->transform == NULL || ssl->session == NULL )
11181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11182 /* There must be no pending incoming or outgoing data */
11183 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11185 if( ssl->out_left != 0 )
11186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11187 /* Protocol must be DLTS, not TLS */
11188 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11190 /* Version must be 1.2 */
11191 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11193 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11195 /* We must be using an AEAD ciphersuite */
11196 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11198 /* Renegotiation must not be enabled */
11199 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011201
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011202 /*
11203 * Version and format identifier
11204 */
11205 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011206
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011207 if( used <= buf_len )
11208 {
11209 memcpy( p, ssl_serialized_context_header,
11210 sizeof( ssl_serialized_context_header ) );
11211 p += sizeof( ssl_serialized_context_header );
11212 }
11213
11214 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011215 * Session (length + data)
11216 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011217 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011218 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11219 return( ret );
11220
11221 used += 4 + session_len;
11222 if( used <= buf_len )
11223 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011224 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011225
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011226 ret = ssl_session_save( ssl->session, 1,
11227 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011228 if( ret != 0 )
11229 return( ret );
11230
11231 p += session_len;
11232 }
11233
11234 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011235 * Transform
11236 */
11237 used += sizeof( ssl->transform->randbytes );
11238 if( used <= buf_len )
11239 {
11240 memcpy( p, ssl->transform->randbytes,
11241 sizeof( ssl->transform->randbytes ) );
11242 p += sizeof( ssl->transform->randbytes );
11243 }
11244
11245#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11246 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11247 if( used <= buf_len )
11248 {
11249 *p++ = ssl->transform->in_cid_len;
11250 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11251 p += ssl->transform->in_cid_len;
11252
11253 *p++ = ssl->transform->out_cid_len;
11254 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11255 p += ssl->transform->out_cid_len;
11256 }
11257#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11258
11259 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011260 * Saved fields from top-level ssl_context structure
11261 */
11262#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11263 used += 4;
11264 if( used <= buf_len )
11265 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011266 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011267 }
11268#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11269
11270#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11271 used += 16;
11272 if( used <= buf_len )
11273 {
11274 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11275 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11276 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11277 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11278 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11279 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11280 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11281 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11282
11283 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11284 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11285 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11286 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11287 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11288 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11289 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11290 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11291 }
11292#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11293
11294#if defined(MBEDTLS_SSL_PROTO_DTLS)
11295 used += 1;
11296 if( used <= buf_len )
11297 {
11298 *p++ = ssl->disable_datagram_packing;
11299 }
11300#endif /* MBEDTLS_SSL_PROTO_DTLS */
11301
11302 used += 8;
11303 if( used <= buf_len )
11304 {
11305 memcpy( p, ssl->cur_out_ctr, 8 );
11306 p += 8;
11307 }
11308
11309#if defined(MBEDTLS_SSL_PROTO_DTLS)
11310 used += 2;
11311 if( used <= buf_len )
11312 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011313 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011314 }
11315#endif /* MBEDTLS_SSL_PROTO_DTLS */
11316
11317#if defined(MBEDTLS_SSL_ALPN)
11318 {
11319 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011320 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011321 : 0;
11322
11323 used += 1 + alpn_len;
11324 if( used <= buf_len )
11325 {
11326 *p++ = alpn_len;
11327
11328 if( ssl->alpn_chosen != NULL )
11329 {
11330 memcpy( p, ssl->alpn_chosen, alpn_len );
11331 p += alpn_len;
11332 }
11333 }
11334 }
11335#endif /* MBEDTLS_SSL_ALPN */
11336
11337 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011338 * Done
11339 */
11340 *olen = used;
11341
11342 if( used > buf_len )
11343 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011344
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011345 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11346
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011347 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011348}
11349
11350/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011351 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011352 *
11353 * This internal version is wrapped by a public function that cleans up in
11354 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011355 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011356static int ssl_context_load( mbedtls_ssl_context *ssl,
11357 const unsigned char *buf,
11358 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011359{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011360 const unsigned char *p = buf;
11361 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011362 size_t session_len;
11363 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011364
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011365 /*
11366 * The context should have been freshly setup or reset.
11367 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011368 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011369 * renegotiating, or if the user mistakenly loaded a session first.)
11370 */
11371 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11372 ssl->session != NULL )
11373 {
11374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11375 }
11376
11377 /*
11378 * We can't check that the config matches the initial one, but we can at
11379 * least check it matches the requirements for serializing.
11380 */
11381 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011382 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11383 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11384 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11385 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11386 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11387 MBEDTLS_SSL_MINOR_VERSION_3 ||
11388 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11389 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011390 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011391 {
11392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11393 }
11394
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011395 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11396
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011397 /*
11398 * Check version identifier
11399 */
11400 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11402
11403 if( memcmp( p, ssl_serialized_context_header,
11404 sizeof( ssl_serialized_context_header ) ) != 0 )
11405 {
11406 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11407 }
11408 p += sizeof( ssl_serialized_context_header );
11409
11410 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011411 * Session
11412 */
11413 if( (size_t)( end - p ) < 4 )
11414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11415
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011416 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011417 p += 4;
11418
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011419 /* This has been allocated by ssl_handshake_init(), called by
11420 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11421 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011422 ssl->session_in = ssl->session;
11423 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011424 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011425
11426 if( (size_t)( end - p ) < session_len )
11427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11428
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011429 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011430 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011431 {
11432 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011433 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011434 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011435
11436 p += session_len;
11437
11438 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011439 * Transform
11440 */
11441
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011442 /* This has been allocated by ssl_handshake_init(), called by
11443 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11444 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011445 ssl->transform_in = ssl->transform;
11446 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011447 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011448
11449 /* Read random bytes and populate structure */
11450 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11452
11453 ret = ssl_populate_transform( ssl->transform,
11454 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11455 ssl->session->master,
11456#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11457#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11458 ssl->session->encrypt_then_mac,
11459#endif
11460#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11461 ssl->session->trunc_hmac,
11462#endif
11463#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11464#if defined(MBEDTLS_ZLIB_SUPPORT)
11465 ssl->session->compression,
11466#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011467 p, /* currently pointing to randbytes */
11468 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11469 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11470 ssl );
11471 if( ret != 0 )
11472 return( ret );
11473
11474 p += sizeof( ssl->transform->randbytes );
11475
11476#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11477 /* Read connection IDs and store them */
11478 if( (size_t)( end - p ) < 1 )
11479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11480
11481 ssl->transform->in_cid_len = *p++;
11482
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011483 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11485
11486 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11487 p += ssl->transform->in_cid_len;
11488
11489 ssl->transform->out_cid_len = *p++;
11490
11491 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11493
11494 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11495 p += ssl->transform->out_cid_len;
11496#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11497
11498 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011499 * Saved fields from top-level ssl_context structure
11500 */
11501#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11502 if( (size_t)( end - p ) < 4 )
11503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11504
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011505 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011506 p += 4;
11507#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11508
11509#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11510 if( (size_t)( end - p ) < 16 )
11511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11512
11513 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11514 ( (uint64_t) p[1] << 48 ) |
11515 ( (uint64_t) p[2] << 40 ) |
11516 ( (uint64_t) p[3] << 32 ) |
11517 ( (uint64_t) p[4] << 24 ) |
11518 ( (uint64_t) p[5] << 16 ) |
11519 ( (uint64_t) p[6] << 8 ) |
11520 ( (uint64_t) p[7] );
11521 p += 8;
11522
11523 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11524 ( (uint64_t) p[1] << 48 ) |
11525 ( (uint64_t) p[2] << 40 ) |
11526 ( (uint64_t) p[3] << 32 ) |
11527 ( (uint64_t) p[4] << 24 ) |
11528 ( (uint64_t) p[5] << 16 ) |
11529 ( (uint64_t) p[6] << 8 ) |
11530 ( (uint64_t) p[7] );
11531 p += 8;
11532#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11533
11534#if defined(MBEDTLS_SSL_PROTO_DTLS)
11535 if( (size_t)( end - p ) < 1 )
11536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11537
11538 ssl->disable_datagram_packing = *p++;
11539#endif /* MBEDTLS_SSL_PROTO_DTLS */
11540
11541 if( (size_t)( end - p ) < 8 )
11542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11543
11544 memcpy( ssl->cur_out_ctr, p, 8 );
11545 p += 8;
11546
11547#if defined(MBEDTLS_SSL_PROTO_DTLS)
11548 if( (size_t)( end - p ) < 2 )
11549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011550 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011551 p += 2;
11552#endif /* MBEDTLS_SSL_PROTO_DTLS */
11553
11554#if defined(MBEDTLS_SSL_ALPN)
11555 {
11556 uint8_t alpn_len;
11557 const char **cur;
11558
11559 if( (size_t)( end - p ) < 1 )
11560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11561
11562 alpn_len = *p++;
11563
11564 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11565 {
11566 /* alpn_chosen should point to an item in the configured list */
11567 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11568 {
11569 if( strlen( *cur ) == alpn_len &&
11570 memcmp( p, cur, alpn_len ) == 0 )
11571 {
11572 ssl->alpn_chosen = *cur;
11573 break;
11574 }
11575 }
11576 }
11577
11578 /* can only happen on conf mismatch */
11579 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11581
11582 p += alpn_len;
11583 }
11584#endif /* MBEDTLS_SSL_ALPN */
11585
11586 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011587 * Forced fields from top-level ssl_context structure
11588 *
11589 * Most of them already set to the correct value by mbedtls_ssl_init() and
11590 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11591 */
11592 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11593
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011594#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011595 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011596#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11597#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011598 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011599#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011600
Hanno Becker83985822019-08-30 10:42:49 +010011601 /* Adjust pointers for header fields of outgoing records to
11602 * the given transform, accounting for explicit IV and CID. */
11603 ssl_update_out_pointers( ssl, ssl->transform );
11604
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011605#if defined(MBEDTLS_SSL_PROTO_DTLS)
11606 ssl->in_epoch = 1;
11607#endif
11608
11609 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11610 * which we don't want - otherwise we'd end up freeing the wrong transform
11611 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11612 if( ssl->handshake != NULL )
11613 {
11614 mbedtls_ssl_handshake_free( ssl );
11615 mbedtls_free( ssl->handshake );
11616 ssl->handshake = NULL;
11617 }
11618
11619 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011620 * Done - should have consumed entire buffer
11621 */
11622 if( p != end )
11623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011624
11625 return( 0 );
11626}
11627
11628/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011629 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011630 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011631int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011632 const unsigned char *buf,
11633 size_t len )
11634{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011635 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011636
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011637 if( ret != 0 )
11638 mbedtls_ssl_free( context );
11639
11640 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011641}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011642#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011643
11644/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011645 * Free an SSL context
11646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011647void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011648{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011649 if( ssl == NULL )
11650 return;
11651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011653
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011654 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011655 {
Angus Grattond8213d02016-05-25 20:56:48 +100011656 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011657 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011658 }
11659
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011660 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011661 {
Angus Grattond8213d02016-05-25 20:56:48 +100011662 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011663 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011664 }
11665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011666#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011667 if( ssl->compress_buf != NULL )
11668 {
Angus Grattond8213d02016-05-25 20:56:48 +100011669 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011670 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011671 }
11672#endif
11673
Paul Bakker48916f92012-09-16 19:57:18 +000011674 if( ssl->transform )
11675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011676 mbedtls_ssl_transform_free( ssl->transform );
11677 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011678 }
11679
11680 if( ssl->handshake )
11681 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011682 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011683 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11684 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011686 mbedtls_free( ssl->handshake );
11687 mbedtls_free( ssl->transform_negotiate );
11688 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011689 }
11690
Paul Bakkerc0463502013-02-14 11:19:38 +010011691 if( ssl->session )
11692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011693 mbedtls_ssl_session_free( ssl->session );
11694 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011695 }
11696
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011697#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011698 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011699 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011700 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011701 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011702 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011703#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011705#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11706 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11709 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011710 }
11711#endif
11712
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011713#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011714 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011715#endif
11716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011718
Paul Bakker86f04f42013-02-14 11:20:09 +010011719 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011720 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011721}
11722
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011723/*
11724 * Initialze mbedtls_ssl_config
11725 */
11726void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11727{
11728 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011729
11730#if !defined(MBEDTLS_SSL_PROTO_TLS)
11731 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11732#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011733}
11734
Simon Butcherc97b6972015-12-27 23:48:17 +000011735#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011736#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011737static int ssl_preset_default_hashes[] = {
11738#if defined(MBEDTLS_SHA512_C)
11739 MBEDTLS_MD_SHA512,
11740 MBEDTLS_MD_SHA384,
11741#endif
11742#if defined(MBEDTLS_SHA256_C)
11743 MBEDTLS_MD_SHA256,
11744 MBEDTLS_MD_SHA224,
11745#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011746#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011747 MBEDTLS_MD_SHA1,
11748#endif
11749 MBEDTLS_MD_NONE
11750};
Simon Butcherc97b6972015-12-27 23:48:17 +000011751#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011752#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011753
Hanno Becker73f4cb12019-06-27 13:51:07 +010011754#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011755static int ssl_preset_suiteb_ciphersuites[] = {
11756 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11757 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11758 0
11759};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011760#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011761
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011762#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011763#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011764static int ssl_preset_suiteb_hashes[] = {
11765 MBEDTLS_MD_SHA256,
11766 MBEDTLS_MD_SHA384,
11767 MBEDTLS_MD_NONE
11768};
11769#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011770#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011771
Hanno Beckerc1096e72019-06-19 12:30:41 +010011772#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011773static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011774#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011775 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011776#endif
11777#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011778 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011779#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011780 MBEDTLS_ECP_DP_NONE
11781};
11782#endif
11783
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011784/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011785 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011786 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011787int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011788 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011789{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011790#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011791 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011792#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011793
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011794 /* Use the functions here so that they are covered in tests,
11795 * but otherwise access member directly for efficiency */
11796 mbedtls_ssl_conf_endpoint( conf, endpoint );
11797 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011798
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011799 /*
11800 * Things that are common to all presets
11801 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011802#if defined(MBEDTLS_SSL_CLI_C)
11803 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11804 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011805#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011806 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011807#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011808#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11809 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11810#endif
11811 }
11812#endif
11813
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011814#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011815 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011816#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011817
11818#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11819 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11820#endif
11821
11822#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011823#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011824 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011825#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11826#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011827 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011828 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011829#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011830#endif
11831
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011832#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11833 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11834#endif
11835
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011836#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011837 conf->f_cookie_write = ssl_cookie_write_dummy;
11838 conf->f_cookie_check = ssl_cookie_check_dummy;
11839#endif
11840
Hanno Becker7f376f42019-06-12 16:20:48 +010011841#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11842 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011843 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11844#endif
11845
Janos Follath088ce432017-04-10 12:42:31 +010011846#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011847#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011848 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011849#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11850#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011851
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011853#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011854 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011855#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11856#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011857 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011858#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11859#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011860
11861#if defined(MBEDTLS_SSL_RENEGOTIATION)
11862 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011863 memset( conf->renego_period, 0x00, 2 );
11864 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011865#endif
11866
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011867#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11868 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11869 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011870 const unsigned char dhm_p[] =
11871 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11872 const unsigned char dhm_g[] =
11873 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11874
Hanno Beckera90658f2017-10-04 15:29:08 +010011875 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11876 dhm_p, sizeof( dhm_p ),
11877 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011878 {
11879 return( ret );
11880 }
11881 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011882#endif
11883
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011884 /*
11885 * Preset-specific defaults
11886 */
11887 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011888 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011889 /*
11890 * NSA Suite B
11891 */
11892 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011893#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011894 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011895#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11896#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011897 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011898#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11899#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011900 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011901#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11902#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011903 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011904#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011905
Hanno Becker73f4cb12019-06-27 13:51:07 +010011906#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011907 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11908 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11909 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11910 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11911 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011912#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011913
11914#if defined(MBEDTLS_X509_CRT_PARSE_C)
11915 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011916#endif
11917
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011918#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011919#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011920 conf->sig_hashes = ssl_preset_suiteb_hashes;
11921#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011922#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011923
11924#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011925#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011926 conf->curve_list = ssl_preset_suiteb_curves;
11927#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011928#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011929 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011930
11931 /*
11932 * Default
11933 */
11934 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011935#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011936 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11937 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11938 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11939 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011940#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11941#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011942 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11943 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11944 MBEDTLS_SSL_MIN_MINOR_VERSION :
11945 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011947 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011948 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11949#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011950#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11951#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11952 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11953#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11954#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11955 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11956#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011957
Hanno Becker73f4cb12019-06-27 13:51:07 +010011958#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011959 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11960 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11961 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11962 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11963 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011964#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011965
11966#if defined(MBEDTLS_X509_CRT_PARSE_C)
11967 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11968#endif
11969
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011970#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011971#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011972 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011974#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011975
11976#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011977#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011978 conf->curve_list = mbedtls_ecp_grp_id_list();
11979#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011980#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011981
11982#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11983 conf->dhm_min_bitlen = 1024;
11984#endif
11985 }
11986
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011987 return( 0 );
11988}
11989
11990/*
11991 * Free mbedtls_ssl_config
11992 */
11993void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11994{
11995#if defined(MBEDTLS_DHM_C)
11996 mbedtls_mpi_free( &conf->dhm_P );
11997 mbedtls_mpi_free( &conf->dhm_G );
11998#endif
11999
12000#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12001 if( conf->psk != NULL )
12002 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012003 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012004 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012005 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012006 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012007 }
12008
12009 if( conf->psk_identity != NULL )
12010 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012011 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012012 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012013 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012014 conf->psk_identity_len = 0;
12015 }
12016#endif
12017
12018#if defined(MBEDTLS_X509_CRT_PARSE_C)
12019 ssl_key_cert_free( conf->key_cert );
12020#endif
12021
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012022 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012023}
12024
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012025#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012026 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12027 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012028/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012029 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012031unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012033#if defined(MBEDTLS_RSA_C)
12034 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12035 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012036#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012037#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012038 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12039 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012041 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012042}
12043
Hanno Becker7e5437a2017-04-28 17:15:26 +010012044unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12045{
12046 switch( type ) {
12047 case MBEDTLS_PK_RSA:
12048 return( MBEDTLS_SSL_SIG_RSA );
12049 case MBEDTLS_PK_ECDSA:
12050 case MBEDTLS_PK_ECKEY:
12051 return( MBEDTLS_SSL_SIG_ECDSA );
12052 default:
12053 return( MBEDTLS_SSL_SIG_ANON );
12054 }
12055}
12056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012057mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012058{
12059 switch( sig )
12060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012061#if defined(MBEDTLS_RSA_C)
12062 case MBEDTLS_SSL_SIG_RSA:
12063 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012064#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012065#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012066 case MBEDTLS_SSL_SIG_ECDSA:
12067 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012068#endif
12069 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012070 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012071 }
12072}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012073#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012074
Hanno Becker7e5437a2017-04-28 17:15:26 +010012075#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12076 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12077
12078/* Find an entry in a signature-hash set matching a given hash algorithm. */
12079mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12080 mbedtls_pk_type_t sig_alg )
12081{
12082 switch( sig_alg )
12083 {
12084 case MBEDTLS_PK_RSA:
12085 return( set->rsa );
12086 case MBEDTLS_PK_ECDSA:
12087 return( set->ecdsa );
12088 default:
12089 return( MBEDTLS_MD_NONE );
12090 }
12091}
12092
12093/* Add a signature-hash-pair to a signature-hash set */
12094void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12095 mbedtls_pk_type_t sig_alg,
12096 mbedtls_md_type_t md_alg )
12097{
12098 switch( sig_alg )
12099 {
12100 case MBEDTLS_PK_RSA:
12101 if( set->rsa == MBEDTLS_MD_NONE )
12102 set->rsa = md_alg;
12103 break;
12104
12105 case MBEDTLS_PK_ECDSA:
12106 if( set->ecdsa == MBEDTLS_MD_NONE )
12107 set->ecdsa = md_alg;
12108 break;
12109
12110 default:
12111 break;
12112 }
12113}
12114
12115/* Allow exactly one hash algorithm for each signature. */
12116void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12117 mbedtls_md_type_t md_alg )
12118{
12119 set->rsa = md_alg;
12120 set->ecdsa = md_alg;
12121}
12122
12123#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12124 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12125
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012126/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012127 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012129mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012130{
12131 switch( hash )
12132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012133#if defined(MBEDTLS_MD5_C)
12134 case MBEDTLS_SSL_HASH_MD5:
12135 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012137#if defined(MBEDTLS_SHA1_C)
12138 case MBEDTLS_SSL_HASH_SHA1:
12139 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012141#if defined(MBEDTLS_SHA256_C)
12142 case MBEDTLS_SSL_HASH_SHA224:
12143 return( MBEDTLS_MD_SHA224 );
12144 case MBEDTLS_SSL_HASH_SHA256:
12145 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012147#if defined(MBEDTLS_SHA512_C)
12148 case MBEDTLS_SSL_HASH_SHA384:
12149 return( MBEDTLS_MD_SHA384 );
12150 case MBEDTLS_SSL_HASH_SHA512:
12151 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012152#endif
12153 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012154 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012155 }
12156}
12157
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012158/*
12159 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12160 */
12161unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12162{
12163 switch( md )
12164 {
12165#if defined(MBEDTLS_MD5_C)
12166 case MBEDTLS_MD_MD5:
12167 return( MBEDTLS_SSL_HASH_MD5 );
12168#endif
12169#if defined(MBEDTLS_SHA1_C)
12170 case MBEDTLS_MD_SHA1:
12171 return( MBEDTLS_SSL_HASH_SHA1 );
12172#endif
12173#if defined(MBEDTLS_SHA256_C)
12174 case MBEDTLS_MD_SHA224:
12175 return( MBEDTLS_SSL_HASH_SHA224 );
12176 case MBEDTLS_MD_SHA256:
12177 return( MBEDTLS_SSL_HASH_SHA256 );
12178#endif
12179#if defined(MBEDTLS_SHA512_C)
12180 case MBEDTLS_MD_SHA384:
12181 return( MBEDTLS_SSL_HASH_SHA384 );
12182 case MBEDTLS_MD_SHA512:
12183 return( MBEDTLS_SSL_HASH_SHA512 );
12184#endif
12185 default:
12186 return( MBEDTLS_SSL_HASH_NONE );
12187 }
12188}
12189
Hanno Beckeree902df2019-08-23 13:47:47 +010012190#if defined(MBEDTLS_USE_TINYCRYPT)
12191/*
12192 * Check if a curve proposed by the peer is in our list.
12193 * Return 0 if we're willing to use it, -1 otherwise.
12194 */
12195int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12196 mbedtls_uecc_group_id grp_id )
12197{
12198 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12199 if( own_ec_id == grp_id )
12200 return( 0 );
12201 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12202
12203 return( -1 );
12204}
12205#endif /* MBEDTLS_USE_TINYCRYPT */
12206
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012207#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012208/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012209 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012210 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012211 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012212int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12213 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012214{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012215 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12216 if( own_ec_id == grp_id )
12217 return( 0 );
12218 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012219
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012220 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012221}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012222#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012223
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012224#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012225/*
12226 * Check if a hash proposed by the peer is in our list.
12227 * Return 0 if we're willing to use it, -1 otherwise.
12228 */
12229int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12230 mbedtls_md_type_t md )
12231{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012232 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12233 if( md_alg == md )
12234 return( 0 );
12235 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012236
12237 return( -1 );
12238}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012239#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012241#if defined(MBEDTLS_X509_CRT_PARSE_C)
12242int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012243 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012244 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012245 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012246{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012247 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012248#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012249 int usage = 0;
12250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012251#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012252 const char *ext_oid;
12253 size_t ext_len;
12254#endif
12255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012256#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12257 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012258 ((void) cert);
12259 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012260 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012261#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012263#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12264 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012265 {
12266 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012267 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012269 case MBEDTLS_KEY_EXCHANGE_RSA:
12270 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012271 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012272 break;
12273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012274 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12275 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12276 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12277 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012278 break;
12279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012280 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12281 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012282 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012283 break;
12284
12285 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012286 case MBEDTLS_KEY_EXCHANGE_NONE:
12287 case MBEDTLS_KEY_EXCHANGE_PSK:
12288 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12289 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012290 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012291 usage = 0;
12292 }
12293 }
12294 else
12295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012296 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12297 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012298 }
12299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012300 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012301 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012302 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012303 ret = -1;
12304 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012305#else
12306 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012307#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012309#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12310 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012312 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12313 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012314 }
12315 else
12316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012317 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12318 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012319 }
12320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012321 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012322 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012323 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012324 ret = -1;
12325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012326#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012327
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012328 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012331
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012332#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12333 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12334int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12335 unsigned char *output,
12336 unsigned char *data, size_t data_len )
12337{
12338 int ret = 0;
12339 mbedtls_md5_context mbedtls_md5;
12340 mbedtls_sha1_context mbedtls_sha1;
12341
12342 mbedtls_md5_init( &mbedtls_md5 );
12343 mbedtls_sha1_init( &mbedtls_sha1 );
12344
12345 /*
12346 * digitally-signed struct {
12347 * opaque md5_hash[16];
12348 * opaque sha_hash[20];
12349 * };
12350 *
12351 * md5_hash
12352 * MD5(ClientHello.random + ServerHello.random
12353 * + ServerParams);
12354 * sha_hash
12355 * SHA(ClientHello.random + ServerHello.random
12356 * + ServerParams);
12357 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012358 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012359 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012361 goto exit;
12362 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012363 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012364 ssl->handshake->randbytes, 64 ) ) != 0 )
12365 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012367 goto exit;
12368 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012369 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012370 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012372 goto exit;
12373 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012374 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012375 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012377 goto exit;
12378 }
12379
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012380 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012381 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012383 goto exit;
12384 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012385 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012386 ssl->handshake->randbytes, 64 ) ) != 0 )
12387 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012389 goto exit;
12390 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012391 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392 data_len ) ) != 0 )
12393 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012395 goto exit;
12396 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012397 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012398 output + 16 ) ) != 0 )
12399 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012401 goto exit;
12402 }
12403
12404exit:
12405 mbedtls_md5_free( &mbedtls_md5 );
12406 mbedtls_sha1_free( &mbedtls_sha1 );
12407
12408 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012409 mbedtls_ssl_pend_fatal_alert( ssl,
12410 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012411
12412 return( ret );
12413
12414}
12415#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12416 MBEDTLS_SSL_PROTO_TLS1_1 */
12417
12418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12420int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012421 unsigned char *hash, size_t *hashlen,
12422 unsigned char *data, size_t data_len,
12423 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012424{
12425 int ret = 0;
12426 mbedtls_md_context_t ctx;
12427 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012428 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012429
12430 mbedtls_md_init( &ctx );
12431
12432 /*
12433 * digitally-signed struct {
12434 * opaque client_random[32];
12435 * opaque server_random[32];
12436 * ServerDHParams params;
12437 * };
12438 */
12439 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12440 {
12441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12442 goto exit;
12443 }
12444 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12445 {
12446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12447 goto exit;
12448 }
12449 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12450 {
12451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12452 goto exit;
12453 }
12454 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12455 {
12456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12457 goto exit;
12458 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012459 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012460 {
12461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12462 goto exit;
12463 }
12464
12465exit:
12466 mbedtls_md_free( &ctx );
12467
12468 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012469 mbedtls_ssl_pend_fatal_alert( ssl,
12470 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012471
12472 return( ret );
12473}
12474#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12475 MBEDTLS_SSL_PROTO_TLS1_2 */
12476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012477#endif /* MBEDTLS_SSL_TLS_C */