blob: c8e708037478a754e096c43d06cda82b2af8c913 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001955 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001957 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1958 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1959 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1960 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1961 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962 {
1963 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001964 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965
1966 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1967 ssl->handshake->ecdh_privkey,
1968 ssl->handshake->premaster,
1969 uecc_curve ) )
1970 {
1971 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1972 }
1973
1974 ssl->handshake->pmslen = NUM_ECC_BYTES;
1975 }
1976 else
1977#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001978#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1980 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1981 {
1982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1983 ssl->handshake->premaster,
1984 MBEDTLS_PREMASTER_SIZE,
1985 &ssl->handshake->pmslen,
1986 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001987 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001988 {
1989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1990 return( ret );
1991 }
1992
1993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1994 }
1995 else
1996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001997#if defined(MBEDTLS_ECDH_C) && \
1998 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2001 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2006 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2007 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2008 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2009 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2010 {
2011 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2012 &ssl->handshake->pmslen,
2013 ssl->handshake->premaster,
2014 MBEDTLS_MPI_MAX_SIZE,
2015 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002016 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002017 {
2018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2019 return( ret );
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2023 }
2024 else
2025#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2026 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2027 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2029#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2030 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2031 {
2032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002033 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2036 return( ret );
2037 }
2038 }
2039 else
2040#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2041#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2042 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2043 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2044 {
2045 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2046 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2047 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002048 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( ret != 0 )
2050 {
2051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2052 return( ret );
2053 }
2054 }
2055 else
2056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2057#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_RSA )
2060 {
2061 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002062 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002063 * ssl_rsa_generate_partial_pms(). Only the
2064 * PMS length needs to be set. */
2065 ssl->handshake->pmslen = 48;
2066 }
2067 else
2068#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2072 }
2073
2074 return( 0 );
2075}
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2078int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 unsigned char *p = ssl->handshake->premaster;
2081 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002082 const unsigned char *psk = ssl->conf->psk;
2083 size_t psk_len = ssl->conf->psk_len;
2084
2085 /* If the psk callback was called, use its result */
2086 if( ssl->handshake->psk != NULL )
2087 {
2088 psk = ssl->handshake->psk;
2089 psk_len = ssl->handshake->psk_len;
2090 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091
2092 /*
2093 * PMS = struct {
2094 * opaque other_secret<0..2^16-1>;
2095 * opaque psk<0..2^16-1>;
2096 * };
2097 * with "other_secret" depending on the particular key exchange
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2100 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002101 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002102 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
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;
2188
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002189 }
2190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002195 }
2196
2197 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002198 if( end - p < 2 )
2199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002200
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002201 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002202
2203 if( end < p || (size_t)( end - p ) < psk_len )
2204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2205
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002206 memcpy( p, psk, psk_len );
2207 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002208
2209 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2210
2211 return( 0 );
2212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002216/*
2217 * SSLv3.0 MAC functions
2218 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002219#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002220static void ssl_mac( mbedtls_md_context_t *md_ctx,
2221 const unsigned char *secret,
2222 const unsigned char *buf, size_t len,
2223 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002224 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225{
2226 unsigned char header[11];
2227 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002228 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2230 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002231
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002232 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002234 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002235 else
Paul Bakker68884e32013-01-07 18:20:04 +01002236 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002237
2238 memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002239 header[8] = (unsigned char) type;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002240 mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
Paul Bakker68884e32013-01-07 18:20:04 +01002242 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_starts( md_ctx );
2244 mbedtls_md_update( md_ctx, secret, md_size );
2245 mbedtls_md_update( md_ctx, padding, padlen );
2246 mbedtls_md_update( md_ctx, header, 11 );
2247 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002248 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Paul Bakker68884e32013-01-07 18:20:04 +01002250 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_md_starts( md_ctx );
2252 mbedtls_md_update( md_ctx, secret, md_size );
2253 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002254 mbedtls_md_update( md_ctx, out, md_size );
2255 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002258
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002259/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002260 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002261#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2263 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2264 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2265/* This function makes sure every byte in the memory region is accessed
2266 * (in ascending addresses order) */
2267static void ssl_read_memory( unsigned char *p, size_t len )
2268{
2269 unsigned char acc = 0;
2270 volatile unsigned char force;
2271
2272 for( ; len != 0; p++, len-- )
2273 acc ^= *p;
2274
2275 force = acc;
2276 (void) force;
2277}
2278#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2279
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002280/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002282 */
Hanno Becker3307b532017-12-27 21:37:21 +00002283
Hanno Beckera5a2b082019-05-15 14:03:01 +01002284#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002285/* This functions transforms a DTLS plaintext fragment and a record content
2286 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002287 *
2288 * struct {
2289 * opaque content[DTLSPlaintext.length];
2290 * ContentType real_type;
2291 * uint8 zeros[length_of_padding];
2292 * } DTLSInnerPlaintext;
2293 *
2294 * Input:
2295 * - `content`: The beginning of the buffer holding the
2296 * plaintext to be wrapped.
2297 * - `*content_size`: The length of the plaintext in Bytes.
2298 * - `max_len`: The number of Bytes available starting from
2299 * `content`. This must be `>= *content_size`.
2300 * - `rec_type`: The desired record content type.
2301 *
2302 * Output:
2303 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2304 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2305 *
2306 * Returns:
2307 * - `0` on success.
2308 * - A negative error code if `max_len` didn't offer enough space
2309 * for the expansion.
2310 */
2311static int ssl_cid_build_inner_plaintext( unsigned char *content,
2312 size_t *content_size,
2313 size_t remaining,
2314 uint8_t rec_type )
2315{
2316 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002317 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2318 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2319 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002320
2321 /* Write real content type */
2322 if( remaining == 0 )
2323 return( -1 );
2324 content[ len ] = rec_type;
2325 len++;
2326 remaining--;
2327
2328 if( remaining < pad )
2329 return( -1 );
2330 memset( content + len, 0, pad );
2331 len += pad;
2332 remaining -= pad;
2333
2334 *content_size = len;
2335 return( 0 );
2336}
2337
Hanno Becker7dc25772019-05-20 15:08:01 +01002338/* This function parses a DTLSInnerPlaintext structure.
2339 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002340static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2341 size_t *content_size,
2342 uint8_t *rec_type )
2343{
2344 size_t remaining = *content_size;
2345
2346 /* Determine length of padding by skipping zeroes from the back. */
2347 do
2348 {
2349 if( remaining == 0 )
2350 return( -1 );
2351 remaining--;
2352 } while( content[ remaining ] == 0 );
2353
2354 *content_size = remaining;
2355 *rec_type = content[ remaining ];
2356
2357 return( 0 );
2358}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002360
Hanno Becker99abf512019-05-20 14:50:53 +01002361/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002362 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002363static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002364 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002365 mbedtls_record *rec )
2366{
Hanno Becker99abf512019-05-20 14:50:53 +01002367 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002368 *
2369 * additional_data = seq_num + TLSCompressed.type +
2370 * TLSCompressed.version + TLSCompressed.length;
2371 *
Hanno Becker99abf512019-05-20 14:50:53 +01002372 * For the CID extension, this is extended as follows
2373 * (quoting draft-ietf-tls-dtls-connection-id-05,
2374 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002375 *
2376 * additional_data = seq_num + DTLSPlaintext.type +
2377 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002378 * cid +
2379 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 * length_of_DTLSInnerPlaintext;
2381 */
2382
Hanno Becker3307b532017-12-27 21:37:21 +00002383 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2384 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002385 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002386
Hanno Beckera5a2b082019-05-15 14:03:01 +01002387#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002388 if( rec->cid_len != 0 )
2389 {
2390 memcpy( add_data + 11, rec->cid, rec->cid_len );
2391 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002392 mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002393 *add_data_len = 13 + 1 + rec->cid_len;
2394 }
2395 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002397 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002398 mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002399 *add_data_len = 13;
2400 }
Hanno Becker3307b532017-12-27 21:37:21 +00002401}
2402
Hanno Becker611a83b2018-01-03 14:27:32 +00002403int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2404 mbedtls_ssl_transform *transform,
2405 mbedtls_record *rec,
2406 int (*f_rng)(void *, unsigned char *, size_t),
2407 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002410 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002411 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002412 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002413 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002414 size_t post_avail;
2415
2416 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002417#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002418 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002419 ((void) ssl);
2420#endif
2421
2422 /* The PRNG is used for dynamic IV generation that's used
2423 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2424#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2425 ( defined(MBEDTLS_AES_C) || \
2426 defined(MBEDTLS_ARIA_C) || \
2427 defined(MBEDTLS_CAMELLIA_C) ) && \
2428 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2429 ((void) f_rng);
2430 ((void) p_rng);
2431#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Hanno Becker3307b532017-12-27 21:37:21 +00002435 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002436 {
Hanno Becker3307b532017-12-27 21:37:21 +00002437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2439 }
Hanno Becker505089d2019-05-01 09:45:57 +01002440 if( rec == NULL
2441 || rec->buf == NULL
2442 || rec->buf_len < rec->data_offset
2443 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002445 || rec->cid_len != 0
2446#endif
2447 )
Hanno Becker3307b532017-12-27 21:37:21 +00002448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002451 }
2452
Hanno Becker3307b532017-12-27 21:37:21 +00002453 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002454 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002456 data, rec->data_len );
2457
2458 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2459
2460 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2461 {
2462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2463 (unsigned) rec->data_len,
2464 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2466 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002467
Hanno Beckera5a2b082019-05-15 14:03:01 +01002468#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002469 /*
2470 * Add CID information
2471 */
2472 rec->cid_len = transform->out_cid_len;
2473 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2474 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002475
2476 if( rec->cid_len != 0 )
2477 {
2478 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002479 * Wrap plaintext into DTLSInnerPlaintext structure.
2480 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002481 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002482 * Note that this changes `rec->data_len`, and hence
2483 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002484 */
2485 if( ssl_cid_build_inner_plaintext( data,
2486 &rec->data_len,
2487 post_avail,
2488 rec->type ) != 0 )
2489 {
2490 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2491 }
2492
2493 rec->type = MBEDTLS_SSL_MSG_CID;
2494 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002495#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002496
Hanno Becker92c930f2019-04-29 17:31:37 +01002497 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2498
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002500 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002502#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 if( mode == MBEDTLS_MODE_STREAM ||
2504 ( mode == MBEDTLS_MODE_CBC
2505#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002506 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002507#endif
2508 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
Hanno Becker3307b532017-12-27 21:37:21 +00002510 if( post_avail < transform->maclen )
2511 {
2512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2514 }
2515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002517 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2518 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002519 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002520 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002521 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2522 data, rec->data_len, rec->ctr, rec->type, mac );
2523 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002524 }
2525 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2528 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002529 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2530 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 {
Hanno Becker992b6872017-11-09 18:57:39 +00002532 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2533
Hanno Beckere83efe62019-04-29 13:52:53 +01002534 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002535
Hanno Becker3307b532017-12-27 21:37:21 +00002536 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002537 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002538 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2539 data, rec->data_len );
2540 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2541 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2542
2543 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002544 }
2545 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002546#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 }
2551
Hanno Becker3307b532017-12-27 21:37:21 +00002552 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2553 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554
Hanno Becker3307b532017-12-27 21:37:21 +00002555 rec->data_len += transform->maclen;
2556 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002557 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002558 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002559#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561 /*
2562 * Encrypt
2563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2565 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002567 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002568 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002570 "including %d bytes of padding",
2571 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Becker3307b532017-12-27 21:37:21 +00002573 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2574 transform->iv_enc, transform->ivlen,
2575 data, rec->data_len,
2576 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002579 return( ret );
2580 }
2581
Hanno Becker3307b532017-12-27 21:37:21 +00002582 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
Paul Bakker68884e32013-01-07 18:20:04 +01002588 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002591#if defined(MBEDTLS_GCM_C) || \
2592 defined(MBEDTLS_CCM_C) || \
2593 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002595 mode == MBEDTLS_MODE_CCM ||
2596 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002598 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002600 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002601
Hanno Becker3307b532017-12-27 21:37:21 +00002602 /* Check that there's space for both the authentication tag
2603 * and the explicit IV before and after the record content. */
2604 if( post_avail < transform->taglen ||
2605 rec->data_offset < explicit_iv_len )
2606 {
2607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2608 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2609 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002610
Paul Bakker68884e32013-01-07 18:20:04 +01002611 /*
2612 * Generate IV
2613 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2615 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002616 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002617 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002618 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2619 explicit_iv_len );
2620 /* Prefix record content with explicit IV. */
2621 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 }
2623 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2624 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002625 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 unsigned char i;
2627
2628 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2629
2630 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002631 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 }
2633 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002634 {
2635 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002638 }
2639
Hanno Beckere83efe62019-04-29 13:52:53 +01002640 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002641
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2643 iv, transform->ivlen );
2644 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002645 data - explicit_iv_len, explicit_iv_len );
2646 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002647 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002649 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002650 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002651
Paul Bakker68884e32013-01-07 18:20:04 +01002652 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002653 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002654 */
Hanno Becker3307b532017-12-27 21:37:21 +00002655
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002656 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002657 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002658 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002659 data, rec->data_len, /* source */
2660 data, &rec->data_len, /* destination */
2661 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002664 return( ret );
2665 }
2666
Hanno Becker3307b532017-12-27 21:37:21 +00002667 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2668 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669
Hanno Becker3307b532017-12-27 21:37:21 +00002670 rec->data_len += transform->taglen + explicit_iv_len;
2671 rec->data_offset -= explicit_iv_len;
2672 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002673 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2677#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002678 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002681 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002682 size_t padlen, i;
2683 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684
Hanno Becker3307b532017-12-27 21:37:21 +00002685 /* Currently we're always using minimal padding
2686 * (up to 255 bytes would be allowed). */
2687 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2688 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 padlen = 0;
2690
Hanno Becker3307b532017-12-27 21:37:21 +00002691 /* Check there's enough space in the buffer for the padding. */
2692 if( post_avail < padlen + 1 )
2693 {
2694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2695 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2696 }
2697
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002699 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Hanno Becker3307b532017-12-27 21:37:21 +00002701 rec->data_len += padlen + 1;
2702 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002705 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002706 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2707 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002708 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002709 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2710 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711 {
Hanno Becker3307b532017-12-27 21:37:21 +00002712 if( f_rng == NULL )
2713 {
2714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2716 }
2717
2718 if( rec->data_offset < transform->ivlen )
2719 {
2720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2721 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2722 }
2723
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002724 /*
2725 * Generate IV
2726 */
Hanno Becker3307b532017-12-27 21:37:21 +00002727 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002728 if( ret != 0 )
2729 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002730
Hanno Becker3307b532017-12-27 21:37:21 +00002731 memcpy( data - transform->ivlen, transform->iv_enc,
2732 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002733
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002738 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002739 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002740 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Hanno Becker3307b532017-12-27 21:37:21 +00002742 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2743 transform->iv_enc,
2744 transform->ivlen,
2745 data, rec->data_len,
2746 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002749 return( ret );
2750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002751
Hanno Becker3307b532017-12-27 21:37:21 +00002752 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002756 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002759 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2760 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002761 {
2762 /*
2763 * Save IV in SSL3 and TLS1
2764 */
Hanno Becker3307b532017-12-27 21:37:21 +00002765 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2766 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 }
Hanno Becker3307b532017-12-27 21:37:21 +00002768 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002769#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002770 {
2771 data -= transform->ivlen;
2772 rec->data_offset -= transform->ivlen;
2773 rec->data_len += transform->ivlen;
2774 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002777 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002779 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2780
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781 /*
2782 * MAC(MAC_write_key, seq_num +
2783 * TLSCipherText.type +
2784 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002785 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786 * IV + // except for TLS 1.0
2787 * ENC(content + padding + padding_length));
2788 */
Hanno Becker3307b532017-12-27 21:37:21 +00002789
2790 if( post_avail < transform->maclen)
2791 {
2792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2793 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2794 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795
Hanno Beckere83efe62019-04-29 13:52:53 +01002796 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002799 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002800 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002801
Hanno Becker3307b532017-12-27 21:37:21 +00002802 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002803 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002804 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2805 data, rec->data_len );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2807 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002808
Hanno Becker3307b532017-12-27 21:37:21 +00002809 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002810
Hanno Becker3307b532017-12-27 21:37:21 +00002811 rec->data_len += transform->maclen;
2812 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002813 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002819 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002825 /* Make extra sure authentication was performed, exactly once */
2826 if( auth_done != 1 )
2827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002830 }
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
2834 return( 0 );
2835}
2836
Hanno Becker40478be2019-07-12 08:23:59 +01002837int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002838 mbedtls_ssl_transform *transform,
2839 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002841 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002843 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002844#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002845 size_t padlen = 0, correct = 1;
2846#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002847 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002848 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002849 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002850
Hanno Becker611a83b2018-01-03 14:27:32 +00002851#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002852 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 ((void) ssl);
2854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857 if( rec == NULL ||
2858 rec->buf == NULL ||
2859 rec->buf_len < rec->data_offset ||
2860 rec->buf_len - rec->data_offset < rec->data_len )
2861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002864 }
2865
Hanno Becker4c6876b2017-12-27 21:28:58 +00002866 data = rec->buf + rec->data_offset;
2867 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002870 /*
2871 * Match record's CID with incoming CID.
2872 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002873 if( rec->cid_len != transform->in_cid_len ||
2874 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2875 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002876 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002877 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002878#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2881 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002882 {
2883 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002884 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2885 transform->iv_dec,
2886 transform->ivlen,
2887 data, rec->data_len,
2888 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002891 return( ret );
2892 }
2893
Hanno Becker4c6876b2017-12-27 21:28:58 +00002894 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 }
Paul Bakker68884e32013-01-07 18:20:04 +01002900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002902#if defined(MBEDTLS_GCM_C) || \
2903 defined(MBEDTLS_CCM_C) || \
2904 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002906 mode == MBEDTLS_MODE_CCM ||
2907 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002908 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002910 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002911
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002912 /*
2913 * Prepare IV from explicit and implicit data.
2914 */
2915
2916 /* Check that there's enough space for the explicit IV
2917 * (at the beginning of the record) and the MAC (at the
2918 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002919 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002922 "+ taglen (%d)", rec->data_len,
2923 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002925 }
Paul Bakker68884e32013-01-07 18:20:04 +01002926
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002927#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002928 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2929 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002930 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002931
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002932 /* Fixed */
2933 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2934 /* Explicit */
2935 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002936 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002937 else
2938#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2939#if defined(MBEDTLS_CHACHAPOLY_C)
2940 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002941 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002942 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002943 unsigned char i;
2944
2945 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2946
2947 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002949 }
2950 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002951#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002952 {
2953 /* Reminder if we ever add an AEAD mode with a different size */
2954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2956 }
2957
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002958 /* Group changes to data, data_len, and add_data, because
2959 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002960 data += explicit_iv_len;
2961 rec->data_offset += explicit_iv_len;
2962 rec->data_len -= explicit_iv_len + transform->taglen;
2963
Hanno Beckere83efe62019-04-29 13:52:53 +01002964 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002965 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002966 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002967
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002968 /* Because of the check above, we know that there are
2969 * explicit_iv_len Bytes preceeding data, and taglen
2970 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002971 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002972 * mbedtls_cipher_auth_decrypt() below. */
2973
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002974 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002975 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002976 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002977
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002978 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002979 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002980 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002981 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2982 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002983 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002984 data, rec->data_len,
2985 data, &olen,
2986 data + rec->data_len,
2987 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2992 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002993
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002994 return( ret );
2995 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002996 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002998 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002999 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003003 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3007#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003008 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003011 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003012
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 /*
Paul Bakker45829992013-01-03 14:52:21 +01003014 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003017 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3018 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003019 {
3020 /* The ciphertext is prefixed with the CBC IV. */
3021 minlen += transform->ivlen;
3022 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003023#endif
Paul Bakker45829992013-01-03 14:52:21 +01003024
Hanno Becker4c6876b2017-12-27 21:28:58 +00003025 /* Size considerations:
3026 *
3027 * - The CBC cipher text must not be empty and hence
3028 * at least of size transform->ivlen.
3029 *
3030 * Together with the potential IV-prefix, this explains
3031 * the first of the two checks below.
3032 *
3033 * - The record must contain a MAC, either in plain or
3034 * encrypted, depending on whether Encrypt-then-MAC
3035 * is used or not.
3036 * - If it is, the message contains the IV-prefix,
3037 * the CBC ciphertext, and the MAC.
3038 * - If it is not, the padded plaintext, and hence
3039 * the CBC ciphertext, has at least length maclen + 1
3040 * because there is at least the padding length byte.
3041 *
3042 * As the CBC ciphertext is not empty, both cases give the
3043 * lower bound minlen + maclen + 1 on the record size, which
3044 * we test for in the second check below.
3045 */
3046 if( rec->data_len < minlen + transform->ivlen ||
3047 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003050 "+ 1 ) ( + expl IV )", rec->data_len,
3051 transform->ivlen,
3052 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003054 }
3055
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003056 /*
3057 * Authenticate before decrypt if enabled
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003060 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003061 {
Hanno Becker992b6872017-11-09 18:57:39 +00003062 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003065
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003066 /* Update data_len in tandem with add_data.
3067 *
3068 * The subtraction is safe because of the previous check
3069 * data_len >= minlen + maclen + 1.
3070 *
3071 * Afterwards, we know that data + data_len is followed by at
3072 * least maclen Bytes, which justifies the call to
3073 * mbedtls_ssl_safer_memcmp() below.
3074 *
3075 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003077 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003078
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003079 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003080 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3081 add_data_len );
3082 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3083 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003084 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3085 data, rec->data_len );
3086 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3087 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003088
Hanno Becker4c6876b2017-12-27 21:28:58 +00003089 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3090 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003091 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003092 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003093
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003094 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003095 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3096 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003101 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104
3105 /*
3106 * Check length sanity
3107 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003108
3109 /* We know from above that data_len > minlen >= 0,
3110 * so the following check in particular implies that
3111 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003112 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003115 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003117 }
3118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003120 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003122 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003123 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3124 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003125 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003126 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003127 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003128
Hanno Becker4c6876b2017-12-27 21:28:58 +00003129 data += transform->ivlen;
3130 rec->data_offset += transform->ivlen;
3131 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003134
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003135 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3136
Hanno Becker4c6876b2017-12-27 21:28:58 +00003137 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3138 transform->iv_dec, transform->ivlen,
3139 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003142 return( ret );
3143 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003144
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003145 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003146 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003150 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003153 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3154 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003155 {
3156 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3158 * records is equivalent to CBC decryption of the concatenation
3159 * of the records; in other words, IVs are maintained across
3160 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003161 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3163 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 /* Safe since data_len >= minlen + maclen + 1, so after having
3168 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003169 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3170 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003171 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003172
Hanno Becker4c6876b2017-12-27 21:28:58 +00003173 if( auth_done == 1 )
3174 {
3175 correct *= ( rec->data_len >= padlen + 1 );
3176 padlen *= ( rec->data_len >= padlen + 1 );
3177 }
3178 else
Paul Bakker45829992013-01-03 14:52:21 +01003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 if( rec->data_len < transform->maclen + padlen + 1 )
3182 {
3183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3184 rec->data_len,
3185 transform->maclen,
3186 padlen + 1 ) );
3187 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003188#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003189
3190 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3191 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Hanno Becker4c6876b2017-12-27 21:28:58 +00003194 padlen++;
3195
3196 /* Regardless of the validity of the padding,
3197 * we have data_len >= padlen here. */
3198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003200 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3201 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003203 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205#if defined(MBEDTLS_SSL_DEBUG_ALL)
3206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 "should be no more than %d",
3208 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003209#endif
Paul Bakker45829992013-01-03 14:52:21 +01003210 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212 }
3213 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3215#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3216 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003217 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3218 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003220 /* The padding check involves a series of up to 256
3221 * consecutive memory reads at the end of the record
3222 * plaintext buffer. In order to hide the length and
3223 * validity of the padding, always perform exactly
3224 * `min(256,plaintext_len)` reads (but take into account
3225 * only the last `padlen` bytes for the padding check). */
3226 size_t pad_count = 0;
3227 size_t real_count = 0;
3228 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003229
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 /* Index of first padding byte; it has been ensured above
3231 * that the subtraction is safe. */
3232 size_t const padding_idx = rec->data_len - padlen;
3233 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3234 size_t const start_idx = rec->data_len - num_checks;
3235 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003236
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003238 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 real_count |= ( idx >= padding_idx );
3240 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003241 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003242 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003245 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003247#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003248 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3252 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003256 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003257
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 /* If the padding was found to be invalid, padlen == 0
3259 * and the subtraction is safe. If the padding was found valid,
3260 * padlen hasn't been changed and the previous assertion
3261 * data_len >= padlen still holds. */
3262 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003266 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003278 * Authenticate if not done yet.
3279 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003281#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003282 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003283 {
Hanno Becker992b6872017-11-09 18:57:39 +00003284 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003285
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286 /* If the initial value of padlen was such that
3287 * data_len < maclen + padlen + 1, then padlen
3288 * got reset to 1, and the initial check
3289 * data_len >= minlen + maclen + 1
3290 * guarantees that at this point we still
3291 * have at least data_len >= maclen.
3292 *
3293 * If the initial value of padlen was such that
3294 * data_len >= maclen + padlen + 1, then we have
3295 * subtracted either padlen + 1 (if the padding was correct)
3296 * or 0 (if the padding was incorrect) since then,
3297 * hence data_len >= maclen in any case.
3298 */
3299 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003300 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003303 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3304 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 ssl_mac( &transform->md_ctx_dec,
3307 transform->mac_dec,
3308 data, rec->data_len,
3309 rec->ctr, rec->type,
3310 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003311 }
3312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3314#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3315 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003316 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3317 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003318 {
3319 /*
3320 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003321 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003322 *
3323 * Known timing attacks:
3324 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3325 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003326 * To compensate for different timings for the MAC calculation
3327 * depending on how much padding was removed (which is determined
3328 * by padlen), process extra_run more blocks through the hash
3329 * function.
3330 *
3331 * The formula in the paper is
3332 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3333 * where L1 is the size of the header plus the decrypted message
3334 * plus CBC padding and L2 is the size of the header plus the
3335 * decrypted message. This is for an underlying hash function
3336 * with 64-byte blocks.
3337 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3338 * correctly. We round down instead of up, so -56 is the correct
3339 * value for our calculations instead of -55.
3340 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003341 * Repeat the formula rather than defining a block_size variable.
3342 * This avoids requiring division by a variable at runtime
3343 * (which would be marginally less efficient and would require
3344 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003345 */
3346 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003347 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003348
3349 /*
3350 * The next two sizes are the minimum and maximum values of
3351 * in_msglen over all padlen values.
3352 *
3353 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003354 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003355 *
3356 * Note that max_len + maclen is never more than the buffer
3357 * length, as we previously did in_msglen -= maclen too.
3358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003359 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003360 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3361
Hanno Becker4c6876b2017-12-27 21:28:58 +00003362 memset( tmp, 0, sizeof( tmp ) );
3363
3364 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003365 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003366#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3367 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003368 case MBEDTLS_MD_MD5:
3369 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003371 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003372 extra_run =
3373 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3374 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003375 break;
3376#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003377#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003379 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003380 extra_run =
3381 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3382 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003383 break;
3384#endif
3385 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3388 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003389
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003390 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003391
Hanno Beckere83efe62019-04-29 13:52:53 +01003392 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3393 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003394 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3395 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003396 /* Make sure we access everything even when padlen > 0. This
3397 * makes the synchronisation requirements for just-in-time
3398 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003399 ssl_read_memory( data + rec->data_len, padlen );
3400 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003401
3402 /* Call mbedtls_md_process at least once due to cache attacks
3403 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003404 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003405 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003406
Hanno Becker4c6876b2017-12-27 21:28:58 +00003407 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003408
3409 /* Make sure we access all the memory that could contain the MAC,
3410 * before we check it in the next code block. This makes the
3411 * synchronisation requirements for just-in-time Prime+Probe
3412 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003413 ssl_read_memory( data + min_len,
3414 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003415 }
3416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3418 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003424#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003425 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3426 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003427#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003428
Hanno Becker4c6876b2017-12-27 21:28:58 +00003429 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3430 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_DEBUG_ALL)
3433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003434#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003435 correct = 0;
3436 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003437 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003438 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003439
3440 /*
3441 * Finally check the correct flag
3442 */
3443 if( correct == 0 )
3444 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003445#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003446
3447 /* Make extra sure authentication was performed, exactly once */
3448 if( auth_done != 1 )
3449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Hanno Beckera5a2b082019-05-15 14:03:01 +01003454#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003455 if( rec->cid_len != 0 )
3456 {
3457 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3458 &rec->type );
3459 if( ret != 0 )
3460 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3461 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 return( 0 );
3467}
3468
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003469#undef MAC_NONE
3470#undef MAC_PLAINTEXT
3471#undef MAC_CIPHERTEXT
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003474/*
3475 * Compression/decompression functions
3476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003478{
3479 int ret;
3480 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003481 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003482 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003483 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003486
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003487 if( len_pre == 0 )
3488 return( 0 );
3489
Paul Bakker2770fbd2012-07-03 13:30:23 +00003490 memcpy( msg_pre, ssl->out_msg, len_pre );
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493 ssl->out_msglen ) );
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003496 ssl->out_msg, ssl->out_msglen );
3497
Paul Bakker48916f92012-09-16 19:57:18 +00003498 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3499 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3500 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003501 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502
Paul Bakker48916f92012-09-16 19:57:18 +00003503 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504 if( ret != Z_OK )
3505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3507 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 }
3509
Angus Grattond8213d02016-05-25 20:56:48 +10003510 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003511 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 ssl->out_msglen ) );
3515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003517 ssl->out_msg, ssl->out_msglen );
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
3521 return( 0 );
3522}
3523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525{
3526 int ret;
3527 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003528 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003530 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003534 if( len_pre == 0 )
3535 return( 0 );
3536
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537 memcpy( msg_pre, ssl->in_msg, len_pre );
3538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540 ssl->in_msglen ) );
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 ssl->in_msg, ssl->in_msglen );
3544
Paul Bakker48916f92012-09-16 19:57:18 +00003545 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3546 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3547 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003548 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003549 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 if( ret != Z_OK )
3553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3555 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556 }
3557
Angus Grattond8213d02016-05-25 20:56:48 +10003558 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003559 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562 ssl->in_msglen ) );
3563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003565 ssl->in_msg, ssl->in_msglen );
3566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568
3569 return( 0 );
3570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3574static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_PROTO_DTLS)
3577static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003578{
3579 /* If renegotiation is not enforced, retransmit until we would reach max
3580 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003581 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003582 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003583 uint32_t ratio =
3584 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3585 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003586 unsigned char doublings = 1;
3587
3588 while( ratio != 0 )
3589 {
3590 ++doublings;
3591 ratio >>= 1;
3592 }
3593
3594 if( ++ssl->renego_records_seen > doublings )
3595 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003597 return( 0 );
3598 }
3599 }
3600
3601 return( ssl_write_hello_request( ssl ) );
3602}
3603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003605
Paul Bakker5121ce52009-01-03 21:22:43 +00003606/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003607 * Fill the input message buffer by appending data to it.
3608 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003609 *
3610 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3611 * available (from this read and/or a previous one). Otherwise, an error code
3612 * is returned (possibly EOF or WANT_READ).
3613 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003614 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3615 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3616 * since we always read a whole datagram at once.
3617 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003618 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003619 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622{
Paul Bakker23986e52011-04-24 08:57:21 +00003623 int ret;
3624 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Hanno Beckera58a8962019-06-13 16:11:15 +01003628 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3629 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003632 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003634 }
3635
Angus Grattond8213d02016-05-25 20:56:48 +10003636 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003640 }
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003643 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003645 uint32_t timeout;
3646
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003647 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003648 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3649 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003650 {
3651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3652 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3654 }
3655
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003656 /*
3657 * The point is, we need to always read a full datagram at once, so we
3658 * sometimes read more then requested, and handle the additional data.
3659 * It could be the rest of the current record (while fetching the
3660 * header) and/or some other records in the same datagram.
3661 */
3662
3663 /*
3664 * Move to the next record in the already read datagram if applicable
3665 */
3666 if( ssl->next_record_offset != 0 )
3667 {
3668 if( ssl->in_left < ssl->next_record_offset )
3669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003672 }
3673
3674 ssl->in_left -= ssl->next_record_offset;
3675
3676 if( ssl->in_left != 0 )
3677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 ssl->next_record_offset ) );
3680 memmove( ssl->in_hdr,
3681 ssl->in_hdr + ssl->next_record_offset,
3682 ssl->in_left );
3683 }
3684
3685 ssl->next_record_offset = 0;
3686 }
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003690
3691 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003692 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003693 */
3694 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003698 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003699
3700 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003701 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003702 * are not at the beginning of a new record, the caller did something
3703 * wrong.
3704 */
3705 if( ssl->in_left != 0 )
3706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003709 }
3710
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003711 /*
3712 * Don't even try to read if time's out already.
3713 * This avoids by-passing the timer when repeatedly receiving messages
3714 * that will end up being dropped.
3715 */
3716 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003717 {
3718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003719 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003720 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003721 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003722 {
Angus Grattond8213d02016-05-25 20:56:48 +10003723 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003726 timeout = ssl->handshake->retransmit_timeout;
3727 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003728 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
Hanno Beckera58a8962019-06-13 16:11:15 +01003732 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3733 {
3734 ret = mbedtls_ssl_get_recv_timeout( ssl )
3735 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3736 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003737 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003738 {
3739 ret = mbedtls_ssl_get_recv( ssl )
3740 ( ssl->p_bio, ssl->in_hdr, len );
3741 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744
3745 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003747 }
3748
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003749 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003752 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003755 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003756 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003759 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003760 }
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003765 return( ret );
3766 }
3767
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003768 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003771 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3772 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003774 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003775 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003778 return( ret );
3779 }
3780
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003781 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003784 }
3785
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 if( ret < 0 )
3787 return( ret );
3788
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003789 ssl->in_left = ret;
3790 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003791 MBEDTLS_SSL_TRANSPORT_ELSE
3792#endif /* MBEDTLS_SSL_PROTO_DTLS */
3793#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003796 ssl->in_left, nb_want ) );
3797
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003798 while( ssl->in_left < nb_want )
3799 {
3800 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003801
3802 if( ssl_check_timer( ssl ) != 0 )
3803 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3804 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003805 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003806 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003807 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003808 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3809 ssl->in_hdr + ssl->in_left, len,
3810 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003811 }
3812 else
3813 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003814 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003815 ssl->in_hdr + ssl->in_left, len );
3816 }
3817 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003820 ssl->in_left, nb_want ) );
3821 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003822
3823 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003825
3826 if( ret < 0 )
3827 return( ret );
3828
mohammad160352aecb92018-03-28 23:41:40 -07003829 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003830 {
Darryl Green11999bb2018-03-13 15:22:58 +00003831 MBEDTLS_SSL_DEBUG_MSG( 1,
3832 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003833 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3835 }
3836
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003837 ssl->in_left += ret;
3838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003840#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
3844 return( 0 );
3845}
3846
3847/*
3848 * Flush any data not yet written
3849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003851{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003852 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003853 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Hanno Beckera58a8962019-06-13 16:11:15 +01003857 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003860 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003862 }
3863
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003864 /* Avoid incrementing counter if data is flushed */
3865 if( ssl->out_left == 0 )
3866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003868 return( 0 );
3869 }
3870
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 while( ssl->out_left > 0 )
3872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003874 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003875
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003877 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
3881 if( ret <= 0 )
3882 return( ret );
3883
mohammad160352aecb92018-03-28 23:41:40 -07003884 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003885 {
Darryl Green11999bb2018-03-13 15:22:58 +00003886 MBEDTLS_SSL_DEBUG_MSG( 1,
3887 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003888 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3890 }
3891
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 ssl->out_left -= ret;
3893 }
3894
Hanno Becker2b1e3542018-08-06 11:19:13 +01003895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003896 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003897 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003898 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003899 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003900 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003901#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003902#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003903 {
3904 ssl->out_hdr = ssl->out_buf + 8;
3905 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003906#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003907 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 return( 0 );
3912}
3913
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003914/*
3915 * Functions to handle the DTLS retransmission state machine
3916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003918/*
3919 * Append current handshake message to current outgoing flight
3920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3925 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3926 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003927
3928 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003929 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003930 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003933 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 }
3935
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003936 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941 }
3942
3943 /* Copy current handshake message with headers */
3944 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3945 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003946 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003947 msg->next = NULL;
3948
3949 /* Append to the current flight */
3950 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003951 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952 else
3953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955 while( cur->next != NULL )
3956 cur = cur->next;
3957 cur->next = msg;
3958 }
3959
Hanno Becker3b235902018-08-06 09:54:53 +01003960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003961 return( 0 );
3962}
3963
3964/*
3965 * Free the current flight of handshake messages
3966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 mbedtls_ssl_flight_item *cur = flight;
3970 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971
3972 while( cur != NULL )
3973 {
3974 next = cur->next;
3975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 mbedtls_free( cur->p );
3977 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978
3979 cur = next;
3980 }
3981}
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3984static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003985#endif
3986
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003988 * Swap transform_out and out_ctr with the alternative ones
3989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003993 unsigned char tmp_out_ctr[8];
3994
3995 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998 return;
3999 }
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004003 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004004 tmp_transform = ssl->transform_out;
4005 ssl->transform_out = ssl->handshake->alt_transform_out;
4006 ssl->handshake->alt_transform_out = tmp_transform;
4007
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004008 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004009 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4010 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004012
4013 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004014 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4017 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4022 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004023 }
4024 }
4025#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004026}
4027
4028/*
4029 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004030 */
4031int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4032{
4033 int ret = 0;
4034
4035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4036
4037 ret = mbedtls_ssl_flight_transmit( ssl );
4038
4039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4040
4041 return( ret );
4042}
4043
4044/*
4045 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004046 *
4047 * Need to remember the current message in case flush_output returns
4048 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004051int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004053 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004057 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004059
4060 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004061 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004062 ssl_swap_epochs( ssl );
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004065 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 while( ssl->handshake->cur_msg != NULL )
4068 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004069 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004070 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071
Hanno Beckere1dcb032018-08-17 16:47:58 +01004072 int const is_finished =
4073 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4074 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4075
Hanno Becker04da1892018-08-14 13:22:10 +01004076 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4077 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4078
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004079 /* Swap epochs before sending Finished: we can't do it after
4080 * sending ChangeCipherSpec, in case write returns WANT_READ.
4081 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004082 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004083 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004085 ssl_swap_epochs( ssl );
4086 }
4087
Hanno Becker67bc7c32018-08-06 11:33:50 +01004088 ret = ssl_get_remaining_payload_in_datagram( ssl );
4089 if( ret < 0 )
4090 return( ret );
4091 max_frag_len = (size_t) ret;
4092
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004093 /* CCS is copied as is, while HS messages may need fragmentation */
4094 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4095 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004096 if( max_frag_len == 0 )
4097 {
4098 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4099 return( ret );
4100
4101 continue;
4102 }
4103
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004104 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004105 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004106 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004107
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004108 /* Update position inside current message */
4109 ssl->handshake->cur_msg_p += cur->len;
4110 }
4111 else
4112 {
4113 const unsigned char * const p = ssl->handshake->cur_msg_p;
4114 const size_t hs_len = cur->len - 12;
4115 const size_t frag_off = p - ( cur->p + 12 );
4116 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004117 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004118
Hanno Beckere1dcb032018-08-17 16:47:58 +01004119 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004120 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004121 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004122 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004123
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4125 return( ret );
4126
4127 continue;
4128 }
4129 max_hs_frag_len = max_frag_len - 12;
4130
4131 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4132 max_hs_frag_len : rem_len;
4133
4134 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004137 (unsigned) cur_hs_frag_len,
4138 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004139 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004140
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004141 /* Messages are stored with handshake headers as if not fragmented,
4142 * copy beginning of headers then fill fragmentation fields.
4143 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4144 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004145
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004146 mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4147 mbedtls_platform_put_uint24_be( &ssl->out_msg[9], cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004148
4149 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4150
Hanno Becker3f7b9732018-08-28 09:53:25 +01004151 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004152 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4153 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004154 ssl->out_msgtype = cur->type;
4155
4156 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 }
4159
4160 /* If done with the current message move to the next one if any */
4161 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4162 {
4163 if( cur->next != NULL )
4164 {
4165 ssl->handshake->cur_msg = cur->next;
4166 ssl->handshake->cur_msg_p = cur->next->p + 12;
4167 }
4168 else
4169 {
4170 ssl->handshake->cur_msg = NULL;
4171 ssl->handshake->cur_msg_p = NULL;
4172 }
4173 }
4174
4175 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004176 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004179 return( ret );
4180 }
4181 }
4182
Hanno Becker67bc7c32018-08-06 11:33:50 +01004183 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4184 return( ret );
4185
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004186 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4188 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004189 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004192 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4193 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004194
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004196
4197 return( 0 );
4198}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004199
4200/*
4201 * To be called when the last message of an incoming flight is received.
4202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004204{
4205 /* We won't need to resend that one any more */
4206 ssl_flight_free( ssl->handshake->flight );
4207 ssl->handshake->flight = NULL;
4208 ssl->handshake->cur_msg = NULL;
4209
4210 /* The next incoming flight will start with this msg_seq */
4211 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4212
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004213 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004214 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004215
Hanno Becker0271f962018-08-16 13:23:47 +01004216 /* Clear future message buffering structure. */
4217 ssl_buffering_free( ssl );
4218
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004219 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004220 ssl_set_timer( ssl, 0 );
4221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4223 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004226 }
4227 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004229}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004230
4231/*
4232 * To be called when the last message of an outgoing flight is send.
4233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004235{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004236 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004237 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4240 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004243 }
4244 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004248
Paul Bakker5121ce52009-01-03 21:22:43 +00004249/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004250 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004252
4253/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004254 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004255 *
4256 * - fill in handshake headers
4257 * - update handshake checksum
4258 * - DTLS: save message for resending
4259 * - then pass to the record layer
4260 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004261 * DTLS: except for HelloRequest, messages are only queued, and will only be
4262 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004263 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004264 * Inputs:
4265 * - ssl->out_msglen: 4 + actual handshake message len
4266 * (4 is the size of handshake headers for TLS)
4267 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4268 * - ssl->out_msg + 4: the handshake message body
4269 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004270 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004271 * - ssl->out_msglen: the length of the record contents
4272 * (including handshake headers but excluding record headers)
4273 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004274 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004275int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004276{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004277 int ret;
4278 const size_t hs_len = ssl->out_msglen - 4;
4279 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4282
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004283 /*
4284 * Sanity checks
4285 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004286 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004287 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4288 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004289 /* In SSLv3, the client might send a NoCertificate alert. */
4290#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004291 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004292 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004293 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4294 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004295#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4296 {
4297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4299 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004302 /* Whenever we send anything different from a
4303 * HelloRequest we should be in a handshake - double check. */
4304 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4305 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004306 ssl->handshake == NULL )
4307 {
4308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004313 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004314 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004316 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004319 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004320#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004321
Hanno Beckerb50a2532018-08-06 11:52:54 +01004322 /* Double-check that we did not exceed the bounds
4323 * of the outgoing record buffer.
4324 * This should never fail as the various message
4325 * writing functions must obey the bounds of the
4326 * outgoing record buffer, but better be safe.
4327 *
4328 * Note: We deliberately do not check for the MTU or MFL here.
4329 */
4330 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4331 {
4332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4333 "size %u, maximum %u",
4334 (unsigned) ssl->out_msglen,
4335 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4337 }
4338
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004339 /*
4340 * Fill handshake headers
4341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004344 mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004345
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004346 /*
4347 * DTLS has additional fields in the Handshake layer,
4348 * between the length field and the actual payload:
4349 * uint16 message_seq;
4350 * uint24 fragment_offset;
4351 * uint24 fragment_length;
4352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004354 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004355 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004356 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004357 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004358 {
4359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4360 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004361 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004362 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4364 }
4365
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004366 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004367 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004368
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004369 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004370 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004371 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004372 mbedtls_platform_put_uint16_be( &ssl->out_msg[4], ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004373 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004374 }
4375 else
4376 {
4377 ssl->out_msg[4] = 0;
4378 ssl->out_msg[5] = 0;
4379 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004380
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004381 /* Handshake hashes are computed without fragmentation,
4382 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004383 memset( ssl->out_msg + 6, 0x00, 3 );
4384 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004387
Hanno Becker0207e532018-08-28 10:28:28 +01004388 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004389 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004390 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 }
4392
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004393 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004395 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004396 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4397 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004398 {
4399 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004402 return( ret );
4403 }
4404 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004405 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004406#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004407 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004408 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004409 {
4410 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4411 return( ret );
4412 }
4413 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004414
4415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4416
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004417 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004418}
4419
4420/*
4421 * Record layer functions
4422 */
4423
4424/*
4425 * Write current record.
4426 *
4427 * Uses:
4428 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4429 * - ssl->out_msglen: length of the record content (excl headers)
4430 * - ssl->out_msg: record content
4431 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004432int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004433{
4434 int ret, done = 0;
4435 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004436 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004437
4438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004441 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004443 {
4444 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004447 return( ret );
4448 }
4449
4450 len = ssl->out_msglen;
4451 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4455 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 ret = mbedtls_ssl_hw_record_write( ssl );
4460 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4463 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004464 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004465
4466 if( ret == 0 )
4467 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004468 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004470 if( !done )
4471 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004472 unsigned i;
4473 size_t protected_record_size;
4474
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004475 /* Skip writing the record content type to after the encryption,
4476 * as it may change when using the CID extension. */
4477
Hanno Becker2881d802019-05-22 14:44:53 +01004478 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4479 mbedtls_ssl_get_minor_ver( ssl ),
4480 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004481
Hanno Becker19859472018-08-06 09:40:20 +01004482 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004483 mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004484
Paul Bakker48916f92012-09-16 19:57:18 +00004485 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004486 {
Hanno Becker3307b532017-12-27 21:37:21 +00004487 mbedtls_record rec;
4488
4489 rec.buf = ssl->out_iv;
4490 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4491 ( ssl->out_iv - ssl->out_buf );
4492 rec.data_len = ssl->out_msglen;
4493 rec.data_offset = ssl->out_msg - rec.buf;
4494
4495 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004496 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4497 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004498 ssl->conf->transport, rec.ver );
4499 rec.type = ssl->out_msgtype;
4500
Hanno Beckera5a2b082019-05-15 14:03:01 +01004501#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004502 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004503 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004505
Hanno Becker611a83b2018-01-03 14:27:32 +00004506 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004507 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004508 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004511 return( ret );
4512 }
4513
Hanno Becker3307b532017-12-27 21:37:21 +00004514 if( rec.data_offset != 0 )
4515 {
4516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4518 }
4519
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004520 /* Update the record content type and CID. */
4521 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004522#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004523 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004524#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004525 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004526 mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004527 }
4528
Hanno Becker43395762019-05-03 14:46:38 +01004529 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004530
4531#if defined(MBEDTLS_SSL_PROTO_DTLS)
4532 /* In case of DTLS, double-check that we don't exceed
4533 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004534 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004535 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004536 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004537 if( ret < 0 )
4538 return( ret );
4539
4540 if( protected_record_size > (size_t) ret )
4541 {
4542 /* Should never happen */
4543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4544 }
4545 }
4546#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004547
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004548 /* Now write the potentially updated record content type. */
4549 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004552 "version = [%d:%d], msglen = %d",
4553 ssl->out_hdr[0], ssl->out_hdr[1],
4554 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004557 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004558
4559 ssl->out_left += protected_record_size;
4560 ssl->out_hdr += protected_record_size;
4561 ssl_update_out_pointers( ssl, ssl->transform_out );
4562
Hanno Becker04484622018-08-06 09:49:38 +01004563 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4564 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4565 break;
4566
4567 /* The loop goes to its end iff the counter is wrapping */
4568 if( i == ssl_ep_len( ssl ) )
4569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4571 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 }
4574
Hanno Becker67bc7c32018-08-06 11:33:50 +01004575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004576 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004577 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004578 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004579 size_t remaining;
4580 ret = ssl_get_remaining_payload_in_datagram( ssl );
4581 if( ret < 0 )
4582 {
4583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4584 ret );
4585 return( ret );
4586 }
4587
4588 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004589 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004590 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004591 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004592 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004593 else
4594 {
Hanno Becker513815a2018-08-20 11:56:09 +01004595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004596 }
4597 }
4598#endif /* MBEDTLS_SSL_PROTO_DTLS */
4599
4600 if( ( flush == SSL_FORCE_FLUSH ) &&
4601 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004604 return( ret );
4605 }
4606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
4609 return( 0 );
4610}
4611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004613
4614static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4615{
4616 if( ssl->in_msglen < ssl->in_hslen ||
4617 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4618 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4619 {
4620 return( 1 );
4621 }
4622 return( 0 );
4623}
Hanno Becker44650b72018-08-16 12:51:11 +01004624
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004625static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004626{
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03004627 return ( mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004628}
4629
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004630static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004631{
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03004632 return ( mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004633}
4634
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004635static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004636{
4637 uint32_t msg_len, frag_off, frag_len;
4638
4639 msg_len = ssl_get_hs_total_len( ssl );
4640 frag_off = ssl_get_hs_frag_off( ssl );
4641 frag_len = ssl_get_hs_frag_len( ssl );
4642
4643 if( frag_off > msg_len )
4644 return( -1 );
4645
4646 if( frag_len > msg_len - frag_off )
4647 return( -1 );
4648
4649 if( frag_len + 12 > ssl->in_msglen )
4650 return( -1 );
4651
4652 return( 0 );
4653}
4654
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004655/*
4656 * Mark bits in bitmask (used for DTLS HS reassembly)
4657 */
4658static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4659{
4660 unsigned int start_bits, end_bits;
4661
4662 start_bits = 8 - ( offset % 8 );
4663 if( start_bits != 8 )
4664 {
4665 size_t first_byte_idx = offset / 8;
4666
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004667 /* Special case */
4668 if( len <= start_bits )
4669 {
4670 for( ; len != 0; len-- )
4671 mask[first_byte_idx] |= 1 << ( start_bits - len );
4672
4673 /* Avoid potential issues with offset or len becoming invalid */
4674 return;
4675 }
4676
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004677 offset += start_bits; /* Now offset % 8 == 0 */
4678 len -= start_bits;
4679
4680 for( ; start_bits != 0; start_bits-- )
4681 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4682 }
4683
4684 end_bits = len % 8;
4685 if( end_bits != 0 )
4686 {
4687 size_t last_byte_idx = ( offset + len ) / 8;
4688
4689 len -= end_bits; /* Now len % 8 == 0 */
4690
4691 for( ; end_bits != 0; end_bits-- )
4692 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4693 }
4694
4695 memset( mask + offset / 8, 0xFF, len / 8 );
4696}
4697
4698/*
4699 * Check that bitmask is full
4700 */
4701static int ssl_bitmask_check( unsigned char *mask, size_t len )
4702{
4703 size_t i;
4704
4705 for( i = 0; i < len / 8; i++ )
4706 if( mask[i] != 0xFF )
4707 return( -1 );
4708
4709 for( i = 0; i < len % 8; i++ )
4710 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4711 return( -1 );
4712
4713 return( 0 );
4714}
4715
Hanno Becker56e205e2018-08-16 09:06:12 +01004716/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004717static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004718 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004719{
Hanno Becker56e205e2018-08-16 09:06:12 +01004720 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004721
Hanno Becker56e205e2018-08-16 09:06:12 +01004722 alloc_len = 12; /* Handshake header */
4723 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004724
Hanno Beckerd07df862018-08-16 09:14:58 +01004725 if( add_bitmap )
4726 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004727
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004728 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004729}
Hanno Becker56e205e2018-08-16 09:06:12 +01004730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004732
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004733static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004734{
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03004735 return ( mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004736}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004737
Simon Butcher99000142016-10-13 17:21:01 +01004738int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004743 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004745 }
4746
Hanno Becker12555c62018-08-16 12:47:53 +01004747 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004750 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004751 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004754 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004755 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004756 int ret;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03004757 unsigned int recv_msg_seq = 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 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005380 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
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 Kinnunen0b62ce82019-09-04 14:04:57 +03005430 unsigned int rec_epoch = mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005431
5432 /*
5433 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5434 * access the first byte of record content (handshake type), as we
5435 * have an active transform (possibly iv_len != 0), so use the
5436 * fact that the record header len is 13 instead.
5437 */
5438 if( rec_epoch == 0 &&
5439 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5440 MBEDTLS_SSL_IS_SERVER &&
5441 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5442 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5443 ssl->in_left > 13 &&
5444 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5445 {
5446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5447 "from the same port" ) );
5448 return( ssl_handle_possible_reconnect( ssl ) );
5449 }
5450
5451 return( 0 );
5452}
5453#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005455/*
5456 * If applicable, decrypt (and decompress) record content
5457 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005458static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5459 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005460{
5461 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005464 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5467 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 ret = mbedtls_ssl_hw_record_read( ssl );
5472 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5475 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005476 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005477
5478 if( ret == 0 )
5479 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005482 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005484 unsigned char const old_msg_type = rec->type;
5485
Hanno Becker611a83b2018-01-03 14:27:32 +00005486 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005487 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005490
Hanno Beckera5a2b082019-05-15 14:03:01 +01005491#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005492 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005493 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5494 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005495 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005497 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005498 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005499#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005500
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 return( ret );
5502 }
5503
Hanno Becker106f3da2019-07-12 09:35:58 +01005504 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005505 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005506 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005507 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005509
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005511 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Hanno Beckera5a2b082019-05-15 14:03:01 +01005513#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005514 /* We have already checked the record content type
5515 * in ssl_parse_record_header(), failing or silently
5516 * dropping the record in the case of an unknown type.
5517 *
5518 * Since with the use of CIDs, the record content type
5519 * might change during decryption, re-check the record
5520 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005521 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005522 {
5523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5524 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5525 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005526#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005527
Hanno Becker106f3da2019-07-12 09:35:58 +01005528 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005529 {
5530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005531 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005532 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005533 {
5534 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5536 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5537 }
5538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5539
5540 ssl->nb_zero++;
5541
5542 /*
5543 * Three or more empty messages may be a DoS attack
5544 * (excessive CPU consumption).
5545 */
5546 if( ssl->nb_zero > 3 )
5547 {
5548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005549 "messages, possible DoS attack" ) );
5550 /* Treat the records as if they were not properly authenticated,
5551 * thereby failing the connection if we see more than allowed
5552 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005553 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5554 }
5555 }
5556 else
5557 ssl->nb_zero = 0;
5558
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005559 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5560#if defined(MBEDTLS_SSL_PROTO_TLS)
5561 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005562 {
5563 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005564 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005565 if( ++ssl->in_ctr[i - 1] != 0 )
5566 break;
5567
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005568 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005569 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5572 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5573 }
5574 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005575#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005576
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 }
5578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005580 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005582 {
5583 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005586 return( ret );
5587 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005588 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005592 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005595 }
5596#endif
5597
Hanno Beckerf0242852019-07-09 17:30:02 +01005598 /* Check actual (decrypted) record content length against
5599 * configured maximum. */
5600 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5601 {
5602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5603 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5604 }
5605
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005606 return( 0 );
5607}
5608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005610
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005611/*
5612 * Read a record.
5613 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005614 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5615 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5616 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005617 */
Hanno Becker1097b342018-08-15 14:09:41 +01005618
5619/* Helper functions for mbedtls_ssl_read_record(). */
5620static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005621static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5622static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005623
Hanno Becker327c93b2018-08-15 13:56:18 +01005624int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005625 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005626{
5627 int ret;
5628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005631 if( ssl->keep_current_message == 0 )
5632 {
5633 do {
Simon Butcher99000142016-10-13 17:21:01 +01005634
Hanno Becker26994592018-08-15 14:14:59 +01005635 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005636 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005637 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005638
Hanno Beckere74d5562018-08-15 14:26:08 +01005639 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005640 {
Hanno Becker40f50842018-08-15 14:48:01 +01005641#if defined(MBEDTLS_SSL_PROTO_DTLS)
5642 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005643
Hanno Becker40f50842018-08-15 14:48:01 +01005644 /* We only check for buffered messages if the
5645 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005646 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005647 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005648 {
Hanno Becker40f50842018-08-15 14:48:01 +01005649 if( ssl_load_buffered_message( ssl ) == 0 )
5650 have_buffered = 1;
5651 }
5652
5653 if( have_buffered == 0 )
5654#endif /* MBEDTLS_SSL_PROTO_DTLS */
5655 {
5656 ret = ssl_get_next_record( ssl );
5657 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5658 continue;
5659
5660 if( ret != 0 )
5661 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005662 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005663 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005664 return( ret );
5665 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005666 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005667 }
5668
5669 ret = mbedtls_ssl_handle_message_type( ssl );
5670
Hanno Becker40f50842018-08-15 14:48:01 +01005671#if defined(MBEDTLS_SSL_PROTO_DTLS)
5672 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5673 {
5674 /* Buffer future message */
5675 ret = ssl_buffer_message( ssl );
5676 if( ret != 0 )
5677 return( ret );
5678
5679 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5680 }
5681#endif /* MBEDTLS_SSL_PROTO_DTLS */
5682
Hanno Becker90333da2017-10-10 11:27:13 +01005683 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5684 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005685
5686 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005687 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005688 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005689 return( ret );
5690 }
5691
Hanno Becker327c93b2018-08-15 13:56:18 +01005692 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005693 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005694 {
5695 mbedtls_ssl_update_handshake_status( ssl );
5696 }
Simon Butcher99000142016-10-13 17:21:01 +01005697 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005698 else
Simon Butcher99000142016-10-13 17:21:01 +01005699 {
Hanno Becker02f59072018-08-15 14:00:24 +01005700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005701 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005702 }
5703
5704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5705
5706 return( 0 );
5707}
5708
Hanno Becker40f50842018-08-15 14:48:01 +01005709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005710static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005711{
Hanno Becker40f50842018-08-15 14:48:01 +01005712 if( ssl->in_left > ssl->next_record_offset )
5713 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005714
Hanno Becker40f50842018-08-15 14:48:01 +01005715 return( 0 );
5716}
5717
5718static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5719{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005720 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005721 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005722 int ret = 0;
5723
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005724 if( hs == NULL )
5725 return( -1 );
5726
Hanno Beckere00ae372018-08-20 09:39:42 +01005727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5728
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005729 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5730 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5731 {
5732 /* Check if we have seen a ChangeCipherSpec before.
5733 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005734 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005735 {
5736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5737 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005738 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005739 }
5740
Hanno Becker39b8bc92018-08-28 17:17:13 +01005741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005742 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5743 ssl->in_msglen = 1;
5744 ssl->in_msg[0] = 1;
5745
5746 /* As long as they are equal, the exact value doesn't matter. */
5747 ssl->in_left = 0;
5748 ssl->next_record_offset = 0;
5749
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005750 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005751 goto exit;
5752 }
Hanno Becker37f95322018-08-16 13:55:32 +01005753
Hanno Beckerb8f50142018-08-28 10:01:34 +01005754#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005755 /* Debug only */
5756 {
5757 unsigned offset;
5758 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5759 {
5760 hs_buf = &hs->buffering.hs[offset];
5761 if( hs_buf->is_valid == 1 )
5762 {
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5764 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005765 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005766 }
5767 }
5768 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005769#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005770
5771 /* Check if we have buffered and/or fully reassembled the
5772 * next handshake message. */
5773 hs_buf = &hs->buffering.hs[0];
5774 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5775 {
5776 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005777 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005778
5779 /* Double-check that we haven't accidentally buffered
5780 * a message that doesn't fit into the input buffer. */
5781 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5782 {
5783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5785 }
5786
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5788 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5789 hs_buf->data, msg_len + 12 );
5790
5791 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5792 ssl->in_hslen = msg_len + 12;
5793 ssl->in_msglen = msg_len + 12;
5794 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5795
5796 ret = 0;
5797 goto exit;
5798 }
5799 else
5800 {
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5802 hs->in_msg_seq ) );
5803 }
5804
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005805 ret = -1;
5806
5807exit:
5808
5809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5810 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005811}
5812
Hanno Beckera02b0b42018-08-21 17:20:27 +01005813static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5814 size_t desired )
5815{
5816 int offset;
5817 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5819 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005820
Hanno Becker01315ea2018-08-21 17:22:17 +01005821 /* Get rid of future records epoch first, if such exist. */
5822 ssl_free_buffered_record( ssl );
5823
5824 /* Check if we have enough space available now. */
5825 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5826 hs->buffering.total_bytes_buffered ) )
5827 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005829 return( 0 );
5830 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005831
Hanno Becker4f432ad2018-08-28 10:02:32 +01005832 /* We don't have enough space to buffer the next expected handshake
5833 * message. Remove buffers used for future messages to gain space,
5834 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005835 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5836 offset >= 0; offset-- )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5839 offset ) );
5840
Hanno Beckerb309b922018-08-23 13:18:05 +01005841 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005842
5843 /* Check if we have enough space available now. */
5844 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5845 hs->buffering.total_bytes_buffered ) )
5846 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005848 return( 0 );
5849 }
5850 }
5851
5852 return( -1 );
5853}
5854
Hanno Becker40f50842018-08-15 14:48:01 +01005855static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5856{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857 int ret = 0;
5858 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5859
5860 if( hs == NULL )
5861 return( 0 );
5862
5863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5864
5865 switch( ssl->in_msgtype )
5866 {
5867 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005869
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005870 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871 break;
5872
5873 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005874 {
5875 unsigned recv_msg_seq_offset;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005876 unsigned recv_msg_seq = mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
5877
Hanno Becker37f95322018-08-16 13:55:32 +01005878 mbedtls_ssl_hs_buffer *hs_buf;
5879 size_t msg_len = ssl->in_hslen - 12;
5880
5881 /* We should never receive an old handshake
5882 * message - double-check nonetheless. */
5883 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5884 {
5885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5886 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5887 }
5888
5889 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5890 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5891 {
5892 /* Silently ignore -- message too far in the future */
5893 MBEDTLS_SSL_DEBUG_MSG( 2,
5894 ( "Ignore future HS message with sequence number %u, "
5895 "buffering window %u - %u",
5896 recv_msg_seq, ssl->handshake->in_msg_seq,
5897 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5898
5899 goto exit;
5900 }
5901
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5903 recv_msg_seq, recv_msg_seq_offset ) );
5904
5905 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5906
5907 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005908 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005909 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005910 size_t reassembly_buf_sz;
5911
Hanno Becker37f95322018-08-16 13:55:32 +01005912 hs_buf->is_fragmented =
5913 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5914
5915 /* We copy the message back into the input buffer
5916 * after reassembly, so check that it's not too large.
5917 * This is an implementation-specific limitation
5918 * and not one from the standard, hence it is not
5919 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005920 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005921 {
5922 /* Ignore message */
5923 goto exit;
5924 }
5925
Hanno Beckere0b150f2018-08-21 15:51:03 +01005926 /* Check if we have enough space to buffer the message. */
5927 if( hs->buffering.total_bytes_buffered >
5928 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5932 }
5933
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005934 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5935 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005936
5937 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5938 hs->buffering.total_bytes_buffered ) )
5939 {
5940 if( recv_msg_seq_offset > 0 )
5941 {
5942 /* If we can't buffer a future message because
5943 * of space limitations -- ignore. */
5944 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",
5945 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5946 (unsigned) hs->buffering.total_bytes_buffered ) );
5947 goto exit;
5948 }
Hanno Beckere1801392018-08-21 16:51:05 +01005949 else
5950 {
5951 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",
5952 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5953 (unsigned) hs->buffering.total_bytes_buffered ) );
5954 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005955
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005957 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005958 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",
5959 (unsigned) msg_len,
5960 (unsigned) reassembly_buf_sz,
5961 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005962 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005963 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5964 goto exit;
5965 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005966 }
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5969 msg_len ) );
5970
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005971 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5972 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005973 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005974 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005975 goto exit;
5976 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005977 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005978
5979 /* Prepare final header: copy msg_type, length and message_seq,
5980 * then add standardised fragment_offset and fragment_length */
5981 memcpy( hs_buf->data, ssl->in_msg, 6 );
5982 memset( hs_buf->data + 6, 0, 3 );
5983 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5984
5985 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005986
5987 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005988 }
5989 else
5990 {
5991 /* Make sure msg_type and length are consistent */
5992 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5993 {
5994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5995 /* Ignore */
5996 goto exit;
5997 }
5998 }
5999
Hanno Becker4422bbb2018-08-20 09:40:19 +01006000 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006001 {
6002 size_t frag_len, frag_off;
6003 unsigned char * const msg = hs_buf->data + 12;
6004
6005 /*
6006 * Check and copy current fragment
6007 */
6008
6009 /* Validation of header fields already done in
6010 * mbedtls_ssl_prepare_handshake_record(). */
6011 frag_off = ssl_get_hs_frag_off( ssl );
6012 frag_len = ssl_get_hs_frag_len( ssl );
6013
6014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6015 frag_off, frag_len ) );
6016 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6017
6018 if( hs_buf->is_fragmented )
6019 {
6020 unsigned char * const bitmask = msg + msg_len;
6021 ssl_bitmask_set( bitmask, frag_off, frag_len );
6022 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6023 msg_len ) == 0 );
6024 }
6025 else
6026 {
6027 hs_buf->is_complete = 1;
6028 }
6029
6030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6031 hs_buf->is_complete ? "" : "not yet " ) );
6032 }
6033
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006034 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006035 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006036
6037 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006038 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006039 break;
6040 }
6041
6042exit:
6043
6044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6045 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006046}
6047#endif /* MBEDTLS_SSL_PROTO_DTLS */
6048
Hanno Becker1097b342018-08-15 14:09:41 +01006049static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006050{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006051 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006052 * Consume last content-layer message and potentially
6053 * update in_msglen which keeps track of the contents'
6054 * consumption state.
6055 *
6056 * (1) Handshake messages:
6057 * Remove last handshake message, move content
6058 * and adapt in_msglen.
6059 *
6060 * (2) Alert messages:
6061 * Consume whole record content, in_msglen = 0.
6062 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006063 * (3) Change cipher spec:
6064 * Consume whole record content, in_msglen = 0.
6065 *
6066 * (4) Application data:
6067 * Don't do anything - the record layer provides
6068 * the application data as a stream transport
6069 * and consumes through mbedtls_ssl_read only.
6070 *
6071 */
6072
6073 /* Case (1): Handshake messages */
6074 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006075 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006076 /* Hard assertion to be sure that no application data
6077 * is in flight, as corrupting ssl->in_msglen during
6078 * ssl->in_offt != NULL is fatal. */
6079 if( ssl->in_offt != NULL )
6080 {
6081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6083 }
6084
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006085 /*
6086 * Get next Handshake message in the current record
6087 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006088
Hanno Becker4a810fb2017-05-24 16:27:30 +01006089 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006090 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006091 * current handshake content: If DTLS handshake
6092 * fragmentation is used, that's the fragment
6093 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006094 * size here is faulty and should be changed at
6095 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 * (2) While it doesn't seem to cause problems, one
6097 * has to be very careful not to assume that in_hslen
6098 * is always <= in_msglen in a sensible communication.
6099 * Again, it's wrong for DTLS handshake fragmentation.
6100 * The following check is therefore mandatory, and
6101 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006102 * Additionally, ssl->in_hslen might be arbitrarily out of
6103 * bounds after handling a DTLS message with an unexpected
6104 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006105 */
6106 if( ssl->in_hslen < ssl->in_msglen )
6107 {
6108 ssl->in_msglen -= ssl->in_hslen;
6109 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6110 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006111
Hanno Becker4a810fb2017-05-24 16:27:30 +01006112 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6113 ssl->in_msg, ssl->in_msglen );
6114 }
6115 else
6116 {
6117 ssl->in_msglen = 0;
6118 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006119
Hanno Becker4a810fb2017-05-24 16:27:30 +01006120 ssl->in_hslen = 0;
6121 }
6122 /* Case (4): Application data */
6123 else if( ssl->in_offt != NULL )
6124 {
6125 return( 0 );
6126 }
6127 /* Everything else (CCS & Alerts) */
6128 else
6129 {
6130 ssl->in_msglen = 0;
6131 }
6132
Hanno Becker1097b342018-08-15 14:09:41 +01006133 return( 0 );
6134}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006135
Hanno Beckere74d5562018-08-15 14:26:08 +01006136static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6137{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006138 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006139 return( 1 );
6140
6141 return( 0 );
6142}
6143
Hanno Becker5f066e72018-08-16 14:56:31 +01006144#if defined(MBEDTLS_SSL_PROTO_DTLS)
6145
6146static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6147{
6148 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6149 if( hs == NULL )
6150 return;
6151
Hanno Becker01315ea2018-08-21 17:22:17 +01006152 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006153 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006154 hs->buffering.total_bytes_buffered -=
6155 hs->buffering.future_record.len;
6156
6157 mbedtls_free( hs->buffering.future_record.data );
6158 hs->buffering.future_record.data = NULL;
6159 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006160}
6161
6162static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6163{
6164 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6165 unsigned char * rec;
6166 size_t rec_len;
6167 unsigned rec_epoch;
6168
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006169 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006170 return( 0 );
6171
6172 if( hs == NULL )
6173 return( 0 );
6174
Hanno Becker5f066e72018-08-16 14:56:31 +01006175 rec = hs->buffering.future_record.data;
6176 rec_len = hs->buffering.future_record.len;
6177 rec_epoch = hs->buffering.future_record.epoch;
6178
6179 if( rec == NULL )
6180 return( 0 );
6181
Hanno Becker4cb782d2018-08-20 11:19:05 +01006182 /* Only consider loading future records if the
6183 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006184 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006185 return( 0 );
6186
Hanno Becker5f066e72018-08-16 14:56:31 +01006187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6188
6189 if( rec_epoch != ssl->in_epoch )
6190 {
6191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6192 goto exit;
6193 }
6194
6195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6196
6197 /* Double-check that the record is not too large */
6198 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6199 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6200 {
6201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6203 }
6204
6205 memcpy( ssl->in_hdr, rec, rec_len );
6206 ssl->in_left = rec_len;
6207 ssl->next_record_offset = 0;
6208
6209 ssl_free_buffered_record( ssl );
6210
6211exit:
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6213 return( 0 );
6214}
6215
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006216static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6217 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006218{
6219 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006220
6221 /* Don't buffer future records outside handshakes. */
6222 if( hs == NULL )
6223 return( 0 );
6224
6225 /* Only buffer handshake records (we are only interested
6226 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006227 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006228 return( 0 );
6229
6230 /* Don't buffer more than one future epoch record. */
6231 if( hs->buffering.future_record.data != NULL )
6232 return( 0 );
6233
Hanno Becker01315ea2018-08-21 17:22:17 +01006234 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006235 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006236 hs->buffering.total_bytes_buffered ) )
6237 {
6238 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 +01006239 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006240 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006241 return( 0 );
6242 }
6243
Hanno Becker5f066e72018-08-16 14:56:31 +01006244 /* Buffer record */
6245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6246 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006247 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006248
6249 /* ssl_parse_record_header() only considers records
6250 * of the next epoch as candidates for buffering. */
6251 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006253
6254 hs->buffering.future_record.data =
6255 mbedtls_calloc( 1, hs->buffering.future_record.len );
6256 if( hs->buffering.future_record.data == NULL )
6257 {
6258 /* If we run out of RAM trying to buffer a
6259 * record from the next epoch, just ignore. */
6260 return( 0 );
6261 }
6262
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006263 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006264
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006265 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006266 return( 0 );
6267}
6268
6269#endif /* MBEDTLS_SSL_PROTO_DTLS */
6270
Hanno Beckere74d5562018-08-15 14:26:08 +01006271static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006272{
6273 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006274 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006275
Hanno Becker5f066e72018-08-16 14:56:31 +01006276#if defined(MBEDTLS_SSL_PROTO_DTLS)
6277 /* We might have buffered a future record; if so,
6278 * and if the epoch matches now, load it.
6279 * On success, this call will set ssl->in_left to
6280 * the length of the buffered record, so that
6281 * the calls to ssl_fetch_input() below will
6282 * essentially be no-ops. */
6283 ret = ssl_load_buffered_record( ssl );
6284 if( ret != 0 )
6285 return( ret );
6286#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006287
Hanno Becker8b09b732019-05-08 12:03:28 +01006288 /* Ensure that we have enough space available for the default form
6289 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6290 * with no space for CIDs counted in). */
6291 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6292 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006295 return( ret );
6296 }
6297
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006298 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6299 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006302 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006303 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006304 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6305 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006306 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006307 if( ret != 0 )
6308 return( ret );
6309
6310 /* Fall through to handling of unexpected records */
6311 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6312 }
6313
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006314 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6315 {
Hanno Becker87b56262019-07-10 14:37:41 +01006316#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006317 /* Reset in pointers to default state for TLS/DTLS records,
6318 * assuming no CID and no offset between record content and
6319 * record plaintext. */
6320 ssl_update_in_pointers( ssl );
6321
Hanno Becker69412452019-07-12 08:33:49 +01006322 /* Setup internal message pointers from record structure. */
6323 ssl->in_msgtype = rec.type;
6324#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6325 ssl->in_len = ssl->in_cid + rec.cid_len;
6326#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006327 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006328 ssl->in_msglen = rec.data_len;
6329
Hanno Becker87b56262019-07-10 14:37:41 +01006330 ret = ssl_check_client_reconnect( ssl );
6331 if( ret != 0 )
6332 return( ret );
6333#endif
6334
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006335 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006336 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006337
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6339 "(header)" ) );
6340 }
6341 else
6342 {
6343 /* Skip invalid record and the rest of the datagram */
6344 ssl->next_record_offset = 0;
6345 ssl->in_left = 0;
6346
6347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6348 "(header)" ) );
6349 }
6350
6351 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006352 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006353 }
Hanno Becker87b56262019-07-10 14:37:41 +01006354 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006355#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006356 {
6357 return( ret );
6358 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006359 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006362 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006363 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006364 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006365 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006366 if( ssl->next_record_offset < ssl->in_left )
6367 {
6368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6369 }
6370 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006371 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006372#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006373#if defined(MBEDTLS_SSL_PROTO_TLS)
6374 {
Hanno Beckere0452772019-07-10 17:12:07 +01006375 /*
6376 * Fetch record contents from underlying transport.
6377 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006378 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006379 if( ret != 0 )
6380 {
6381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6382 return( ret );
6383 }
6384
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006385 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006386 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006387#endif /* MBEDTLS_SSL_PROTO_TLS */
6388
6389 /*
6390 * Decrypt record contents.
6391 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006392
Hanno Beckera89610a2019-07-11 13:07:45 +01006393 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006396 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006397 {
6398 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006400 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006401 /* Except when waiting for Finished as a bad mac here
6402 * probably means something went wrong in the handshake
6403 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6404 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6405 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6406 {
6407#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6408 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6409 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006410 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006411 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6412 }
6413#endif
6414 return( ret );
6415 }
6416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006418 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6419 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6422 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006423 }
6424#endif
6425
Hanno Becker4a810fb2017-05-24 16:27:30 +01006426 /* As above, invalid records cause
6427 * dismissal of the whole datagram. */
6428
6429 ssl->next_record_offset = 0;
6430 ssl->in_left = 0;
6431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006433 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006434 }
6435
6436 return( ret );
6437 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006438 MBEDTLS_SSL_TRANSPORT_ELSE
6439#endif /* MBEDTLS_SSL_PROTO_DTLS */
6440#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006441 {
6442 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6444 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006445 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006446 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006448 }
6449#endif
6450 return( ret );
6451 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006452#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006453 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006454
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006455
6456 /* Reset in pointers to default state for TLS/DTLS records,
6457 * assuming no CID and no offset between record content and
6458 * record plaintext. */
6459 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006460#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6461 ssl->in_len = ssl->in_cid + rec.cid_len;
6462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006463 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006464
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006465 /* The record content type may change during decryption,
6466 * so re-read it. */
6467 ssl->in_msgtype = rec.type;
6468 /* Also update the input buffer, because unfortunately
6469 * the server-side ssl_parse_client_hello() reparses the
6470 * record header when receiving a ClientHello initiating
6471 * a renegotiation. */
6472 ssl->in_hdr[0] = rec.type;
6473 ssl->in_msg = rec.buf + rec.data_offset;
6474 ssl->in_msglen = rec.data_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006475 mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006476
Simon Butcher99000142016-10-13 17:21:01 +01006477 return( 0 );
6478}
6479
6480int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6481{
6482 int ret;
6483
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006484 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006485 * Handle particular types of records
6486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 {
Simon Butcher99000142016-10-13 17:21:01 +01006489 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6490 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006491 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 }
6494
Hanno Beckere678eaa2018-08-21 14:57:46 +01006495 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006496 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006497 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006498 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6500 ssl->in_msglen ) );
6501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006502 }
6503
Hanno Beckere678eaa2018-08-21 14:57:46 +01006504 if( ssl->in_msg[0] != 1 )
6505 {
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6507 ssl->in_msg[0] ) );
6508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6509 }
6510
6511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006513 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6514 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6515 {
6516 if( ssl->handshake == NULL )
6517 {
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6519 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6520 }
6521
6522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6523 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6524 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006525#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006526 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006530 if( ssl->in_msglen != 2 )
6531 {
6532 /* Note: Standard allows for more than one 2 byte alert
6533 to be packed in a single message, but Mbed TLS doesn't
6534 currently support this. */
6535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6536 ssl->in_msglen ) );
6537 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6538 }
6539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 ssl->in_msg[0], ssl->in_msg[1] ) );
6542
6543 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006544 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006549 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6554 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6557 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006559
6560#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6561 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6562 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6563 {
Hanno Becker90333da2017-10-10 11:27:13 +01006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006565 /* Will be handled when trying to parse ServerHello */
6566 return( 0 );
6567 }
6568#endif
6569
6570#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006571 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006572 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6573 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006574 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6575 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6576 {
6577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6578 /* Will be handled in mbedtls_ssl_parse_certificate() */
6579 return( 0 );
6580 }
6581#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6582
6583 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006584 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 }
6586
Hanno Beckerc76c6192017-06-06 10:03:17 +01006587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006588 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006589 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006590 /* Drop unexpected ApplicationData records,
6591 * except at the beginning of renegotiations */
6592 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6593 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6594#if defined(MBEDTLS_SSL_RENEGOTIATION)
6595 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6596 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006597#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006598 )
6599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6601 return( MBEDTLS_ERR_SSL_NON_FATAL );
6602 }
6603
6604 if( ssl->handshake != NULL &&
6605 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6606 {
6607 ssl_handshake_wrapup_free_hs_transform( ssl );
6608 }
6609 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006610#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006611
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 return( 0 );
6613}
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006616{
6617 int ret;
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6620 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6621 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006622 {
6623 return( ret );
6624 }
6625
6626 return( 0 );
6627}
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006630 unsigned char level,
6631 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006632{
6633 int ret;
6634
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006635 if( ssl == NULL || ssl->conf == NULL )
6636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006642 ssl->out_msglen = 2;
6643 ssl->out_msg[0] = level;
6644 ssl->out_msg[1] = message;
6645
Hanno Becker67bc7c32018-08-06 11:33:50 +01006646 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006649 return( ret );
6650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006652
6653 return( 0 );
6654}
6655
Hanno Becker17572472019-02-08 07:19:04 +00006656#if defined(MBEDTLS_X509_CRT_PARSE_C)
6657static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6658{
6659#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6660 if( session->peer_cert != NULL )
6661 {
6662 mbedtls_x509_crt_free( session->peer_cert );
6663 mbedtls_free( session->peer_cert );
6664 session->peer_cert = NULL;
6665 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006666#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006667 if( session->peer_cert_digest != NULL )
6668 {
6669 /* Zeroization is not necessary. */
6670 mbedtls_free( session->peer_cert_digest );
6671 session->peer_cert_digest = NULL;
6672 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6673 session->peer_cert_digest_len = 0;
6674 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006675#else
6676 ((void) session);
6677#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006678}
6679#endif /* MBEDTLS_X509_CRT_PARSE_C */
6680
Paul Bakker5121ce52009-01-03 21:22:43 +00006681/*
6682 * Handshake functions
6683 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006684#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006685/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687{
Hanno Beckerdf645962019-06-26 13:02:22 +01006688 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6689 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
Hanno Becker5097cba2019-02-05 13:36:46 +00006693 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006696 ssl->state++;
6697 return( 0 );
6698 }
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006705{
Hanno Beckerdf645962019-06-26 13:02:22 +01006706 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6707 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006710
Hanno Becker5097cba2019-02-05 13:36:46 +00006711 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006714 ssl->state++;
6715 return( 0 );
6716 }
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720}
Gilles Peskinef9828522017-05-03 12:28:43 +02006721
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006722#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006723/* Some certificate support -> implement write and parse */
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006730 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6731 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Hanno Becker5097cba2019-02-05 13:36:46 +00006735 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 ssl->state++;
6739 return( 0 );
6740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006743 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6744 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 {
6746 if( ssl->client_auth == 0 )
6747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006749 ssl->state++;
6750 return( 0 );
6751 }
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 /*
6755 * If using SSLv3 and got no cert, send an Alert message
6756 * (otherwise an empty Certificate message will be sent).
6757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006759 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 {
6761 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6763 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6764 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 goto write_msg;
6768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#endif /* MBEDTLS_SSL_CLI_C */
6772#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006773 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6778 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 }
6780 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006784
6785 /*
6786 * 0 . 0 handshake type
6787 * 1 . 3 handshake length
6788 * 4 . 6 length of all certs
6789 * 7 . 9 length of cert. 1
6790 * 10 . n-1 peer certificate
6791 * n . n+2 length of cert. 2
6792 * n+3 . ... upper level cert, etc.
6793 */
6794 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796
Paul Bakker29087132010-03-21 21:03:34 +00006797 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
6799 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006800 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006803 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006807 mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
6809 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6810 i += n; crt = crt->next;
6811 }
6812
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006813 mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
6815 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6817 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006819#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006820write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
6823 ssl->state++;
6824
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006825 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 return( ret );
6829 }
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Paul Bakkered27a042013-04-18 22:46:23 +02006833 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834}
6835
Hanno Becker285ff0c2019-01-31 07:44:03 +00006836#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006837
6838#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006839static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6840 unsigned char *crt_buf,
6841 size_t crt_buf_len )
6842{
6843 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6844
6845 if( peer_crt == NULL )
6846 return( -1 );
6847
6848 if( peer_crt->raw.len != crt_buf_len )
6849 return( -1 );
6850
Hanno Becker68b856d2019-02-08 14:00:04 +00006851 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006852}
Hanno Becker5882dd02019-06-06 16:25:57 +01006853#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006854static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6855 unsigned char *crt_buf,
6856 size_t crt_buf_len )
6857{
6858 int ret;
6859 unsigned char const * const peer_cert_digest =
6860 ssl->session->peer_cert_digest;
6861 mbedtls_md_type_t const peer_cert_digest_type =
6862 ssl->session->peer_cert_digest_type;
6863 mbedtls_md_info_t const * const digest_info =
6864 mbedtls_md_info_from_type( peer_cert_digest_type );
6865 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6866 size_t digest_len;
6867
6868 if( peer_cert_digest == NULL || digest_info == NULL )
6869 return( -1 );
6870
6871 digest_len = mbedtls_md_get_size( digest_info );
6872 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6873 return( -1 );
6874
6875 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6876 if( ret != 0 )
6877 return( -1 );
6878
6879 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6880}
Hanno Becker5882dd02019-06-06 16:25:57 +01006881#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006882#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006883
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006884/*
6885 * Once the certificate message is read, parse it into a cert chain and
6886 * perform basic checks, but leave actual verification to the caller
6887 */
Hanno Becker35e41772019-02-05 15:37:23 +00006888static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6889 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006890{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006891 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006892#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6893 int crt_cnt=0;
6894#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006895 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006896 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006901 mbedtls_ssl_pend_fatal_alert( ssl,
6902 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 }
6905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6907 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006910 mbedtls_ssl_pend_fatal_alert( ssl,
6911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006913 }
6914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006916
Paul Bakker5121ce52009-01-03 21:22:43 +00006917 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006919 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006920 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006921
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006922 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006926 mbedtls_ssl_pend_fatal_alert( ssl,
6927 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 }
6930
Hanno Becker33c3dc82019-01-30 14:46:46 +00006931 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6932 i += 3;
6933
Hanno Becker33c3dc82019-01-30 14:46:46 +00006934 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 while( i < ssl->in_hslen )
6936 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006937 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006938 if ( i + 3 > ssl->in_hslen ) {
6939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006940 mbedtls_ssl_pend_fatal_alert( ssl,
6941 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006942 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6943 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006944 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6945 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 if( ssl->in_msg[i] != 0 )
6947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006949 mbedtls_ssl_pend_fatal_alert( ssl,
6950 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006952 }
6953
Hanno Becker33c3dc82019-01-30 14:46:46 +00006954 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006955 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 i += 3;
6957
6958 if( n < 128 || i + n > ssl->in_hslen )
6959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006961 mbedtls_ssl_pend_fatal_alert( ssl,
6962 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006964 }
6965
Hanno Becker33c3dc82019-01-30 14:46:46 +00006966 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006967#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6968 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006969 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6970 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006971 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006972 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006973 /* During client-side renegotiation, check that the server's
6974 * end-CRTs hasn't changed compared to the initial handshake,
6975 * mitigating the triple handshake attack. On success, reuse
6976 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6978 if( ssl_check_peer_crt_unchanged( ssl,
6979 &ssl->in_msg[i],
6980 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006981 {
Hanno Becker35e41772019-02-05 15:37:23 +00006982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006983 mbedtls_ssl_pend_fatal_alert( ssl,
6984 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006985 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006986 }
Hanno Becker35e41772019-02-05 15:37:23 +00006987
6988 /* Now we can safely free the original chain. */
6989 ssl_clear_peer_cert( ssl->session );
6990 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006991#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6992
Hanno Becker33c3dc82019-01-30 14:46:46 +00006993 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006994#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006995 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006996#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006997 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006998 * it in-place from the input buffer instead of making a copy. */
6999 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7000#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007001 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007003 case 0: /*ok*/
7004 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7005 /* Ignore certificate with an unknown algorithm: maybe a
7006 prior certificate was already trusted. */
7007 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007008
Hanno Becker33c3dc82019-01-30 14:46:46 +00007009 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7010 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7011 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007012
Hanno Becker33c3dc82019-01-30 14:46:46 +00007013 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7014 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7015 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007016
Hanno Becker33c3dc82019-01-30 14:46:46 +00007017 default:
7018 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7019 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007020 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007021 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7022 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 }
7024
7025 i += n;
7026 }
7027
Hanno Becker35e41772019-02-05 15:37:23 +00007028 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007029 return( 0 );
7030}
7031
Hanno Beckerb8a08572019-02-05 12:49:06 +00007032#if defined(MBEDTLS_SSL_SRV_C)
7033static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7034{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007035 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007036 return( -1 );
7037
7038#if defined(MBEDTLS_SSL_PROTO_SSL3)
7039 /*
7040 * Check if the client sent an empty certificate
7041 */
Hanno Becker2881d802019-05-22 14:44:53 +01007042 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007043 {
7044 if( ssl->in_msglen == 2 &&
7045 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7046 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7047 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7048 {
7049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7050 return( 0 );
7051 }
7052
7053 return( -1 );
7054 }
7055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7056
7057#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7058 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7059 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7060 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7061 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7062 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7065 return( 0 );
7066 }
7067
Hanno Beckerb8a08572019-02-05 12:49:06 +00007068#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7069 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007070
7071 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007072}
7073#endif /* MBEDTLS_SSL_SRV_C */
7074
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007075/* Check if a certificate message is expected.
7076 * Return either
7077 * - SSL_CERTIFICATE_EXPECTED, or
7078 * - SSL_CERTIFICATE_SKIP
7079 * indicating whether a Certificate message is expected or not.
7080 */
7081#define SSL_CERTIFICATE_EXPECTED 0
7082#define SSL_CERTIFICATE_SKIP 1
7083static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7084 int authmode )
7085{
Hanno Becker473f98f2019-06-26 10:27:32 +01007086 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007087 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007088
7089 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7090 return( SSL_CERTIFICATE_SKIP );
7091
7092#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007093 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007094 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007095 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7096 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7097 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007098 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007099 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007100
7101 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7102 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007103 ssl->session_negotiate->verify_result =
7104 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7105 return( SSL_CERTIFICATE_SKIP );
7106 }
7107 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007108#else
7109 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007110#endif /* MBEDTLS_SSL_SRV_C */
7111
7112 return( SSL_CERTIFICATE_EXPECTED );
7113}
7114
Hanno Becker3cf50612019-02-05 14:36:34 +00007115static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7116 int authmode,
7117 mbedtls_x509_crt *chain,
7118 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007119{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007120 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007121 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007122 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007123 mbedtls_x509_crt *ca_chain;
7124 mbedtls_x509_crl *ca_crl;
7125
7126 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7127 return( 0 );
7128
7129#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7130 if( ssl->handshake->sni_ca_chain != NULL )
7131 {
7132 ca_chain = ssl->handshake->sni_ca_chain;
7133 ca_crl = ssl->handshake->sni_ca_crl;
7134 }
7135 else
7136#endif
7137 {
7138 ca_chain = ssl->conf->ca_chain;
7139 ca_crl = ssl->conf->ca_crl;
7140 }
7141
7142 /*
7143 * Main check: verify certificate
7144 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007145 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007146 chain,
7147 ca_chain, ca_crl,
7148 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007149#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007150 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007151#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007152 &ssl->session_negotiate->verify_result,
7153 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7154
Hanno Becker8c13ee62019-02-26 16:48:17 +00007155 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007156 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007157 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007158 }
7159
7160#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007161 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007162 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7163#endif
7164
7165 /*
7166 * Secondary checks: always done, but change 'ret' only if it was 0
7167 */
7168
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007169#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007170 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007171 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007172#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007173 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007174#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007175 mbedtls_pk_context *pk;
7176 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7177 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007178 {
7179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007180 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007181 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007182
7183 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007184 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007185 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007186 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007187 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007188
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007189 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007190#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191
7192 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007193 {
7194 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7195
7196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007197 if( verify_ret == 0 )
7198 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007199 }
7200 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007201#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007202
7203 if( mbedtls_ssl_check_cert_usage( chain,
7204 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007205 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7206 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007207 &ssl->session_negotiate->verify_result ) != 0 )
7208 {
7209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007210 if( verify_ret == 0 )
7211 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007212 }
7213
7214 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7215 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7216 * with details encoded in the verification flags. All other kinds
7217 * of error codes, including those from the user provided f_vrfy
7218 * functions, are treated as fatal and lead to a failure of
7219 * ssl_parse_certificate even if verification was optional. */
7220 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7222 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007223 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007224 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007225 }
7226
7227 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7228 {
7229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007230 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007231 }
7232
Hanno Becker8c13ee62019-02-26 16:48:17 +00007233 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007234 {
7235 uint8_t alert;
7236
7237 /* The certificate may have been rejected for several reasons.
7238 Pick one and send the corresponding alert. Which alert to send
7239 may be a subject of debate in some cases. */
7240 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7241 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7242 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7243 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7244 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7245 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7246 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7247 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7248 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7249 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7250 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7251 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7252 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7253 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7254 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7255 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7256 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7257 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7258 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7259 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7260 else
7261 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007262 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007263 }
7264
7265#if defined(MBEDTLS_DEBUG_C)
7266 if( ssl->session_negotiate->verify_result != 0 )
7267 {
7268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7269 ssl->session_negotiate->verify_result ) );
7270 }
7271 else
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7274 }
7275#endif /* MBEDTLS_DEBUG_C */
7276
Hanno Becker8c13ee62019-02-26 16:48:17 +00007277 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007278}
7279
Hanno Becker34106f62019-02-08 14:59:05 +00007280#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007281
7282#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007283static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7284 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007285{
7286 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007287 /* Remember digest of the peer's end-CRT. */
7288 ssl->session_negotiate->peer_cert_digest =
7289 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7290 if( ssl->session_negotiate->peer_cert_digest == NULL )
7291 {
7292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7293 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007294 mbedtls_ssl_pend_fatal_alert( ssl,
7295 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007296
7297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7298 }
7299
7300 ret = mbedtls_md( mbedtls_md_info_from_type(
7301 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7302 start, len,
7303 ssl->session_negotiate->peer_cert_digest );
7304
7305 ssl->session_negotiate->peer_cert_digest_type =
7306 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7307 ssl->session_negotiate->peer_cert_digest_len =
7308 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7309
7310 return( ret );
7311}
Hanno Becker5882dd02019-06-06 16:25:57 +01007312#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007313
7314static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7315 unsigned char *start, size_t len )
7316{
7317 unsigned char *end = start + len;
7318 int ret;
7319
7320 /* Make a copy of the peer's raw public key. */
7321 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7322 ret = mbedtls_pk_parse_subpubkey( &start, end,
7323 &ssl->handshake->peer_pubkey );
7324 if( ret != 0 )
7325 {
7326 /* We should have parsed the public key before. */
7327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7328 }
7329
7330 return( 0 );
7331}
7332#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7333
Hanno Becker3cf50612019-02-05 14:36:34 +00007334int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7335{
7336 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007337 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007338#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7339 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7340 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007341 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007342#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007343 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007344#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007345 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007346 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007347
7348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7349
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007350 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7351 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007352 {
7353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007354 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007355 }
7356
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007357#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7358 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007359 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007360 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007361 chain = ssl->handshake->ecrs_peer_cert;
7362 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007363 goto crt_verify;
7364 }
7365#endif
7366
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007367 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007368 {
7369 /* mbedtls_ssl_read_record may have sent an alert already. We
7370 let it decide whether to alert. */
7371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007372 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373 }
7374
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007375#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007376 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7377 {
7378 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007379
Hanno Beckerb8a08572019-02-05 12:49:06 +00007380 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007381 ret = 0;
7382 else
7383 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007384
Hanno Becker613d4902019-02-05 13:11:17 +00007385 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007387#endif /* MBEDTLS_SSL_SRV_C */
7388
Hanno Becker35e41772019-02-05 15:37:23 +00007389 /* Clear existing peer CRT structure in case we tried to
7390 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007391 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007392
7393 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7394 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007395 {
7396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7397 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007398 mbedtls_ssl_pend_fatal_alert( ssl,
7399 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007400
Hanno Beckere4aeb762019-02-05 17:19:52 +00007401 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7402 goto exit;
7403 }
7404 mbedtls_x509_crt_init( chain );
7405
7406 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007407 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007408 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007409
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007410#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7411 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007412 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007413
7414crt_verify:
7415 if( ssl->handshake->ecrs_enabled)
7416 rs_ctx = &ssl->handshake->ecrs_ctx;
7417#endif
7418
Hanno Becker3cf50612019-02-05 14:36:34 +00007419 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007420 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007421 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007422 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
Hanno Becker3008d282019-02-05 17:02:28 +00007424#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007425 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007426 size_t pk_len;
7427 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007428
Hanno Becker34106f62019-02-08 14:59:05 +00007429 /* We parse the CRT chain without copying, so
7430 * these pointers point into the input buffer,
7431 * and are hence still valid after freeing the
7432 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Hanno Becker5882dd02019-06-06 16:25:57 +01007434#if defined(MBEDTLS_SSL_RENEGOTIATION)
7435 unsigned char *crt_start;
7436 size_t crt_len;
7437
Hanno Becker34106f62019-02-08 14:59:05 +00007438 crt_start = chain->raw.p;
7439 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007440#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007441
Hanno Becker8c13ee62019-02-26 16:48:17 +00007442 pk_start = chain->cache->pk_raw.p;
7443 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007444
7445 /* Free the CRT structures before computing
7446 * digest and copying the peer's public key. */
7447 mbedtls_x509_crt_free( chain );
7448 mbedtls_free( chain );
7449 chain = NULL;
7450
Hanno Becker5882dd02019-06-06 16:25:57 +01007451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007452 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007453 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007454 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007455#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007456
Hanno Becker34106f62019-02-08 14:59:05 +00007457 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007459 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007460 }
Hanno Becker34106f62019-02-08 14:59:05 +00007461#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7462 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007463 ssl->session_negotiate->peer_cert = chain;
7464 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007465#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007468
Hanno Becker613d4902019-02-05 13:11:17 +00007469exit:
7470
Hanno Beckere4aeb762019-02-05 17:19:52 +00007471 if( ret == 0 )
7472 ssl->state++;
7473
7474#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7475 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7476 {
7477 ssl->handshake->ecrs_peer_cert = chain;
7478 chain = NULL;
7479 }
7480#endif
7481
7482 if( chain != NULL )
7483 {
7484 mbedtls_x509_crt_free( chain );
7485 mbedtls_free( chain );
7486 }
7487
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 return( ret );
7489}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007490#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007493{
7494 int ret;
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 ssl->out_msglen = 1;
7500 ssl->out_msg[0] = 1;
7501
Paul Bakker5121ce52009-01-03 21:22:43 +00007502 ssl->state++;
7503
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007504 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 return( ret );
7508 }
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
7512 return( 0 );
7513}
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007516{
7517 int ret;
7518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520
Hanno Becker327c93b2018-08-15 13:56:18 +01007521 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 return( ret );
7525 }
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007530 mbedtls_ssl_pend_fatal_alert( ssl,
7531 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 }
7534
Hanno Beckere678eaa2018-08-21 14:57:46 +01007535 /* CCS records are only accepted if they have length 1 and content '1',
7536 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007537
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007538 /*
7539 * Switch to our negotiated transform and session parameters for inbound
7540 * data.
7541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007543 ssl->transform_in = ssl->transform_negotiate;
7544 ssl->session_in = ssl->session_negotiate;
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007547 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007550 ssl_dtls_replay_reset( ssl );
7551#endif
7552
7553 /* Increment epoch */
7554 if( ++ssl->in_epoch == 0 )
7555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007557 /* This is highly unlikely to happen for legitimate reasons, so
7558 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007560 }
7561 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007562 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007564#if defined(MBEDTLS_SSL_PROTO_TLS)
7565 {
7566 memset( ssl->in_ctr, 0, 8 );
7567 }
7568#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007569
Hanno Beckerf5970a02019-05-08 09:38:41 +01007570 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7573 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007578 mbedtls_ssl_pend_fatal_alert( ssl,
7579 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 }
7582 }
7583#endif
7584
Paul Bakker5121ce52009-01-03 21:22:43 +00007585 ssl->state++;
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
7589 return( 0 );
7590}
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7595 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007596 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007597 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7600#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007601 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007604 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007607}
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007612
7613 /*
7614 * Free our handshake params
7615 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007616 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007618 ssl->handshake = NULL;
7619
7620 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007622 */
7623 if( ssl->transform )
7624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 mbedtls_ssl_transform_free( ssl->transform );
7626 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007627 }
7628 ssl->transform = ssl->transform_negotiate;
7629 ssl->transform_negotiate = NULL;
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007632}
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_RENEGOTIATION)
7639 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642 ssl->renego_records_seen = 0;
7643 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007644#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645
7646 /*
7647 * Free the previous session and switch in the current one
7648 */
Paul Bakker0a597072012-09-25 21:55:46 +00007649 if( ssl->session )
7650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007652 /* RFC 7366 3.1: keep the EtM state */
7653 ssl->session_negotiate->encrypt_then_mac =
7654 ssl->session->encrypt_then_mac;
7655#endif
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 mbedtls_ssl_session_free( ssl->session );
7658 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007659 }
7660 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007661 ssl->session_negotiate = NULL;
7662
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007663#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007664 /*
7665 * Add cache entry
7666 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007667 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007668 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007669 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007670 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007671 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007673 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007674#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007678 ssl->handshake->flight != NULL )
7679 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007680 /* Cancel handshake timer */
7681 ssl_set_timer( ssl, 0 );
7682
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007683 /* Keep last flight around in case we need to resend it:
7684 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007686 }
7687 else
7688#endif
7689 ssl_handshake_wrapup_free_hs_transform( ssl );
7690
Paul Bakker48916f92012-09-16 19:57:18 +00007691 ssl->state++;
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007694}
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007698 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007702 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007703
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007704 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7705 mbedtls_ssl_suite_get_mac(
7706 mbedtls_ssl_ciphersuite_from_id(
7707 mbedtls_ssl_session_get_ciphersuite(
7708 ssl->session_negotiate ) ) ),
7709 ssl, ssl->out_msg + 4,
7710 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007712 /*
7713 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7714 * may define some other value. Currently (early 2016), no defined
7715 * ciphersuite does this (and this is unlikely to change as activity has
7716 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7717 */
Hanno Becker2881d802019-05-22 14:44:53 +01007718 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007721 ssl->verify_data_len = hash_len;
7722 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007723#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007724
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7727 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007729#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 /*
7731 * In case of session resuming, invert the client and server
7732 * ChangeCipherSpec messages order.
7733 */
Paul Bakker0a597072012-09-25 21:55:46 +00007734 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007737 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7738 MBEDTLS_SSL_IS_CLIENT )
7739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007741 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007744 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7745 MBEDTLS_SSL_IS_SERVER )
7746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007748 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 }
7751 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007752#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 ssl->state++;
7754
Paul Bakker48916f92012-09-16 19:57:18 +00007755 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007756 * Switch to our negotiated transform and session parameters for outbound
7757 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007762 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007763 {
7764 unsigned char i;
7765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007766 /* Remember current epoch settings for resending */
7767 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007768 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007769
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007770 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007771 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007772
7773 /* Increment epoch */
7774 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007775 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007776 break;
7777
7778 /* The loop goes to its end iff the counter is wrapping */
7779 if( i == 0 )
7780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7782 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007783 }
7784 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007785 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007787#if defined(MBEDTLS_SSL_PROTO_TLS)
7788 {
7789 memset( ssl->cur_out_ctr, 0, 8 );
7790 }
7791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007792
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007793 ssl->transform_out = ssl->transform_negotiate;
7794 ssl->session_out = ssl->session_negotiate;
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7797 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7802 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007803 }
7804 }
7805#endif
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007808 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007810#endif
7811
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007812 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007813 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 return( ret );
7816 }
7817
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007819 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007820 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7821 {
7822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7823 return( ret );
7824 }
7825#endif
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007828
7829 return( 0 );
7830}
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007833#define SSL_MAX_HASH_LEN 36
7834#else
7835#define SSL_MAX_HASH_LEN 12
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007839{
Paul Bakker23986e52011-04-24 08:57:21 +00007840 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007841 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007842 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007846 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7847 mbedtls_ssl_suite_get_mac(
7848 mbedtls_ssl_ciphersuite_from_id(
7849 mbedtls_ssl_session_get_ciphersuite(
7850 ssl->session_negotiate ) ) ),
7851 ssl, buf,
7852 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007853
Hanno Becker327c93b2018-08-15 13:56:18 +01007854 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 return( ret );
7858 }
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007863 mbedtls_ssl_pend_fatal_alert( ssl,
7864 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 }
7867
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007868 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007870 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007871 hash_len = 36;
7872 else
7873#endif
7874 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7877 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007880 mbedtls_ssl_pend_fatal_alert( ssl,
7881 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 }
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007886 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007889 mbedtls_ssl_pend_fatal_alert( ssl,
7890 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 }
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007895 ssl->verify_data_len = hash_len;
7896 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007897#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007898
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007899#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007900 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007903 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007907 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007909#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007910 }
7911 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007912#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 ssl->state++;
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007916 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007918#endif
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
7922 return( 0 );
7923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7930 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7931 mbedtls_md5_init( &handshake->fin_md5 );
7932 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007933 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7934 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7937#if defined(MBEDTLS_SHA256_C)
7938 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007939 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SHA512_C)
7942 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007943 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007946
Hanno Becker7e5437a2017-04-28 17:15:26 +01007947#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7948 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7949 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7950#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952#if defined(MBEDTLS_DHM_C)
7953 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_ECDH_C)
7956 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007958#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007959 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007960#if defined(MBEDTLS_SSL_CLI_C)
7961 handshake->ecjpake_cache = NULL;
7962 handshake->ecjpake_cache_len = 0;
7963#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007964#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007965
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007966#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007967 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007968#endif
7969
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007970#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7971 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7972#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007973
7974#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7975 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7976 mbedtls_pk_init( &handshake->peer_pubkey );
7977#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978}
7979
Hanno Becker611a83b2018-01-03 14:27:32 +00007980void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7985 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007986
Hanno Becker92231322018-01-03 15:32:51 +00007987#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 mbedtls_md_init( &transform->md_ctx_enc );
7989 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007990#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991}
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996}
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007999{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008000 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008001 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008003 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008006 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008007
8008 /*
8009 * Either the pointers are now NULL or cleared properly and can be freed.
8010 * Now allocate missing structures.
8011 */
8012 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008013 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008014 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008015 }
Paul Bakker48916f92012-09-16 19:57:18 +00008016
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008017 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008018 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008019 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008020 }
Paul Bakker48916f92012-09-16 19:57:18 +00008021
Paul Bakker82788fb2014-10-20 13:59:19 +02008022 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008023 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008024 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008025 }
Paul Bakker48916f92012-09-16 19:57:18 +00008026
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008028 if( ssl->handshake == NULL ||
8029 ssl->transform_negotiate == NULL ||
8030 ssl->session_negotiate == NULL )
8031 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 mbedtls_free( ssl->handshake );
8035 mbedtls_free( ssl->transform_negotiate );
8036 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008037
8038 ssl->handshake = NULL;
8039 ssl->transform_negotiate = NULL;
8040 ssl->session_negotiate = NULL;
8041
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008042 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008043 }
8044
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008045 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008047 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008048 ssl_handshake_params_init( ssl->handshake );
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008051 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008052 {
8053 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008054
Hanno Becker2d9623f2019-06-13 12:07:05 +01008055 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008056 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8057 else
8058 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8059 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008060#endif
8061
Paul Bakker48916f92012-09-16 19:57:18 +00008062 return( 0 );
8063}
8064
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008065#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008066/* Dummy cookie callbacks for defaults */
8067static int ssl_cookie_write_dummy( void *ctx,
8068 unsigned char **p, unsigned char *end,
8069 const unsigned char *cli_id, size_t cli_id_len )
8070{
8071 ((void) ctx);
8072 ((void) p);
8073 ((void) end);
8074 ((void) cli_id);
8075 ((void) cli_id_len);
8076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008078}
8079
8080static int ssl_cookie_check_dummy( void *ctx,
8081 const unsigned char *cookie, size_t cookie_len,
8082 const unsigned char *cli_id, size_t cli_id_len )
8083{
8084 ((void) ctx);
8085 ((void) cookie);
8086 ((void) cookie_len);
8087 ((void) cli_id);
8088 ((void) cli_id_len);
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008091}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008092#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008093
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008094/* Once ssl->out_hdr as the address of the beginning of the
8095 * next outgoing record is set, deduce the other pointers.
8096 *
8097 * Note: For TLS, we save the implicit record sequence number
8098 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8099 * and the caller has to make sure there's space for this.
8100 */
8101
8102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8103 mbedtls_ssl_transform *transform )
8104{
8105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008106 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008107 {
8108 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008110 ssl->out_cid = ssl->out_ctr + 8;
8111 ssl->out_len = ssl->out_cid;
8112 if( transform != NULL )
8113 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008114#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008115 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008116#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008117 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008118 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008119 MBEDTLS_SSL_TRANSPORT_ELSE
8120#endif /* MBEDTLS_SSL_PROTO_DTLS */
8121#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008122 {
8123 ssl->out_ctr = ssl->out_hdr - 8;
8124 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008125#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008126 ssl->out_cid = ssl->out_len;
8127#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128 ssl->out_iv = ssl->out_hdr + 5;
8129 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008130#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008131
8132 /* Adjust out_msg to make space for explicit IV, if used. */
8133 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008134 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008135 {
8136 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8137 }
8138 else
8139 ssl->out_msg = ssl->out_iv;
8140}
8141
8142/* Once ssl->in_hdr as the address of the beginning of the
8143 * next incoming record is set, deduce the other pointers.
8144 *
8145 * Note: For TLS, we save the implicit record sequence number
8146 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8147 * and the caller has to make sure there's space for this.
8148 */
8149
Hanno Beckerf5970a02019-05-08 09:38:41 +01008150static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008151{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008152 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008153 * of unprotected TLS/DTLS records, with ssl->in_msg
8154 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008155 *
8156 * When decrypting a protected record, ssl->in_msg
8157 * will be shifted to point to the beginning of the
8158 * record plaintext.
8159 */
8160
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163 {
Hanno Becker70e79282019-05-03 14:34:53 +01008164 /* This sets the header pointers to match records
8165 * without CID. When we receive a record containing
8166 * a CID, the fields are shifted accordingly in
8167 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008169#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008170 ssl->in_cid = ssl->in_ctr + 8;
8171 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008172#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008173 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008175 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008176 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008177 MBEDTLS_SSL_TRANSPORT_ELSE
8178#endif /* MBEDTLS_SSL_PROTO_DTLS */
8179#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008180 {
8181 ssl->in_ctr = ssl->in_hdr - 8;
8182 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008184 ssl->in_cid = ssl->in_len;
8185#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008186 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008187 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008188#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008189}
8190
Paul Bakker5121ce52009-01-03 21:22:43 +00008191/*
8192 * Initialize an SSL context
8193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8195{
8196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8197}
8198
8199/*
8200 * Setup an SSL context
8201 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008202
8203static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8204{
8205 /* Set the incoming and outgoing record pointers. */
8206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008207 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008208 {
8209 ssl->out_hdr = ssl->out_buf;
8210 ssl->in_hdr = ssl->in_buf;
8211 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008214#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215 {
8216 ssl->out_hdr = ssl->out_buf + 8;
8217 ssl->in_hdr = ssl->in_buf + 8;
8218 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008220
8221 /* Derive other internal pointers. */
8222 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008223 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008224}
8225
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008226int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008227 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008228{
Paul Bakker48916f92012-09-16 19:57:18 +00008229 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008230
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008231 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008232
Hanno Beckeref982d52019-07-23 15:56:18 +01008233#if defined(MBEDTLS_USE_TINYCRYPT)
8234 uECC_set_rng( &uecc_rng_wrapper );
8235#endif
8236
Paul Bakker62f2dee2012-09-28 07:31:51 +00008237 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008238 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008239 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008240
8241 /* Set to NULL in case of an error condition */
8242 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008243
Angus Grattond8213d02016-05-25 20:56:48 +10008244 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8245 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008246 {
Angus Grattond8213d02016-05-25 20:56:48 +10008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008248 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008249 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008250 }
8251
8252 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8253 if( ssl->out_buf == NULL )
8254 {
8255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008256 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008257 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008258 }
8259
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008260 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008261
Paul Bakker48916f92012-09-16 19:57:18 +00008262 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008263 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008264
Hanno Beckerc8f52992019-07-25 11:15:08 +01008265 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008266
Paul Bakker5121ce52009-01-03 21:22:43 +00008267 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008268
8269error:
8270 mbedtls_free( ssl->in_buf );
8271 mbedtls_free( ssl->out_buf );
8272
8273 ssl->conf = NULL;
8274
8275 ssl->in_buf = NULL;
8276 ssl->out_buf = NULL;
8277
8278 ssl->in_hdr = NULL;
8279 ssl->in_ctr = NULL;
8280 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008281 ssl->in_msg = NULL;
8282
8283 ssl->out_hdr = NULL;
8284 ssl->out_ctr = NULL;
8285 ssl->out_len = NULL;
8286 ssl->out_iv = NULL;
8287 ssl->out_msg = NULL;
8288
k-stachowiak9f7798e2018-07-31 16:52:32 +02008289 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008290}
8291
8292/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008293 * Reset an initialized and used SSL context for re-use while retaining
8294 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008295 *
8296 * If partial is non-zero, keep data in the input buffer and client ID.
8297 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008299static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008300{
Paul Bakker48916f92012-09-16 19:57:18 +00008301 int ret;
8302
Hanno Becker7e772132018-08-10 12:38:21 +01008303#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8304 !defined(MBEDTLS_SSL_SRV_C)
8305 ((void) partial);
8306#endif
8307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008309
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008310 /* Cancel any possibly running timer */
8311 ssl_set_timer( ssl, 0 );
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#if defined(MBEDTLS_SSL_RENEGOTIATION)
8314 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008315 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008316
8317 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8319 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008322
Paul Bakker7eb013f2011-10-06 12:37:39 +00008323 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008324 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325
8326 ssl->in_msgtype = 0;
8327 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008329 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008330 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008333 ssl_dtls_replay_reset( ssl );
8334#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008335
8336 ssl->in_hslen = 0;
8337 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008338
8339 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008340
8341 ssl->out_msgtype = 0;
8342 ssl->out_msglen = 0;
8343 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8345 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008346 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008347#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008348
Hanno Becker19859472018-08-06 09:40:20 +01008349 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8350
Paul Bakker48916f92012-09-16 19:57:18 +00008351 ssl->transform_in = NULL;
8352 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008353
Hanno Becker78640902018-08-13 16:35:15 +01008354 ssl->session_in = NULL;
8355 ssl->session_out = NULL;
8356
Angus Grattond8213d02016-05-25 20:56:48 +10008357 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008358
8359#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008360 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008361#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8362 {
8363 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008364 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008365 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8368 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8371 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8374 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008375 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008376 }
8377#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008378
Paul Bakker48916f92012-09-16 19:57:18 +00008379 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 mbedtls_ssl_transform_free( ssl->transform );
8382 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008383 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008384 }
Paul Bakker48916f92012-09-16 19:57:18 +00008385
Paul Bakkerc0463502013-02-14 11:19:38 +01008386 if( ssl->session )
8387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 mbedtls_ssl_session_free( ssl->session );
8389 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008390 ssl->session = NULL;
8391 }
8392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008394 ssl->alpn_chosen = NULL;
8395#endif
8396
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008397#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008398#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008399 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008400#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008401 {
8402 mbedtls_free( ssl->cli_id );
8403 ssl->cli_id = NULL;
8404 ssl->cli_id_len = 0;
8405 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008406#endif
8407
Paul Bakker48916f92012-09-16 19:57:18 +00008408 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8409 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008410
8411 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008412}
8413
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008414/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008415 * Reset an initialized and used SSL context for re-use while retaining
8416 * all application-set variables, function pointers and data.
8417 */
8418int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8419{
8420 return( ssl_session_reset_int( ssl, 0 ) );
8421}
8422
8423/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 * SSL set accessors
8425 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008426#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008427void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008429 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008430}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008431#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008432
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008433void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008435 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008436}
8437
Hanno Becker7f376f42019-06-12 16:20:48 +01008438#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8439 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008440void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008441{
Hanno Becker7f376f42019-06-12 16:20:48 +01008442 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008443}
Hanno Becker7f376f42019-06-12 16:20:48 +01008444#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008445
Hanno Beckerde671542019-06-12 16:30:46 +01008446#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8447 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8448void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8449 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008451 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008452}
Hanno Beckerde671542019-06-12 16:30:46 +01008453#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008456
Hanno Becker1841b0a2018-08-24 11:13:57 +01008457void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8458 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008459{
8460 ssl->disable_datagram_packing = !allow_packing;
8461}
8462
Hanno Becker1f835fa2019-06-13 10:14:59 +01008463#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8464 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008465void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8466 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->hs_timeout_min = min;
8469 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008470}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008471#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8472 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8473void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8474 uint32_t min, uint32_t max )
8475{
8476 ((void) conf);
8477 ((void) min);
8478 ((void) max);
8479}
8480#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8481 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8482
8483#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008484
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008485void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008486{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008487#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8488 conf->authmode = authmode;
8489#else
8490 ((void) conf);
8491 ((void) authmode);
8492#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008493}
8494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008496void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008497 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008498 void *p_vrfy )
8499{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008500 conf->f_vrfy = f_vrfy;
8501 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008504
Hanno Beckerece325c2019-06-13 15:39:27 +01008505#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008507 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008508 void *p_rng )
8509{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008510 conf->f_rng = f_rng;
8511 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008512}
Hanno Beckerece325c2019-06-13 15:39:27 +01008513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008514
Hanno Becker14a4a442019-07-02 17:00:34 +01008515#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008517 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008518 void *p_dbg )
8519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008520 conf->f_dbg = f_dbg;
8521 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008522}
Hanno Becker14a4a442019-07-02 17:00:34 +01008523#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
Hanno Beckera58a8962019-06-13 16:11:15 +01008525#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8526 !defined(MBEDTLS_SSL_CONF_SEND) && \
8527 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008529 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008530 mbedtls_ssl_send_t *f_send,
8531 mbedtls_ssl_recv_t *f_recv,
8532 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008533{
Hanno Beckera58a8962019-06-13 16:11:15 +01008534 ssl->p_bio = p_bio;
8535 ssl->f_send = f_send;
8536 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008537 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008538}
Hanno Beckera58a8962019-06-13 16:11:15 +01008539#else
8540void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8541 void *p_bio )
8542{
8543 ssl->p_bio = p_bio;
8544}
8545#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008546
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008547#if defined(MBEDTLS_SSL_PROTO_DTLS)
8548void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8549{
8550 ssl->mtu = mtu;
8551}
8552#endif
8553
Hanno Becker1f835fa2019-06-13 10:14:59 +01008554#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008556{
8557 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008558}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008559#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008560
Hanno Becker0ae6b242019-06-13 16:45:36 +01008561#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8562 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008563void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8564 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008565 mbedtls_ssl_set_timer_t *f_set_timer,
8566 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008567{
8568 ssl->p_timer = p_timer;
8569 ssl->f_set_timer = f_set_timer;
8570 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008571 /* Make sure we start with no timer running */
8572 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008573}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008574#else
8575void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8576 void *p_timer )
8577{
8578 ssl->p_timer = p_timer;
8579 /* Make sure we start with no timer running */
8580 ssl_set_timer( ssl, 0 );
8581}
8582#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008583
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008584#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008585void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008586 void *p_cache,
8587 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8588 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008589{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008590 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008592 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008593}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008594#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008595
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008596#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008598{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008599 int ret;
8600
8601 if( ssl == NULL ||
8602 session == NULL ||
8603 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008604 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008607 }
8608
Hanno Becker58fccf22019-02-06 14:30:46 +00008609 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8610 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008611 return( ret );
8612
Paul Bakker0a597072012-09-25 21:55:46 +00008613 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008614
8615 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008616}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008617#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008618
Hanno Becker73f4cb12019-06-27 13:51:07 +01008619#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008621 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8624 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8625 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8626 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008627}
8628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008630 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008631 int major, int minor )
8632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008634 return;
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008637 return;
8638
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008639 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008640}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008641#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008644void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008645 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008646{
8647 conf->cert_profile = profile;
8648}
8649
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008650/* Append a new keycert entry to a (possibly empty) list */
8651static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8652 mbedtls_x509_crt *cert,
8653 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008654{
niisato8ee24222018-06-25 19:05:48 +09008655 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008656
niisato8ee24222018-06-25 19:05:48 +09008657 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8658 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008659 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008660
niisato8ee24222018-06-25 19:05:48 +09008661 new_cert->cert = cert;
8662 new_cert->key = key;
8663 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008664
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008665 /* Update head is the list was null, else add to the end */
8666 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008667 {
niisato8ee24222018-06-25 19:05:48 +09008668 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008669 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008670 else
8671 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008672 mbedtls_ssl_key_cert *cur = *head;
8673 while( cur->next != NULL )
8674 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008675 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008676 }
8677
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008678 return( 0 );
8679}
8680
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008681int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008682 mbedtls_x509_crt *own_cert,
8683 mbedtls_pk_context *pk_key )
8684{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008685 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008686}
8687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008688void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008689 mbedtls_x509_crt *ca_chain,
8690 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008691{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008692 conf->ca_chain = ca_chain;
8693 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008696
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008697#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8698int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8699 mbedtls_x509_crt *own_cert,
8700 mbedtls_pk_context *pk_key )
8701{
8702 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8703 own_cert, pk_key ) );
8704}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008705
8706void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8707 mbedtls_x509_crt *ca_chain,
8708 mbedtls_x509_crl *ca_crl )
8709{
8710 ssl->handshake->sni_ca_chain = ca_chain;
8711 ssl->handshake->sni_ca_crl = ca_crl;
8712}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008713
8714void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8715 int authmode )
8716{
8717 ssl->handshake->sni_authmode = authmode;
8718}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008719#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8720
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008721#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008722/*
8723 * Set EC J-PAKE password for current handshake
8724 */
8725int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8726 const unsigned char *pw,
8727 size_t pw_len )
8728{
8729 mbedtls_ecjpake_role role;
8730
Janos Follath8eb64132016-06-03 15:40:57 +01008731 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8733
Hanno Becker2d9623f2019-06-13 12:07:05 +01008734 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008735 role = MBEDTLS_ECJPAKE_SERVER;
8736 else
8737 role = MBEDTLS_ECJPAKE_CLIENT;
8738
8739 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8740 role,
8741 MBEDTLS_MD_SHA256,
8742 MBEDTLS_ECP_DP_SECP256R1,
8743 pw, pw_len ) );
8744}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008745#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008748int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008749 const unsigned char *psk, size_t psk_len,
8750 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008751{
Paul Bakker6db455e2013-09-18 17:29:31 +02008752 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008757
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008758 /* Identity len will be encoded on two bytes */
8759 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008760 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008761 {
8762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8763 }
8764
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008765 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008766 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008767 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008768
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008769 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008770 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008771 conf->psk_len = 0;
8772 }
8773 if( conf->psk_identity != NULL )
8774 {
8775 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008776 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008777 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008778 }
8779
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008780 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8781 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008782 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008783 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008784 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008785 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008786 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008787 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008788 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008789
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008790 conf->psk_len = psk_len;
8791 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008792
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008793 memcpy( conf->psk, psk, conf->psk_len );
8794 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008795
8796 return( 0 );
8797}
8798
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008799int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8800 const unsigned char *psk, size_t psk_len )
8801{
8802 if( psk == NULL || ssl->handshake == NULL )
8803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8804
8805 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8807
8808 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008809 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008810 mbedtls_platform_zeroize( ssl->handshake->psk,
8811 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008812 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008813 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008814 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008815
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008816 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008818
8819 ssl->handshake->psk_len = psk_len;
8820 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8821
8822 return( 0 );
8823}
8824
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008825void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008827 size_t),
8828 void *p_psk )
8829{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008830 conf->f_psk = f_psk;
8831 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008834
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008836
8837#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008838int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008839{
8840 int ret;
8841
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008842 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8843 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8844 {
8845 mbedtls_mpi_free( &conf->dhm_P );
8846 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008847 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008849
8850 return( 0 );
8851}
Hanno Becker470a8c42017-10-04 15:28:46 +01008852#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008853
Hanno Beckera90658f2017-10-04 15:29:08 +01008854int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8855 const unsigned char *dhm_P, size_t P_len,
8856 const unsigned char *dhm_G, size_t G_len )
8857{
8858 int ret;
8859
8860 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8861 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8862 {
8863 mbedtls_mpi_free( &conf->dhm_P );
8864 mbedtls_mpi_free( &conf->dhm_G );
8865 return( ret );
8866 }
8867
8868 return( 0 );
8869}
Paul Bakker5121ce52009-01-03 21:22:43 +00008870
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008871int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008872{
8873 int ret;
8874
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008875 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8876 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8877 {
8878 mbedtls_mpi_free( &conf->dhm_P );
8879 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008880 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008881 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008882
8883 return( 0 );
8884}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008885#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008886
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008887#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8888/*
8889 * Set the minimum length for Diffie-Hellman parameters
8890 */
8891void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8892 unsigned int bitlen )
8893{
8894 conf->dhm_min_bitlen = bitlen;
8895}
8896#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8897
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008898#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008899/*
8900 * Set allowed/preferred hashes for handshake signatures
8901 */
8902void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8903 const int *hashes )
8904{
Hanno Becker56595f42019-06-19 16:31:38 +01008905#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008906 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008907#else
8908 ((void) conf);
8909 ((void) hashes);
8910#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008911}
Hanno Becker947194e2017-04-07 13:25:49 +01008912#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008913
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008914#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008915#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008916/*
8917 * Set the allowed elliptic curves
8918 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008919void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008920 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008922 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008923}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008924#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008925#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008926
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008927#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008929{
Hanno Becker947194e2017-04-07 13:25:49 +01008930 /* Initialize to suppress unnecessary compiler warning */
8931 size_t hostname_len = 0;
8932
8933 /* Check if new hostname is valid before
8934 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008935 if( hostname != NULL )
8936 {
8937 hostname_len = strlen( hostname );
8938
8939 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8941 }
8942
8943 /* Now it's clear that we will overwrite the old hostname,
8944 * so we can free it safely */
8945
8946 if( ssl->hostname != NULL )
8947 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008948 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008949 mbedtls_free( ssl->hostname );
8950 }
8951
8952 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008953
Paul Bakker5121ce52009-01-03 21:22:43 +00008954 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008955 {
8956 ssl->hostname = NULL;
8957 }
8958 else
8959 {
8960 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008961 if( ssl->hostname == NULL )
8962 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008963
Hanno Becker947194e2017-04-07 13:25:49 +01008964 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008965
Hanno Becker947194e2017-04-07 13:25:49 +01008966 ssl->hostname[hostname_len] = '\0';
8967 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008968
8969 return( 0 );
8970}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008971#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008972
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008973#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008976 const unsigned char *, size_t),
8977 void *p_sni )
8978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008979 conf->f_sni = f_sni;
8980 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008985int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008986{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008987 size_t cur_len, tot_len;
8988 const char **p;
8989
8990 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008991 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8992 * MUST NOT be truncated."
8993 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008994 */
8995 tot_len = 0;
8996 for( p = protos; *p != NULL; p++ )
8997 {
8998 cur_len = strlen( *p );
8999 tot_len += cur_len;
9000
9001 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009003 }
9004
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009005 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009006
9007 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009008}
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009011{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009012 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009015
Hanno Becker33b9b252019-07-05 11:23:25 +01009016#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9017 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9018void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9019 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009020{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009021 conf->max_major_ver = major;
9022 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009023}
Hanno Becker33b9b252019-07-05 11:23:25 +01009024#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9025 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009026
Hanno Becker33b9b252019-07-05 11:23:25 +01009027#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9028 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9029void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9030 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->min_major_ver = major;
9033 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009034}
Hanno Becker33b9b252019-07-05 11:23:25 +01009035#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9036 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009039void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009040{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009041 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009042}
9043#endif
9044
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009045#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009046void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9047 char cert_req_ca_list )
9048{
9049 conf->cert_req_ca_list = cert_req_ca_list;
9050}
9051#endif
9052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009054void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009057}
9058#endif
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009061#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009062void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009064 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009065}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009066#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9067#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009068void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009069 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009070{
9071 conf->enforce_extended_master_secret = ems_enf;
9072}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009073#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009074#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009075
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009076#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009080}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009084int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009087 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090 }
9091
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009092 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093
9094 return( 0 );
9095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009099void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009101 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009107{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009108 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009109}
9110#endif
9111
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009112#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009113void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009115 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009116}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009117#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009120void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009122 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009123}
9124
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009128}
9129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009131 const unsigned char period[8] )
9132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009137#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009138#if defined(MBEDTLS_SSL_CLI_C)
9139void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009140{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009141 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009142}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009144
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009145#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009146void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9147 mbedtls_ssl_ticket_write_t *f_ticket_write,
9148 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9149 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009150{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009151 conf->f_ticket_write = f_ticket_write;
9152 conf->f_ticket_parse = f_ticket_parse;
9153 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009154}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009157
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009158#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9159void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9160 mbedtls_ssl_export_keys_t *f_export_keys,
9161 void *p_export_keys )
9162{
9163 conf->f_export_keys = f_export_keys;
9164 conf->p_export_keys = p_export_keys;
9165}
9166#endif
9167
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009168#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009169void mbedtls_ssl_conf_async_private_cb(
9170 mbedtls_ssl_config *conf,
9171 mbedtls_ssl_async_sign_t *f_async_sign,
9172 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9173 mbedtls_ssl_async_resume_t *f_async_resume,
9174 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009175 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009176{
9177 conf->f_async_sign_start = f_async_sign;
9178 conf->f_async_decrypt_start = f_async_decrypt;
9179 conf->f_async_resume = f_async_resume;
9180 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009181 conf->p_async_config_data = async_config_data;
9182}
9183
Gilles Peskine8f97af72018-04-26 11:46:10 +02009184void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9185{
9186 return( conf->p_async_config_data );
9187}
9188
Gilles Peskine1febfef2018-04-30 11:54:39 +02009189void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190{
9191 if( ssl->handshake == NULL )
9192 return( NULL );
9193 else
9194 return( ssl->handshake->user_async_ctx );
9195}
9196
Gilles Peskine1febfef2018-04-30 11:54:39 +02009197void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009198 void *ctx )
9199{
9200 if( ssl->handshake != NULL )
9201 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009202}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009203#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204
Paul Bakker5121ce52009-01-03 21:22:43 +00009205/*
9206 * SSL get accessors
9207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009209{
9210 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9211}
9212
Hanno Becker8b170a02017-10-10 11:51:19 +01009213int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9214{
9215 /*
9216 * Case A: We're currently holding back
9217 * a message for further processing.
9218 */
9219
9220 if( ssl->keep_current_message == 1 )
9221 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009223 return( 1 );
9224 }
9225
9226 /*
9227 * Case B: Further records are pending in the current datagram.
9228 */
9229
9230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009232 ssl->in_left > ssl->next_record_offset )
9233 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009235 return( 1 );
9236 }
9237#endif /* MBEDTLS_SSL_PROTO_DTLS */
9238
9239 /*
9240 * Case C: A handshake message is being processed.
9241 */
9242
Hanno Becker8b170a02017-10-10 11:51:19 +01009243 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9244 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 return( 1 );
9247 }
9248
9249 /*
9250 * Case D: An application data message is being processed
9251 */
9252 if( ssl->in_offt != NULL )
9253 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009255 return( 1 );
9256 }
9257
9258 /*
9259 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009260 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 * we implement support for multiple alerts in single records.
9262 */
9263
9264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9265 return( 0 );
9266}
9267
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009268uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009269{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009270 if( ssl->session != NULL )
9271 return( ssl->session->verify_result );
9272
9273 if( ssl->session_negotiate != NULL )
9274 return( ssl->session_negotiate->verify_result );
9275
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009276 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009277}
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009280{
Hanno Beckere02758c2019-06-26 15:31:31 +01009281 int suite;
9282
Paul Bakker926c8e42013-03-06 10:23:34 +01009283 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009284 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009285
Hanno Beckere02758c2019-06-26 15:31:31 +01009286 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9287 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009294 {
Hanno Becker2881d802019-05-22 14:44:53 +01009295 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009298 return( "DTLSv1.0" );
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009301 return( "DTLSv1.2" );
9302
9303 default:
9304 return( "unknown (DTLS)" );
9305 }
9306 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009307 MBEDTLS_SSL_TRANSPORT_ELSE
9308#endif /* MBEDTLS_SSL_PROTO_DTLS */
9309#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009310 {
Hanno Becker2881d802019-05-22 14:44:53 +01009311 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009312 {
9313 case MBEDTLS_SSL_MINOR_VERSION_0:
9314 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009316 case MBEDTLS_SSL_MINOR_VERSION_1:
9317 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009319 case MBEDTLS_SSL_MINOR_VERSION_2:
9320 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009322 case MBEDTLS_SSL_MINOR_VERSION_3:
9323 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009324
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009325 default:
9326 return( "unknown" );
9327 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009329#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330}
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009333{
Hanno Becker3136ede2018-08-17 15:28:19 +01009334 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009336
Hanno Becker43395762019-05-03 14:46:38 +01009337 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9338
Hanno Becker78640902018-08-13 16:35:15 +01009339 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009340 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_ZLIB_SUPPORT)
9343 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9344 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345#endif
9346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009348 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009349#if defined(MBEDTLS_GCM_C) || \
9350 defined(MBEDTLS_CCM_C) || \
9351 defined(MBEDTLS_CHACHAPOLY_C)
9352#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009354#endif
9355#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009357#endif
9358#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009359 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009360#endif
9361 transform_expansion =
9362 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009363 break;
9364
Hanno Beckera9d5c452019-07-25 16:47:12 +01009365#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9366 MBEDTLS_CHACHAPOLY_C */
9367
9368#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9369 case MBEDTLS_MODE_STREAM:
9370 transform_expansion = transform->maclen;
9371 break;
9372#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9373
9374#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009376 {
9377 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009378
9379 block_size = mbedtls_cipher_get_block_size(
9380 &transform->cipher_ctx_enc );
9381
Hanno Becker3136ede2018-08-17 15:28:19 +01009382 /* Expansion due to the addition of the MAC. */
9383 transform_expansion += transform->maclen;
9384
9385 /* Expansion due to the addition of CBC padding;
9386 * Theoretically up to 256 bytes, but we never use
9387 * more than the block size of the underlying cipher. */
9388 transform_expansion += block_size;
9389
9390 /* For TLS 1.1 or higher, an explicit IV is added
9391 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009392#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009393 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009394 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009395#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009396
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009397 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009398 }
9399#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009400
9401 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009404 }
9405
Hanno Beckera5a2b082019-05-15 14:03:01 +01009406#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009407 if( transform->out_cid_len != 0 )
9408 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009410
Hanno Becker43395762019-05-03 14:46:38 +01009411 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009412}
9413
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9415size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9416{
9417 size_t max_len;
9418
9419 /*
9420 * Assume mfl_code is correct since it was checked when set
9421 */
Angus Grattond8213d02016-05-25 20:56:48 +10009422 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009423
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009424 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009425 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009426 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009427 {
Angus Grattond8213d02016-05-25 20:56:48 +10009428 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009429 }
9430
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009431 /* During a handshake, use the value being negotiated */
9432 if( ssl->session_negotiate != NULL &&
9433 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9434 {
9435 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9436 }
9437
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009438 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009439}
9440#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9441
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009442#if defined(MBEDTLS_SSL_PROTO_DTLS)
9443static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9444{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009445 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009446 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009447 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9448 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9449 return ( 0 );
9450
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009451 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9452 return( ssl->mtu );
9453
9454 if( ssl->mtu == 0 )
9455 return( ssl->handshake->mtu );
9456
9457 return( ssl->mtu < ssl->handshake->mtu ?
9458 ssl->mtu : ssl->handshake->mtu );
9459}
9460#endif /* MBEDTLS_SSL_PROTO_DTLS */
9461
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009462int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9463{
9464 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9465
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009466#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9467 !defined(MBEDTLS_SSL_PROTO_DTLS)
9468 (void) ssl;
9469#endif
9470
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009471#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9472 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9473
9474 if( max_len > mfl )
9475 max_len = mfl;
9476#endif
9477
9478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009479 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009480 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009481 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9483 const size_t overhead = (size_t) ret;
9484
9485 if( ret < 0 )
9486 return( ret );
9487
9488 if( mtu <= overhead )
9489 {
9490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9491 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9492 }
9493
9494 if( max_len > mtu - overhead )
9495 max_len = mtu - overhead;
9496 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009497#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009498
Hanno Becker0defedb2018-08-10 12:35:02 +01009499#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9500 !defined(MBEDTLS_SSL_PROTO_DTLS)
9501 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009502#endif
9503
9504 return( (int) max_len );
9505}
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_X509_CRT_PARSE_C)
9508const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009509{
9510 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009511 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009512
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009513#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009514 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009515#else
9516 return( NULL );
9517#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009522int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9523 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009524{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009525 if( ssl == NULL ||
9526 dst == NULL ||
9527 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009528 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009531 }
9532
Hanno Becker58fccf22019-02-06 14:30:46 +00009533 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009537const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9538{
9539 if( ssl == NULL )
9540 return( NULL );
9541
9542 return( ssl->session );
9543}
9544
Paul Bakker5121ce52009-01-03 21:22:43 +00009545/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009546 * Define ticket header determining Mbed TLS version
9547 * and structure of the ticket.
9548 */
9549
Hanno Becker41527622019-05-16 12:50:45 +01009550/*
Hanno Becker26829e92019-05-28 14:30:45 +01009551 * Define bitflag determining compile-time settings influencing
9552 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009553 */
9554
Hanno Becker26829e92019-05-28 14:30:45 +01009555#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009556#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009557#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009558#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009559#endif /* MBEDTLS_HAVE_TIME */
9560
9561#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009562#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009563#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009564#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009565#endif /* MBEDTLS_X509_CRT_PARSE_C */
9566
9567#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009568#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009569#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009570#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009571#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9572
9573#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009574#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009575#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009576#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009577#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9578
9579#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009580#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009581#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009582#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009583#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9584
9585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009586#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009587#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009588#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009589#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9590
Hanno Becker41527622019-05-16 12:50:45 +01009591#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9592#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9593#else
9594#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9595#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9596
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009597#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9598#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9599#else
9600#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9601#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9602
Hanno Becker88440552019-07-03 14:16:13 +01009603#if defined(MBEDTLS_ZLIB_SUPPORT)
9604#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9605#else
9606#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9607#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9608
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009609#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9610#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9611#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9612#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9613#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9614#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9615#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009616#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009617#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009618
Hanno Becker26829e92019-05-28 14:30:45 +01009619#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009620 ( (uint16_t) ( \
9621 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9622 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9623 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9624 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9625 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9626 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009627 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009628 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009629 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009630
Hanno Becker557fe9f2019-05-16 12:41:07 +01009631static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009632 MBEDTLS_VERSION_MAJOR,
9633 MBEDTLS_VERSION_MINOR,
9634 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009635 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9636 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009637};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009638
9639/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009640 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009641 * (in the presentation language of TLS, RFC 8446 section 3)
9642 *
Hanno Becker26829e92019-05-28 14:30:45 +01009643 * opaque mbedtls_version[3]; // major, minor, patch
9644 * opaque session_format[2]; // version-specific 16-bit field determining
9645 * // the format of the remaining
9646 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009647 *
9648 * Note: When updating the format, remember to keep
9649 * these version+format bytes.
9650 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009651 * // In this version, `session_format` determines
9652 * // the setting of those compile-time
9653 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009654 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009655 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009656 * uint8 ciphersuite[2]; // defined by the standard
9657 * uint8 compression; // 0 or 1
9658 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009659 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009660 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009661 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009662 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9663 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9664 * case disabled: uint8_t peer_cert_digest_type;
9665 * opaque peer_cert_digest<0..2^8-1>;
9666 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009667 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009668 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009669 * uint8 mfl_code; // up to 255 according to standard
9670 * uint8 trunc_hmac; // 0 or 1
9671 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009672 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009673 * The order is the same as in the definition of the structure, except
9674 * verify_result is put before peer_cert so that all mandatory fields come
9675 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009676 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009677static int ssl_session_save( const mbedtls_ssl_session *session,
9678 unsigned char omit_header,
9679 unsigned char *buf,
9680 size_t buf_len,
9681 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009682{
9683 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009684 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009685#if defined(MBEDTLS_HAVE_TIME)
9686 uint64_t start;
9687#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009688#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009689#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009690 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009691#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009692#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009693
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009694 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009695 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009696 /*
9697 * Add version identifier
9698 */
9699
9700 used += sizeof( ssl_serialized_session_header );
9701
9702 if( used <= buf_len )
9703 {
9704 memcpy( p, ssl_serialized_session_header,
9705 sizeof( ssl_serialized_session_header ) );
9706 p += sizeof( ssl_serialized_session_header );
9707 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009708 }
9709
9710 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009711 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009712 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009713#if defined(MBEDTLS_HAVE_TIME)
9714 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009715
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009716 if( used <= buf_len )
9717 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009718 start = (uint64_t) session->start;
9719
9720 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9721 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9722 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9723 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9724 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9725 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9727 *p++ = (unsigned char)( ( start ) & 0xFF );
9728 }
9729#endif /* MBEDTLS_HAVE_TIME */
9730
9731 /*
9732 * Basic mandatory fields
9733 */
Hanno Becker88440552019-07-03 14:16:13 +01009734 {
9735 size_t const ciphersuite_len = 2;
9736#if defined(MBEDTLS_ZLIB_SUPPORT)
9737 size_t const compression_len = 1;
9738#else
9739 size_t const compression_len = 0;
9740#endif
9741 size_t const id_len_len = 1;
9742 size_t const id_len = 32;
9743 size_t const master_len = 48;
9744 size_t const verif_result_len = 4;
9745
9746 size_t const basic_len =
9747 ciphersuite_len +
9748 compression_len +
9749 id_len_len +
9750 id_len +
9751 master_len +
9752 verif_result_len;
9753
9754 used += basic_len;
9755 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009756
9757 if( used <= buf_len )
9758 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009759 const int ciphersuite =
9760 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009761 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009762
Hanno Becker88440552019-07-03 14:16:13 +01009763#if defined(MBEDTLS_ZLIB_SUPPORT)
9764 *p++ = (unsigned char)(
9765 mbedtls_ssl_session_get_compression( session ) );
9766#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009767
9768 *p++ = (unsigned char)( session->id_len & 0xFF );
9769 memcpy( p, session->id, 32 );
9770 p += 32;
9771
9772 memcpy( p, session->master, 48 );
9773 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009774 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009775 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009776
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009777 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009778 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009779 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009780#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009781#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009782 if( session->peer_cert == NULL )
9783 cert_len = 0;
9784 else
9785 cert_len = session->peer_cert->raw.len;
9786
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009787 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009788
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009789 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009790 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009791 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009792
9793 if( session->peer_cert != NULL )
9794 {
9795 memcpy( p, session->peer_cert->raw.p, cert_len );
9796 p += cert_len;
9797 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009798 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009799
Hanno Becker5882dd02019-06-06 16:25:57 +01009800#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009801 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009802 if( session->peer_cert_digest != NULL )
9803 {
9804 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9805 if( used <= buf_len )
9806 {
9807 *p++ = (unsigned char) session->peer_cert_digest_type;
9808 *p++ = (unsigned char) session->peer_cert_digest_len;
9809 memcpy( p, session->peer_cert_digest,
9810 session->peer_cert_digest_len );
9811 p += session->peer_cert_digest_len;
9812 }
9813 }
9814 else
9815 {
9816 used += 2;
9817 if( used <= buf_len )
9818 {
9819 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9820 *p++ = 0;
9821 }
9822 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009823#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009824#endif /* MBEDTLS_X509_CRT_PARSE_C */
9825
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009826 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009827 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009828 */
9829#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009830 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009831
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009832 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009833 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009834 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009835
9836 if( session->ticket != NULL )
9837 {
9838 memcpy( p, session->ticket, session->ticket_len );
9839 p += session->ticket_len;
9840 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009841
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009842 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009843 }
9844#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9845
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009846 /*
9847 * Misc extension-related info
9848 */
9849#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9850 used += 1;
9851
9852 if( used <= buf_len )
9853 *p++ = session->mfl_code;
9854#endif
9855
9856#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9857 used += 1;
9858
9859 if( used <= buf_len )
9860 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9861#endif
9862
9863#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9864 used += 1;
9865
9866 if( used <= buf_len )
9867 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9868#endif
9869
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009870 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009871 *olen = used;
9872
9873 if( used > buf_len )
9874 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009875
9876 return( 0 );
9877}
9878
9879/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009880 * Public wrapper for ssl_session_save()
9881 */
9882int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9883 unsigned char *buf,
9884 size_t buf_len,
9885 size_t *olen )
9886{
9887 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9888}
9889
9890/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009891 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009892 *
9893 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009894 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009895 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009896static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009897 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009898 const unsigned char *buf,
9899 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009900{
9901 const unsigned char *p = buf;
9902 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009903 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009904#if defined(MBEDTLS_HAVE_TIME)
9905 uint64_t start;
9906#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009907#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009908#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009909 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009910#endif
9911#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009912
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009913 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009914 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009915 /*
9916 * Check version identifier
9917 */
9918
9919 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9921
9922 if( memcmp( p, ssl_serialized_session_header,
9923 sizeof( ssl_serialized_session_header ) ) != 0 )
9924 {
9925 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9926 }
9927 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009928 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009929
9930 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009931 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009932 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009933#if defined(MBEDTLS_HAVE_TIME)
9934 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9936
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009937 start = ( (uint64_t) p[0] << 56 ) |
9938 ( (uint64_t) p[1] << 48 ) |
9939 ( (uint64_t) p[2] << 40 ) |
9940 ( (uint64_t) p[3] << 32 ) |
9941 ( (uint64_t) p[4] << 24 ) |
9942 ( (uint64_t) p[5] << 16 ) |
9943 ( (uint64_t) p[6] << 8 ) |
9944 ( (uint64_t) p[7] );
9945 p += 8;
9946
9947 session->start = (time_t) start;
9948#endif /* MBEDTLS_HAVE_TIME */
9949
9950 /*
9951 * Basic mandatory fields
9952 */
Hanno Becker88440552019-07-03 14:16:13 +01009953 {
9954 size_t const ciphersuite_len = 2;
9955#if defined(MBEDTLS_ZLIB_SUPPORT)
9956 size_t const compression_len = 1;
9957#else
9958 size_t const compression_len = 0;
9959#endif
9960 size_t const id_len_len = 1;
9961 size_t const id_len = 32;
9962 size_t const master_len = 48;
9963 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009964
Hanno Becker88440552019-07-03 14:16:13 +01009965 size_t const basic_len =
9966 ciphersuite_len +
9967 compression_len +
9968 id_len_len +
9969 id_len +
9970 master_len +
9971 verif_result_len;
9972
9973 if( basic_len > (size_t)( end - p ) )
9974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9975 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009976
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009977 ciphersuite = mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009978 p += 2;
9979
Hanno Becker73f4cb12019-06-27 13:51:07 +01009980#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009981 session->ciphersuite = ciphersuite;
9982#else
9983 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009984 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009985 {
9986 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9987 }
9988#endif
9989
Hanno Becker88440552019-07-03 14:16:13 +01009990#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009991 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +01009992#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009993
9994 session->id_len = *p++;
9995 memcpy( session->id, p, 32 );
9996 p += 32;
9997
9998 memcpy( session->master, p, 48 );
9999 p += 48;
10000
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010001 session->verify_result = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010002 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010003
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010004 /* Immediately clear invalid pointer values that have been read, in case
10005 * we exit early before we replaced them with valid ones. */
10006#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010007#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010008 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010009#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010010 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010011#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010012#endif
10013#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10014 session->ticket = NULL;
10015#endif
10016
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010017 /*
10018 * Peer certificate
10019 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010021#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010022 if( 3 > (size_t)( end - p ) )
10023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10024
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010025 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10026
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010027 p += 3;
10028
10029 if( cert_len == 0 )
10030 {
10031 session->peer_cert = NULL;
10032 }
10033 else
10034 {
10035 int ret;
10036
10037 if( cert_len > (size_t)( end - p ) )
10038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10039
10040 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10041
10042 if( session->peer_cert == NULL )
10043 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10044
10045 mbedtls_x509_crt_init( session->peer_cert );
10046
10047 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10048 p, cert_len ) ) != 0 )
10049 {
10050 mbedtls_x509_crt_free( session->peer_cert );
10051 mbedtls_free( session->peer_cert );
10052 session->peer_cert = NULL;
10053 return( ret );
10054 }
10055
10056 p += cert_len;
10057 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010058#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010059 /* Deserialize CRT digest from the end of the ticket. */
10060 if( 2 > (size_t)( end - p ) )
10061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10062
10063 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10064 session->peer_cert_digest_len = (size_t) *p++;
10065
10066 if( session->peer_cert_digest_len != 0 )
10067 {
Hanno Becker2326d202019-06-06 14:54:55 +010010068 const mbedtls_md_info_t *md_info =
10069 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10070 if( md_info == NULL )
10071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10072 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10074
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010075 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10077
10078 session->peer_cert_digest =
10079 mbedtls_calloc( 1, session->peer_cert_digest_len );
10080 if( session->peer_cert_digest == NULL )
10081 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10082
10083 memcpy( session->peer_cert_digest, p,
10084 session->peer_cert_digest_len );
10085 p += session->peer_cert_digest_len;
10086 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010087#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010088#endif /* MBEDTLS_X509_CRT_PARSE_C */
10089
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010090 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010091 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010092 */
10093#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10094 if( 3 > (size_t)( end - p ) )
10095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10096
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010097 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010098 p += 3;
10099
10100 if( session->ticket_len != 0 )
10101 {
10102 if( session->ticket_len > (size_t)( end - p ) )
10103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10104
10105 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10106 if( session->ticket == NULL )
10107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10108
10109 memcpy( session->ticket, p, session->ticket_len );
10110 p += session->ticket_len;
10111 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010112
10113 if( 4 > (size_t)( end - p ) )
10114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10115
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010116 session->ticket_lifetime = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010117 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010118#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10119
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010120 /*
10121 * Misc extension-related info
10122 */
10123#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10124 if( 1 > (size_t)( end - p ) )
10125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10126
10127 session->mfl_code = *p++;
10128#endif
10129
10130#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10131 if( 1 > (size_t)( end - p ) )
10132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10133
10134 session->trunc_hmac = *p++;
10135#endif
10136
10137#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10138 if( 1 > (size_t)( end - p ) )
10139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10140
10141 session->encrypt_then_mac = *p++;
10142#endif
10143
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010144 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010145 if( p != end )
10146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10147
10148 return( 0 );
10149}
10150
10151/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010152 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010153 */
10154int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10155 const unsigned char *buf,
10156 size_t len )
10157{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010158 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010159
10160 if( ret != 0 )
10161 mbedtls_ssl_session_free( session );
10162
10163 return( ret );
10164}
10165
10166/*
Paul Bakker1961b702013-01-25 14:49:24 +010010167 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010172
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010173 if( ssl == NULL || ssl->conf == NULL )
10174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010177 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010181 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010183#endif
10184
Hanno Beckerb82350b2019-07-26 07:24:05 +010010185 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010186 return( ret );
10187}
10188
10189/*
10190 * Perform the SSL handshake
10191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010193{
10194 int ret = 0;
10195
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010196 if( ssl == NULL || ssl->conf == NULL )
10197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010204
10205 if( ret != 0 )
10206 break;
10207 }
10208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010210
10211 return( ret );
10212}
10213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214#if defined(MBEDTLS_SSL_RENEGOTIATION)
10215#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010216/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010217 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010220{
10221 int ret;
10222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010224
10225 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10227 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010228
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010229 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010230 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010232 return( ret );
10233 }
10234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010236
10237 return( 0 );
10238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010240
10241/*
10242 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 * - any side: calling mbedtls_ssl_renegotiate(),
10244 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10245 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010246 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010247 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010251{
10252 int ret;
10253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010255
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010256 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10257 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010258
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010259 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10260 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010262 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010264 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010265 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10266 MBEDTLS_SSL_IS_SERVER )
10267 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010268 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010269 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010270 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010271 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010272 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010273 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010274 }
10275#endif
10276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10278 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010283 return( ret );
10284 }
10285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010287
10288 return( 0 );
10289}
10290
10291/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010292 * Renegotiate current connection on client,
10293 * or request renegotiation on server
10294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010298
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010299 if( ssl == NULL || ssl->conf == NULL )
10300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010303 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010304 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010310
10311 /* Did we already try/start sending HelloRequest? */
10312 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010314
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010315 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010320 /*
10321 * On client, either start the renegotiation process or,
10322 * if already in progress, continue the handshake
10323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010328
10329 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010332 return( ret );
10333 }
10334 }
10335 else
10336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340 return( ret );
10341 }
10342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010344
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010345 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010346}
10347
10348/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010349 * Check record counters and renegotiate if they're above the limit.
10350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010352{
Andres AG2196c7f2016-12-15 17:01:16 +000010353 size_t ep_len = ssl_ep_len( ssl );
10354 int in_ctr_cmp;
10355 int out_ctr_cmp;
10356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10358 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010359 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010360 {
10361 return( 0 );
10362 }
10363
Andres AG2196c7f2016-12-15 17:01:16 +000010364 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10365 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010366 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010367 ssl->conf->renego_period + ep_len, 8 - ep_len );
10368
10369 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010370 {
10371 return( 0 );
10372 }
10373
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010378
10379/*
10380 * Receive application data decrypted from the SSL layer
10381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010383{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010384 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010385 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010386
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010387 if( ssl == NULL || ssl->conf == NULL )
10388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010393 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010396 return( ret );
10397
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010398 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010400 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010401 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010402 return( ret );
10403 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010404 }
10405#endif
10406
Hanno Becker4a810fb2017-05-24 16:27:30 +010010407 /*
10408 * Check if renegotiation is necessary and/or handshake is
10409 * in process. If yes, perform/continue, and fall through
10410 * if an unexpected packet is received while the client
10411 * is waiting for the ServerHello.
10412 *
10413 * (There is no equivalent to the last condition on
10414 * the server-side as it is not treated as within
10415 * a handshake while waiting for the ClientHello
10416 * after a renegotiation request.)
10417 */
10418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010420 ret = ssl_check_ctr_renegotiate( ssl );
10421 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10422 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010425 return( ret );
10426 }
10427#endif
10428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010432 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10433 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 return( ret );
10437 }
10438 }
10439
Hanno Beckere41158b2017-10-23 13:30:32 +010010440 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010441 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010442 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010443 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010444 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10445 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010446 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010447 ssl_set_timer( ssl,
10448 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010449 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010450
Hanno Becker327c93b2018-08-15 13:56:18 +010010451 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010452 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010453 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10454 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010455
Hanno Becker4a810fb2017-05-24 16:27:30 +010010456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10457 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 }
10459
10460 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010462 {
10463 /*
10464 * OpenSSL sends empty messages to randomize the IV
10465 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010466 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010469 return( 0 );
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010472 return( ret );
10473 }
10474 }
10475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010479
Hanno Becker4a810fb2017-05-24 16:27:30 +010010480 /*
10481 * - For client-side, expect SERVER_HELLO_REQUEST.
10482 * - For server-side, expect CLIENT_HELLO.
10483 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10484 */
10485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010487 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10488 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010490 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010493
10494 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010496 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010497 {
10498 continue;
10499 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010500 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010501#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010502#if defined(MBEDTLS_SSL_PROTO_TLS)
10503 {
10504 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10505 }
10506#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010507 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010508#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010509
Hanno Becker4a810fb2017-05-24 16:27:30 +010010510#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010511 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10512 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010516
10517 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010519 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010520 {
10521 continue;
10522 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010523 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010524#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010525#if defined(MBEDTLS_SSL_PROTO_TLS)
10526 {
10527 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10528 }
10529#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010530 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010531#endif /* MBEDTLS_SSL_SRV_C */
10532
Hanno Becker21df7f92017-10-17 11:03:26 +010010533#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010534 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010535 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10536 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010537 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010538 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10539 {
10540 /*
10541 * Accept renegotiation request
10542 */
Paul Bakker48916f92012-09-16 19:57:18 +000010543
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010544 /* DTLS clients need to know renego is server-initiated */
10545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010546 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010547 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10548 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010549 {
10550 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10551 }
10552#endif
10553 ret = ssl_start_renegotiation( ssl );
10554 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10555 ret != 0 )
10556 {
10557 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10558 return( ret );
10559 }
10560 }
10561 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010562#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010563 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010564 /*
10565 * Refuse renegotiation
10566 */
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010570#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010571 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010572 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010573 /* SSLv3 does not have a "no_renegotiation" warning, so
10574 we send a fatal alert and abort the connection. */
10575 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10576 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10577 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010578 }
10579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10581#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10582 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010583 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010584 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010585 ret = mbedtls_ssl_send_alert_message( ssl,
10586 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10587 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10588 if( ret != 0 )
10589 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010590 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10593 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010597 }
Paul Bakker48916f92012-09-16 19:57:18 +000010598 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010599
Hanno Becker90333da2017-10-10 11:27:13 +010010600 /* At this point, we don't know whether the renegotiation has been
10601 * completed or not. The cases to consider are the following:
10602 * 1) The renegotiation is complete. In this case, no new record
10603 * has been read yet.
10604 * 2) The renegotiation is incomplete because the client received
10605 * an application data record while awaiting the ServerHello.
10606 * 3) The renegotiation is incomplete because the client received
10607 * a non-handshake, non-application data message while awaiting
10608 * the ServerHello.
10609 * In each of these case, looping will be the proper action:
10610 * - For 1), the next iteration will read a new record and check
10611 * if it's application data.
10612 * - For 2), the loop condition isn't satisfied as application data
10613 * is present, hence continue is the same as break
10614 * - For 3), the loop condition is satisfied and read_record
10615 * will re-deliver the message that was held back by the client
10616 * when expecting the ServerHello.
10617 */
10618 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010619 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010620#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010622 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010623 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010624 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010625 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010628 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010630 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010631 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10636 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010639 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010640 }
10641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10645 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010646 }
10647
10648 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010649
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010650 /* We're going to return something now, cancel timer,
10651 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010653 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010654
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010656 /* If we requested renego but received AppData, resend HelloRequest.
10657 * Do it now, after setting in_offt, to avoid taking this branch
10658 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010660 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10661 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010663 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010664 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010667 return( ret );
10668 }
10669 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010671#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010672 }
10673
10674 n = ( len < ssl->in_msglen )
10675 ? len : ssl->in_msglen;
10676
10677 memcpy( buf, ssl->in_offt, n );
10678 ssl->in_msglen -= n;
10679
10680 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010681 {
10682 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010683 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010684 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010685 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010686 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010687 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010688 /* more data available */
10689 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010690 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010693
Paul Bakker23986e52011-04-24 08:57:21 +000010694 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010695}
10696
10697/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010698 * Send application data to be encrypted by the SSL layer, taking care of max
10699 * fragment length and buffer size.
10700 *
10701 * According to RFC 5246 Section 6.2.1:
10702 *
10703 * Zero-length fragments of Application data MAY be sent as they are
10704 * potentially useful as a traffic analysis countermeasure.
10705 *
10706 * Therefore, it is possible that the input message length is 0 and the
10707 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010708 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010709static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010710 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010711{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010712 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10713 const size_t max_len = (size_t) ret;
10714
10715 if( ret < 0 )
10716 {
10717 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10718 return( ret );
10719 }
10720
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010721 if( len > max_len )
10722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010724 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010727 "maximum fragment length: %d > %d",
10728 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010730 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010731 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010732#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010733#if defined(MBEDTLS_SSL_PROTO_TLS)
10734 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010735 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010736 }
10737#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010738 }
Paul Bakker887bd502011-06-08 13:10:54 +000010739
Paul Bakker5121ce52009-01-03 21:22:43 +000010740 if( ssl->out_left != 0 )
10741 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010742 /*
10743 * The user has previously tried to send the data and
10744 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10745 * written. In this case, we expect the high-level write function
10746 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010751 return( ret );
10752 }
10753 }
Paul Bakker887bd502011-06-08 13:10:54 +000010754 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010755 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010756 /*
10757 * The user is trying to send a message the first time, so we need to
10758 * copy the data into the internal buffers and setup the data structure
10759 * to keep track of partial writes
10760 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010761 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010763 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010764
Hanno Becker67bc7c32018-08-06 11:33:50 +010010765 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010768 return( ret );
10769 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010770 }
10771
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010772 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010773}
10774
10775/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010776 * Write application data, doing 1/n-1 splitting if necessary.
10777 *
10778 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010779 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010780 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010783static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010784 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010785{
10786 int ret;
10787
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010788 if( ssl->conf->cbc_record_splitting ==
10789 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010790 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010791 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10793 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010794 {
10795 return( ssl_write_real( ssl, buf, len ) );
10796 }
10797
10798 if( ssl->split_done == 0 )
10799 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010800 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010801 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010802 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010803 }
10804
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010805 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10806 return( ret );
10807 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010808
10809 return( ret + 1 );
10810}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010812
10813/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010814 * Write application data (public-facing wrapper)
10815 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010816int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010817{
10818 int ret;
10819
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010821
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010822 if( ssl == NULL || ssl->conf == NULL )
10823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10824
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010825#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010826 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10827 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010828 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010829 return( ret );
10830 }
10831#endif
10832
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010833 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010834 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010835 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010836 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010838 return( ret );
10839 }
10840 }
10841
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010842#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010843 ret = ssl_write_split( ssl, buf, len );
10844#else
10845 ret = ssl_write_real( ssl, buf, len );
10846#endif
10847
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010849
10850 return( ret );
10851}
10852
10853/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010854 * Notify the peer that the connection is being closed
10855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010857{
10858 int ret;
10859
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010860 if( ssl == NULL || ssl->conf == NULL )
10861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010864
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010865 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10871 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10872 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010875 return( ret );
10876 }
10877 }
10878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010880
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010881 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010882}
10883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010885{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010886 if( transform == NULL )
10887 return;
10888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010889#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010890 deflateEnd( &transform->ctx_deflate );
10891 inflateEnd( &transform->ctx_inflate );
10892#endif
10893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10895 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010896
Hanno Becker92231322018-01-03 15:32:51 +000010897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 mbedtls_md_free( &transform->md_ctx_enc );
10899 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010900#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010901
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010902 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010903}
10904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010905#if defined(MBEDTLS_X509_CRT_PARSE_C)
10906static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010909
10910 while( cur != NULL )
10911 {
10912 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010914 cur = next;
10915 }
10916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010918
Hanno Becker0271f962018-08-16 13:23:47 +010010919#if defined(MBEDTLS_SSL_PROTO_DTLS)
10920
10921static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10922{
10923 unsigned offset;
10924 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10925
10926 if( hs == NULL )
10927 return;
10928
Hanno Becker283f5ef2018-08-24 09:34:47 +010010929 ssl_free_buffered_record( ssl );
10930
Hanno Becker0271f962018-08-16 13:23:47 +010010931 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010932 ssl_buffering_free_slot( ssl, offset );
10933}
10934
10935static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10936 uint8_t slot )
10937{
10938 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10939 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010940
10941 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10942 return;
10943
Hanno Beckere605b192018-08-21 15:59:07 +010010944 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010945 {
Hanno Beckere605b192018-08-21 15:59:07 +010010946 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010947 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010948 mbedtls_free( hs_buf->data );
10949 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010950 }
10951}
10952
10953#endif /* MBEDTLS_SSL_PROTO_DTLS */
10954
Gilles Peskine9b562d52018-04-25 20:32:43 +020010955void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010956{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010957 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10958
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010959 if( handshake == NULL )
10960 return;
10961
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010962#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10963 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10964 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010965 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010966 handshake->async_in_progress = 0;
10967 }
10968#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10969
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010970#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10971 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10972 mbedtls_md5_free( &handshake->fin_md5 );
10973 mbedtls_sha1_free( &handshake->fin_sha1 );
10974#endif
10975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10976#if defined(MBEDTLS_SHA256_C)
10977 mbedtls_sha256_free( &handshake->fin_sha256 );
10978#endif
10979#if defined(MBEDTLS_SHA512_C)
10980 mbedtls_sha512_free( &handshake->fin_sha512 );
10981#endif
10982#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984#if defined(MBEDTLS_DHM_C)
10985 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010987#if defined(MBEDTLS_ECDH_C)
10988 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010989#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010990#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010991 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010992#if defined(MBEDTLS_SSL_CLI_C)
10993 mbedtls_free( handshake->ecjpake_cache );
10994 handshake->ecjpake_cache = NULL;
10995 handshake->ecjpake_cache_len = 0;
10996#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010997#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010998
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010999#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11000 if( handshake->psk != NULL )
11001 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011002 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011003 mbedtls_free( handshake->psk );
11004 }
11005#endif
11006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11008 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011009 /*
11010 * Free only the linked list wrapper, not the keys themselves
11011 * since the belong to the SNI callback
11012 */
11013 if( handshake->sni_key_cert != NULL )
11014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011016
11017 while( cur != NULL )
11018 {
11019 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011021 cur = next;
11022 }
11023 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011025
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011026#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011027 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011028 if( handshake->ecrs_peer_cert != NULL )
11029 {
11030 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11031 mbedtls_free( handshake->ecrs_peer_cert );
11032 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011033#endif
11034
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011035#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11036 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11037 mbedtls_pk_free( &handshake->peer_pubkey );
11038#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#if defined(MBEDTLS_SSL_PROTO_DTLS)
11041 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011042 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011043 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011044#endif
11045
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011046 mbedtls_platform_zeroize( handshake,
11047 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011048}
11049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011051{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011052 if( session == NULL )
11053 return;
11054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011056 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011057#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011058
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011059#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011061#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011062
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011063 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011064}
11065
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011066#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011067
11068#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11069#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11070#else
11071#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11072#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11073
11074#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11075#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11076#else
11077#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11078#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11079
11080#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11081#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11082#else
11083#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11084#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11085
11086#if defined(MBEDTLS_SSL_ALPN)
11087#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11088#else
11089#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11090#endif /* MBEDTLS_SSL_ALPN */
11091
11092#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11093#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11094#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11095#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11096
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11098 ( (uint32_t) ( \
11099 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11100 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11101 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11102 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11103 0u ) )
11104
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011105static unsigned char ssl_serialized_context_header[] = {
11106 MBEDTLS_VERSION_MAJOR,
11107 MBEDTLS_VERSION_MINOR,
11108 MBEDTLS_VERSION_PATCH,
11109 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11110 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011111 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11112 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11113 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011114};
11115
Paul Bakker5121ce52009-01-03 21:22:43 +000011116/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011117 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011118 *
11119 * The format of the serialized data is:
11120 * (in the presentation language of TLS, RFC 8446 section 3)
11121 *
11122 * // header
11123 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011124 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011125 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011126 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011127 * Note: When updating the format, remember to keep these
11128 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011129 *
11130 * // session sub-structure
11131 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11132 * // transform sub-structure
11133 * uint8 random[64]; // ServerHello.random+ClientHello.random
11134 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11135 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11136 * // fields from ssl_context
11137 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11138 * uint64 in_window_top; // DTLS: last validated record seq_num
11139 * uint64 in_window; // DTLS: bitmask for replay protection
11140 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11141 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11142 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11143 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11144 *
11145 * Note that many fields of the ssl_context or sub-structures are not
11146 * serialized, as they fall in one of the following categories:
11147 *
11148 * 1. forced value (eg in_left must be 0)
11149 * 2. pointer to dynamically-allocated memory (eg session, transform)
11150 * 3. value can be re-derived from other data (eg session keys from MS)
11151 * 4. value was temporary (eg content of input buffer)
11152 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011153 */
11154int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11155 unsigned char *buf,
11156 size_t buf_len,
11157 size_t *olen )
11158{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011159 unsigned char *p = buf;
11160 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011161 size_t session_len;
11162 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011163
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011164 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011165 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11166 * this function's documentation.
11167 *
11168 * These are due to assumptions/limitations in the implementation. Some of
11169 * them are likely to stay (no handshake in progress) some might go away
11170 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011171 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011172 /* The initial handshake must be over */
11173 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011175 if( ssl->handshake != NULL )
11176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11177 /* Double-check that sub-structures are indeed ready */
11178 if( ssl->transform == NULL || ssl->session == NULL )
11179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11180 /* There must be no pending incoming or outgoing data */
11181 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11183 if( ssl->out_left != 0 )
11184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11185 /* Protocol must be DLTS, not TLS */
11186 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11188 /* Version must be 1.2 */
11189 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11191 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11193 /* We must be using an AEAD ciphersuite */
11194 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11196 /* Renegotiation must not be enabled */
11197 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011199
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011200 /*
11201 * Version and format identifier
11202 */
11203 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011204
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011205 if( used <= buf_len )
11206 {
11207 memcpy( p, ssl_serialized_context_header,
11208 sizeof( ssl_serialized_context_header ) );
11209 p += sizeof( ssl_serialized_context_header );
11210 }
11211
11212 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011213 * Session (length + data)
11214 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011215 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011216 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11217 return( ret );
11218
11219 used += 4 + session_len;
11220 if( used <= buf_len )
11221 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011222 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011223
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011224 ret = ssl_session_save( ssl->session, 1,
11225 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011226 if( ret != 0 )
11227 return( ret );
11228
11229 p += session_len;
11230 }
11231
11232 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011233 * Transform
11234 */
11235 used += sizeof( ssl->transform->randbytes );
11236 if( used <= buf_len )
11237 {
11238 memcpy( p, ssl->transform->randbytes,
11239 sizeof( ssl->transform->randbytes ) );
11240 p += sizeof( ssl->transform->randbytes );
11241 }
11242
11243#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11244 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11245 if( used <= buf_len )
11246 {
11247 *p++ = ssl->transform->in_cid_len;
11248 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11249 p += ssl->transform->in_cid_len;
11250
11251 *p++ = ssl->transform->out_cid_len;
11252 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11253 p += ssl->transform->out_cid_len;
11254 }
11255#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11256
11257 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011258 * Saved fields from top-level ssl_context structure
11259 */
11260#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11261 used += 4;
11262 if( used <= buf_len )
11263 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011264 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011265 }
11266#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11267
11268#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11269 used += 16;
11270 if( used <= buf_len )
11271 {
11272 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11273 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11274 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11275 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11276 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11277 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11278 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11279 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11280
11281 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11282 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11283 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11284 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11285 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11286 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11287 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11288 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11289 }
11290#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11291
11292#if defined(MBEDTLS_SSL_PROTO_DTLS)
11293 used += 1;
11294 if( used <= buf_len )
11295 {
11296 *p++ = ssl->disable_datagram_packing;
11297 }
11298#endif /* MBEDTLS_SSL_PROTO_DTLS */
11299
11300 used += 8;
11301 if( used <= buf_len )
11302 {
11303 memcpy( p, ssl->cur_out_ctr, 8 );
11304 p += 8;
11305 }
11306
11307#if defined(MBEDTLS_SSL_PROTO_DTLS)
11308 used += 2;
11309 if( used <= buf_len )
11310 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011311 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011312 }
11313#endif /* MBEDTLS_SSL_PROTO_DTLS */
11314
11315#if defined(MBEDTLS_SSL_ALPN)
11316 {
11317 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011318 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011319 : 0;
11320
11321 used += 1 + alpn_len;
11322 if( used <= buf_len )
11323 {
11324 *p++ = alpn_len;
11325
11326 if( ssl->alpn_chosen != NULL )
11327 {
11328 memcpy( p, ssl->alpn_chosen, alpn_len );
11329 p += alpn_len;
11330 }
11331 }
11332 }
11333#endif /* MBEDTLS_SSL_ALPN */
11334
11335 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011336 * Done
11337 */
11338 *olen = used;
11339
11340 if( used > buf_len )
11341 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011342
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011343 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11344
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011345 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011346}
11347
11348/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011349 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011350 *
11351 * This internal version is wrapped by a public function that cleans up in
11352 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011353 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011354static int ssl_context_load( mbedtls_ssl_context *ssl,
11355 const unsigned char *buf,
11356 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011357{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011358 const unsigned char *p = buf;
11359 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011360 size_t session_len;
11361 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011362
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011363 /*
11364 * The context should have been freshly setup or reset.
11365 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011366 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011367 * renegotiating, or if the user mistakenly loaded a session first.)
11368 */
11369 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11370 ssl->session != NULL )
11371 {
11372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11373 }
11374
11375 /*
11376 * We can't check that the config matches the initial one, but we can at
11377 * least check it matches the requirements for serializing.
11378 */
11379 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011380 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11381 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11382 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11383 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11384 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11385 MBEDTLS_SSL_MINOR_VERSION_3 ||
11386 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11387 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011388 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011389 {
11390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11391 }
11392
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011393 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11394
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011395 /*
11396 * Check version identifier
11397 */
11398 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11400
11401 if( memcmp( p, ssl_serialized_context_header,
11402 sizeof( ssl_serialized_context_header ) ) != 0 )
11403 {
11404 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11405 }
11406 p += sizeof( ssl_serialized_context_header );
11407
11408 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011409 * Session
11410 */
11411 if( (size_t)( end - p ) < 4 )
11412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11413
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011414 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011415 p += 4;
11416
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011417 /* This has been allocated by ssl_handshake_init(), called by
11418 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11419 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011420 ssl->session_in = ssl->session;
11421 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011422 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011423
11424 if( (size_t)( end - p ) < session_len )
11425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11426
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011427 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011428 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011429 {
11430 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011431 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011432 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011433
11434 p += session_len;
11435
11436 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011437 * Transform
11438 */
11439
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011440 /* This has been allocated by ssl_handshake_init(), called by
11441 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11442 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011443 ssl->transform_in = ssl->transform;
11444 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011445 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011446
11447 /* Read random bytes and populate structure */
11448 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11450
11451 ret = ssl_populate_transform( ssl->transform,
11452 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11453 ssl->session->master,
11454#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11455#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11456 ssl->session->encrypt_then_mac,
11457#endif
11458#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11459 ssl->session->trunc_hmac,
11460#endif
11461#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11462#if defined(MBEDTLS_ZLIB_SUPPORT)
11463 ssl->session->compression,
11464#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011465 p, /* currently pointing to randbytes */
11466 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11467 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11468 ssl );
11469 if( ret != 0 )
11470 return( ret );
11471
11472 p += sizeof( ssl->transform->randbytes );
11473
11474#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11475 /* Read connection IDs and store them */
11476 if( (size_t)( end - p ) < 1 )
11477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11478
11479 ssl->transform->in_cid_len = *p++;
11480
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011481 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11483
11484 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11485 p += ssl->transform->in_cid_len;
11486
11487 ssl->transform->out_cid_len = *p++;
11488
11489 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11490 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11491
11492 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11493 p += ssl->transform->out_cid_len;
11494#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11495
11496 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011497 * Saved fields from top-level ssl_context structure
11498 */
11499#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11500 if( (size_t)( end - p ) < 4 )
11501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11502
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011503 ssl->badmac_seen = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011504 p += 4;
11505#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11506
11507#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11508 if( (size_t)( end - p ) < 16 )
11509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11510
11511 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11512 ( (uint64_t) p[1] << 48 ) |
11513 ( (uint64_t) p[2] << 40 ) |
11514 ( (uint64_t) p[3] << 32 ) |
11515 ( (uint64_t) p[4] << 24 ) |
11516 ( (uint64_t) p[5] << 16 ) |
11517 ( (uint64_t) p[6] << 8 ) |
11518 ( (uint64_t) p[7] );
11519 p += 8;
11520
11521 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11522 ( (uint64_t) p[1] << 48 ) |
11523 ( (uint64_t) p[2] << 40 ) |
11524 ( (uint64_t) p[3] << 32 ) |
11525 ( (uint64_t) p[4] << 24 ) |
11526 ( (uint64_t) p[5] << 16 ) |
11527 ( (uint64_t) p[6] << 8 ) |
11528 ( (uint64_t) p[7] );
11529 p += 8;
11530#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11531
11532#if defined(MBEDTLS_SSL_PROTO_DTLS)
11533 if( (size_t)( end - p ) < 1 )
11534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11535
11536 ssl->disable_datagram_packing = *p++;
11537#endif /* MBEDTLS_SSL_PROTO_DTLS */
11538
11539 if( (size_t)( end - p ) < 8 )
11540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11541
11542 memcpy( ssl->cur_out_ctr, p, 8 );
11543 p += 8;
11544
11545#if defined(MBEDTLS_SSL_PROTO_DTLS)
11546 if( (size_t)( end - p ) < 2 )
11547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11548
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011549 ssl->mtu = mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011550 p += 2;
11551#endif /* MBEDTLS_SSL_PROTO_DTLS */
11552
11553#if defined(MBEDTLS_SSL_ALPN)
11554 {
11555 uint8_t alpn_len;
11556 const char **cur;
11557
11558 if( (size_t)( end - p ) < 1 )
11559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11560
11561 alpn_len = *p++;
11562
11563 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11564 {
11565 /* alpn_chosen should point to an item in the configured list */
11566 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11567 {
11568 if( strlen( *cur ) == alpn_len &&
11569 memcmp( p, cur, alpn_len ) == 0 )
11570 {
11571 ssl->alpn_chosen = *cur;
11572 break;
11573 }
11574 }
11575 }
11576
11577 /* can only happen on conf mismatch */
11578 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11580
11581 p += alpn_len;
11582 }
11583#endif /* MBEDTLS_SSL_ALPN */
11584
11585 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011586 * Forced fields from top-level ssl_context structure
11587 *
11588 * Most of them already set to the correct value by mbedtls_ssl_init() and
11589 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11590 */
11591 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11592
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011593#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011594 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011595#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11596#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011597 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011598#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011599
Hanno Becker83985822019-08-30 10:42:49 +010011600 /* Adjust pointers for header fields of outgoing records to
11601 * the given transform, accounting for explicit IV and CID. */
11602 ssl_update_out_pointers( ssl, ssl->transform );
11603
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011604#if defined(MBEDTLS_SSL_PROTO_DTLS)
11605 ssl->in_epoch = 1;
11606#endif
11607
11608 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11609 * which we don't want - otherwise we'd end up freeing the wrong transform
11610 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11611 if( ssl->handshake != NULL )
11612 {
11613 mbedtls_ssl_handshake_free( ssl );
11614 mbedtls_free( ssl->handshake );
11615 ssl->handshake = NULL;
11616 }
11617
11618 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011619 * Done - should have consumed entire buffer
11620 */
11621 if( p != end )
11622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011623
11624 return( 0 );
11625}
11626
11627/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011628 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011629 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011630int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011631 const unsigned char *buf,
11632 size_t len )
11633{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011634 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011635
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011636 if( ret != 0 )
11637 mbedtls_ssl_free( context );
11638
11639 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011640}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011641#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011642
11643/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011644 * Free an SSL context
11645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011646void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011647{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011648 if( ssl == NULL )
11649 return;
11650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011652
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011653 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011654 {
Angus Grattond8213d02016-05-25 20:56:48 +100011655 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011656 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011657 }
11658
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011659 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011660 {
Angus Grattond8213d02016-05-25 20:56:48 +100011661 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011662 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011663 }
11664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011665#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011666 if( ssl->compress_buf != NULL )
11667 {
Angus Grattond8213d02016-05-25 20:56:48 +100011668 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011669 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011670 }
11671#endif
11672
Paul Bakker48916f92012-09-16 19:57:18 +000011673 if( ssl->transform )
11674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011675 mbedtls_ssl_transform_free( ssl->transform );
11676 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011677 }
11678
11679 if( ssl->handshake )
11680 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011681 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011682 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11683 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011685 mbedtls_free( ssl->handshake );
11686 mbedtls_free( ssl->transform_negotiate );
11687 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011688 }
11689
Paul Bakkerc0463502013-02-14 11:19:38 +010011690 if( ssl->session )
11691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011692 mbedtls_ssl_session_free( ssl->session );
11693 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011694 }
11695
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011696#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011697 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011698 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011699 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011700 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011701 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011702#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011704#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11705 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11708 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011709 }
11710#endif
11711
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011712#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011713 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011714#endif
11715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011717
Paul Bakker86f04f42013-02-14 11:20:09 +010011718 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011719 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011720}
11721
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011722/*
11723 * Initialze mbedtls_ssl_config
11724 */
11725void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11726{
11727 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011728
11729#if !defined(MBEDTLS_SSL_PROTO_TLS)
11730 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11731#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011732}
11733
Simon Butcherc97b6972015-12-27 23:48:17 +000011734#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011735#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011736static int ssl_preset_default_hashes[] = {
11737#if defined(MBEDTLS_SHA512_C)
11738 MBEDTLS_MD_SHA512,
11739 MBEDTLS_MD_SHA384,
11740#endif
11741#if defined(MBEDTLS_SHA256_C)
11742 MBEDTLS_MD_SHA256,
11743 MBEDTLS_MD_SHA224,
11744#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011745#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011746 MBEDTLS_MD_SHA1,
11747#endif
11748 MBEDTLS_MD_NONE
11749};
Simon Butcherc97b6972015-12-27 23:48:17 +000011750#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011751#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011752
Hanno Becker73f4cb12019-06-27 13:51:07 +010011753#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011754static int ssl_preset_suiteb_ciphersuites[] = {
11755 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11756 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11757 0
11758};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011759#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011760
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011761#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011762#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011763static int ssl_preset_suiteb_hashes[] = {
11764 MBEDTLS_MD_SHA256,
11765 MBEDTLS_MD_SHA384,
11766 MBEDTLS_MD_NONE
11767};
11768#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011769#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011770
Hanno Beckerc1096e72019-06-19 12:30:41 +010011771#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011772static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011773#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011774 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011775#endif
11776#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011777 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011778#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011779 MBEDTLS_ECP_DP_NONE
11780};
11781#endif
11782
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011783/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011784 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011785 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011786int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011787 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011788{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011789#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011790 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011791#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011792
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011793 /* Use the functions here so that they are covered in tests,
11794 * but otherwise access member directly for efficiency */
11795 mbedtls_ssl_conf_endpoint( conf, endpoint );
11796 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011797
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011798 /*
11799 * Things that are common to all presets
11800 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011801#if defined(MBEDTLS_SSL_CLI_C)
11802 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11803 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011804#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011805 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011806#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011807#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11808 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11809#endif
11810 }
11811#endif
11812
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011813#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011814 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011815#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011816
11817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11818 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11819#endif
11820
11821#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011822#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011823 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011824#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11825#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011826 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011827 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011828#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011829#endif
11830
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011831#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11832 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11833#endif
11834
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011835#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011836 conf->f_cookie_write = ssl_cookie_write_dummy;
11837 conf->f_cookie_check = ssl_cookie_check_dummy;
11838#endif
11839
Hanno Becker7f376f42019-06-12 16:20:48 +010011840#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11841 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011842 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11843#endif
11844
Janos Follath088ce432017-04-10 12:42:31 +010011845#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011846#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011847 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011848#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11849#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011850
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011852#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011853 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011854#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11855#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011856 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011857#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11858#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011859
11860#if defined(MBEDTLS_SSL_RENEGOTIATION)
11861 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011862 memset( conf->renego_period, 0x00, 2 );
11863 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011864#endif
11865
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011866#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11867 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11868 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011869 const unsigned char dhm_p[] =
11870 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11871 const unsigned char dhm_g[] =
11872 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11873
Hanno Beckera90658f2017-10-04 15:29:08 +010011874 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11875 dhm_p, sizeof( dhm_p ),
11876 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011877 {
11878 return( ret );
11879 }
11880 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011881#endif
11882
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011883 /*
11884 * Preset-specific defaults
11885 */
11886 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011887 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011888 /*
11889 * NSA Suite B
11890 */
11891 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011892#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011893 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011894#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11895#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011896 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011897#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11898#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011899 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011900#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11901#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011902 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011903#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011904
Hanno Becker73f4cb12019-06-27 13:51:07 +010011905#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011906 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11907 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11908 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11909 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11910 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011911#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011912
11913#if defined(MBEDTLS_X509_CRT_PARSE_C)
11914 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011915#endif
11916
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011917#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011918#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011919 conf->sig_hashes = ssl_preset_suiteb_hashes;
11920#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011921#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011922
11923#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011924#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011925 conf->curve_list = ssl_preset_suiteb_curves;
11926#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011927#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011928 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011929
11930 /*
11931 * Default
11932 */
11933 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011934#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011935 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11936 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11937 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11938 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011939#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11940#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011941 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11942 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11943 MBEDTLS_SSL_MIN_MINOR_VERSION :
11944 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011946 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11948#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011949#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11950#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11951 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11952#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11953#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11954 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11955#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011956
Hanno Becker73f4cb12019-06-27 13:51:07 +010011957#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011958 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11959 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11960 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11961 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11962 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011963#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011964
11965#if defined(MBEDTLS_X509_CRT_PARSE_C)
11966 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11967#endif
11968
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011969#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011970#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011971 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011972#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011973#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011974
11975#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011976#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011977 conf->curve_list = mbedtls_ecp_grp_id_list();
11978#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011979#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011980
11981#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11982 conf->dhm_min_bitlen = 1024;
11983#endif
11984 }
11985
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011986 return( 0 );
11987}
11988
11989/*
11990 * Free mbedtls_ssl_config
11991 */
11992void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11993{
11994#if defined(MBEDTLS_DHM_C)
11995 mbedtls_mpi_free( &conf->dhm_P );
11996 mbedtls_mpi_free( &conf->dhm_G );
11997#endif
11998
11999#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12000 if( conf->psk != NULL )
12001 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012002 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012003 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012004 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012005 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012006 }
12007
12008 if( conf->psk_identity != NULL )
12009 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012010 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012011 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012012 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012013 conf->psk_identity_len = 0;
12014 }
12015#endif
12016
12017#if defined(MBEDTLS_X509_CRT_PARSE_C)
12018 ssl_key_cert_free( conf->key_cert );
12019#endif
12020
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012021 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012022}
12023
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012024#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012025 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12026 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012027/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012028 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012030unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012032#if defined(MBEDTLS_RSA_C)
12033 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12034 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012035#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012036#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012037 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12038 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012040 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012041}
12042
Hanno Becker7e5437a2017-04-28 17:15:26 +010012043unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12044{
12045 switch( type ) {
12046 case MBEDTLS_PK_RSA:
12047 return( MBEDTLS_SSL_SIG_RSA );
12048 case MBEDTLS_PK_ECDSA:
12049 case MBEDTLS_PK_ECKEY:
12050 return( MBEDTLS_SSL_SIG_ECDSA );
12051 default:
12052 return( MBEDTLS_SSL_SIG_ANON );
12053 }
12054}
12055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012056mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012057{
12058 switch( sig )
12059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012060#if defined(MBEDTLS_RSA_C)
12061 case MBEDTLS_SSL_SIG_RSA:
12062 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012063#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012064#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012065 case MBEDTLS_SSL_SIG_ECDSA:
12066 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012067#endif
12068 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012069 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012070 }
12071}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012072#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012073
Hanno Becker7e5437a2017-04-28 17:15:26 +010012074#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12075 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12076
12077/* Find an entry in a signature-hash set matching a given hash algorithm. */
12078mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12079 mbedtls_pk_type_t sig_alg )
12080{
12081 switch( sig_alg )
12082 {
12083 case MBEDTLS_PK_RSA:
12084 return( set->rsa );
12085 case MBEDTLS_PK_ECDSA:
12086 return( set->ecdsa );
12087 default:
12088 return( MBEDTLS_MD_NONE );
12089 }
12090}
12091
12092/* Add a signature-hash-pair to a signature-hash set */
12093void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12094 mbedtls_pk_type_t sig_alg,
12095 mbedtls_md_type_t md_alg )
12096{
12097 switch( sig_alg )
12098 {
12099 case MBEDTLS_PK_RSA:
12100 if( set->rsa == MBEDTLS_MD_NONE )
12101 set->rsa = md_alg;
12102 break;
12103
12104 case MBEDTLS_PK_ECDSA:
12105 if( set->ecdsa == MBEDTLS_MD_NONE )
12106 set->ecdsa = md_alg;
12107 break;
12108
12109 default:
12110 break;
12111 }
12112}
12113
12114/* Allow exactly one hash algorithm for each signature. */
12115void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12116 mbedtls_md_type_t md_alg )
12117{
12118 set->rsa = md_alg;
12119 set->ecdsa = md_alg;
12120}
12121
12122#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12123 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12124
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012125/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012126 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012128mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012129{
12130 switch( hash )
12131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012132#if defined(MBEDTLS_MD5_C)
12133 case MBEDTLS_SSL_HASH_MD5:
12134 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136#if defined(MBEDTLS_SHA1_C)
12137 case MBEDTLS_SSL_HASH_SHA1:
12138 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012140#if defined(MBEDTLS_SHA256_C)
12141 case MBEDTLS_SSL_HASH_SHA224:
12142 return( MBEDTLS_MD_SHA224 );
12143 case MBEDTLS_SSL_HASH_SHA256:
12144 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012146#if defined(MBEDTLS_SHA512_C)
12147 case MBEDTLS_SSL_HASH_SHA384:
12148 return( MBEDTLS_MD_SHA384 );
12149 case MBEDTLS_SSL_HASH_SHA512:
12150 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012151#endif
12152 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012153 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012154 }
12155}
12156
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012157/*
12158 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12159 */
12160unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12161{
12162 switch( md )
12163 {
12164#if defined(MBEDTLS_MD5_C)
12165 case MBEDTLS_MD_MD5:
12166 return( MBEDTLS_SSL_HASH_MD5 );
12167#endif
12168#if defined(MBEDTLS_SHA1_C)
12169 case MBEDTLS_MD_SHA1:
12170 return( MBEDTLS_SSL_HASH_SHA1 );
12171#endif
12172#if defined(MBEDTLS_SHA256_C)
12173 case MBEDTLS_MD_SHA224:
12174 return( MBEDTLS_SSL_HASH_SHA224 );
12175 case MBEDTLS_MD_SHA256:
12176 return( MBEDTLS_SSL_HASH_SHA256 );
12177#endif
12178#if defined(MBEDTLS_SHA512_C)
12179 case MBEDTLS_MD_SHA384:
12180 return( MBEDTLS_SSL_HASH_SHA384 );
12181 case MBEDTLS_MD_SHA512:
12182 return( MBEDTLS_SSL_HASH_SHA512 );
12183#endif
12184 default:
12185 return( MBEDTLS_SSL_HASH_NONE );
12186 }
12187}
12188
Hanno Beckeree902df2019-08-23 13:47:47 +010012189#if defined(MBEDTLS_USE_TINYCRYPT)
12190/*
12191 * Check if a curve proposed by the peer is in our list.
12192 * Return 0 if we're willing to use it, -1 otherwise.
12193 */
12194int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12195 mbedtls_uecc_group_id grp_id )
12196{
12197 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12198 if( own_ec_id == grp_id )
12199 return( 0 );
12200 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12201
12202 return( -1 );
12203}
12204#endif /* MBEDTLS_USE_TINYCRYPT */
12205
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012206#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012207/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012208 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012209 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012210 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012211int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12212 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012213{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012214 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12215 if( own_ec_id == grp_id )
12216 return( 0 );
12217 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012218
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012219 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012220}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012221#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012222
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012223#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012224/*
12225 * Check if a hash proposed by the peer is in our list.
12226 * Return 0 if we're willing to use it, -1 otherwise.
12227 */
12228int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12229 mbedtls_md_type_t md )
12230{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012231 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12232 if( md_alg == md )
12233 return( 0 );
12234 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012235
12236 return( -1 );
12237}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012238#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012240#if defined(MBEDTLS_X509_CRT_PARSE_C)
12241int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012242 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012243 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012244 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012245{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012246 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012247#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012248 int usage = 0;
12249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012250#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012251 const char *ext_oid;
12252 size_t ext_len;
12253#endif
12254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012255#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12256 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012257 ((void) cert);
12258 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012259 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012260#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012262#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12263 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012264 {
12265 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012266 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012268 case MBEDTLS_KEY_EXCHANGE_RSA:
12269 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012270 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012271 break;
12272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012273 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12274 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12275 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12276 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012277 break;
12278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012279 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12280 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012281 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012282 break;
12283
12284 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012285 case MBEDTLS_KEY_EXCHANGE_NONE:
12286 case MBEDTLS_KEY_EXCHANGE_PSK:
12287 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12288 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012289 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012290 usage = 0;
12291 }
12292 }
12293 else
12294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012295 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12296 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012297 }
12298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012299 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012300 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012301 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012302 ret = -1;
12303 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012304#else
12305 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012306#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012308#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12309 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012311 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12312 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012313 }
12314 else
12315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012316 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12317 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012318 }
12319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012320 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012321 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012322 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012323 ret = -1;
12324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012325#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012326
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012327 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012329#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012330
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012331#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12332 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12333int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12334 unsigned char *output,
12335 unsigned char *data, size_t data_len )
12336{
12337 int ret = 0;
12338 mbedtls_md5_context mbedtls_md5;
12339 mbedtls_sha1_context mbedtls_sha1;
12340
12341 mbedtls_md5_init( &mbedtls_md5 );
12342 mbedtls_sha1_init( &mbedtls_sha1 );
12343
12344 /*
12345 * digitally-signed struct {
12346 * opaque md5_hash[16];
12347 * opaque sha_hash[20];
12348 * };
12349 *
12350 * md5_hash
12351 * MD5(ClientHello.random + ServerHello.random
12352 * + ServerParams);
12353 * sha_hash
12354 * SHA(ClientHello.random + ServerHello.random
12355 * + ServerParams);
12356 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012357 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012358 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012360 goto exit;
12361 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012362 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012363 ssl->handshake->randbytes, 64 ) ) != 0 )
12364 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012366 goto exit;
12367 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012368 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012369 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012371 goto exit;
12372 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012373 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012374 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012376 goto exit;
12377 }
12378
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012379 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012380 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012382 goto exit;
12383 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012384 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012385 ssl->handshake->randbytes, 64 ) ) != 0 )
12386 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012388 goto exit;
12389 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012390 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012391 data_len ) ) != 0 )
12392 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012394 goto exit;
12395 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012396 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012397 output + 16 ) ) != 0 )
12398 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012400 goto exit;
12401 }
12402
12403exit:
12404 mbedtls_md5_free( &mbedtls_md5 );
12405 mbedtls_sha1_free( &mbedtls_sha1 );
12406
12407 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012408 mbedtls_ssl_pend_fatal_alert( ssl,
12409 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012410
12411 return( ret );
12412
12413}
12414#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12415 MBEDTLS_SSL_PROTO_TLS1_1 */
12416
12417#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12418 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12419int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012420 unsigned char *hash, size_t *hashlen,
12421 unsigned char *data, size_t data_len,
12422 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012423{
12424 int ret = 0;
12425 mbedtls_md_context_t ctx;
12426 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012427 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012428
12429 mbedtls_md_init( &ctx );
12430
12431 /*
12432 * digitally-signed struct {
12433 * opaque client_random[32];
12434 * opaque server_random[32];
12435 * ServerDHParams params;
12436 * };
12437 */
12438 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12439 {
12440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12441 goto exit;
12442 }
12443 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12444 {
12445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12446 goto exit;
12447 }
12448 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12449 {
12450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12451 goto exit;
12452 }
12453 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12454 {
12455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12456 goto exit;
12457 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012458 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012459 {
12460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12461 goto exit;
12462 }
12463
12464exit:
12465 mbedtls_md_free( &ctx );
12466
12467 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012468 mbedtls_ssl_pend_fatal_alert( ssl,
12469 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012470
12471 return( ret );
12472}
12473#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12474 MBEDTLS_SSL_PROTO_TLS1_2 */
12475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012476#endif /* MBEDTLS_SSL_TLS_C */