blob: 2e8a0764540246a461a93f99f2e2e22c9c98141c [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 )
1955 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1957 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA )
1958 {
1959 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001960 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001961
1962 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1963 ssl->handshake->ecdh_privkey,
1964 ssl->handshake->premaster,
1965 uecc_curve ) )
1966 {
1967 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1968 }
1969
1970 ssl->handshake->pmslen = NUM_ECC_BYTES;
1971 }
1972 else
1973#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001974#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1975 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1976 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1977 {
1978 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1979 ssl->handshake->premaster,
1980 MBEDTLS_PREMASTER_SIZE,
1981 &ssl->handshake->pmslen,
1982 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001983 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001984 {
1985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1986 return( ret );
1987 }
1988
1989 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1990 }
1991 else
1992#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001993#if defined(MBEDTLS_ECDH_C) && \
1994 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1995 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1996 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1997 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01001998 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1999 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2000 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2001 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2002 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2006 {
2007 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2008 &ssl->handshake->pmslen,
2009 ssl->handshake->premaster,
2010 MBEDTLS_MPI_MAX_SIZE,
2011 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002012 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002013 {
2014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2015 return( ret );
2016 }
2017
2018 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2019 }
2020 else
2021#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2022 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2023 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2024 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2025#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2026 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2027 {
2028 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002029 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002030 {
2031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2032 return( ret );
2033 }
2034 }
2035 else
2036#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2037#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2038 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2039 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2040 {
2041 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2042 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2043 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002044 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002045 if( ret != 0 )
2046 {
2047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2048 return( ret );
2049 }
2050 }
2051 else
2052#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2053#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2054 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2055 == MBEDTLS_KEY_EXCHANGE_RSA )
2056 {
2057 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002058 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002059 * ssl_rsa_generate_partial_pms(). Only the
2060 * PMS length needs to be set. */
2061 ssl->handshake->pmslen = 48;
2062 }
2063 else
2064#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2065 {
2066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2068 }
2069
2070 return( 0 );
2071}
2072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2074int 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 +02002075{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076 unsigned char *p = ssl->handshake->premaster;
2077 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002078 const unsigned char *psk = ssl->conf->psk;
2079 size_t psk_len = ssl->conf->psk_len;
2080
2081 /* If the psk callback was called, use its result */
2082 if( ssl->handshake->psk != NULL )
2083 {
2084 psk = ssl->handshake->psk;
2085 psk_len = ssl->handshake->psk_len;
2086 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087
2088 /*
2089 * PMS = struct {
2090 * opaque other_secret<0..2^16-1>;
2091 * opaque psk<0..2^16-1>;
2092 * };
2093 * with "other_secret" depending on the particular key exchange
2094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2096 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002098 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002100
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002101 *(p++) = (unsigned char)( psk_len >> 8 );
2102 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002103
2104 if( end < p || (size_t)( end - p ) < psk_len )
2105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2106
2107 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002108 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002109 }
2110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2112#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2113 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002114 {
2115 /*
2116 * other_secret already set by the ClientKeyExchange message,
2117 * and is 48 bytes long
2118 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002119 if( end - p < 2 )
2120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2121
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002122 *p++ = 0;
2123 *p++ = 48;
2124 p += 48;
2125 }
2126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2128#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2129 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002130 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002131 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002132 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002134 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002136 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002137 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002138 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002141 return( ret );
2142 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002143 *(p++) = (unsigned char)( len >> 8 );
2144 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145 p += len;
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002148 }
2149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2151#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2152 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002153 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002154 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 size_t zlen;
2156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002158 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002159 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002160 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002163 return( ret );
2164 }
2165
2166 *(p++) = (unsigned char)( zlen >> 8 );
2167 *(p++) = (unsigned char)( zlen );
2168 p += zlen;
2169
Janos Follath3fbdada2018-08-15 10:26:53 +01002170 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2171 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002172 }
2173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002178 }
2179
2180 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181 if( end - p < 2 )
2182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002183
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002184 *(p++) = (unsigned char)( psk_len >> 8 );
2185 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002186
2187 if( end < p || (size_t)( end - p ) < psk_len )
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002190 memcpy( p, psk, psk_len );
2191 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192
2193 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2194
2195 return( 0 );
2196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002200/*
2201 * SSLv3.0 MAC functions
2202 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002203#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002204static void ssl_mac( mbedtls_md_context_t *md_ctx,
2205 const unsigned char *secret,
2206 const unsigned char *buf, size_t len,
2207 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002208 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209{
2210 unsigned char header[11];
2211 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002212 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2214 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002215
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002216 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002218 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002219 else
Paul Bakker68884e32013-01-07 18:20:04 +01002220 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 memcpy( header, ctr, 8 );
2223 header[ 8] = (unsigned char) type;
2224 header[ 9] = (unsigned char)( len >> 8 );
2225 header[10] = (unsigned char)( len );
2226
Paul Bakker68884e32013-01-07 18:20:04 +01002227 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 mbedtls_md_starts( md_ctx );
2229 mbedtls_md_update( md_ctx, secret, md_size );
2230 mbedtls_md_update( md_ctx, padding, padlen );
2231 mbedtls_md_update( md_ctx, header, 11 );
2232 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002233 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Paul Bakker68884e32013-01-07 18:20:04 +01002235 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_starts( md_ctx );
2237 mbedtls_md_update( md_ctx, secret, md_size );
2238 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002239 mbedtls_md_update( md_ctx, out, md_size );
2240 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002243
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002244/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002245 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002246#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002247 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2248 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2249 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2250/* This function makes sure every byte in the memory region is accessed
2251 * (in ascending addresses order) */
2252static void ssl_read_memory( unsigned char *p, size_t len )
2253{
2254 unsigned char acc = 0;
2255 volatile unsigned char force;
2256
2257 for( ; len != 0; p++, len-- )
2258 acc ^= *p;
2259
2260 force = acc;
2261 (void) force;
2262}
2263#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2264
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002265/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002267 */
Hanno Becker3307b532017-12-27 21:37:21 +00002268
Hanno Beckera5a2b082019-05-15 14:03:01 +01002269#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002270/* This functions transforms a DTLS plaintext fragment and a record content
2271 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002272 *
2273 * struct {
2274 * opaque content[DTLSPlaintext.length];
2275 * ContentType real_type;
2276 * uint8 zeros[length_of_padding];
2277 * } DTLSInnerPlaintext;
2278 *
2279 * Input:
2280 * - `content`: The beginning of the buffer holding the
2281 * plaintext to be wrapped.
2282 * - `*content_size`: The length of the plaintext in Bytes.
2283 * - `max_len`: The number of Bytes available starting from
2284 * `content`. This must be `>= *content_size`.
2285 * - `rec_type`: The desired record content type.
2286 *
2287 * Output:
2288 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2289 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2290 *
2291 * Returns:
2292 * - `0` on success.
2293 * - A negative error code if `max_len` didn't offer enough space
2294 * for the expansion.
2295 */
2296static int ssl_cid_build_inner_plaintext( unsigned char *content,
2297 size_t *content_size,
2298 size_t remaining,
2299 uint8_t rec_type )
2300{
2301 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002302 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2303 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2304 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002305
2306 /* Write real content type */
2307 if( remaining == 0 )
2308 return( -1 );
2309 content[ len ] = rec_type;
2310 len++;
2311 remaining--;
2312
2313 if( remaining < pad )
2314 return( -1 );
2315 memset( content + len, 0, pad );
2316 len += pad;
2317 remaining -= pad;
2318
2319 *content_size = len;
2320 return( 0 );
2321}
2322
Hanno Becker7dc25772019-05-20 15:08:01 +01002323/* This function parses a DTLSInnerPlaintext structure.
2324 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002325static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2326 size_t *content_size,
2327 uint8_t *rec_type )
2328{
2329 size_t remaining = *content_size;
2330
2331 /* Determine length of padding by skipping zeroes from the back. */
2332 do
2333 {
2334 if( remaining == 0 )
2335 return( -1 );
2336 remaining--;
2337 } while( content[ remaining ] == 0 );
2338
2339 *content_size = remaining;
2340 *rec_type = content[ remaining ];
2341
2342 return( 0 );
2343}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002345
Hanno Becker99abf512019-05-20 14:50:53 +01002346/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002347 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002348static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002349 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002350 mbedtls_record *rec )
2351{
Hanno Becker99abf512019-05-20 14:50:53 +01002352 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002353 *
2354 * additional_data = seq_num + TLSCompressed.type +
2355 * TLSCompressed.version + TLSCompressed.length;
2356 *
Hanno Becker99abf512019-05-20 14:50:53 +01002357 * For the CID extension, this is extended as follows
2358 * (quoting draft-ietf-tls-dtls-connection-id-05,
2359 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002360 *
2361 * additional_data = seq_num + DTLSPlaintext.type +
2362 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002363 * cid +
2364 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002365 * length_of_DTLSInnerPlaintext;
2366 */
2367
Hanno Becker3307b532017-12-27 21:37:21 +00002368 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2369 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002370 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002371
Hanno Beckera5a2b082019-05-15 14:03:01 +01002372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002373 if( rec->cid_len != 0 )
2374 {
2375 memcpy( add_data + 11, rec->cid, rec->cid_len );
2376 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2377 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2378 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2379 *add_data_len = 13 + 1 + rec->cid_len;
2380 }
2381 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002382#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002383 {
2384 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2385 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2386 *add_data_len = 13;
2387 }
Hanno Becker3307b532017-12-27 21:37:21 +00002388}
2389
Hanno Becker611a83b2018-01-03 14:27:32 +00002390int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2391 mbedtls_ssl_transform *transform,
2392 mbedtls_record *rec,
2393 int (*f_rng)(void *, unsigned char *, size_t),
2394 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002397 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002398 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002399 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002401 size_t post_avail;
2402
2403 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002404#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002405 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002406 ((void) ssl);
2407#endif
2408
2409 /* The PRNG is used for dynamic IV generation that's used
2410 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2411#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2412 ( defined(MBEDTLS_AES_C) || \
2413 defined(MBEDTLS_ARIA_C) || \
2414 defined(MBEDTLS_CAMELLIA_C) ) && \
2415 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2416 ((void) f_rng);
2417 ((void) p_rng);
2418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker3307b532017-12-27 21:37:21 +00002422 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002423 {
Hanno Becker3307b532017-12-27 21:37:21 +00002424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2426 }
Hanno Becker505089d2019-05-01 09:45:57 +01002427 if( rec == NULL
2428 || rec->buf == NULL
2429 || rec->buf_len < rec->data_offset
2430 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002431#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002432 || rec->cid_len != 0
2433#endif
2434 )
Hanno Becker3307b532017-12-27 21:37:21 +00002435 {
2436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002438 }
2439
Hanno Becker3307b532017-12-27 21:37:21 +00002440 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002441 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002443 data, rec->data_len );
2444
2445 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2446
2447 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2450 (unsigned) rec->data_len,
2451 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2453 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002454
Hanno Beckera5a2b082019-05-15 14:03:01 +01002455#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002456 /*
2457 * Add CID information
2458 */
2459 rec->cid_len = transform->out_cid_len;
2460 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2461 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002462
2463 if( rec->cid_len != 0 )
2464 {
2465 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002466 * Wrap plaintext into DTLSInnerPlaintext structure.
2467 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002468 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002469 * Note that this changes `rec->data_len`, and hence
2470 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002471 */
2472 if( ssl_cid_build_inner_plaintext( data,
2473 &rec->data_len,
2474 post_avail,
2475 rec->type ) != 0 )
2476 {
2477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2478 }
2479
2480 rec->type = MBEDTLS_SSL_MSG_CID;
2481 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002483
Hanno Becker92c930f2019-04-29 17:31:37 +01002484 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002487 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002489#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 if( mode == MBEDTLS_MODE_STREAM ||
2491 ( mode == MBEDTLS_MODE_CBC
2492#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002493 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002494#endif
2495 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 {
Hanno Becker3307b532017-12-27 21:37:21 +00002497 if( post_avail < transform->maclen )
2498 {
2499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2500 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2501 }
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002504 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2505 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002506 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002507 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002508 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2509 data, rec->data_len, rec->ctr, rec->type, mac );
2510 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002511 }
2512 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2515 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002516 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2517 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002518 {
Hanno Becker992b6872017-11-09 18:57:39 +00002519 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2520
Hanno Beckere83efe62019-04-29 13:52:53 +01002521 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002522
Hanno Becker3307b532017-12-27 21:37:21 +00002523 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002524 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002525 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2526 data, rec->data_len );
2527 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2528 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2529
2530 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 }
2532 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002537 }
2538
Hanno Becker3307b532017-12-27 21:37:21 +00002539 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2540 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002541
Hanno Becker3307b532017-12-27 21:37:21 +00002542 rec->data_len += transform->maclen;
2543 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002545 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002546#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002548 /*
2549 * Encrypt
2550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2552 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002554 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002555 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002557 "including %d bytes of padding",
2558 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Becker3307b532017-12-27 21:37:21 +00002560 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2561 transform->iv_enc, transform->ivlen,
2562 data, rec->data_len,
2563 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002566 return( ret );
2567 }
2568
Hanno Becker3307b532017-12-27 21:37:21 +00002569 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakker68884e32013-01-07 18:20:04 +01002575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002578#if defined(MBEDTLS_GCM_C) || \
2579 defined(MBEDTLS_CCM_C) || \
2580 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002582 mode == MBEDTLS_MODE_CCM ||
2583 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002584 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002585 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002586 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002587 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002588
Hanno Becker3307b532017-12-27 21:37:21 +00002589 /* Check that there's space for both the authentication tag
2590 * and the explicit IV before and after the record content. */
2591 if( post_avail < transform->taglen ||
2592 rec->data_offset < explicit_iv_len )
2593 {
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2595 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2596 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597
Paul Bakker68884e32013-01-07 18:20:04 +01002598 /*
2599 * Generate IV
2600 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2602 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002603 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002605 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2606 explicit_iv_len );
2607 /* Prefix record content with explicit IV. */
2608 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 }
2610 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2611 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002612 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 unsigned char i;
2614
2615 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2616
2617 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002618 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 }
2620 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002621 {
2622 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002625 }
2626
Hanno Beckere83efe62019-04-29 13:52:53 +01002627 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2630 iv, transform->ivlen );
2631 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002632 data - explicit_iv_len, explicit_iv_len );
2633 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002634 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002637 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002638
Paul Bakker68884e32013-01-07 18:20:04 +01002639 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002640 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002641 */
Hanno Becker3307b532017-12-27 21:37:21 +00002642
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002643 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002644 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002645 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002646 data, rec->data_len, /* source */
2647 data, &rec->data_len, /* destination */
2648 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002651 return( ret );
2652 }
2653
Hanno Becker3307b532017-12-27 21:37:21 +00002654 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2655 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002656
Hanno Becker3307b532017-12-27 21:37:21 +00002657 rec->data_len += transform->taglen + explicit_iv_len;
2658 rec->data_offset -= explicit_iv_len;
2659 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002660 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2664#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002665 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002668 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002669 size_t padlen, i;
2670 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002671
Hanno Becker3307b532017-12-27 21:37:21 +00002672 /* Currently we're always using minimal padding
2673 * (up to 255 bytes would be allowed). */
2674 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2675 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 padlen = 0;
2677
Hanno Becker3307b532017-12-27 21:37:21 +00002678 /* Check there's enough space in the buffer for the padding. */
2679 if( post_avail < padlen + 1 )
2680 {
2681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2682 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2683 }
2684
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002686 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Becker3307b532017-12-27 21:37:21 +00002688 rec->data_len += padlen + 1;
2689 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002692 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002693 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2694 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002695 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002696 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2697 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002698 {
Hanno Becker3307b532017-12-27 21:37:21 +00002699 if( f_rng == NULL )
2700 {
2701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2703 }
2704
2705 if( rec->data_offset < transform->ivlen )
2706 {
2707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2708 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2709 }
2710
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711 /*
2712 * Generate IV
2713 */
Hanno Becker3307b532017-12-27 21:37:21 +00002714 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002715 if( ret != 0 )
2716 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002717
Hanno Becker3307b532017-12-27 21:37:21 +00002718 memcpy( data - transform->ivlen, transform->iv_enc,
2719 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002720
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002721 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002725 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002726 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002727 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Hanno Becker3307b532017-12-27 21:37:21 +00002729 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2730 transform->iv_enc,
2731 transform->ivlen,
2732 data, rec->data_len,
2733 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002736 return( ret );
2737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002738
Hanno Becker3307b532017-12-27 21:37:21 +00002739 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002746 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2747 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002748 {
2749 /*
2750 * Save IV in SSL3 and TLS1
2751 */
Hanno Becker3307b532017-12-27 21:37:21 +00002752 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2753 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
Hanno Becker3307b532017-12-27 21:37:21 +00002755 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002756#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002757 {
2758 data -= transform->ivlen;
2759 rec->data_offset -= transform->ivlen;
2760 rec->data_len += transform->ivlen;
2761 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002764 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002766 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768 /*
2769 * MAC(MAC_write_key, seq_num +
2770 * TLSCipherText.type +
2771 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002772 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 * IV + // except for TLS 1.0
2774 * ENC(content + padding + padding_length));
2775 */
Hanno Becker3307b532017-12-27 21:37:21 +00002776
2777 if( post_avail < transform->maclen)
2778 {
2779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2780 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2781 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002786 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002788
Hanno Becker3307b532017-12-27 21:37:21 +00002789 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002790 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002791 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2792 data, rec->data_len );
2793 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2794 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002795
Hanno Becker3307b532017-12-27 21:37:21 +00002796 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002797
Hanno Becker3307b532017-12-27 21:37:21 +00002798 rec->data_len += transform->maclen;
2799 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002800 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002804 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002806 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002812 /* Make extra sure authentication was performed, exactly once */
2813 if( auth_done != 1 )
2814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002817 }
2818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821 return( 0 );
2822}
2823
Hanno Becker40478be2019-07-12 08:23:59 +01002824int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002825 mbedtls_ssl_transform *transform,
2826 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002827{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002828 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002830 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002831#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002832 size_t padlen = 0, correct = 1;
2833#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002834 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002835 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002836 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002837
Hanno Becker611a83b2018-01-03 14:27:32 +00002838#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002839 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 ((void) ssl);
2841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 if( rec == NULL ||
2845 rec->buf == NULL ||
2846 rec->buf_len < rec->data_offset ||
2847 rec->buf_len - rec->data_offset < rec->data_len )
2848 {
2849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002851 }
2852
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 data = rec->buf + rec->data_offset;
2854 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Hanno Beckera5a2b082019-05-15 14:03:01 +01002856#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002857 /*
2858 * Match record's CID with incoming CID.
2859 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002860 if( rec->cid_len != transform->in_cid_len ||
2861 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2862 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002863 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2868 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002869 {
2870 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002871 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2872 transform->iv_dec,
2873 transform->ivlen,
2874 data, rec->data_len,
2875 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002878 return( ret );
2879 }
2880
Hanno Becker4c6876b2017-12-27 21:28:58 +00002881 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 }
Paul Bakker68884e32013-01-07 18:20:04 +01002887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002889#if defined(MBEDTLS_GCM_C) || \
2890 defined(MBEDTLS_CCM_C) || \
2891 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002893 mode == MBEDTLS_MODE_CCM ||
2894 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002895 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002896 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002897 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002898
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002899 /*
2900 * Prepare IV from explicit and implicit data.
2901 */
2902
2903 /* Check that there's enough space for the explicit IV
2904 * (at the beginning of the record) and the MAC (at the
2905 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002906 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002909 "+ taglen (%d)", rec->data_len,
2910 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002912 }
Paul Bakker68884e32013-01-07 18:20:04 +01002913
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002914#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002915 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2916 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002917 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002918
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002919 /* Fixed */
2920 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2921 /* Explicit */
2922 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002923 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002924 else
2925#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2926#if defined(MBEDTLS_CHACHAPOLY_C)
2927 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002928 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002929 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002930 unsigned char i;
2931
2932 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2933
2934 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002936 }
2937 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002938#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002939 {
2940 /* Reminder if we ever add an AEAD mode with a different size */
2941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2943 }
2944
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002945 /* Group changes to data, data_len, and add_data, because
2946 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 data += explicit_iv_len;
2948 rec->data_offset += explicit_iv_len;
2949 rec->data_len -= explicit_iv_len + transform->taglen;
2950
Hanno Beckere83efe62019-04-29 13:52:53 +01002951 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002952 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002953 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002954
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002955 /* Because of the check above, we know that there are
2956 * explicit_iv_len Bytes preceeding data, and taglen
2957 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002958 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002959 * mbedtls_cipher_auth_decrypt() below. */
2960
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002961 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002962 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002963 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002964
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002965 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002966 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002967 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002968 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2969 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002970 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002971 data, rec->data_len,
2972 data, &olen,
2973 data + rec->data_len,
2974 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2979 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002980
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002981 return( ret );
2982 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002983 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002984
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002985 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002986 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002990 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2994#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002995 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002998 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002999
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 /*
Paul Bakker45829992013-01-03 14:52:21 +01003001 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003004 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3005 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003006 {
3007 /* The ciphertext is prefixed with the CBC IV. */
3008 minlen += transform->ivlen;
3009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010#endif
Paul Bakker45829992013-01-03 14:52:21 +01003011
Hanno Becker4c6876b2017-12-27 21:28:58 +00003012 /* Size considerations:
3013 *
3014 * - The CBC cipher text must not be empty and hence
3015 * at least of size transform->ivlen.
3016 *
3017 * Together with the potential IV-prefix, this explains
3018 * the first of the two checks below.
3019 *
3020 * - The record must contain a MAC, either in plain or
3021 * encrypted, depending on whether Encrypt-then-MAC
3022 * is used or not.
3023 * - If it is, the message contains the IV-prefix,
3024 * the CBC ciphertext, and the MAC.
3025 * - If it is not, the padded plaintext, and hence
3026 * the CBC ciphertext, has at least length maclen + 1
3027 * because there is at least the padding length byte.
3028 *
3029 * As the CBC ciphertext is not empty, both cases give the
3030 * lower bound minlen + maclen + 1 on the record size, which
3031 * we test for in the second check below.
3032 */
3033 if( rec->data_len < minlen + transform->ivlen ||
3034 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003037 "+ 1 ) ( + expl IV )", rec->data_len,
3038 transform->ivlen,
3039 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003041 }
3042
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003043 /*
3044 * Authenticate before decrypt if enabled
3045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003048 {
Hanno Becker992b6872017-11-09 18:57:39 +00003049 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003052
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003053 /* Update data_len in tandem with add_data.
3054 *
3055 * The subtraction is safe because of the previous check
3056 * data_len >= minlen + maclen + 1.
3057 *
3058 * Afterwards, we know that data + data_len is followed by at
3059 * least maclen Bytes, which justifies the call to
3060 * mbedtls_ssl_safer_memcmp() below.
3061 *
3062 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003064 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003065
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003066 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003067 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3068 add_data_len );
3069 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3070 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003071 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3072 data, rec->data_len );
3073 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3074 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003075
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3077 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003078 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003079 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003080
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003081 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003082 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3083 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003087 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003088 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003091
3092 /*
3093 * Check length sanity
3094 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003095
3096 /* We know from above that data_len > minlen >= 0,
3097 * so the following check in particular implies that
3098 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003099 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104 }
3105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003107 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003108 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003109 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003110 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3111 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003112 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003113 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003114 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003115
Hanno Becker4c6876b2017-12-27 21:28:58 +00003116 data += transform->ivlen;
3117 rec->data_offset += transform->ivlen;
3118 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003119 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003121
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003122 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3123
Hanno Becker4c6876b2017-12-27 21:28:58 +00003124 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3125 transform->iv_dec, transform->ivlen,
3126 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003129 return( ret );
3130 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003131
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003132 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003137 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003140 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3141 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003142 {
3143 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003144 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3145 * records is equivalent to CBC decryption of the concatenation
3146 * of the records; in other words, IVs are maintained across
3147 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003148 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003149 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3150 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Hanno Becker4c6876b2017-12-27 21:28:58 +00003154 /* Safe since data_len >= minlen + maclen + 1, so after having
3155 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003156 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3157 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003158 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003159
Hanno Becker4c6876b2017-12-27 21:28:58 +00003160 if( auth_done == 1 )
3161 {
3162 correct *= ( rec->data_len >= padlen + 1 );
3163 padlen *= ( rec->data_len >= padlen + 1 );
3164 }
3165 else
Paul Bakker45829992013-01-03 14:52:21 +01003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003168 if( rec->data_len < transform->maclen + padlen + 1 )
3169 {
3170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3171 rec->data_len,
3172 transform->maclen,
3173 padlen + 1 ) );
3174 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003175#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176
3177 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3178 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 padlen++;
3182
3183 /* Regardless of the validity of the padding,
3184 * we have data_len >= padlen here. */
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003187 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3188 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003190 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_DEBUG_ALL)
3193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003194 "should be no more than %d",
3195 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003196#endif
Paul Bakker45829992013-01-03 14:52:21 +01003197 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 }
3199 }
3200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3202#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3203 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003204 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3205 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 /* The padding check involves a series of up to 256
3208 * consecutive memory reads at the end of the record
3209 * plaintext buffer. In order to hide the length and
3210 * validity of the padding, always perform exactly
3211 * `min(256,plaintext_len)` reads (but take into account
3212 * only the last `padlen` bytes for the padding check). */
3213 size_t pad_count = 0;
3214 size_t real_count = 0;
3215 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003216
Hanno Becker4c6876b2017-12-27 21:28:58 +00003217 /* Index of first padding byte; it has been ensured above
3218 * that the subtraction is safe. */
3219 size_t const padding_idx = rec->data_len - padlen;
3220 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3221 size_t const start_idx = rec->data_len - num_checks;
3222 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003223
Hanno Becker4c6876b2017-12-27 21:28:58 +00003224 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003225 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003226 real_count |= ( idx >= padding_idx );
3227 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003228 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003229 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003232 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003234#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003235 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3239 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003243 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003244
Hanno Becker4c6876b2017-12-27 21:28:58 +00003245 /* If the padding was found to be invalid, padlen == 0
3246 * and the subtraction is safe. If the padding was found valid,
3247 * padlen hasn't been changed and the previous assertion
3248 * data_len >= padlen still holds. */
3249 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003251 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003253 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003259#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003261 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
3264 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003265 * Authenticate if not done yet.
3266 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003269 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003270 {
Hanno Becker992b6872017-11-09 18:57:39 +00003271 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003272
Hanno Becker4c6876b2017-12-27 21:28:58 +00003273 /* If the initial value of padlen was such that
3274 * data_len < maclen + padlen + 1, then padlen
3275 * got reset to 1, and the initial check
3276 * data_len >= minlen + maclen + 1
3277 * guarantees that at this point we still
3278 * have at least data_len >= maclen.
3279 *
3280 * If the initial value of padlen was such that
3281 * data_len >= maclen + padlen + 1, then we have
3282 * subtracted either padlen + 1 (if the padding was correct)
3283 * or 0 (if the padding was incorrect) since then,
3284 * hence data_len >= maclen in any case.
3285 */
3286 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003287 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003290 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3291 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003292 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003293 ssl_mac( &transform->md_ctx_dec,
3294 transform->mac_dec,
3295 data, rec->data_len,
3296 rec->ctr, rec->type,
3297 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003298 }
3299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3301#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3302 defined(MBEDTLS_SSL_PROTO_TLS1_2)
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 {
3306 /*
3307 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003308 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003309 *
3310 * Known timing attacks:
3311 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3312 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003313 * To compensate for different timings for the MAC calculation
3314 * depending on how much padding was removed (which is determined
3315 * by padlen), process extra_run more blocks through the hash
3316 * function.
3317 *
3318 * The formula in the paper is
3319 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3320 * where L1 is the size of the header plus the decrypted message
3321 * plus CBC padding and L2 is the size of the header plus the
3322 * decrypted message. This is for an underlying hash function
3323 * with 64-byte blocks.
3324 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3325 * correctly. We round down instead of up, so -56 is the correct
3326 * value for our calculations instead of -55.
3327 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003328 * Repeat the formula rather than defining a block_size variable.
3329 * This avoids requiring division by a variable at runtime
3330 * (which would be marginally less efficient and would require
3331 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003332 */
3333 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003335
3336 /*
3337 * The next two sizes are the minimum and maximum values of
3338 * in_msglen over all padlen values.
3339 *
3340 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003341 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003342 *
3343 * Note that max_len + maclen is never more than the buffer
3344 * length, as we previously did in_msglen -= maclen too.
3345 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003346 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003347 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3348
Hanno Becker4c6876b2017-12-27 21:28:58 +00003349 memset( tmp, 0, sizeof( tmp ) );
3350
3351 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003352 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003353#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3354 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003355 case MBEDTLS_MD_MD5:
3356 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003357 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003358 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003359 extra_run =
3360 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3361 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003362 break;
3363#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003364#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003365 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003366 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003367 extra_run =
3368 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3369 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 break;
3371#endif
3372 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3375 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003376
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003377 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003378
Hanno Beckere83efe62019-04-29 13:52:53 +01003379 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3380 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003381 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3382 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003383 /* Make sure we access everything even when padlen > 0. This
3384 * makes the synchronisation requirements for just-in-time
3385 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003386 ssl_read_memory( data + rec->data_len, padlen );
3387 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003388
3389 /* Call mbedtls_md_process at least once due to cache attacks
3390 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003391 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003392 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003393
Hanno Becker4c6876b2017-12-27 21:28:58 +00003394 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003395
3396 /* Make sure we access all the memory that could contain the MAC,
3397 * before we check it in the next code block. This makes the
3398 * synchronisation requirements for just-in-time Prime+Probe
3399 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003400 ssl_read_memory( data + min_len,
3401 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003402 }
3403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3405 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003411#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3413 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003414#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003415
Hanno Becker4c6876b2017-12-27 21:28:58 +00003416 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3417 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_DEBUG_ALL)
3420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003421#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 correct = 0;
3423 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003424 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003425 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003426
3427 /*
3428 * Finally check the correct flag
3429 */
3430 if( correct == 0 )
3431 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003432#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003433
3434 /* Make extra sure authentication was performed, exactly once */
3435 if( auth_done != 1 )
3436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
Hanno Beckera5a2b082019-05-15 14:03:01 +01003441#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003442 if( rec->cid_len != 0 )
3443 {
3444 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3445 &rec->type );
3446 if( ret != 0 )
3447 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3448 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003449#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
3453 return( 0 );
3454}
3455
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003456#undef MAC_NONE
3457#undef MAC_PLAINTEXT
3458#undef MAC_CIPHERTEXT
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461/*
3462 * Compression/decompression functions
3463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465{
3466 int ret;
3467 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003468 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003470 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003474 if( len_pre == 0 )
3475 return( 0 );
3476
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477 memcpy( msg_pre, ssl->out_msg, len_pre );
3478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003480 ssl->out_msglen ) );
3481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003483 ssl->out_msg, ssl->out_msglen );
3484
Paul Bakker48916f92012-09-16 19:57:18 +00003485 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3486 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3487 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003488 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489
Paul Bakker48916f92012-09-16 19:57:18 +00003490 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003491 if( ret != Z_OK )
3492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3494 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 }
3496
Angus Grattond8213d02016-05-25 20:56:48 +10003497 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003498 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501 ssl->out_msglen ) );
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504 ssl->out_msg, ssl->out_msglen );
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507
3508 return( 0 );
3509}
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512{
3513 int ret;
3514 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003515 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003517 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003521 if( len_pre == 0 )
3522 return( 0 );
3523
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 memcpy( msg_pre, ssl->in_msg, len_pre );
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003527 ssl->in_msglen ) );
3528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003530 ssl->in_msg, ssl->in_msglen );
3531
Paul Bakker48916f92012-09-16 19:57:18 +00003532 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3533 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3534 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003535 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003536 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537
Paul Bakker48916f92012-09-16 19:57:18 +00003538 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539 if( ret != Z_OK )
3540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3542 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 }
3544
Angus Grattond8213d02016-05-25 20:56:48 +10003545 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003546 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549 ssl->in_msglen ) );
3550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 ssl->in_msg, ssl->in_msglen );
3553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555
3556 return( 0 );
3557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3561static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_SSL_PROTO_DTLS)
3564static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003565{
3566 /* If renegotiation is not enforced, retransmit until we would reach max
3567 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003568 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003569 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003570 uint32_t ratio =
3571 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3572 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003573 unsigned char doublings = 1;
3574
3575 while( ratio != 0 )
3576 {
3577 ++doublings;
3578 ratio >>= 1;
3579 }
3580
3581 if( ++ssl->renego_records_seen > doublings )
3582 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003584 return( 0 );
3585 }
3586 }
3587
3588 return( ssl_write_hello_request( ssl ) );
3589}
3590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003594 * Fill the input message buffer by appending data to it.
3595 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003596 *
3597 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3598 * available (from this read and/or a previous one). Otherwise, an error code
3599 * is returned (possibly EOF or WANT_READ).
3600 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003601 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3602 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3603 * since we always read a whole datagram at once.
3604 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003605 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003606 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609{
Paul Bakker23986e52011-04-24 08:57:21 +00003610 int ret;
3611 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Hanno Beckera58a8962019-06-13 16:11:15 +01003615 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3616 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003619 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003621 }
3622
Angus Grattond8213d02016-05-25 20:56:48 +10003623 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003627 }
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003630 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003632 uint32_t timeout;
3633
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003634 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003635 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3636 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003637 {
3638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3639 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3641 }
3642
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003643 /*
3644 * The point is, we need to always read a full datagram at once, so we
3645 * sometimes read more then requested, and handle the additional data.
3646 * It could be the rest of the current record (while fetching the
3647 * header) and/or some other records in the same datagram.
3648 */
3649
3650 /*
3651 * Move to the next record in the already read datagram if applicable
3652 */
3653 if( ssl->next_record_offset != 0 )
3654 {
3655 if( ssl->in_left < ssl->next_record_offset )
3656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003659 }
3660
3661 ssl->in_left -= ssl->next_record_offset;
3662
3663 if( ssl->in_left != 0 )
3664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003666 ssl->next_record_offset ) );
3667 memmove( ssl->in_hdr,
3668 ssl->in_hdr + ssl->next_record_offset,
3669 ssl->in_left );
3670 }
3671
3672 ssl->next_record_offset = 0;
3673 }
3674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003677
3678 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003680 */
3681 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003684 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003685 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003686
3687 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003688 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003689 * are not at the beginning of a new record, the caller did something
3690 * wrong.
3691 */
3692 if( ssl->in_left != 0 )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003696 }
3697
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003698 /*
3699 * Don't even try to read if time's out already.
3700 * This avoids by-passing the timer when repeatedly receiving messages
3701 * that will end up being dropped.
3702 */
3703 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003704 {
3705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003706 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003707 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003708 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003709 {
Angus Grattond8213d02016-05-25 20:56:48 +10003710 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003713 timeout = ssl->handshake->retransmit_timeout;
3714 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003715 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003718
Hanno Beckera58a8962019-06-13 16:11:15 +01003719 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3720 {
3721 ret = mbedtls_ssl_get_recv_timeout( ssl )
3722 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3723 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003724 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003725 {
3726 ret = mbedtls_ssl_get_recv( ssl )
3727 ( ssl->p_bio, ssl->in_hdr, len );
3728 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
3732 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003734 }
3735
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003736 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003739 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003742 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003743 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003746 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003747 }
3748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003752 return( ret );
3753 }
3754
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003755 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003758 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3759 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003761 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003762 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003765 return( ret );
3766 }
3767
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003768 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003771 }
3772
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 if( ret < 0 )
3774 return( ret );
3775
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003776 ssl->in_left = ret;
3777 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003778 MBEDTLS_SSL_TRANSPORT_ELSE
3779#endif /* MBEDTLS_SSL_PROTO_DTLS */
3780#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003783 ssl->in_left, nb_want ) );
3784
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 while( ssl->in_left < nb_want )
3786 {
3787 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003788
3789 if( ssl_check_timer( ssl ) != 0 )
3790 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3791 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003792 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003793 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003794 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003795 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3796 ssl->in_hdr + ssl->in_left, len,
3797 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003798 }
3799 else
3800 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003801 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003802 ssl->in_hdr + ssl->in_left, len );
3803 }
3804 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003807 ssl->in_left, nb_want ) );
3808 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003809
3810 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003812
3813 if( ret < 0 )
3814 return( ret );
3815
mohammad160352aecb92018-03-28 23:41:40 -07003816 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003817 {
Darryl Green11999bb2018-03-13 15:22:58 +00003818 MBEDTLS_SSL_DEBUG_MSG( 1,
3819 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003820 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3822 }
3823
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003824 ssl->in_left += ret;
3825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003827#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003830
3831 return( 0 );
3832}
3833
3834/*
3835 * Flush any data not yet written
3836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003839 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003840 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
Hanno Beckera58a8962019-06-13 16:11:15 +01003844 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003847 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003849 }
3850
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003851 /* Avoid incrementing counter if data is flushed */
3852 if( ssl->out_left == 0 )
3853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003855 return( 0 );
3856 }
3857
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 while( ssl->out_left > 0 )
3859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003861 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
Hanno Becker2b1e3542018-08-06 11:19:13 +01003863 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003864 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
3868 if( ret <= 0 )
3869 return( ret );
3870
mohammad160352aecb92018-03-28 23:41:40 -07003871 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003872 {
Darryl Green11999bb2018-03-13 15:22:58 +00003873 MBEDTLS_SSL_DEBUG_MSG( 1,
3874 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003875 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3877 }
3878
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 ssl->out_left -= ret;
3880 }
3881
Hanno Becker2b1e3542018-08-06 11:19:13 +01003882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003883 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003884 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003885 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003886 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003887 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003888#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003889#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003890 {
3891 ssl->out_hdr = ssl->out_buf + 8;
3892 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003893#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
3898 return( 0 );
3899}
3900
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901/*
3902 * Functions to handle the DTLS retransmission state machine
3903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905/*
3906 * Append current handshake message to current outgoing flight
3907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3912 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3913 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003914
3915 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003916 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003917 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003920 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921 }
3922
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003923 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003927 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 }
3929
3930 /* Copy current handshake message with headers */
3931 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3932 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003933 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 msg->next = NULL;
3935
3936 /* Append to the current flight */
3937 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003938 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003939 else
3940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942 while( cur->next != NULL )
3943 cur = cur->next;
3944 cur->next = msg;
3945 }
3946
Hanno Becker3b235902018-08-06 09:54:53 +01003947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 return( 0 );
3949}
3950
3951/*
3952 * Free the current flight of handshake messages
3953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 mbedtls_ssl_flight_item *cur = flight;
3957 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003958
3959 while( cur != NULL )
3960 {
3961 next = cur->next;
3962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 mbedtls_free( cur->p );
3964 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965
3966 cur = next;
3967 }
3968}
3969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3971static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003972#endif
3973
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003974/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975 * Swap transform_out and out_ctr with the alternative ones
3976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980 unsigned char tmp_out_ctr[8];
3981
3982 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003985 return;
3986 }
3987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003989
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003990 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003991 tmp_transform = ssl->transform_out;
3992 ssl->transform_out = ssl->handshake->alt_transform_out;
3993 ssl->handshake->alt_transform_out = tmp_transform;
3994
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003995 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003996 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3997 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003999
4000 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004001 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4004 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4009 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004010 }
4011 }
4012#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004013}
4014
4015/*
4016 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004017 */
4018int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4019{
4020 int ret = 0;
4021
4022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4023
4024 ret = mbedtls_ssl_flight_transmit( ssl );
4025
4026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4027
4028 return( ret );
4029}
4030
4031/*
4032 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 *
4034 * Need to remember the current message in case flush_output returns
4035 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004036 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004038int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004040 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046
4047 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004048 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049 ssl_swap_epochs( ssl );
4050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004052 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053
4054 while( ssl->handshake->cur_msg != NULL )
4055 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004056 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004057 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004058
Hanno Beckere1dcb032018-08-17 16:47:58 +01004059 int const is_finished =
4060 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4061 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4062
Hanno Becker04da1892018-08-14 13:22:10 +01004063 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4064 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4065
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004066 /* Swap epochs before sending Finished: we can't do it after
4067 * sending ChangeCipherSpec, in case write returns WANT_READ.
4068 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004069 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004070 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004072 ssl_swap_epochs( ssl );
4073 }
4074
Hanno Becker67bc7c32018-08-06 11:33:50 +01004075 ret = ssl_get_remaining_payload_in_datagram( ssl );
4076 if( ret < 0 )
4077 return( ret );
4078 max_frag_len = (size_t) ret;
4079
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004080 /* CCS is copied as is, while HS messages may need fragmentation */
4081 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4082 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004083 if( max_frag_len == 0 )
4084 {
4085 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4086 return( ret );
4087
4088 continue;
4089 }
4090
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004091 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004092 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004093 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004094
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004095 /* Update position inside current message */
4096 ssl->handshake->cur_msg_p += cur->len;
4097 }
4098 else
4099 {
4100 const unsigned char * const p = ssl->handshake->cur_msg_p;
4101 const size_t hs_len = cur->len - 12;
4102 const size_t frag_off = p - ( cur->p + 12 );
4103 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004104 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004105
Hanno Beckere1dcb032018-08-17 16:47:58 +01004106 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004107 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004108 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004109 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004110
Hanno Becker67bc7c32018-08-06 11:33:50 +01004111 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4112 return( ret );
4113
4114 continue;
4115 }
4116 max_hs_frag_len = max_frag_len - 12;
4117
4118 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4119 max_hs_frag_len : rem_len;
4120
4121 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 (unsigned) cur_hs_frag_len,
4125 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004126 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004127
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004128 /* Messages are stored with handshake headers as if not fragmented,
4129 * copy beginning of headers then fill fragmentation fields.
4130 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4131 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004132
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004133 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4134 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4135 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4136
Hanno Becker67bc7c32018-08-06 11:33:50 +01004137 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4138 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4139 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004140
4141 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4142
Hanno Becker3f7b9732018-08-28 09:53:25 +01004143 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004144 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4145 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004146 ssl->out_msgtype = cur->type;
4147
4148 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004149 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004150 }
4151
4152 /* If done with the current message move to the next one if any */
4153 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4154 {
4155 if( cur->next != NULL )
4156 {
4157 ssl->handshake->cur_msg = cur->next;
4158 ssl->handshake->cur_msg_p = cur->next->p + 12;
4159 }
4160 else
4161 {
4162 ssl->handshake->cur_msg = NULL;
4163 ssl->handshake->cur_msg_p = NULL;
4164 }
4165 }
4166
4167 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004168 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004171 return( ret );
4172 }
4173 }
4174
Hanno Becker67bc7c32018-08-06 11:33:50 +01004175 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4176 return( ret );
4177
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004178 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4180 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004181 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004184 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4185 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004186
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004188
4189 return( 0 );
4190}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004191
4192/*
4193 * To be called when the last message of an incoming flight is received.
4194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004196{
4197 /* We won't need to resend that one any more */
4198 ssl_flight_free( ssl->handshake->flight );
4199 ssl->handshake->flight = NULL;
4200 ssl->handshake->cur_msg = NULL;
4201
4202 /* The next incoming flight will start with this msg_seq */
4203 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4204
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004205 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004206 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004207
Hanno Becker0271f962018-08-16 13:23:47 +01004208 /* Clear future message buffering structure. */
4209 ssl_buffering_free( ssl );
4210
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004211 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004212 ssl_set_timer( ssl, 0 );
4213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4215 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004218 }
4219 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004221}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004222
4223/*
4224 * To be called when the last message of an outgoing flight is send.
4225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004227{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004228 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004229 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4232 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004235 }
4236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004240
Paul Bakker5121ce52009-01-03 21:22:43 +00004241/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004242 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004244
4245/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004246 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004247 *
4248 * - fill in handshake headers
4249 * - update handshake checksum
4250 * - DTLS: save message for resending
4251 * - then pass to the record layer
4252 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004253 * DTLS: except for HelloRequest, messages are only queued, and will only be
4254 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004255 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004256 * Inputs:
4257 * - ssl->out_msglen: 4 + actual handshake message len
4258 * (4 is the size of handshake headers for TLS)
4259 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4260 * - ssl->out_msg + 4: the handshake message body
4261 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004262 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004263 * - ssl->out_msglen: the length of the record contents
4264 * (including handshake headers but excluding record headers)
4265 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004268{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004269 int ret;
4270 const size_t hs_len = ssl->out_msglen - 4;
4271 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4274
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004275 /*
4276 * Sanity checks
4277 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004278 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004279 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4280 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004281 /* In SSLv3, the client might send a NoCertificate alert. */
4282#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004283 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004284 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004285 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4286 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004287#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4288 {
4289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4291 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004293
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004294 /* Whenever we send anything different from a
4295 * HelloRequest we should be in a handshake - double check. */
4296 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4297 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004298 ssl->handshake == NULL )
4299 {
4300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004305 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004308 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004311 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004312#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004313
Hanno Beckerb50a2532018-08-06 11:52:54 +01004314 /* Double-check that we did not exceed the bounds
4315 * of the outgoing record buffer.
4316 * This should never fail as the various message
4317 * writing functions must obey the bounds of the
4318 * outgoing record buffer, but better be safe.
4319 *
4320 * Note: We deliberately do not check for the MTU or MFL here.
4321 */
4322 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4323 {
4324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4325 "size %u, maximum %u",
4326 (unsigned) ssl->out_msglen,
4327 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4329 }
4330
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004331 /*
4332 * Fill handshake headers
4333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004336 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4337 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4338 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004340 /*
4341 * DTLS has additional fields in the Handshake layer,
4342 * between the length field and the actual payload:
4343 * uint16 message_seq;
4344 * uint24 fragment_offset;
4345 * uint24 fragment_length;
4346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004348 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004349 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004350 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004351 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004352 {
4353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4354 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004355 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004356 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4358 }
4359
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004360 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004361 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004362
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004363 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004364 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004365 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004366 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4367 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4368 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004369 }
4370 else
4371 {
4372 ssl->out_msg[4] = 0;
4373 ssl->out_msg[5] = 0;
4374 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004375
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004376 /* Handshake hashes are computed without fragmentation,
4377 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004378 memset( ssl->out_msg + 6, 0x00, 3 );
4379 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004382
Hanno Becker0207e532018-08-28 10:28:28 +01004383 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004384 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004385 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 }
4387
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004388 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004390 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004391 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4392 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004393 {
4394 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004397 return( ret );
4398 }
4399 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004400 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004401#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004402 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004403 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004404 {
4405 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4406 return( ret );
4407 }
4408 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004409
4410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4411
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004412 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004413}
4414
4415/*
4416 * Record layer functions
4417 */
4418
4419/*
4420 * Write current record.
4421 *
4422 * Uses:
4423 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4424 * - ssl->out_msglen: length of the record content (excl headers)
4425 * - ssl->out_msg: record content
4426 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004427int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004428{
4429 int ret, done = 0;
4430 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004432
4433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004436 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004438 {
4439 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004442 return( ret );
4443 }
4444
4445 len = ssl->out_msglen;
4446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4450 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 ret = mbedtls_ssl_hw_record_write( ssl );
4455 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4458 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004459 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004460
4461 if( ret == 0 )
4462 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004465 if( !done )
4466 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004467 unsigned i;
4468 size_t protected_record_size;
4469
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004470 /* Skip writing the record content type to after the encryption,
4471 * as it may change when using the CID extension. */
4472
Hanno Becker2881d802019-05-22 14:44:53 +01004473 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4474 mbedtls_ssl_get_minor_ver( ssl ),
4475 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004476
Hanno Becker19859472018-08-06 09:40:20 +01004477 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004478 ssl->out_len[0] = (unsigned char)( len >> 8 );
4479 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004480
Paul Bakker48916f92012-09-16 19:57:18 +00004481 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004482 {
Hanno Becker3307b532017-12-27 21:37:21 +00004483 mbedtls_record rec;
4484
4485 rec.buf = ssl->out_iv;
4486 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4487 ( ssl->out_iv - ssl->out_buf );
4488 rec.data_len = ssl->out_msglen;
4489 rec.data_offset = ssl->out_msg - rec.buf;
4490
4491 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004492 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4493 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004494 ssl->conf->transport, rec.ver );
4495 rec.type = ssl->out_msgtype;
4496
Hanno Beckera5a2b082019-05-15 14:03:01 +01004497#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004498 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004499 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004500#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004501
Hanno Becker611a83b2018-01-03 14:27:32 +00004502 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004503 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004504 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004507 return( ret );
4508 }
4509
Hanno Becker3307b532017-12-27 21:37:21 +00004510 if( rec.data_offset != 0 )
4511 {
4512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4514 }
4515
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004516 /* Update the record content type and CID. */
4517 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004518#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004519 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004521 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004522 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4523 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004524 }
4525
Hanno Becker43395762019-05-03 14:46:38 +01004526 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004527
4528#if defined(MBEDTLS_SSL_PROTO_DTLS)
4529 /* In case of DTLS, double-check that we don't exceed
4530 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004531 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004532 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004533 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004534 if( ret < 0 )
4535 return( ret );
4536
4537 if( protected_record_size > (size_t) ret )
4538 {
4539 /* Should never happen */
4540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4541 }
4542 }
4543#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004544
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004545 /* Now write the potentially updated record content type. */
4546 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004549 "version = [%d:%d], msglen = %d",
4550 ssl->out_hdr[0], ssl->out_hdr[1],
4551 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004554 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004555
4556 ssl->out_left += protected_record_size;
4557 ssl->out_hdr += protected_record_size;
4558 ssl_update_out_pointers( ssl, ssl->transform_out );
4559
Hanno Becker04484622018-08-06 09:49:38 +01004560 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4561 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4562 break;
4563
4564 /* The loop goes to its end iff the counter is wrapping */
4565 if( i == ssl_ep_len( ssl ) )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4568 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571
Hanno Becker67bc7c32018-08-06 11:33:50 +01004572#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004573 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004574 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004575 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004576 size_t remaining;
4577 ret = ssl_get_remaining_payload_in_datagram( ssl );
4578 if( ret < 0 )
4579 {
4580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4581 ret );
4582 return( ret );
4583 }
4584
4585 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004586 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004587 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004588 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004589 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004590 else
4591 {
Hanno Becker513815a2018-08-20 11:56:09 +01004592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004593 }
4594 }
4595#endif /* MBEDTLS_SSL_PROTO_DTLS */
4596
4597 if( ( flush == SSL_FORCE_FLUSH ) &&
4598 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 return( ret );
4602 }
4603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605
4606 return( 0 );
4607}
4608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004610
4611static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4612{
4613 if( ssl->in_msglen < ssl->in_hslen ||
4614 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4615 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4616 {
4617 return( 1 );
4618 }
4619 return( 0 );
4620}
Hanno Becker44650b72018-08-16 12:51:11 +01004621
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004622static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004623{
4624 return( ( ssl->in_msg[9] << 16 ) |
4625 ( ssl->in_msg[10] << 8 ) |
4626 ssl->in_msg[11] );
4627}
4628
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004629static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004630{
4631 return( ( ssl->in_msg[6] << 16 ) |
4632 ( ssl->in_msg[7] << 8 ) |
4633 ssl->in_msg[8] );
4634}
4635
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004636static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004637{
4638 uint32_t msg_len, frag_off, frag_len;
4639
4640 msg_len = ssl_get_hs_total_len( ssl );
4641 frag_off = ssl_get_hs_frag_off( ssl );
4642 frag_len = ssl_get_hs_frag_len( ssl );
4643
4644 if( frag_off > msg_len )
4645 return( -1 );
4646
4647 if( frag_len > msg_len - frag_off )
4648 return( -1 );
4649
4650 if( frag_len + 12 > ssl->in_msglen )
4651 return( -1 );
4652
4653 return( 0 );
4654}
4655
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004656/*
4657 * Mark bits in bitmask (used for DTLS HS reassembly)
4658 */
4659static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4660{
4661 unsigned int start_bits, end_bits;
4662
4663 start_bits = 8 - ( offset % 8 );
4664 if( start_bits != 8 )
4665 {
4666 size_t first_byte_idx = offset / 8;
4667
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004668 /* Special case */
4669 if( len <= start_bits )
4670 {
4671 for( ; len != 0; len-- )
4672 mask[first_byte_idx] |= 1 << ( start_bits - len );
4673
4674 /* Avoid potential issues with offset or len becoming invalid */
4675 return;
4676 }
4677
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004678 offset += start_bits; /* Now offset % 8 == 0 */
4679 len -= start_bits;
4680
4681 for( ; start_bits != 0; start_bits-- )
4682 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4683 }
4684
4685 end_bits = len % 8;
4686 if( end_bits != 0 )
4687 {
4688 size_t last_byte_idx = ( offset + len ) / 8;
4689
4690 len -= end_bits; /* Now len % 8 == 0 */
4691
4692 for( ; end_bits != 0; end_bits-- )
4693 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4694 }
4695
4696 memset( mask + offset / 8, 0xFF, len / 8 );
4697}
4698
4699/*
4700 * Check that bitmask is full
4701 */
4702static int ssl_bitmask_check( unsigned char *mask, size_t len )
4703{
4704 size_t i;
4705
4706 for( i = 0; i < len / 8; i++ )
4707 if( mask[i] != 0xFF )
4708 return( -1 );
4709
4710 for( i = 0; i < len % 8; i++ )
4711 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4712 return( -1 );
4713
4714 return( 0 );
4715}
4716
Hanno Becker56e205e2018-08-16 09:06:12 +01004717/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004718static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004719 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004720{
Hanno Becker56e205e2018-08-16 09:06:12 +01004721 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004722
Hanno Becker56e205e2018-08-16 09:06:12 +01004723 alloc_len = 12; /* Handshake header */
4724 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004725
Hanno Beckerd07df862018-08-16 09:14:58 +01004726 if( add_bitmap )
4727 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004728
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004729 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004730}
Hanno Becker56e205e2018-08-16 09:06:12 +01004731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004733
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004734static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004735{
4736 return( ( ssl->in_msg[1] << 16 ) |
4737 ( ssl->in_msg[2] << 8 ) |
4738 ssl->in_msg[3] );
4739}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004740
Simon Butcher99000142016-10-13 17:21:01 +01004741int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004746 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004748 }
4749
Hanno Becker12555c62018-08-16 12:47:53 +01004750 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004753 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004754 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004757 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004758 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004759 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004760 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004761
Hanno Becker44650b72018-08-16 12:51:11 +01004762 if( ssl_check_hs_header( ssl ) != 0 )
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004768 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004769 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4770 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4771 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4772 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004773 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004774 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4777 recv_msg_seq,
4778 ssl->handshake->in_msg_seq ) );
4779 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4780 }
4781
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004782 /* Retransmit only on last message from previous flight, to avoid
4783 * too many retransmissions.
4784 * Besides, No sane server ever retransmits HelloVerifyRequest */
4785 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004789 "message_seq = %d, start_of_flight = %d",
4790 recv_msg_seq,
4791 ssl->handshake->in_flight_start_seq ) );
4792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004796 return( ret );
4797 }
4798 }
4799 else
4800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004802 "message_seq = %d, expected = %d",
4803 recv_msg_seq,
4804 ssl->handshake->in_msg_seq ) );
4805 }
4806
Hanno Becker90333da2017-10-10 11:27:13 +01004807 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004808 }
4809 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004810
Hanno Becker6d97ef52018-08-16 13:09:04 +01004811 /* Message reassembly is handled alongside buffering of future
4812 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004813 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004814 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004815 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004818 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004819 }
4820 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004821 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004823#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004824 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004825 /* With TLS we don't handle fragmentation (for now) */
4826 if( ssl->in_msglen < ssl->in_hslen )
4827 {
4828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4829 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4830 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004831 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004832#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004833
Simon Butcher99000142016-10-13 17:21:01 +01004834 return( 0 );
4835}
4836
4837void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4838{
Hanno Becker0271f962018-08-16 13:23:47 +01004839 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004840
Hanno Becker0271f962018-08-16 13:23:47 +01004841 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004842 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004843
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004844 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004846 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004847 ssl->handshake != NULL )
4848 {
Hanno Becker0271f962018-08-16 13:23:47 +01004849 unsigned offset;
4850 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004851
Hanno Becker0271f962018-08-16 13:23:47 +01004852 /* Increment handshake sequence number */
4853 hs->in_msg_seq++;
4854
4855 /*
4856 * Clear up handshake buffering and reassembly structure.
4857 */
4858
4859 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004860 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004861
4862 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004863 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4864 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004865 offset++, hs_buf++ )
4866 {
4867 *hs_buf = *(hs_buf + 1);
4868 }
4869
4870 /* Create a fresh last entry */
4871 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004872 }
4873#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004874}
4875
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004877 * DTLS anti-replay: RFC 6347 4.1.2.6
4878 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004879 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4880 * Bit n is set iff record number in_window_top - n has been seen.
4881 *
4882 * Usually, in_window_top is the last record number seen and the lsb of
4883 * in_window is set. The only exception is the initial state (record number 0
4884 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4887static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004888{
4889 ssl->in_window_top = 0;
4890 ssl->in_window = 0;
4891}
4892
4893static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4894{
4895 return( ( (uint64_t) buf[0] << 40 ) |
4896 ( (uint64_t) buf[1] << 32 ) |
4897 ( (uint64_t) buf[2] << 24 ) |
4898 ( (uint64_t) buf[3] << 16 ) |
4899 ( (uint64_t) buf[4] << 8 ) |
4900 ( (uint64_t) buf[5] ) );
4901}
4902
4903/*
4904 * Return 0 if sequence number is acceptable, -1 otherwise
4905 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004906int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004907{
4908 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4909 uint64_t bit;
4910
Hanno Becker7f376f42019-06-12 16:20:48 +01004911 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4912 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4913 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004914 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004915 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004916
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004917 if( rec_seqnum > ssl->in_window_top )
4918 return( 0 );
4919
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004920 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004921
4922 if( bit >= 64 )
4923 return( -1 );
4924
4925 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4926 return( -1 );
4927
4928 return( 0 );
4929}
4930
4931/*
4932 * Update replay window on new validated record
4933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004935{
4936 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4937
Hanno Becker7f376f42019-06-12 16:20:48 +01004938 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4939 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4940 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004941 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004942 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004943
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004944 if( rec_seqnum > ssl->in_window_top )
4945 {
4946 /* Update window_top and the contents of the window */
4947 uint64_t shift = rec_seqnum - ssl->in_window_top;
4948
4949 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004950 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004951 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004952 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004954 ssl->in_window |= 1;
4955 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004956
4957 ssl->in_window_top = rec_seqnum;
4958 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004959 else
4960 {
4961 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004962 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004963
4964 if( bit < 64 ) /* Always true, but be extra sure */
4965 ssl->in_window |= (uint64_t) 1 << bit;
4966 }
4967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004969
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004970#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004971/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004972static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4973
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004974/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 * Without any SSL context, check if a datagram looks like a ClientHello with
4976 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004977 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004978 *
4979 * - if cookie is valid, return 0
4980 * - if ClientHello looks superficially valid but cookie is not,
4981 * fill obuf and set olen, then
4982 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4983 * - otherwise return a specific error code
4984 */
4985static int ssl_check_dtls_clihlo_cookie(
4986 mbedtls_ssl_cookie_write_t *f_cookie_write,
4987 mbedtls_ssl_cookie_check_t *f_cookie_check,
4988 void *p_cookie,
4989 const unsigned char *cli_id, size_t cli_id_len,
4990 const unsigned char *in, size_t in_len,
4991 unsigned char *obuf, size_t buf_len, size_t *olen )
4992{
4993 size_t sid_len, cookie_len;
4994 unsigned char *p;
4995
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 /*
4997 * Structure of ClientHello with record and handshake headers,
4998 * and expected values. We don't need to check a lot, more checks will be
4999 * done when actually parsing the ClientHello - skipping those checks
5000 * avoids code duplication and does not make cookie forging any easier.
5001 *
5002 * 0-0 ContentType type; copied, must be handshake
5003 * 1-2 ProtocolVersion version; copied
5004 * 3-4 uint16 epoch; copied, must be 0
5005 * 5-10 uint48 sequence_number; copied
5006 * 11-12 uint16 length; (ignored)
5007 *
5008 * 13-13 HandshakeType msg_type; (ignored)
5009 * 14-16 uint24 length; (ignored)
5010 * 17-18 uint16 message_seq; copied
5011 * 19-21 uint24 fragment_offset; copied, must be 0
5012 * 22-24 uint24 fragment_length; (ignored)
5013 *
5014 * 25-26 ProtocolVersion client_version; (ignored)
5015 * 27-58 Random random; (ignored)
5016 * 59-xx SessionID session_id; 1 byte len + sid_len content
5017 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5018 * ...
5019 *
5020 * Minimum length is 61 bytes.
5021 */
5022 if( in_len < 61 ||
5023 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5024 in[3] != 0 || in[4] != 0 ||
5025 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5026 {
5027 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5028 }
5029
5030 sid_len = in[59];
5031 if( sid_len > in_len - 61 )
5032 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5033
5034 cookie_len = in[60 + sid_len];
5035 if( cookie_len > in_len - 60 )
5036 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5037
5038 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5039 cli_id, cli_id_len ) == 0 )
5040 {
5041 /* Valid cookie */
5042 return( 0 );
5043 }
5044
5045 /*
5046 * If we get here, we've got an invalid cookie, let's prepare HVR.
5047 *
5048 * 0-0 ContentType type; copied
5049 * 1-2 ProtocolVersion version; copied
5050 * 3-4 uint16 epoch; copied
5051 * 5-10 uint48 sequence_number; copied
5052 * 11-12 uint16 length; olen - 13
5053 *
5054 * 13-13 HandshakeType msg_type; hello_verify_request
5055 * 14-16 uint24 length; olen - 25
5056 * 17-18 uint16 message_seq; copied
5057 * 19-21 uint24 fragment_offset; copied
5058 * 22-24 uint24 fragment_length; olen - 25
5059 *
5060 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5061 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5062 *
5063 * Minimum length is 28.
5064 */
5065 if( buf_len < 28 )
5066 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5067
5068 /* Copy most fields and adapt others */
5069 memcpy( obuf, in, 25 );
5070 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5071 obuf[25] = 0xfe;
5072 obuf[26] = 0xff;
5073
5074 /* Generate and write actual cookie */
5075 p = obuf + 28;
5076 if( f_cookie_write( p_cookie,
5077 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5078 {
5079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5080 }
5081
5082 *olen = p - obuf;
5083
5084 /* Go back and fill length fields */
5085 obuf[27] = (unsigned char)( *olen - 28 );
5086
5087 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5088 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5089 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5090
5091 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5092 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5093
5094 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5095}
5096
5097/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005098 * Handle possible client reconnect with the same UDP quadruplet
5099 * (RFC 6347 Section 4.2.8).
5100 *
5101 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5102 * that looks like a ClientHello.
5103 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005104 * - if the input looks like a ClientHello without cookies,
5105 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005106 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005107 * - if the input looks like a ClientHello with a valid cookie,
5108 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005109 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005111 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005113 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5114 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005115 */
5116static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5117{
5118 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005119 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005120
Hanno Becker87b56262019-07-10 14:37:41 +01005121 if( ssl->conf->f_cookie_write == NULL ||
5122 ssl->conf->f_cookie_check == NULL )
5123 {
5124 /* If we can't use cookies to verify reachability of the peer,
5125 * drop the record. */
5126 return( 0 );
5127 }
5128
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005129 ret = ssl_check_dtls_clihlo_cookie(
5130 ssl->conf->f_cookie_write,
5131 ssl->conf->f_cookie_check,
5132 ssl->conf->p_cookie,
5133 ssl->cli_id, ssl->cli_id_len,
5134 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005135 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005136
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5138
5139 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005141 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005142 * If the error is permanent we'll catch it later,
5143 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005144 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005145 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005146 }
5147
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005148 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005149 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005150 /* Got a valid cookie, partially reset context */
5151 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5152 {
5153 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5154 return( ret );
5155 }
5156
5157 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158 }
5159
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160 return( ret );
5161}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005162#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005163
Hanno Becker46483f12019-05-03 13:25:54 +01005164static int ssl_check_record_type( uint8_t record_type )
5165{
5166 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5167 record_type != MBEDTLS_SSL_MSG_ALERT &&
5168 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5169 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5170 {
5171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5172 }
5173
5174 return( 0 );
5175}
5176
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005177/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005178 * ContentType type;
5179 * ProtocolVersion version;
5180 * uint16 epoch; // DTLS only
5181 * uint48 sequence_number; // DTLS only
5182 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005183 *
5184 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005185 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005186 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5187 *
5188 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005189 * 1. proceed with the record if this function returns 0
5190 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5191 * 3. return CLIENT_RECONNECT if this function return that value
5192 * 4. drop the whole datagram if this function returns anything else.
5193 * Point 2 is needed when the peer is resending, and we have already received
5194 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005195 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005196static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005197 unsigned char *buf,
5198 size_t len,
5199 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005200{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005201 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005202
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005203 size_t const rec_hdr_type_offset = 0;
5204 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005205
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005206 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5207 rec_hdr_type_len;
5208 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005210 size_t const rec_hdr_ctr_len = 8;
5211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005212 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005213 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5214 rec_hdr_version_len;
5215
Hanno Beckera5a2b082019-05-15 14:03:01 +01005216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5218 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005219 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5221#endif /* MBEDTLS_SSL_PROTO_DTLS */
5222
5223 size_t rec_hdr_len_offset; /* To be determined */
5224 size_t const rec_hdr_len_len = 2;
5225
5226 /*
5227 * Check minimum lengths for record header.
5228 */
5229
5230#if defined(MBEDTLS_SSL_PROTO_DTLS)
5231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5232 {
5233 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5234 }
5235 MBEDTLS_SSL_TRANSPORT_ELSE
5236#endif /* MBEDTLS_SSL_PROTO_DTLS */
5237#if defined(MBEDTLS_SSL_PROTO_TLS)
5238 {
5239 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5240 }
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
5243 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5244 {
5245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5246 (unsigned) len,
5247 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5248 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5249 }
5250
5251 /*
5252 * Parse and validate record content type
5253 */
5254
5255 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005256
5257 /* Check record content type */
5258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5259 rec->cid_len = 0;
5260
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005261 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005262 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5263 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005264 {
5265 /* Shift pointers to account for record header including CID
5266 * struct {
5267 * ContentType special_type = tls12_cid;
5268 * ProtocolVersion version;
5269 * uint16 epoch;
5270 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005271 * opaque cid[cid_length]; // Additional field compared to
5272 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005273 * uint16 length;
5274 * opaque enc_content[DTLSCiphertext.length];
5275 * } DTLSCiphertext;
5276 */
5277
5278 /* So far, we only support static CID lengths
5279 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5281 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005282
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005283 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005284 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5286 (unsigned) len,
5287 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005288 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005289 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005290
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005291 /* configured CID len is guaranteed at most 255, see
5292 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5293 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005294 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005295 }
5296 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005298 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005299 if( ssl_check_record_type( rec->type ) )
5300 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5302 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5304 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005305 }
5306
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005307 /*
5308 * Parse and validate record version
5309 */
5310
Hanno Becker8061c6e2019-07-26 08:07:03 +01005311 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5312 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5314 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005315 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005316
Hanno Becker2881d802019-05-22 14:44:53 +01005317 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5320 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 }
5322
Hanno Beckere965bd32019-06-12 14:04:34 +01005323 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5326 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
5328
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 /*
5330 * Parse/Copy record sequence number.
5331 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005332
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333#if defined(MBEDTLS_SSL_PROTO_DTLS)
5334 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5335 {
5336 /* Copy explicit record sequence number from input buffer. */
5337 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5338 rec_hdr_ctr_len );
5339 }
5340 MBEDTLS_SSL_TRANSPORT_ELSE
5341#endif /* MBEDTLS_SSL_PROTO_DTLS */
5342#if defined(MBEDTLS_SSL_PROTO_TLS)
5343 {
5344 /* Copy implicit record sequence number from SSL context structure. */
5345 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5346 }
5347#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 /*
5350 * Parse record length.
5351 */
5352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005354 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5355 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5357
Hanno Becker8b09b732019-05-08 12:03:28 +01005358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005359 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005360 rec->type,
5361 major_ver, minor_ver, rec->data_len ) );
5362
5363 rec->buf = buf;
5364 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005365
Hanno Beckerec014082019-07-26 08:20:27 +01005366 if( rec->data_len == 0 )
5367 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5368
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005369 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005370 * DTLS-related tests.
5371 * Check epoch before checking length constraint because
5372 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5373 * message gets duplicated before the corresponding Finished message,
5374 * the second ChangeCipherSpec should be discarded because it belongs
5375 * to an old epoch, but not because its length is shorter than
5376 * the minimum record length for packets using the new record transform.
5377 * Note that these two kinds of failures are handled differently,
5378 * as an unexpected record is silently skipped but an invalid
5379 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005380 */
5381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005382 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005383 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005384 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005385
Hanno Beckere0452772019-07-10 17:12:07 +01005386 /* Check that the datagram is large enough to contain a record
5387 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005388 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005389 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5391 (unsigned) len,
5392 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005393 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5394 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005395
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005396 /* Records from other, non-matching epochs are silently discarded.
5397 * (The case of same-port Client reconnects must be considered in
5398 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005399 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5402 "expected %d, received %d",
5403 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005404
5405 /* Records from the next epoch are considered for buffering
5406 * (concretely: early Finished messages). */
5407 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5408 {
5409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5410 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5411 }
5412
Hanno Becker87b56262019-07-10 14:37:41 +01005413 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005414 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005415#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005416 /* For records from the correct epoch, check whether their
5417 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005418 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005419 {
5420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5421 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5422 }
5423#endif
5424 }
5425#endif /* MBEDTLS_SSL_PROTO_DTLS */
5426
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427 return( 0 );
5428}
Paul Bakker5121ce52009-01-03 21:22:43 +00005429
Hanno Becker87b56262019-07-10 14:37:41 +01005430
5431#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5432static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5433{
5434 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5435
5436 /*
5437 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5438 * access the first byte of record content (handshake type), as we
5439 * have an active transform (possibly iv_len != 0), so use the
5440 * fact that the record header len is 13 instead.
5441 */
5442 if( rec_epoch == 0 &&
5443 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5444 MBEDTLS_SSL_IS_SERVER &&
5445 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5446 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5447 ssl->in_left > 13 &&
5448 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5451 "from the same port" ) );
5452 return( ssl_handle_possible_reconnect( ssl ) );
5453 }
5454
5455 return( 0 );
5456}
5457#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459/*
5460 * If applicable, decrypt (and decompress) record content
5461 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005462static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5463 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464{
5465 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005468 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5471 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 ret = mbedtls_ssl_hw_record_read( ssl );
5476 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5479 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005480 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005481
5482 if( ret == 0 )
5483 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005486 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005488 unsigned char const old_msg_type = rec->type;
5489
Hanno Becker611a83b2018-01-03 14:27:32 +00005490 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005491 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005494
Hanno Beckera5a2b082019-05-15 14:03:01 +01005495#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005496 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005497 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5498 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005499 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005501 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005502 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005503#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005504
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 return( ret );
5506 }
5507
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005509 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005510 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005511 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005515 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Hanno Beckera5a2b082019-05-15 14:03:01 +01005517#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005518 /* We have already checked the record content type
5519 * in ssl_parse_record_header(), failing or silently
5520 * dropping the record in the case of an unknown type.
5521 *
5522 * Since with the use of CIDs, the record content type
5523 * might change during decryption, re-check the record
5524 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005525 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5528 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5529 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005530#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005531
Hanno Becker106f3da2019-07-12 09:35:58 +01005532 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005533 {
5534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005535 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005536 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005537 {
5538 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5541 }
5542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5543
5544 ssl->nb_zero++;
5545
5546 /*
5547 * Three or more empty messages may be a DoS attack
5548 * (excessive CPU consumption).
5549 */
5550 if( ssl->nb_zero > 3 )
5551 {
5552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005553 "messages, possible DoS attack" ) );
5554 /* Treat the records as if they were not properly authenticated,
5555 * thereby failing the connection if we see more than allowed
5556 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5558 }
5559 }
5560 else
5561 ssl->nb_zero = 0;
5562
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005563 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5564#if defined(MBEDTLS_SSL_PROTO_TLS)
5565 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005566 {
5567 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005568 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005569 if( ++ssl->in_ctr[i - 1] != 0 )
5570 break;
5571
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005572 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005573 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005574 {
5575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5576 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5577 }
5578 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005579#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005580
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005584 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005586 {
5587 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005590 return( ret );
5591 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005596 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005599 }
5600#endif
5601
Hanno Beckerf0242852019-07-09 17:30:02 +01005602 /* Check actual (decrypted) record content length against
5603 * configured maximum. */
5604 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5607 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5608 }
5609
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 return( 0 );
5611}
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005614
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615/*
5616 * Read a record.
5617 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005618 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5619 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5620 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005621 */
Hanno Becker1097b342018-08-15 14:09:41 +01005622
5623/* Helper functions for mbedtls_ssl_read_record(). */
5624static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005625static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5626static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005627
Hanno Becker327c93b2018-08-15 13:56:18 +01005628int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005629 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630{
5631 int ret;
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005634
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005635 if( ssl->keep_current_message == 0 )
5636 {
5637 do {
Simon Butcher99000142016-10-13 17:21:01 +01005638
Hanno Becker26994592018-08-15 14:14:59 +01005639 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005640 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005641 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005644 {
Hanno Becker40f50842018-08-15 14:48:01 +01005645#if defined(MBEDTLS_SSL_PROTO_DTLS)
5646 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005647
Hanno Becker40f50842018-08-15 14:48:01 +01005648 /* We only check for buffered messages if the
5649 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005651 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005652 {
Hanno Becker40f50842018-08-15 14:48:01 +01005653 if( ssl_load_buffered_message( ssl ) == 0 )
5654 have_buffered = 1;
5655 }
5656
5657 if( have_buffered == 0 )
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
5659 {
5660 ret = ssl_get_next_record( ssl );
5661 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5662 continue;
5663
5664 if( ret != 0 )
5665 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005666 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005667 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005668 return( ret );
5669 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005670 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005671 }
5672
5673 ret = mbedtls_ssl_handle_message_type( ssl );
5674
Hanno Becker40f50842018-08-15 14:48:01 +01005675#if defined(MBEDTLS_SSL_PROTO_DTLS)
5676 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5677 {
5678 /* Buffer future message */
5679 ret = ssl_buffer_message( ssl );
5680 if( ret != 0 )
5681 return( ret );
5682
5683 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5684 }
5685#endif /* MBEDTLS_SSL_PROTO_DTLS */
5686
Hanno Becker90333da2017-10-10 11:27:13 +01005687 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5688 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005689
5690 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005691 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005692 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005693 return( ret );
5694 }
5695
Hanno Becker327c93b2018-08-15 13:56:18 +01005696 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005697 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005698 {
5699 mbedtls_ssl_update_handshake_status( ssl );
5700 }
Simon Butcher99000142016-10-13 17:21:01 +01005701 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005702 else
Simon Butcher99000142016-10-13 17:21:01 +01005703 {
Hanno Becker02f59072018-08-15 14:00:24 +01005704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005705 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005706 }
5707
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5709
5710 return( 0 );
5711}
5712
Hanno Becker40f50842018-08-15 14:48:01 +01005713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005714static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005715{
Hanno Becker40f50842018-08-15 14:48:01 +01005716 if( ssl->in_left > ssl->next_record_offset )
5717 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005718
Hanno Becker40f50842018-08-15 14:48:01 +01005719 return( 0 );
5720}
5721
5722static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5723{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005724 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005725 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005726 int ret = 0;
5727
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005728 if( hs == NULL )
5729 return( -1 );
5730
Hanno Beckere00ae372018-08-20 09:39:42 +01005731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5732
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005733 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5734 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5735 {
5736 /* Check if we have seen a ChangeCipherSpec before.
5737 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005738 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005739 {
5740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5741 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005742 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 }
5744
Hanno Becker39b8bc92018-08-28 17:17:13 +01005745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005746 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5747 ssl->in_msglen = 1;
5748 ssl->in_msg[0] = 1;
5749
5750 /* As long as they are equal, the exact value doesn't matter. */
5751 ssl->in_left = 0;
5752 ssl->next_record_offset = 0;
5753
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005754 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005755 goto exit;
5756 }
Hanno Becker37f95322018-08-16 13:55:32 +01005757
Hanno Beckerb8f50142018-08-28 10:01:34 +01005758#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005759 /* Debug only */
5760 {
5761 unsigned offset;
5762 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5763 {
5764 hs_buf = &hs->buffering.hs[offset];
5765 if( hs_buf->is_valid == 1 )
5766 {
5767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5768 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005769 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005770 }
5771 }
5772 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005773#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005774
5775 /* Check if we have buffered and/or fully reassembled the
5776 * next handshake message. */
5777 hs_buf = &hs->buffering.hs[0];
5778 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5779 {
5780 /* Synthesize a record containing the buffered HS message. */
5781 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5782 ( hs_buf->data[2] << 8 ) |
5783 hs_buf->data[3];
5784
5785 /* Double-check that we haven't accidentally buffered
5786 * a message that doesn't fit into the input buffer. */
5787 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5788 {
5789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5791 }
5792
5793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5794 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5795 hs_buf->data, msg_len + 12 );
5796
5797 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5798 ssl->in_hslen = msg_len + 12;
5799 ssl->in_msglen = msg_len + 12;
5800 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5801
5802 ret = 0;
5803 goto exit;
5804 }
5805 else
5806 {
5807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5808 hs->in_msg_seq ) );
5809 }
5810
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005811 ret = -1;
5812
5813exit:
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5816 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005817}
5818
Hanno Beckera02b0b42018-08-21 17:20:27 +01005819static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5820 size_t desired )
5821{
5822 int offset;
5823 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5825 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005826
Hanno Becker01315ea2018-08-21 17:22:17 +01005827 /* Get rid of future records epoch first, if such exist. */
5828 ssl_free_buffered_record( ssl );
5829
5830 /* Check if we have enough space available now. */
5831 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5832 hs->buffering.total_bytes_buffered ) )
5833 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005835 return( 0 );
5836 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005837
Hanno Becker4f432ad2018-08-28 10:02:32 +01005838 /* We don't have enough space to buffer the next expected handshake
5839 * message. Remove buffers used for future messages to gain space,
5840 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005841 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5842 offset >= 0; offset-- )
5843 {
5844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5845 offset ) );
5846
Hanno Beckerb309b922018-08-23 13:18:05 +01005847 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005848
5849 /* Check if we have enough space available now. */
5850 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5851 hs->buffering.total_bytes_buffered ) )
5852 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005854 return( 0 );
5855 }
5856 }
5857
5858 return( -1 );
5859}
5860
Hanno Becker40f50842018-08-15 14:48:01 +01005861static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5862{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 int ret = 0;
5864 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5865
5866 if( hs == NULL )
5867 return( 0 );
5868
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5870
5871 switch( ssl->in_msgtype )
5872 {
5873 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005875
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005876 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005877 break;
5878
5879 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005880 {
5881 unsigned recv_msg_seq_offset;
5882 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5883 mbedtls_ssl_hs_buffer *hs_buf;
5884 size_t msg_len = ssl->in_hslen - 12;
5885
5886 /* We should never receive an old handshake
5887 * message - double-check nonetheless. */
5888 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5889 {
5890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5892 }
5893
5894 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5895 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5896 {
5897 /* Silently ignore -- message too far in the future */
5898 MBEDTLS_SSL_DEBUG_MSG( 2,
5899 ( "Ignore future HS message with sequence number %u, "
5900 "buffering window %u - %u",
5901 recv_msg_seq, ssl->handshake->in_msg_seq,
5902 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5903
5904 goto exit;
5905 }
5906
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5908 recv_msg_seq, recv_msg_seq_offset ) );
5909
5910 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5911
5912 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005913 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005914 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005915 size_t reassembly_buf_sz;
5916
Hanno Becker37f95322018-08-16 13:55:32 +01005917 hs_buf->is_fragmented =
5918 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5919
5920 /* We copy the message back into the input buffer
5921 * after reassembly, so check that it's not too large.
5922 * This is an implementation-specific limitation
5923 * and not one from the standard, hence it is not
5924 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005925 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005926 {
5927 /* Ignore message */
5928 goto exit;
5929 }
5930
Hanno Beckere0b150f2018-08-21 15:51:03 +01005931 /* Check if we have enough space to buffer the message. */
5932 if( hs->buffering.total_bytes_buffered >
5933 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5937 }
5938
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005939 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5940 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005941
5942 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5943 hs->buffering.total_bytes_buffered ) )
5944 {
5945 if( recv_msg_seq_offset > 0 )
5946 {
5947 /* If we can't buffer a future message because
5948 * of space limitations -- ignore. */
5949 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",
5950 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5951 (unsigned) hs->buffering.total_bytes_buffered ) );
5952 goto exit;
5953 }
Hanno Beckere1801392018-08-21 16:51:05 +01005954 else
5955 {
5956 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",
5957 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5958 (unsigned) hs->buffering.total_bytes_buffered ) );
5959 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005960
Hanno Beckera02b0b42018-08-21 17:20:27 +01005961 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005962 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005963 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",
5964 (unsigned) msg_len,
5965 (unsigned) reassembly_buf_sz,
5966 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005967 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005968 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5969 goto exit;
5970 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005971 }
5972
5973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5974 msg_len ) );
5975
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005976 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5977 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005978 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005980 goto exit;
5981 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005982 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005983
5984 /* Prepare final header: copy msg_type, length and message_seq,
5985 * then add standardised fragment_offset and fragment_length */
5986 memcpy( hs_buf->data, ssl->in_msg, 6 );
5987 memset( hs_buf->data + 6, 0, 3 );
5988 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5989
5990 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005991
5992 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005993 }
5994 else
5995 {
5996 /* Make sure msg_type and length are consistent */
5997 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6000 /* Ignore */
6001 goto exit;
6002 }
6003 }
6004
Hanno Becker4422bbb2018-08-20 09:40:19 +01006005 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006006 {
6007 size_t frag_len, frag_off;
6008 unsigned char * const msg = hs_buf->data + 12;
6009
6010 /*
6011 * Check and copy current fragment
6012 */
6013
6014 /* Validation of header fields already done in
6015 * mbedtls_ssl_prepare_handshake_record(). */
6016 frag_off = ssl_get_hs_frag_off( ssl );
6017 frag_len = ssl_get_hs_frag_len( ssl );
6018
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6020 frag_off, frag_len ) );
6021 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6022
6023 if( hs_buf->is_fragmented )
6024 {
6025 unsigned char * const bitmask = msg + msg_len;
6026 ssl_bitmask_set( bitmask, frag_off, frag_len );
6027 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6028 msg_len ) == 0 );
6029 }
6030 else
6031 {
6032 hs_buf->is_complete = 1;
6033 }
6034
6035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6036 hs_buf->is_complete ? "" : "not yet " ) );
6037 }
6038
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006039 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006040 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006041
6042 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006043 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006044 break;
6045 }
6046
6047exit:
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6050 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006051}
6052#endif /* MBEDTLS_SSL_PROTO_DTLS */
6053
Hanno Becker1097b342018-08-15 14:09:41 +01006054static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006055{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006056 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006057 * Consume last content-layer message and potentially
6058 * update in_msglen which keeps track of the contents'
6059 * consumption state.
6060 *
6061 * (1) Handshake messages:
6062 * Remove last handshake message, move content
6063 * and adapt in_msglen.
6064 *
6065 * (2) Alert messages:
6066 * Consume whole record content, in_msglen = 0.
6067 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006068 * (3) Change cipher spec:
6069 * Consume whole record content, in_msglen = 0.
6070 *
6071 * (4) Application data:
6072 * Don't do anything - the record layer provides
6073 * the application data as a stream transport
6074 * and consumes through mbedtls_ssl_read only.
6075 *
6076 */
6077
6078 /* Case (1): Handshake messages */
6079 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006080 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006081 /* Hard assertion to be sure that no application data
6082 * is in flight, as corrupting ssl->in_msglen during
6083 * ssl->in_offt != NULL is fatal. */
6084 if( ssl->in_offt != NULL )
6085 {
6086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6088 }
6089
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006090 /*
6091 * Get next Handshake message in the current record
6092 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006093
Hanno Becker4a810fb2017-05-24 16:27:30 +01006094 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006095 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 * current handshake content: If DTLS handshake
6097 * fragmentation is used, that's the fragment
6098 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006099 * size here is faulty and should be changed at
6100 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006101 * (2) While it doesn't seem to cause problems, one
6102 * has to be very careful not to assume that in_hslen
6103 * is always <= in_msglen in a sensible communication.
6104 * Again, it's wrong for DTLS handshake fragmentation.
6105 * The following check is therefore mandatory, and
6106 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006107 * Additionally, ssl->in_hslen might be arbitrarily out of
6108 * bounds after handling a DTLS message with an unexpected
6109 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006110 */
6111 if( ssl->in_hslen < ssl->in_msglen )
6112 {
6113 ssl->in_msglen -= ssl->in_hslen;
6114 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6115 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006116
Hanno Becker4a810fb2017-05-24 16:27:30 +01006117 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6118 ssl->in_msg, ssl->in_msglen );
6119 }
6120 else
6121 {
6122 ssl->in_msglen = 0;
6123 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006124
Hanno Becker4a810fb2017-05-24 16:27:30 +01006125 ssl->in_hslen = 0;
6126 }
6127 /* Case (4): Application data */
6128 else if( ssl->in_offt != NULL )
6129 {
6130 return( 0 );
6131 }
6132 /* Everything else (CCS & Alerts) */
6133 else
6134 {
6135 ssl->in_msglen = 0;
6136 }
6137
Hanno Becker1097b342018-08-15 14:09:41 +01006138 return( 0 );
6139}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006140
Hanno Beckere74d5562018-08-15 14:26:08 +01006141static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6142{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006143 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006144 return( 1 );
6145
6146 return( 0 );
6147}
6148
Hanno Becker5f066e72018-08-16 14:56:31 +01006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
6150
6151static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6152{
6153 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6154 if( hs == NULL )
6155 return;
6156
Hanno Becker01315ea2018-08-21 17:22:17 +01006157 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006158 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006159 hs->buffering.total_bytes_buffered -=
6160 hs->buffering.future_record.len;
6161
6162 mbedtls_free( hs->buffering.future_record.data );
6163 hs->buffering.future_record.data = NULL;
6164 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006165}
6166
6167static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6168{
6169 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6170 unsigned char * rec;
6171 size_t rec_len;
6172 unsigned rec_epoch;
6173
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006174 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006175 return( 0 );
6176
6177 if( hs == NULL )
6178 return( 0 );
6179
Hanno Becker5f066e72018-08-16 14:56:31 +01006180 rec = hs->buffering.future_record.data;
6181 rec_len = hs->buffering.future_record.len;
6182 rec_epoch = hs->buffering.future_record.epoch;
6183
6184 if( rec == NULL )
6185 return( 0 );
6186
Hanno Becker4cb782d2018-08-20 11:19:05 +01006187 /* Only consider loading future records if the
6188 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006189 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006190 return( 0 );
6191
Hanno Becker5f066e72018-08-16 14:56:31 +01006192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6193
6194 if( rec_epoch != ssl->in_epoch )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6197 goto exit;
6198 }
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6201
6202 /* Double-check that the record is not too large */
6203 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6204 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6205 {
6206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6208 }
6209
6210 memcpy( ssl->in_hdr, rec, rec_len );
6211 ssl->in_left = rec_len;
6212 ssl->next_record_offset = 0;
6213
6214 ssl_free_buffered_record( ssl );
6215
6216exit:
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6218 return( 0 );
6219}
6220
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006221static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6222 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006223{
6224 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006225
6226 /* Don't buffer future records outside handshakes. */
6227 if( hs == NULL )
6228 return( 0 );
6229
6230 /* Only buffer handshake records (we are only interested
6231 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006232 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006233 return( 0 );
6234
6235 /* Don't buffer more than one future epoch record. */
6236 if( hs->buffering.future_record.data != NULL )
6237 return( 0 );
6238
Hanno Becker01315ea2018-08-21 17:22:17 +01006239 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006240 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006241 hs->buffering.total_bytes_buffered ) )
6242 {
6243 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 +01006244 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006245 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006246 return( 0 );
6247 }
6248
Hanno Becker5f066e72018-08-16 14:56:31 +01006249 /* Buffer record */
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6251 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006253
6254 /* ssl_parse_record_header() only considers records
6255 * of the next epoch as candidates for buffering. */
6256 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006257 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006258
6259 hs->buffering.future_record.data =
6260 mbedtls_calloc( 1, hs->buffering.future_record.len );
6261 if( hs->buffering.future_record.data == NULL )
6262 {
6263 /* If we run out of RAM trying to buffer a
6264 * record from the next epoch, just ignore. */
6265 return( 0 );
6266 }
6267
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006268 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006269
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006270 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006271 return( 0 );
6272}
6273
6274#endif /* MBEDTLS_SSL_PROTO_DTLS */
6275
Hanno Beckere74d5562018-08-15 14:26:08 +01006276static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006277{
6278 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006279 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006280
Hanno Becker5f066e72018-08-16 14:56:31 +01006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282 /* We might have buffered a future record; if so,
6283 * and if the epoch matches now, load it.
6284 * On success, this call will set ssl->in_left to
6285 * the length of the buffered record, so that
6286 * the calls to ssl_fetch_input() below will
6287 * essentially be no-ops. */
6288 ret = ssl_load_buffered_record( ssl );
6289 if( ret != 0 )
6290 return( ret );
6291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006292
Hanno Becker8b09b732019-05-08 12:03:28 +01006293 /* Ensure that we have enough space available for the default form
6294 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6295 * with no space for CIDs counted in). */
6296 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6297 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006300 return( ret );
6301 }
6302
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006303 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6304 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006307 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006308 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006309 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6310 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006311 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006312 if( ret != 0 )
6313 return( ret );
6314
6315 /* Fall through to handling of unexpected records */
6316 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6317 }
6318
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006319 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6320 {
Hanno Becker87b56262019-07-10 14:37:41 +01006321#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006322 /* Reset in pointers to default state for TLS/DTLS records,
6323 * assuming no CID and no offset between record content and
6324 * record plaintext. */
6325 ssl_update_in_pointers( ssl );
6326
Hanno Becker69412452019-07-12 08:33:49 +01006327 /* Setup internal message pointers from record structure. */
6328 ssl->in_msgtype = rec.type;
6329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6330 ssl->in_len = ssl->in_cid + rec.cid_len;
6331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006332 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006333 ssl->in_msglen = rec.data_len;
6334
Hanno Becker87b56262019-07-10 14:37:41 +01006335 ret = ssl_check_client_reconnect( ssl );
6336 if( ret != 0 )
6337 return( ret );
6338#endif
6339
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006340 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006341 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006342
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6344 "(header)" ) );
6345 }
6346 else
6347 {
6348 /* Skip invalid record and the rest of the datagram */
6349 ssl->next_record_offset = 0;
6350 ssl->in_left = 0;
6351
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6353 "(header)" ) );
6354 }
6355
6356 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006357 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006358 }
Hanno Becker87b56262019-07-10 14:37:41 +01006359 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006360#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006361 {
6362 return( ret );
6363 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006364 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006367 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006368 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006369 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006370 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006371 if( ssl->next_record_offset < ssl->in_left )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6374 }
6375 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006376 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006378#if defined(MBEDTLS_SSL_PROTO_TLS)
6379 {
Hanno Beckere0452772019-07-10 17:12:07 +01006380 /*
6381 * Fetch record contents from underlying transport.
6382 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006383 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006384 if( ret != 0 )
6385 {
6386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6387 return( ret );
6388 }
6389
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006390 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006391 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006392#endif /* MBEDTLS_SSL_PROTO_TLS */
6393
6394 /*
6395 * Decrypt record contents.
6396 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006397
Hanno Beckera89610a2019-07-11 13:07:45 +01006398 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006401 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006402 {
6403 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006404 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006405 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006406 /* Except when waiting for Finished as a bad mac here
6407 * probably means something went wrong in the handshake
6408 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6409 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6410 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6411 {
6412#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6413 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6414 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006415 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006416 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6417 }
6418#endif
6419 return( ret );
6420 }
6421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006423 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6424 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006428 }
6429#endif
6430
Hanno Becker4a810fb2017-05-24 16:27:30 +01006431 /* As above, invalid records cause
6432 * dismissal of the whole datagram. */
6433
6434 ssl->next_record_offset = 0;
6435 ssl->in_left = 0;
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006438 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006439 }
6440
6441 return( ret );
6442 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006443 MBEDTLS_SSL_TRANSPORT_ELSE
6444#endif /* MBEDTLS_SSL_PROTO_DTLS */
6445#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006446 {
6447 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6449 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006450 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006451 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006453 }
6454#endif
6455 return( ret );
6456 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006457#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006458 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006459
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006460
6461 /* Reset in pointers to default state for TLS/DTLS records,
6462 * assuming no CID and no offset between record content and
6463 * record plaintext. */
6464 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006465#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6466 ssl->in_len = ssl->in_cid + rec.cid_len;
6467#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006468 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006469
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006470 /* The record content type may change during decryption,
6471 * so re-read it. */
6472 ssl->in_msgtype = rec.type;
6473 /* Also update the input buffer, because unfortunately
6474 * the server-side ssl_parse_client_hello() reparses the
6475 * record header when receiving a ClientHello initiating
6476 * a renegotiation. */
6477 ssl->in_hdr[0] = rec.type;
6478 ssl->in_msg = rec.buf + rec.data_offset;
6479 ssl->in_msglen = rec.data_len;
6480 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6481 ssl->in_len[1] = (unsigned char)( rec.data_len );
6482
Simon Butcher99000142016-10-13 17:21:01 +01006483 return( 0 );
6484}
6485
6486int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6487{
6488 int ret;
6489
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006490 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006491 * Handle particular types of records
6492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 {
Simon Butcher99000142016-10-13 17:21:01 +01006495 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6496 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006497 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 }
6500
Hanno Beckere678eaa2018-08-21 14:57:46 +01006501 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006502 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006503 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006504 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6506 ssl->in_msglen ) );
6507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006508 }
6509
Hanno Beckere678eaa2018-08-21 14:57:46 +01006510 if( ssl->in_msg[0] != 1 )
6511 {
6512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6513 ssl->in_msg[0] ) );
6514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6515 }
6516
6517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006518 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006519 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6520 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6521 {
6522 if( ssl->handshake == NULL )
6523 {
6524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6525 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6526 }
6527
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6530 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006531#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006532 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006536 if( ssl->in_msglen != 2 )
6537 {
6538 /* Note: Standard allows for more than one 2 byte alert
6539 to be packed in a single message, but Mbed TLS doesn't
6540 currently support this. */
6541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6542 ssl->in_msglen ) );
6543 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6544 }
6545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 ssl->in_msg[0], ssl->in_msg[1] ) );
6548
6549 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006550 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006555 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 }
6558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6560 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6563 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006564 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006565
6566#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6567 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6568 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6569 {
Hanno Becker90333da2017-10-10 11:27:13 +01006570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006571 /* Will be handled when trying to parse ServerHello */
6572 return( 0 );
6573 }
6574#endif
6575
6576#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006577 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006578 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6579 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006580 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6581 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6582 {
6583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6584 /* Will be handled in mbedtls_ssl_parse_certificate() */
6585 return( 0 );
6586 }
6587#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6588
6589 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006590 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 }
6592
Hanno Beckerc76c6192017-06-06 10:03:17 +01006593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006594 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006595 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006596 /* Drop unexpected ApplicationData records,
6597 * except at the beginning of renegotiations */
6598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6599 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6600#if defined(MBEDTLS_SSL_RENEGOTIATION)
6601 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6602 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006603#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006604 )
6605 {
6606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6607 return( MBEDTLS_ERR_SSL_NON_FATAL );
6608 }
6609
6610 if( ssl->handshake != NULL &&
6611 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6612 {
6613 ssl_handshake_wrapup_free_hs_transform( ssl );
6614 }
6615 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006616#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006617
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 return( 0 );
6619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006622{
6623 int ret;
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6626 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6627 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006628 {
6629 return( ret );
6630 }
6631
6632 return( 0 );
6633}
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006636 unsigned char level,
6637 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006638{
6639 int ret;
6640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006641 if( ssl == NULL || ssl->conf == NULL )
6642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006648 ssl->out_msglen = 2;
6649 ssl->out_msg[0] = level;
6650 ssl->out_msg[1] = message;
6651
Hanno Becker67bc7c32018-08-06 11:33:50 +01006652 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006655 return( ret );
6656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006658
6659 return( 0 );
6660}
6661
Hanno Becker17572472019-02-08 07:19:04 +00006662#if defined(MBEDTLS_X509_CRT_PARSE_C)
6663static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6664{
6665#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6666 if( session->peer_cert != NULL )
6667 {
6668 mbedtls_x509_crt_free( session->peer_cert );
6669 mbedtls_free( session->peer_cert );
6670 session->peer_cert = NULL;
6671 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006672#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006673 if( session->peer_cert_digest != NULL )
6674 {
6675 /* Zeroization is not necessary. */
6676 mbedtls_free( session->peer_cert_digest );
6677 session->peer_cert_digest = NULL;
6678 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6679 session->peer_cert_digest_len = 0;
6680 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006681#else
6682 ((void) session);
6683#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006684}
6685#endif /* MBEDTLS_X509_CRT_PARSE_C */
6686
Paul Bakker5121ce52009-01-03 21:22:43 +00006687/*
6688 * Handshake functions
6689 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006690#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006691/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006693{
Hanno Beckerdf645962019-06-26 13:02:22 +01006694 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6695 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006698
Hanno Becker5097cba2019-02-05 13:36:46 +00006699 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006702 ssl->state++;
6703 return( 0 );
6704 }
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006708}
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006711{
Hanno Beckerdf645962019-06-26 13:02:22 +01006712 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6713 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006716
Hanno Becker5097cba2019-02-05 13:36:46 +00006717 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720 ssl->state++;
6721 return( 0 );
6722 }
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726}
Gilles Peskinef9828522017-05-03 12:28:43 +02006727
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006728#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006729/* Some certificate support -> implement write and parse */
6730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006736 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6737 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006740
Hanno Becker5097cba2019-02-05 13:36:46 +00006741 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744 ssl->state++;
6745 return( 0 );
6746 }
6747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006749 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6750 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 {
6752 if( ssl->client_auth == 0 )
6753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 ssl->state++;
6756 return( 0 );
6757 }
6758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 /*
6761 * If using SSLv3 and got no cert, send an Alert message
6762 * (otherwise an empty Certificate message will be sent).
6763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006765 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 {
6767 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6769 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6770 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 goto write_msg;
6774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#endif /* MBEDTLS_SSL_CLI_C */
6778#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006779 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6784 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 }
6786 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006787#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
6791 /*
6792 * 0 . 0 handshake type
6793 * 1 . 3 handshake length
6794 * 4 . 6 length of all certs
6795 * 7 . 9 length of cert. 1
6796 * 10 . n-1 peer certificate
6797 * n . n+2 length of cert. 2
6798 * n+3 . ... upper level cert, etc.
6799 */
6800 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006802
Paul Bakker29087132010-03-21 21:03:34 +00006803 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 {
6805 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006806 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006809 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 }
6812
6813 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6814 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6815 ssl->out_msg[i + 2] = (unsigned char)( n );
6816
6817 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6818 i += n; crt = crt->next;
6819 }
6820
6821 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6822 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6823 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6824
6825 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6827 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006829#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006830write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 ssl->state++;
6834
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006835 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838 return( ret );
6839 }
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Paul Bakkered27a042013-04-18 22:46:23 +02006843 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844}
6845
Hanno Becker285ff0c2019-01-31 07:44:03 +00006846#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006847
6848#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006849static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6850 unsigned char *crt_buf,
6851 size_t crt_buf_len )
6852{
6853 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6854
6855 if( peer_crt == NULL )
6856 return( -1 );
6857
6858 if( peer_crt->raw.len != crt_buf_len )
6859 return( -1 );
6860
Hanno Becker68b856d2019-02-08 14:00:04 +00006861 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006862}
Hanno Becker5882dd02019-06-06 16:25:57 +01006863#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006864static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6865 unsigned char *crt_buf,
6866 size_t crt_buf_len )
6867{
6868 int ret;
6869 unsigned char const * const peer_cert_digest =
6870 ssl->session->peer_cert_digest;
6871 mbedtls_md_type_t const peer_cert_digest_type =
6872 ssl->session->peer_cert_digest_type;
6873 mbedtls_md_info_t const * const digest_info =
6874 mbedtls_md_info_from_type( peer_cert_digest_type );
6875 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6876 size_t digest_len;
6877
6878 if( peer_cert_digest == NULL || digest_info == NULL )
6879 return( -1 );
6880
6881 digest_len = mbedtls_md_get_size( digest_info );
6882 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6883 return( -1 );
6884
6885 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6886 if( ret != 0 )
6887 return( -1 );
6888
6889 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6890}
Hanno Becker5882dd02019-06-06 16:25:57 +01006891#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006892#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006893
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006894/*
6895 * Once the certificate message is read, parse it into a cert chain and
6896 * perform basic checks, but leave actual verification to the caller
6897 */
Hanno Becker35e41772019-02-05 15:37:23 +00006898static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6899 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006901 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006902#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6903 int crt_cnt=0;
6904#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006905 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006906 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006911 mbedtls_ssl_pend_fatal_alert( ssl,
6912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006914 }
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6917 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006920 mbedtls_ssl_pend_fatal_alert( ssl,
6921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006923 }
6924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006926
Paul Bakker5121ce52009-01-03 21:22:43 +00006927 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006930 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006931
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006932 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006936 mbedtls_ssl_pend_fatal_alert( ssl,
6937 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 }
6940
Hanno Becker33c3dc82019-01-30 14:46:46 +00006941 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6942 i += 3;
6943
Hanno Becker33c3dc82019-01-30 14:46:46 +00006944 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006945 while( i < ssl->in_hslen )
6946 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006947 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006948 if ( i + 3 > ssl->in_hslen ) {
6949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006950 mbedtls_ssl_pend_fatal_alert( ssl,
6951 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006952 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6953 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006954 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6955 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 if( ssl->in_msg[i] != 0 )
6957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006959 mbedtls_ssl_pend_fatal_alert( ssl,
6960 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 }
6963
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6966 | (unsigned int) ssl->in_msg[i + 2];
6967 i += 3;
6968
6969 if( n < 128 || i + n > ssl->in_hslen )
6970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006972 mbedtls_ssl_pend_fatal_alert( ssl,
6973 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Hanno Becker33c3dc82019-01-30 14:46:46 +00006977 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006978#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6979 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006980 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6981 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006982 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006983 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006984 /* During client-side renegotiation, check that the server's
6985 * end-CRTs hasn't changed compared to the initial handshake,
6986 * mitigating the triple handshake attack. On success, reuse
6987 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6989 if( ssl_check_peer_crt_unchanged( ssl,
6990 &ssl->in_msg[i],
6991 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006992 {
Hanno Becker35e41772019-02-05 15:37:23 +00006993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006994 mbedtls_ssl_pend_fatal_alert( ssl,
6995 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006996 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997 }
Hanno Becker35e41772019-02-05 15:37:23 +00006998
6999 /* Now we can safely free the original chain. */
7000 ssl_clear_peer_cert( ssl->session );
7001 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007002#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7003
Hanno Becker33c3dc82019-01-30 14:46:46 +00007004 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007005#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007006 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007007#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007008 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007009 * it in-place from the input buffer instead of making a copy. */
7010 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7011#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007012 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007013 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007014 case 0: /*ok*/
7015 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7016 /* Ignore certificate with an unknown algorithm: maybe a
7017 prior certificate was already trusted. */
7018 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007019
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7021 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7022 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007023
Hanno Becker33c3dc82019-01-30 14:46:46 +00007024 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7026 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007027
Hanno Becker33c3dc82019-01-30 14:46:46 +00007028 default:
7029 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7030 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007031 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007032 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7033 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034 }
7035
7036 i += n;
7037 }
7038
Hanno Becker35e41772019-02-05 15:37:23 +00007039 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007040 return( 0 );
7041}
7042
Hanno Beckerb8a08572019-02-05 12:49:06 +00007043#if defined(MBEDTLS_SSL_SRV_C)
7044static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7045{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007046 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007047 return( -1 );
7048
7049#if defined(MBEDTLS_SSL_PROTO_SSL3)
7050 /*
7051 * Check if the client sent an empty certificate
7052 */
Hanno Becker2881d802019-05-22 14:44:53 +01007053 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007054 {
7055 if( ssl->in_msglen == 2 &&
7056 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7057 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7058 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7059 {
7060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7061 return( 0 );
7062 }
7063
7064 return( -1 );
7065 }
7066#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7067
7068#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7069 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7070 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7071 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7072 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7073 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7074 {
7075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7076 return( 0 );
7077 }
7078
Hanno Beckerb8a08572019-02-05 12:49:06 +00007079#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7080 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007081
7082 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007083}
7084#endif /* MBEDTLS_SSL_SRV_C */
7085
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007086/* Check if a certificate message is expected.
7087 * Return either
7088 * - SSL_CERTIFICATE_EXPECTED, or
7089 * - SSL_CERTIFICATE_SKIP
7090 * indicating whether a Certificate message is expected or not.
7091 */
7092#define SSL_CERTIFICATE_EXPECTED 0
7093#define SSL_CERTIFICATE_SKIP 1
7094static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7095 int authmode )
7096{
Hanno Becker473f98f2019-06-26 10:27:32 +01007097 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007098 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007099
7100 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7101 return( SSL_CERTIFICATE_SKIP );
7102
7103#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007104 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007105 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007106 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7107 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7108 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007109 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007110 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007111
7112 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7113 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007114 ssl->session_negotiate->verify_result =
7115 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7116 return( SSL_CERTIFICATE_SKIP );
7117 }
7118 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007119#else
7120 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007121#endif /* MBEDTLS_SSL_SRV_C */
7122
7123 return( SSL_CERTIFICATE_EXPECTED );
7124}
7125
Hanno Becker3cf50612019-02-05 14:36:34 +00007126static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7127 int authmode,
7128 mbedtls_x509_crt *chain,
7129 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007130{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007131 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007132 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007133 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007134 mbedtls_x509_crt *ca_chain;
7135 mbedtls_x509_crl *ca_crl;
7136
7137 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7138 return( 0 );
7139
7140#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7141 if( ssl->handshake->sni_ca_chain != NULL )
7142 {
7143 ca_chain = ssl->handshake->sni_ca_chain;
7144 ca_crl = ssl->handshake->sni_ca_crl;
7145 }
7146 else
7147#endif
7148 {
7149 ca_chain = ssl->conf->ca_chain;
7150 ca_crl = ssl->conf->ca_crl;
7151 }
7152
7153 /*
7154 * Main check: verify certificate
7155 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007156 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007157 chain,
7158 ca_chain, ca_crl,
7159 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007160#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007161 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007162#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007163 &ssl->session_negotiate->verify_result,
7164 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7165
Hanno Becker8c13ee62019-02-26 16:48:17 +00007166 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007168 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007169 }
7170
7171#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007172 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007173 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7174#endif
7175
7176 /*
7177 * Secondary checks: always done, but change 'ret' only if it was 0
7178 */
7179
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007180#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007181 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007182 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007183#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007184 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007185#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007186 mbedtls_pk_context *pk;
7187 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7188 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007189 {
7190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007192 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007193
7194 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007195 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007196 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007197 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007198 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007199
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007200 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007201#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007202
7203 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007204 {
7205 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7206
7207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007208 if( verify_ret == 0 )
7209 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007210 }
7211 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007212#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007213
7214 if( mbedtls_ssl_check_cert_usage( chain,
7215 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007216 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7217 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007218 &ssl->session_negotiate->verify_result ) != 0 )
7219 {
7220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221 if( verify_ret == 0 )
7222 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007223 }
7224
7225 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7226 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7227 * with details encoded in the verification flags. All other kinds
7228 * of error codes, including those from the user provided f_vrfy
7229 * functions, are treated as fatal and lead to a failure of
7230 * ssl_parse_certificate even if verification was optional. */
7231 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007232 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7233 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007234 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007235 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007236 }
7237
7238 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7239 {
7240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007241 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007242 }
7243
Hanno Becker8c13ee62019-02-26 16:48:17 +00007244 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007245 {
7246 uint8_t alert;
7247
7248 /* The certificate may have been rejected for several reasons.
7249 Pick one and send the corresponding alert. Which alert to send
7250 may be a subject of debate in some cases. */
7251 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7252 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7254 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7256 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7260 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7262 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7264 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7265 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7266 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7267 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7268 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7269 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7270 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7271 else
7272 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007273 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007274 }
7275
7276#if defined(MBEDTLS_DEBUG_C)
7277 if( ssl->session_negotiate->verify_result != 0 )
7278 {
7279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7280 ssl->session_negotiate->verify_result ) );
7281 }
7282 else
7283 {
7284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7285 }
7286#endif /* MBEDTLS_DEBUG_C */
7287
Hanno Becker8c13ee62019-02-26 16:48:17 +00007288 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007289}
7290
Hanno Becker34106f62019-02-08 14:59:05 +00007291#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007292
7293#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007294static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7295 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007296{
7297 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007298 /* Remember digest of the peer's end-CRT. */
7299 ssl->session_negotiate->peer_cert_digest =
7300 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7301 if( ssl->session_negotiate->peer_cert_digest == NULL )
7302 {
7303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7304 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007305 mbedtls_ssl_pend_fatal_alert( ssl,
7306 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007307
7308 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7309 }
7310
7311 ret = mbedtls_md( mbedtls_md_info_from_type(
7312 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7313 start, len,
7314 ssl->session_negotiate->peer_cert_digest );
7315
7316 ssl->session_negotiate->peer_cert_digest_type =
7317 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7318 ssl->session_negotiate->peer_cert_digest_len =
7319 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7320
7321 return( ret );
7322}
Hanno Becker5882dd02019-06-06 16:25:57 +01007323#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007324
7325static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7326 unsigned char *start, size_t len )
7327{
7328 unsigned char *end = start + len;
7329 int ret;
7330
7331 /* Make a copy of the peer's raw public key. */
7332 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7333 ret = mbedtls_pk_parse_subpubkey( &start, end,
7334 &ssl->handshake->peer_pubkey );
7335 if( ret != 0 )
7336 {
7337 /* We should have parsed the public key before. */
7338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7339 }
7340
7341 return( 0 );
7342}
7343#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7344
Hanno Becker3cf50612019-02-05 14:36:34 +00007345int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7346{
7347 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007348 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007349#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7350 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7351 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007352 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007353#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007354 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007355#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007356 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007357 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007358
7359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7360
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007361 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7362 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007363 {
7364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007365 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007366 }
7367
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007368#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7369 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007370 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007371 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007372 chain = ssl->handshake->ecrs_peer_cert;
7373 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007374 goto crt_verify;
7375 }
7376#endif
7377
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007378 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007379 {
7380 /* mbedtls_ssl_read_record may have sent an alert already. We
7381 let it decide whether to alert. */
7382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007383 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007384 }
7385
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007387 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7388 {
7389 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007390
Hanno Beckerb8a08572019-02-05 12:49:06 +00007391 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007392 ret = 0;
7393 else
7394 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007395
Hanno Becker613d4902019-02-05 13:11:17 +00007396 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007397 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007398#endif /* MBEDTLS_SSL_SRV_C */
7399
Hanno Becker35e41772019-02-05 15:37:23 +00007400 /* Clear existing peer CRT structure in case we tried to
7401 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007402 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007403
7404 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7405 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007406 {
7407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7408 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007409 mbedtls_ssl_pend_fatal_alert( ssl,
7410 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007411
Hanno Beckere4aeb762019-02-05 17:19:52 +00007412 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7413 goto exit;
7414 }
7415 mbedtls_x509_crt_init( chain );
7416
7417 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007418 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007419 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007420
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007421#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7422 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007423 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007424
7425crt_verify:
7426 if( ssl->handshake->ecrs_enabled)
7427 rs_ctx = &ssl->handshake->ecrs_ctx;
7428#endif
7429
Hanno Becker3cf50612019-02-05 14:36:34 +00007430 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007431 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007432 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007433 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007434
Hanno Becker3008d282019-02-05 17:02:28 +00007435#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007436 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007437 size_t pk_len;
7438 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007439
Hanno Becker34106f62019-02-08 14:59:05 +00007440 /* We parse the CRT chain without copying, so
7441 * these pointers point into the input buffer,
7442 * and are hence still valid after freeing the
7443 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007444
Hanno Becker5882dd02019-06-06 16:25:57 +01007445#if defined(MBEDTLS_SSL_RENEGOTIATION)
7446 unsigned char *crt_start;
7447 size_t crt_len;
7448
Hanno Becker34106f62019-02-08 14:59:05 +00007449 crt_start = chain->raw.p;
7450 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007451#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007452
Hanno Becker8c13ee62019-02-26 16:48:17 +00007453 pk_start = chain->cache->pk_raw.p;
7454 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007455
7456 /* Free the CRT structures before computing
7457 * digest and copying the peer's public key. */
7458 mbedtls_x509_crt_free( chain );
7459 mbedtls_free( chain );
7460 chain = NULL;
7461
Hanno Becker5882dd02019-06-06 16:25:57 +01007462#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007463 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007465 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007466#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker34106f62019-02-08 14:59:05 +00007468 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007470 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 }
Hanno Becker34106f62019-02-08 14:59:05 +00007472#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7473 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007474 ssl->session_negotiate->peer_cert = chain;
7475 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007479
Hanno Becker613d4902019-02-05 13:11:17 +00007480exit:
7481
Hanno Beckere4aeb762019-02-05 17:19:52 +00007482 if( ret == 0 )
7483 ssl->state++;
7484
7485#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7486 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7487 {
7488 ssl->handshake->ecrs_peer_cert = chain;
7489 chain = NULL;
7490 }
7491#endif
7492
7493 if( chain != NULL )
7494 {
7495 mbedtls_x509_crt_free( chain );
7496 mbedtls_free( chain );
7497 }
7498
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 return( ret );
7500}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007501#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504{
7505 int ret;
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 ssl->out_msglen = 1;
7511 ssl->out_msg[0] = 1;
7512
Paul Bakker5121ce52009-01-03 21:22:43 +00007513 ssl->state++;
7514
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007515 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007518 return( ret );
7519 }
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
7523 return( 0 );
7524}
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527{
7528 int ret;
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007531
Hanno Becker327c93b2018-08-15 13:56:18 +01007532 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007535 return( ret );
7536 }
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007541 mbedtls_ssl_pend_fatal_alert( ssl,
7542 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007544 }
7545
Hanno Beckere678eaa2018-08-21 14:57:46 +01007546 /* CCS records are only accepted if they have length 1 and content '1',
7547 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007548
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007549 /*
7550 * Switch to our negotiated transform and session parameters for inbound
7551 * data.
7552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007554 ssl->transform_in = ssl->transform_negotiate;
7555 ssl->session_in = ssl->session_negotiate;
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007558 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007561 ssl_dtls_replay_reset( ssl );
7562#endif
7563
7564 /* Increment epoch */
7565 if( ++ssl->in_epoch == 0 )
7566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007568 /* This is highly unlikely to happen for legitimate reasons, so
7569 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007571 }
7572 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007573 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007575#if defined(MBEDTLS_SSL_PROTO_TLS)
7576 {
7577 memset( ssl->in_ctr, 0, 8 );
7578 }
7579#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007580
Hanno Beckerf5970a02019-05-08 09:38:41 +01007581 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7584 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007589 mbedtls_ssl_pend_fatal_alert( ssl,
7590 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007592 }
7593 }
7594#endif
7595
Paul Bakker5121ce52009-01-03 21:22:43 +00007596 ssl->state++;
7597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007599
7600 return( 0 );
7601}
7602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7606 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007607 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007608 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7611#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007612 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007615 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007618}
7619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007623
7624 /*
7625 * Free our handshake params
7626 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007627 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007629 ssl->handshake = NULL;
7630
7631 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007632 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007633 */
7634 if( ssl->transform )
7635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 mbedtls_ssl_transform_free( ssl->transform );
7637 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007638 }
7639 ssl->transform = ssl->transform_negotiate;
7640 ssl->transform_negotiate = NULL;
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007643}
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_SSL_RENEGOTIATION)
7650 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007653 ssl->renego_records_seen = 0;
7654 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007655#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007656
7657 /*
7658 * Free the previous session and switch in the current one
7659 */
Paul Bakker0a597072012-09-25 21:55:46 +00007660 if( ssl->session )
7661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007663 /* RFC 7366 3.1: keep the EtM state */
7664 ssl->session_negotiate->encrypt_then_mac =
7665 ssl->session->encrypt_then_mac;
7666#endif
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 mbedtls_ssl_session_free( ssl->session );
7669 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007670 }
7671 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007672 ssl->session_negotiate = NULL;
7673
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007674#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007675 /*
7676 * Add cache entry
7677 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007678 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007679 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007680 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007681 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007682 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007684 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007685#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007688 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007689 ssl->handshake->flight != NULL )
7690 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007691 /* Cancel handshake timer */
7692 ssl_set_timer( ssl, 0 );
7693
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007694 /* Keep last flight around in case we need to resend it:
7695 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007697 }
7698 else
7699#endif
7700 ssl_handshake_wrapup_free_hs_transform( ssl );
7701
Paul Bakker48916f92012-09-16 19:57:18 +00007702 ssl->state++;
7703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007705}
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007708{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007709 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007712
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007713 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007714
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007715 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7716 mbedtls_ssl_suite_get_mac(
7717 mbedtls_ssl_ciphersuite_from_id(
7718 mbedtls_ssl_session_get_ciphersuite(
7719 ssl->session_negotiate ) ) ),
7720 ssl, ssl->out_msg + 4,
7721 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007722
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007723 /*
7724 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7725 * may define some other value. Currently (early 2016), no defined
7726 * ciphersuite does this (and this is unlikely to change as activity has
7727 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7728 */
Hanno Becker2881d802019-05-22 14:44:53 +01007729 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007732 ssl->verify_data_len = hash_len;
7733 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007734#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007735
Paul Bakker5121ce52009-01-03 21:22:43 +00007736 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7738 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007739
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007740#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007741 /*
7742 * In case of session resuming, invert the client and server
7743 * ChangeCipherSpec messages order.
7744 */
Paul Bakker0a597072012-09-25 21:55:46 +00007745 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007748 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7749 MBEDTLS_SSL_IS_CLIENT )
7750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007752 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007755 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7756 MBEDTLS_SSL_IS_SERVER )
7757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007759 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007760#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 }
7762 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007763#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007764 ssl->state++;
7765
Paul Bakker48916f92012-09-16 19:57:18 +00007766 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007767 * Switch to our negotiated transform and session parameters for outbound
7768 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007773 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007774 {
7775 unsigned char i;
7776
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007777 /* Remember current epoch settings for resending */
7778 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007779 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007780
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007781 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007782 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007783
7784 /* Increment epoch */
7785 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007786 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007787 break;
7788
7789 /* The loop goes to its end iff the counter is wrapping */
7790 if( i == 0 )
7791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7793 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007794 }
7795 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007796 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007798#if defined(MBEDTLS_SSL_PROTO_TLS)
7799 {
7800 memset( ssl->cur_out_ctr, 0, 8 );
7801 }
7802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007804 ssl->transform_out = ssl->transform_negotiate;
7805 ssl->session_out = ssl->session_negotiate;
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7808 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7813 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007814 }
7815 }
7816#endif
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007821#endif
7822
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007823 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 return( ret );
7827 }
7828
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007830 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007831 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7832 {
7833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7834 return( ret );
7835 }
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007839
7840 return( 0 );
7841}
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007844#define SSL_MAX_HASH_LEN 36
7845#else
7846#define SSL_MAX_HASH_LEN 12
7847#endif
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007850{
Paul Bakker23986e52011-04-24 08:57:21 +00007851 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007852 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007853 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007856
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007857 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7858 mbedtls_ssl_suite_get_mac(
7859 mbedtls_ssl_ciphersuite_from_id(
7860 mbedtls_ssl_session_get_ciphersuite(
7861 ssl->session_negotiate ) ) ),
7862 ssl, buf,
7863 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007864
Hanno Becker327c93b2018-08-15 13:56:18 +01007865 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 return( ret );
7869 }
7870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007874 mbedtls_ssl_pend_fatal_alert( ssl,
7875 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 }
7878
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007879 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007881 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007882 hash_len = 36;
7883 else
7884#endif
7885 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7888 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007891 mbedtls_ssl_pend_fatal_alert( ssl,
7892 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007894 }
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007897 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007900 mbedtls_ssl_pend_fatal_alert( ssl,
7901 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007903 }
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007906 ssl->verify_data_len = hash_len;
7907 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007908#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007909
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007910#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007911 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007914 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007918 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007921 }
7922 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007923#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007924 ssl->state++;
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007927 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007929#endif
7930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007932
7933 return( 0 );
7934}
7935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7941 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7942 mbedtls_md5_init( &handshake->fin_md5 );
7943 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007944 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7945 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7948#if defined(MBEDTLS_SHA256_C)
7949 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007950 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952#if defined(MBEDTLS_SHA512_C)
7953 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007954 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957
Hanno Becker7e5437a2017-04-28 17:15:26 +01007958#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7959 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7960 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7961#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_DHM_C)
7964 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#if defined(MBEDTLS_ECDH_C)
7967 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007969#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007970 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007971#if defined(MBEDTLS_SSL_CLI_C)
7972 handshake->ecjpake_cache = NULL;
7973 handshake->ecjpake_cache_len = 0;
7974#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007975#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007976
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007977#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007978 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007979#endif
7980
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007981#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7982 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7983#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007984
7985#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7986 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7987 mbedtls_pk_init( &handshake->peer_pubkey );
7988#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007989}
7990
Hanno Becker611a83b2018-01-03 14:27:32 +00007991void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7996 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007997
Hanno Becker92231322018-01-03 15:32:51 +00007998#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 mbedtls_md_init( &transform->md_ctx_enc );
8000 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008001#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002}
8003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008007}
8008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008010{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008012 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008014 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008016 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008017 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018
8019 /*
8020 * Either the pointers are now NULL or cleared properly and can be freed.
8021 * Now allocate missing structures.
8022 */
8023 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008024 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008025 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008026 }
Paul Bakker48916f92012-09-16 19:57:18 +00008027
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008028 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008029 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008030 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008031 }
Paul Bakker48916f92012-09-16 19:57:18 +00008032
Paul Bakker82788fb2014-10-20 13:59:19 +02008033 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008034 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008035 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008036 }
Paul Bakker48916f92012-09-16 19:57:18 +00008037
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008039 if( ssl->handshake == NULL ||
8040 ssl->transform_negotiate == NULL ||
8041 ssl->session_negotiate == NULL )
8042 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 mbedtls_free( ssl->handshake );
8046 mbedtls_free( ssl->transform_negotiate );
8047 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008048
8049 ssl->handshake = NULL;
8050 ssl->transform_negotiate = NULL;
8051 ssl->session_negotiate = NULL;
8052
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008053 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008054 }
8055
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008056 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008058 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008059 ssl_handshake_params_init( ssl->handshake );
8060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008062 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008063 {
8064 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008065
Hanno Becker2d9623f2019-06-13 12:07:05 +01008066 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008067 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8068 else
8069 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8070 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008071#endif
8072
Paul Bakker48916f92012-09-16 19:57:18 +00008073 return( 0 );
8074}
8075
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008076#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008077/* Dummy cookie callbacks for defaults */
8078static int ssl_cookie_write_dummy( void *ctx,
8079 unsigned char **p, unsigned char *end,
8080 const unsigned char *cli_id, size_t cli_id_len )
8081{
8082 ((void) ctx);
8083 ((void) p);
8084 ((void) end);
8085 ((void) cli_id);
8086 ((void) cli_id_len);
8087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008089}
8090
8091static int ssl_cookie_check_dummy( void *ctx,
8092 const unsigned char *cookie, size_t cookie_len,
8093 const unsigned char *cli_id, size_t cli_id_len )
8094{
8095 ((void) ctx);
8096 ((void) cookie);
8097 ((void) cookie_len);
8098 ((void) cli_id);
8099 ((void) cli_id_len);
8100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008102}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008103#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008104
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008105/* Once ssl->out_hdr as the address of the beginning of the
8106 * next outgoing record is set, deduce the other pointers.
8107 *
8108 * Note: For TLS, we save the implicit record sequence number
8109 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8110 * and the caller has to make sure there's space for this.
8111 */
8112
8113static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8114 mbedtls_ssl_transform *transform )
8115{
8116#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008117 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008118 {
8119 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008121 ssl->out_cid = ssl->out_ctr + 8;
8122 ssl->out_len = ssl->out_cid;
8123 if( transform != NULL )
8124 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008125#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008126 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008127#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008128 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008129 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008130 MBEDTLS_SSL_TRANSPORT_ELSE
8131#endif /* MBEDTLS_SSL_PROTO_DTLS */
8132#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133 {
8134 ssl->out_ctr = ssl->out_hdr - 8;
8135 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008136#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008137 ssl->out_cid = ssl->out_len;
8138#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008139 ssl->out_iv = ssl->out_hdr + 5;
8140 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008141#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008142
8143 /* Adjust out_msg to make space for explicit IV, if used. */
8144 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008145 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146 {
8147 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8148 }
8149 else
8150 ssl->out_msg = ssl->out_iv;
8151}
8152
8153/* Once ssl->in_hdr as the address of the beginning of the
8154 * next incoming record is set, deduce the other pointers.
8155 *
8156 * Note: For TLS, we save the implicit record sequence number
8157 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8158 * and the caller has to make sure there's space for this.
8159 */
8160
Hanno Beckerf5970a02019-05-08 09:38:41 +01008161static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008163 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008164 * of unprotected TLS/DTLS records, with ssl->in_msg
8165 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008166 *
8167 * When decrypting a protected record, ssl->in_msg
8168 * will be shifted to point to the beginning of the
8169 * record plaintext.
8170 */
8171
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008173 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008174 {
Hanno Becker70e79282019-05-03 14:34:53 +01008175 /* This sets the header pointers to match records
8176 * without CID. When we receive a record containing
8177 * a CID, the fields are shifted accordingly in
8178 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008179 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008180#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008181 ssl->in_cid = ssl->in_ctr + 8;
8182 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008183#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008184 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008185#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008186 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008187 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008188 MBEDTLS_SSL_TRANSPORT_ELSE
8189#endif /* MBEDTLS_SSL_PROTO_DTLS */
8190#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008191 {
8192 ssl->in_ctr = ssl->in_hdr - 8;
8193 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008194#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008195 ssl->in_cid = ssl->in_len;
8196#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008197 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008198 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008199#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008200}
8201
Paul Bakker5121ce52009-01-03 21:22:43 +00008202/*
8203 * Initialize an SSL context
8204 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008205void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8206{
8207 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8208}
8209
8210/*
8211 * Setup an SSL context
8212 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008213
8214static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8215{
8216 /* Set the incoming and outgoing record pointers. */
8217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008218 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008219 {
8220 ssl->out_hdr = ssl->out_buf;
8221 ssl->in_hdr = ssl->in_buf;
8222 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008223 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008224#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008225#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008226 {
8227 ssl->out_hdr = ssl->out_buf + 8;
8228 ssl->in_hdr = ssl->in_buf + 8;
8229 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008230#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008231
8232 /* Derive other internal pointers. */
8233 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008234 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008235}
8236
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008237int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008238 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008239{
Paul Bakker48916f92012-09-16 19:57:18 +00008240 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008241
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008242 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008243
Hanno Beckeref982d52019-07-23 15:56:18 +01008244#if defined(MBEDTLS_USE_TINYCRYPT)
8245 uECC_set_rng( &uecc_rng_wrapper );
8246#endif
8247
Paul Bakker62f2dee2012-09-28 07:31:51 +00008248 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008249 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008250 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008251
8252 /* Set to NULL in case of an error condition */
8253 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008254
Angus Grattond8213d02016-05-25 20:56:48 +10008255 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8256 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008257 {
Angus Grattond8213d02016-05-25 20:56:48 +10008258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008259 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008260 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008261 }
8262
8263 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8264 if( ssl->out_buf == NULL )
8265 {
8266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008267 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008268 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008269 }
8270
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008271 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008272
Paul Bakker48916f92012-09-16 19:57:18 +00008273 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008274 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008275
Hanno Beckerc8f52992019-07-25 11:15:08 +01008276 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008277
Paul Bakker5121ce52009-01-03 21:22:43 +00008278 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008279
8280error:
8281 mbedtls_free( ssl->in_buf );
8282 mbedtls_free( ssl->out_buf );
8283
8284 ssl->conf = NULL;
8285
8286 ssl->in_buf = NULL;
8287 ssl->out_buf = NULL;
8288
8289 ssl->in_hdr = NULL;
8290 ssl->in_ctr = NULL;
8291 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008292 ssl->in_msg = NULL;
8293
8294 ssl->out_hdr = NULL;
8295 ssl->out_ctr = NULL;
8296 ssl->out_len = NULL;
8297 ssl->out_iv = NULL;
8298 ssl->out_msg = NULL;
8299
k-stachowiak9f7798e2018-07-31 16:52:32 +02008300 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008301}
8302
8303/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008304 * Reset an initialized and used SSL context for re-use while retaining
8305 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008306 *
8307 * If partial is non-zero, keep data in the input buffer and client ID.
8308 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008309 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008310static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008311{
Paul Bakker48916f92012-09-16 19:57:18 +00008312 int ret;
8313
Hanno Becker7e772132018-08-10 12:38:21 +01008314#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8315 !defined(MBEDTLS_SSL_SRV_C)
8316 ((void) partial);
8317#endif
8318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008320
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008321 /* Cancel any possibly running timer */
8322 ssl_set_timer( ssl, 0 );
8323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324#if defined(MBEDTLS_SSL_RENEGOTIATION)
8325 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008326 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008327
8328 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8330 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008333
Paul Bakker7eb013f2011-10-06 12:37:39 +00008334 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008335 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008336
8337 ssl->in_msgtype = 0;
8338 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008340 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008341 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008344 ssl_dtls_replay_reset( ssl );
8345#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008346
8347 ssl->in_hslen = 0;
8348 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008349
8350 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008351
8352 ssl->out_msgtype = 0;
8353 ssl->out_msglen = 0;
8354 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8356 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008357 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008358#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008359
Hanno Becker19859472018-08-06 09:40:20 +01008360 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8361
Paul Bakker48916f92012-09-16 19:57:18 +00008362 ssl->transform_in = NULL;
8363 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008364
Hanno Becker78640902018-08-13 16:35:15 +01008365 ssl->session_in = NULL;
8366 ssl->session_out = NULL;
8367
Angus Grattond8213d02016-05-25 20:56:48 +10008368 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008369
8370#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008371 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008372#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8373 {
8374 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008375 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008376 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8379 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8382 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8385 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008386 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008387 }
8388#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008389
Paul Bakker48916f92012-09-16 19:57:18 +00008390 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392 mbedtls_ssl_transform_free( ssl->transform );
8393 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008394 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008395 }
Paul Bakker48916f92012-09-16 19:57:18 +00008396
Paul Bakkerc0463502013-02-14 11:19:38 +01008397 if( ssl->session )
8398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 mbedtls_ssl_session_free( ssl->session );
8400 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008401 ssl->session = NULL;
8402 }
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008405 ssl->alpn_chosen = NULL;
8406#endif
8407
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008408#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008409#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008410 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008411#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008412 {
8413 mbedtls_free( ssl->cli_id );
8414 ssl->cli_id = NULL;
8415 ssl->cli_id_len = 0;
8416 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008417#endif
8418
Paul Bakker48916f92012-09-16 19:57:18 +00008419 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8420 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008421
8422 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008423}
8424
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008425/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008426 * Reset an initialized and used SSL context for re-use while retaining
8427 * all application-set variables, function pointers and data.
8428 */
8429int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8430{
8431 return( ssl_session_reset_int( ssl, 0 ) );
8432}
8433
8434/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008435 * SSL set accessors
8436 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008437#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008438void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008441}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008442#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008443
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008444void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008446 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008447}
8448
Hanno Becker7f376f42019-06-12 16:20:48 +01008449#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8450 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008452{
Hanno Becker7f376f42019-06-12 16:20:48 +01008453 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008454}
Hanno Becker7f376f42019-06-12 16:20:48 +01008455#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008456
Hanno Beckerde671542019-06-12 16:30:46 +01008457#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8458 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8459void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8460 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008462 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008463}
Hanno Beckerde671542019-06-12 16:30:46 +01008464#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008467
Hanno Becker1841b0a2018-08-24 11:13:57 +01008468void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8469 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008470{
8471 ssl->disable_datagram_packing = !allow_packing;
8472}
8473
Hanno Becker1f835fa2019-06-13 10:14:59 +01008474#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8475 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008476void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8477 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008478{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008479 conf->hs_timeout_min = min;
8480 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008481}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008482#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8483 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8484void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8485 uint32_t min, uint32_t max )
8486{
8487 ((void) conf);
8488 ((void) min);
8489 ((void) max);
8490}
8491#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8492 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8493
8494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008495
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008496void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008497{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008498#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8499 conf->authmode = authmode;
8500#else
8501 ((void) conf);
8502 ((void) authmode);
8503#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008504}
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008507void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008508 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008509 void *p_vrfy )
8510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008511 conf->f_vrfy = f_vrfy;
8512 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008515
Hanno Beckerece325c2019-06-13 15:39:27 +01008516#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008517void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008518 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008519 void *p_rng )
8520{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008521 conf->f_rng = f_rng;
8522 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008523}
Hanno Beckerece325c2019-06-13 15:39:27 +01008524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008525
Hanno Becker14a4a442019-07-02 17:00:34 +01008526#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008528 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008529 void *p_dbg )
8530{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008531 conf->f_dbg = f_dbg;
8532 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008533}
Hanno Becker14a4a442019-07-02 17:00:34 +01008534#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008535
Hanno Beckera58a8962019-06-13 16:11:15 +01008536#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8537 !defined(MBEDTLS_SSL_CONF_SEND) && \
8538 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008540 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008541 mbedtls_ssl_send_t *f_send,
8542 mbedtls_ssl_recv_t *f_recv,
8543 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008544{
Hanno Beckera58a8962019-06-13 16:11:15 +01008545 ssl->p_bio = p_bio;
8546 ssl->f_send = f_send;
8547 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008548 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008549}
Hanno Beckera58a8962019-06-13 16:11:15 +01008550#else
8551void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8552 void *p_bio )
8553{
8554 ssl->p_bio = p_bio;
8555}
8556#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008557
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008558#if defined(MBEDTLS_SSL_PROTO_DTLS)
8559void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8560{
8561 ssl->mtu = mtu;
8562}
8563#endif
8564
Hanno Becker1f835fa2019-06-13 10:14:59 +01008565#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008566void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008567{
8568 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008569}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008570#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008571
Hanno Becker0ae6b242019-06-13 16:45:36 +01008572#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8573 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008574void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8575 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008576 mbedtls_ssl_set_timer_t *f_set_timer,
8577 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008578{
8579 ssl->p_timer = p_timer;
8580 ssl->f_set_timer = f_set_timer;
8581 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008582 /* Make sure we start with no timer running */
8583 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008584}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008585#else
8586void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8587 void *p_timer )
8588{
8589 ssl->p_timer = p_timer;
8590 /* Make sure we start with no timer running */
8591 ssl_set_timer( ssl, 0 );
8592}
8593#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008594
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008595#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008596void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008597 void *p_cache,
8598 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8599 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008600{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008601 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008602 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008603 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008604}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008605#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008606
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008607#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008610 int ret;
8611
8612 if( ssl == NULL ||
8613 session == NULL ||
8614 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008615 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008618 }
8619
Hanno Becker58fccf22019-02-06 14:30:46 +00008620 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8621 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008622 return( ret );
8623
Paul Bakker0a597072012-09-25 21:55:46 +00008624 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008625
8626 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008627}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008628#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008629
Hanno Becker73f4cb12019-06-27 13:51:07 +01008630#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008631void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008632 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008634 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8635 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8636 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8637 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008638}
8639
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008640void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008641 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008642 int major, int minor )
8643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008645 return;
8646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008648 return;
8649
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008650 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008651}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008652#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008655void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008656 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008657{
8658 conf->cert_profile = profile;
8659}
8660
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008661/* Append a new keycert entry to a (possibly empty) list */
8662static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8663 mbedtls_x509_crt *cert,
8664 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008665{
niisato8ee24222018-06-25 19:05:48 +09008666 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008667
niisato8ee24222018-06-25 19:05:48 +09008668 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8669 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008670 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008671
niisato8ee24222018-06-25 19:05:48 +09008672 new_cert->cert = cert;
8673 new_cert->key = key;
8674 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008675
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008676 /* Update head is the list was null, else add to the end */
8677 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008678 {
niisato8ee24222018-06-25 19:05:48 +09008679 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008680 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008681 else
8682 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008683 mbedtls_ssl_key_cert *cur = *head;
8684 while( cur->next != NULL )
8685 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008686 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008687 }
8688
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008689 return( 0 );
8690}
8691
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008692int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008693 mbedtls_x509_crt *own_cert,
8694 mbedtls_pk_context *pk_key )
8695{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008696 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008697}
8698
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008699void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008700 mbedtls_x509_crt *ca_chain,
8701 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008702{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008703 conf->ca_chain = ca_chain;
8704 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008707
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008708#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8709int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8710 mbedtls_x509_crt *own_cert,
8711 mbedtls_pk_context *pk_key )
8712{
8713 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8714 own_cert, pk_key ) );
8715}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008716
8717void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8718 mbedtls_x509_crt *ca_chain,
8719 mbedtls_x509_crl *ca_crl )
8720{
8721 ssl->handshake->sni_ca_chain = ca_chain;
8722 ssl->handshake->sni_ca_crl = ca_crl;
8723}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008724
8725void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8726 int authmode )
8727{
8728 ssl->handshake->sni_authmode = authmode;
8729}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008730#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8731
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008732#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008733/*
8734 * Set EC J-PAKE password for current handshake
8735 */
8736int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8737 const unsigned char *pw,
8738 size_t pw_len )
8739{
8740 mbedtls_ecjpake_role role;
8741
Janos Follath8eb64132016-06-03 15:40:57 +01008742 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8744
Hanno Becker2d9623f2019-06-13 12:07:05 +01008745 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008746 role = MBEDTLS_ECJPAKE_SERVER;
8747 else
8748 role = MBEDTLS_ECJPAKE_CLIENT;
8749
8750 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8751 role,
8752 MBEDTLS_MD_SHA256,
8753 MBEDTLS_ECP_DP_SECP256R1,
8754 pw, pw_len ) );
8755}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008756#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008759int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008760 const unsigned char *psk, size_t psk_len,
8761 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008762{
Paul Bakker6db455e2013-09-18 17:29:31 +02008763 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008768
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008769 /* Identity len will be encoded on two bytes */
8770 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008771 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008772 {
8773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8774 }
8775
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008776 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008777 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008778 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008779
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008780 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008781 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008782 conf->psk_len = 0;
8783 }
8784 if( conf->psk_identity != NULL )
8785 {
8786 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008787 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008788 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008789 }
8790
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008791 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8792 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008793 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008794 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008795 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008796 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008797 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008798 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008799 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008800
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008801 conf->psk_len = psk_len;
8802 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008803
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008804 memcpy( conf->psk, psk, conf->psk_len );
8805 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008806
8807 return( 0 );
8808}
8809
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008810int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8811 const unsigned char *psk, size_t psk_len )
8812{
8813 if( psk == NULL || ssl->handshake == NULL )
8814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8815
8816 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8818
8819 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008820 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008821 mbedtls_platform_zeroize( ssl->handshake->psk,
8822 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008823 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008824 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008825 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008826
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008827 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008828 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008829
8830 ssl->handshake->psk_len = psk_len;
8831 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8832
8833 return( 0 );
8834}
8835
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008836void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008838 size_t),
8839 void *p_psk )
8840{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008841 conf->f_psk = f_psk;
8842 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008843}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008845
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008846#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008847
8848#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008849int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008850{
8851 int ret;
8852
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008853 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8854 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8855 {
8856 mbedtls_mpi_free( &conf->dhm_P );
8857 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008858 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008860
8861 return( 0 );
8862}
Hanno Becker470a8c42017-10-04 15:28:46 +01008863#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008864
Hanno Beckera90658f2017-10-04 15:29:08 +01008865int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8866 const unsigned char *dhm_P, size_t P_len,
8867 const unsigned char *dhm_G, size_t G_len )
8868{
8869 int ret;
8870
8871 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8872 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8873 {
8874 mbedtls_mpi_free( &conf->dhm_P );
8875 mbedtls_mpi_free( &conf->dhm_G );
8876 return( ret );
8877 }
8878
8879 return( 0 );
8880}
Paul Bakker5121ce52009-01-03 21:22:43 +00008881
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008882int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008883{
8884 int ret;
8885
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8887 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8888 {
8889 mbedtls_mpi_free( &conf->dhm_P );
8890 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008891 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008892 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008893
8894 return( 0 );
8895}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008896#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008897
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008898#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8899/*
8900 * Set the minimum length for Diffie-Hellman parameters
8901 */
8902void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8903 unsigned int bitlen )
8904{
8905 conf->dhm_min_bitlen = bitlen;
8906}
8907#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8908
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008909#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008910/*
8911 * Set allowed/preferred hashes for handshake signatures
8912 */
8913void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8914 const int *hashes )
8915{
Hanno Becker56595f42019-06-19 16:31:38 +01008916#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008917 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008918#else
8919 ((void) conf);
8920 ((void) hashes);
8921#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008922}
Hanno Becker947194e2017-04-07 13:25:49 +01008923#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008924
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008925#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008926#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008927/*
8928 * Set the allowed elliptic curves
8929 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008930void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008931 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008932{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008933 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008934}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008935#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008936#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008937
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008938#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008940{
Hanno Becker947194e2017-04-07 13:25:49 +01008941 /* Initialize to suppress unnecessary compiler warning */
8942 size_t hostname_len = 0;
8943
8944 /* Check if new hostname is valid before
8945 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008946 if( hostname != NULL )
8947 {
8948 hostname_len = strlen( hostname );
8949
8950 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8952 }
8953
8954 /* Now it's clear that we will overwrite the old hostname,
8955 * so we can free it safely */
8956
8957 if( ssl->hostname != NULL )
8958 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008959 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008960 mbedtls_free( ssl->hostname );
8961 }
8962
8963 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008964
Paul Bakker5121ce52009-01-03 21:22:43 +00008965 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008966 {
8967 ssl->hostname = NULL;
8968 }
8969 else
8970 {
8971 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008972 if( ssl->hostname == NULL )
8973 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008974
Hanno Becker947194e2017-04-07 13:25:49 +01008975 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008976
Hanno Becker947194e2017-04-07 13:25:49 +01008977 ssl->hostname[hostname_len] = '\0';
8978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008979
8980 return( 0 );
8981}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008982#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008984#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008985void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008987 const unsigned char *, size_t),
8988 void *p_sni )
8989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008990 conf->f_sni = f_sni;
8991 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008996int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008997{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008998 size_t cur_len, tot_len;
8999 const char **p;
9000
9001 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009002 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9003 * MUST NOT be truncated."
9004 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009005 */
9006 tot_len = 0;
9007 for( p = protos; *p != NULL; p++ )
9008 {
9009 cur_len = strlen( *p );
9010 tot_len += cur_len;
9011
9012 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009014 }
9015
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009016 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009017
9018 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009019}
9020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009022{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009023 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009026
Hanno Becker33b9b252019-07-05 11:23:25 +01009027#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9028 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9029void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9030 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->max_major_ver = major;
9033 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009034}
Hanno Becker33b9b252019-07-05 11:23:25 +01009035#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9036 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009037
Hanno Becker33b9b252019-07-05 11:23:25 +01009038#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9039 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9040void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9041 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009043 conf->min_major_ver = major;
9044 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009045}
Hanno Becker33b9b252019-07-05 11:23:25 +01009046#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9047 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009050void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009051{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009052 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009053}
9054#endif
9055
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009056#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009057void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9058 char cert_req_ca_list )
9059{
9060 conf->cert_req_ca_list = cert_req_ca_list;
9061}
9062#endif
9063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009065void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009066{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009067 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009068}
9069#endif
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009072#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009075 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009076}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009077#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9078#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009079void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009080 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009081{
9082 conf->enforce_extended_master_secret = ems_enf;
9083}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009084#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009085#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009086
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009087#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009088void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009090 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009091}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009092#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009095int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009098 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009101 }
9102
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009103 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009104
9105 return( 0 );
9106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009110void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009111{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009112 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009117void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009118{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009119 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009120}
9121#endif
9122
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009123#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009124void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009126 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009127}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009128#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009131void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009134}
9135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009139}
9140
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009141void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009142 const unsigned char period[8] )
9143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009144 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#if defined(MBEDTLS_SSL_CLI_C)
9150void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009151{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009152 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009153}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009154#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009156#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009157void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9158 mbedtls_ssl_ticket_write_t *f_ticket_write,
9159 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9160 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009161{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009162 conf->f_ticket_write = f_ticket_write;
9163 conf->f_ticket_parse = f_ticket_parse;
9164 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009165}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009168
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009169#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9170void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9171 mbedtls_ssl_export_keys_t *f_export_keys,
9172 void *p_export_keys )
9173{
9174 conf->f_export_keys = f_export_keys;
9175 conf->p_export_keys = p_export_keys;
9176}
9177#endif
9178
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009179#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009180void mbedtls_ssl_conf_async_private_cb(
9181 mbedtls_ssl_config *conf,
9182 mbedtls_ssl_async_sign_t *f_async_sign,
9183 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9184 mbedtls_ssl_async_resume_t *f_async_resume,
9185 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009186 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009187{
9188 conf->f_async_sign_start = f_async_sign;
9189 conf->f_async_decrypt_start = f_async_decrypt;
9190 conf->f_async_resume = f_async_resume;
9191 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009192 conf->p_async_config_data = async_config_data;
9193}
9194
Gilles Peskine8f97af72018-04-26 11:46:10 +02009195void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9196{
9197 return( conf->p_async_config_data );
9198}
9199
Gilles Peskine1febfef2018-04-30 11:54:39 +02009200void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009201{
9202 if( ssl->handshake == NULL )
9203 return( NULL );
9204 else
9205 return( ssl->handshake->user_async_ctx );
9206}
9207
Gilles Peskine1febfef2018-04-30 11:54:39 +02009208void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009209 void *ctx )
9210{
9211 if( ssl->handshake != NULL )
9212 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009213}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009214#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009215
Paul Bakker5121ce52009-01-03 21:22:43 +00009216/*
9217 * SSL get accessors
9218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009220{
9221 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9222}
9223
Hanno Becker8b170a02017-10-10 11:51:19 +01009224int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9225{
9226 /*
9227 * Case A: We're currently holding back
9228 * a message for further processing.
9229 */
9230
9231 if( ssl->keep_current_message == 1 )
9232 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009234 return( 1 );
9235 }
9236
9237 /*
9238 * Case B: Further records are pending in the current datagram.
9239 */
9240
9241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009242 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009243 ssl->in_left > ssl->next_record_offset )
9244 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 return( 1 );
9247 }
9248#endif /* MBEDTLS_SSL_PROTO_DTLS */
9249
9250 /*
9251 * Case C: A handshake message is being processed.
9252 */
9253
Hanno Becker8b170a02017-10-10 11:51:19 +01009254 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9255 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 return( 1 );
9258 }
9259
9260 /*
9261 * Case D: An application data message is being processed
9262 */
9263 if( ssl->in_offt != NULL )
9264 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009266 return( 1 );
9267 }
9268
9269 /*
9270 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009271 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009272 * we implement support for multiple alerts in single records.
9273 */
9274
9275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9276 return( 0 );
9277}
9278
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009279uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009280{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009281 if( ssl->session != NULL )
9282 return( ssl->session->verify_result );
9283
9284 if( ssl->session_negotiate != NULL )
9285 return( ssl->session_negotiate->verify_result );
9286
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009287 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009291{
Hanno Beckere02758c2019-06-26 15:31:31 +01009292 int suite;
9293
Paul Bakker926c8e42013-03-06 10:23:34 +01009294 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009295 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009296
Hanno Beckere02758c2019-06-26 15:31:31 +01009297 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9298 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009299}
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009304 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009305 {
Hanno Becker2881d802019-05-22 14:44:53 +01009306 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009309 return( "DTLSv1.0" );
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009312 return( "DTLSv1.2" );
9313
9314 default:
9315 return( "unknown (DTLS)" );
9316 }
9317 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009318 MBEDTLS_SSL_TRANSPORT_ELSE
9319#endif /* MBEDTLS_SSL_PROTO_DTLS */
9320#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321 {
Hanno Becker2881d802019-05-22 14:44:53 +01009322 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009323 {
9324 case MBEDTLS_SSL_MINOR_VERSION_0:
9325 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009327 case MBEDTLS_SSL_MINOR_VERSION_1:
9328 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009330 case MBEDTLS_SSL_MINOR_VERSION_2:
9331 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009332
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009333 case MBEDTLS_SSL_MINOR_VERSION_3:
9334 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009335
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009336 default:
9337 return( "unknown" );
9338 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009339 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009340#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009341}
9342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009344{
Hanno Becker3136ede2018-08-17 15:28:19 +01009345 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009347
Hanno Becker43395762019-05-03 14:46:38 +01009348 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9349
Hanno Becker78640902018-08-13 16:35:15 +01009350 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009351 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353#if defined(MBEDTLS_ZLIB_SUPPORT)
9354 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9355 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009356#endif
9357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009359 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009360#if defined(MBEDTLS_GCM_C) || \
9361 defined(MBEDTLS_CCM_C) || \
9362 defined(MBEDTLS_CHACHAPOLY_C)
9363#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009365#endif
9366#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009368#endif
9369#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009370 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009371#endif
9372 transform_expansion =
9373 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009374 break;
9375
Hanno Beckera9d5c452019-07-25 16:47:12 +01009376#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9377 MBEDTLS_CHACHAPOLY_C */
9378
9379#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9380 case MBEDTLS_MODE_STREAM:
9381 transform_expansion = transform->maclen;
9382 break;
9383#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9384
9385#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009387 {
9388 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009389
9390 block_size = mbedtls_cipher_get_block_size(
9391 &transform->cipher_ctx_enc );
9392
Hanno Becker3136ede2018-08-17 15:28:19 +01009393 /* Expansion due to the addition of the MAC. */
9394 transform_expansion += transform->maclen;
9395
9396 /* Expansion due to the addition of CBC padding;
9397 * Theoretically up to 256 bytes, but we never use
9398 * more than the block size of the underlying cipher. */
9399 transform_expansion += block_size;
9400
9401 /* For TLS 1.1 or higher, an explicit IV is added
9402 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009403#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009404 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009405 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009406#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009407
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009408 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009409 }
9410#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009411
9412 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009415 }
9416
Hanno Beckera5a2b082019-05-15 14:03:01 +01009417#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009418 if( transform->out_cid_len != 0 )
9419 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009420#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009421
Hanno Becker43395762019-05-03 14:46:38 +01009422 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009423}
9424
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009425#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9426size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9427{
9428 size_t max_len;
9429
9430 /*
9431 * Assume mfl_code is correct since it was checked when set
9432 */
Angus Grattond8213d02016-05-25 20:56:48 +10009433 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009434
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009435 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009436 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009437 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009438 {
Angus Grattond8213d02016-05-25 20:56:48 +10009439 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009440 }
9441
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009442 /* During a handshake, use the value being negotiated */
9443 if( ssl->session_negotiate != NULL &&
9444 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9445 {
9446 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9447 }
9448
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009449 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009450}
9451#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9452
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009453#if defined(MBEDTLS_SSL_PROTO_DTLS)
9454static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9455{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009456 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009457 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009458 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9459 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9460 return ( 0 );
9461
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009462 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9463 return( ssl->mtu );
9464
9465 if( ssl->mtu == 0 )
9466 return( ssl->handshake->mtu );
9467
9468 return( ssl->mtu < ssl->handshake->mtu ?
9469 ssl->mtu : ssl->handshake->mtu );
9470}
9471#endif /* MBEDTLS_SSL_PROTO_DTLS */
9472
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009473int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9474{
9475 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9476
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009477#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9478 !defined(MBEDTLS_SSL_PROTO_DTLS)
9479 (void) ssl;
9480#endif
9481
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9483 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9484
9485 if( max_len > mfl )
9486 max_len = mfl;
9487#endif
9488
9489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009490 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009491 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009492 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009493 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9494 const size_t overhead = (size_t) ret;
9495
9496 if( ret < 0 )
9497 return( ret );
9498
9499 if( mtu <= overhead )
9500 {
9501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9502 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9503 }
9504
9505 if( max_len > mtu - overhead )
9506 max_len = mtu - overhead;
9507 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009508#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009509
Hanno Becker0defedb2018-08-10 12:35:02 +01009510#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9511 !defined(MBEDTLS_SSL_PROTO_DTLS)
9512 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009513#endif
9514
9515 return( (int) max_len );
9516}
9517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_X509_CRT_PARSE_C)
9519const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009520{
9521 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009522 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009523
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009524#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009525 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009526#else
9527 return( NULL );
9528#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009533int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9534 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009535{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536 if( ssl == NULL ||
9537 dst == NULL ||
9538 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009539 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009542 }
9543
Hanno Becker58fccf22019-02-06 14:30:46 +00009544 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009547
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009548const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9549{
9550 if( ssl == NULL )
9551 return( NULL );
9552
9553 return( ssl->session );
9554}
9555
Paul Bakker5121ce52009-01-03 21:22:43 +00009556/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009557 * Define ticket header determining Mbed TLS version
9558 * and structure of the ticket.
9559 */
9560
Hanno Becker41527622019-05-16 12:50:45 +01009561/*
Hanno Becker26829e92019-05-28 14:30:45 +01009562 * Define bitflag determining compile-time settings influencing
9563 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009564 */
9565
Hanno Becker26829e92019-05-28 14:30:45 +01009566#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009567#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009568#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009569#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009570#endif /* MBEDTLS_HAVE_TIME */
9571
9572#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009574#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009575#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009576#endif /* MBEDTLS_X509_CRT_PARSE_C */
9577
9578#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009580#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009581#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009582#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9583
9584#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009585#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009586#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009587#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009588#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9589
9590#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009591#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009592#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009593#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009594#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9595
9596#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009597#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009598#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009599#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009600#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9601
Hanno Becker41527622019-05-16 12:50:45 +01009602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9603#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9604#else
9605#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9606#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9607
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009608#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9609#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9610#else
9611#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9613
Hanno Becker88440552019-07-03 14:16:13 +01009614#if defined(MBEDTLS_ZLIB_SUPPORT)
9615#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9616#else
9617#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9618#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9619
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009620#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9621#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9622#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9623#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9624#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9625#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9626#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009627#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009628#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009629
Hanno Becker26829e92019-05-28 14:30:45 +01009630#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009631 ( (uint16_t) ( \
9632 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9633 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9634 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9635 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9636 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9637 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009638 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009639 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009640 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009641
Hanno Becker557fe9f2019-05-16 12:41:07 +01009642static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009643 MBEDTLS_VERSION_MAJOR,
9644 MBEDTLS_VERSION_MINOR,
9645 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009646 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9647 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009648};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009649
9650/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009651 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009652 * (in the presentation language of TLS, RFC 8446 section 3)
9653 *
Hanno Becker26829e92019-05-28 14:30:45 +01009654 * opaque mbedtls_version[3]; // major, minor, patch
9655 * opaque session_format[2]; // version-specific 16-bit field determining
9656 * // the format of the remaining
9657 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009658 *
9659 * Note: When updating the format, remember to keep
9660 * these version+format bytes.
9661 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009662 * // In this version, `session_format` determines
9663 * // the setting of those compile-time
9664 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009665 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009666 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009667 * uint8 ciphersuite[2]; // defined by the standard
9668 * uint8 compression; // 0 or 1
9669 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009670 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009671 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009672 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009673 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9674 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9675 * case disabled: uint8_t peer_cert_digest_type;
9676 * opaque peer_cert_digest<0..2^8-1>;
9677 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009678 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009679 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009680 * uint8 mfl_code; // up to 255 according to standard
9681 * uint8 trunc_hmac; // 0 or 1
9682 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009683 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009684 * The order is the same as in the definition of the structure, except
9685 * verify_result is put before peer_cert so that all mandatory fields come
9686 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009687 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009688static int ssl_session_save( const mbedtls_ssl_session *session,
9689 unsigned char omit_header,
9690 unsigned char *buf,
9691 size_t buf_len,
9692 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009693{
9694 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009695 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009696#if defined(MBEDTLS_HAVE_TIME)
9697 uint64_t start;
9698#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009699#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009700#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009701 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009702#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009703#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009704
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009705 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009706 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009707 /*
9708 * Add version identifier
9709 */
9710
9711 used += sizeof( ssl_serialized_session_header );
9712
9713 if( used <= buf_len )
9714 {
9715 memcpy( p, ssl_serialized_session_header,
9716 sizeof( ssl_serialized_session_header ) );
9717 p += sizeof( ssl_serialized_session_header );
9718 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009719 }
9720
9721 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009722 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009723 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009724#if defined(MBEDTLS_HAVE_TIME)
9725 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009726
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009727 if( used <= buf_len )
9728 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009729 start = (uint64_t) session->start;
9730
9731 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9732 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9733 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9734 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9735 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9736 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9737 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9738 *p++ = (unsigned char)( ( start ) & 0xFF );
9739 }
9740#endif /* MBEDTLS_HAVE_TIME */
9741
9742 /*
9743 * Basic mandatory fields
9744 */
Hanno Becker88440552019-07-03 14:16:13 +01009745 {
9746 size_t const ciphersuite_len = 2;
9747#if defined(MBEDTLS_ZLIB_SUPPORT)
9748 size_t const compression_len = 1;
9749#else
9750 size_t const compression_len = 0;
9751#endif
9752 size_t const id_len_len = 1;
9753 size_t const id_len = 32;
9754 size_t const master_len = 48;
9755 size_t const verif_result_len = 4;
9756
9757 size_t const basic_len =
9758 ciphersuite_len +
9759 compression_len +
9760 id_len_len +
9761 id_len +
9762 master_len +
9763 verif_result_len;
9764
9765 used += basic_len;
9766 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009767
9768 if( used <= buf_len )
9769 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009770 const int ciphersuite =
9771 mbedtls_ssl_session_get_ciphersuite( session );
9772 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9773 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009774
Hanno Becker88440552019-07-03 14:16:13 +01009775#if defined(MBEDTLS_ZLIB_SUPPORT)
9776 *p++ = (unsigned char)(
9777 mbedtls_ssl_session_get_compression( session ) );
9778#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009779
9780 *p++ = (unsigned char)( session->id_len & 0xFF );
9781 memcpy( p, session->id, 32 );
9782 p += 32;
9783
9784 memcpy( p, session->master, 48 );
9785 p += 48;
9786
9787 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9788 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9789 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9790 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009791 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009792
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009793 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009794 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009795 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009796#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009797#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009798 if( session->peer_cert == NULL )
9799 cert_len = 0;
9800 else
9801 cert_len = session->peer_cert->raw.len;
9802
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009803 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009804
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009805 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009806 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009807 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9808 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9809 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9810
9811 if( session->peer_cert != NULL )
9812 {
9813 memcpy( p, session->peer_cert->raw.p, cert_len );
9814 p += cert_len;
9815 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009816 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009817
Hanno Becker5882dd02019-06-06 16:25:57 +01009818#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009819 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009820 if( session->peer_cert_digest != NULL )
9821 {
9822 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9823 if( used <= buf_len )
9824 {
9825 *p++ = (unsigned char) session->peer_cert_digest_type;
9826 *p++ = (unsigned char) session->peer_cert_digest_len;
9827 memcpy( p, session->peer_cert_digest,
9828 session->peer_cert_digest_len );
9829 p += session->peer_cert_digest_len;
9830 }
9831 }
9832 else
9833 {
9834 used += 2;
9835 if( used <= buf_len )
9836 {
9837 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9838 *p++ = 0;
9839 }
9840 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009841#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009842#endif /* MBEDTLS_X509_CRT_PARSE_C */
9843
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009844 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009845 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009846 */
9847#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009848 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009849
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009850 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009851 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009852 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9853 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9854 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9855
9856 if( session->ticket != NULL )
9857 {
9858 memcpy( p, session->ticket, session->ticket_len );
9859 p += session->ticket_len;
9860 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009861
9862 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9863 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9864 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9865 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009866 }
9867#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9868
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009869 /*
9870 * Misc extension-related info
9871 */
9872#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9873 used += 1;
9874
9875 if( used <= buf_len )
9876 *p++ = session->mfl_code;
9877#endif
9878
9879#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9880 used += 1;
9881
9882 if( used <= buf_len )
9883 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9884#endif
9885
9886#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9887 used += 1;
9888
9889 if( used <= buf_len )
9890 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9891#endif
9892
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009893 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009894 *olen = used;
9895
9896 if( used > buf_len )
9897 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009898
9899 return( 0 );
9900}
9901
9902/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009903 * Public wrapper for ssl_session_save()
9904 */
9905int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9906 unsigned char *buf,
9907 size_t buf_len,
9908 size_t *olen )
9909{
9910 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9911}
9912
9913/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009914 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009915 *
9916 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009917 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009918 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009919static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009920 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009921 const unsigned char *buf,
9922 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009923{
9924 const unsigned char *p = buf;
9925 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009926 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009927#if defined(MBEDTLS_HAVE_TIME)
9928 uint64_t start;
9929#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009930#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009931#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009932 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009933#endif
9934#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009935
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009936 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009937 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009938 /*
9939 * Check version identifier
9940 */
9941
9942 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9944
9945 if( memcmp( p, ssl_serialized_session_header,
9946 sizeof( ssl_serialized_session_header ) ) != 0 )
9947 {
9948 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9949 }
9950 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009951 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009952
9953 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009954 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009955 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009956#if defined(MBEDTLS_HAVE_TIME)
9957 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009958 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9959
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009960 start = ( (uint64_t) p[0] << 56 ) |
9961 ( (uint64_t) p[1] << 48 ) |
9962 ( (uint64_t) p[2] << 40 ) |
9963 ( (uint64_t) p[3] << 32 ) |
9964 ( (uint64_t) p[4] << 24 ) |
9965 ( (uint64_t) p[5] << 16 ) |
9966 ( (uint64_t) p[6] << 8 ) |
9967 ( (uint64_t) p[7] );
9968 p += 8;
9969
9970 session->start = (time_t) start;
9971#endif /* MBEDTLS_HAVE_TIME */
9972
9973 /*
9974 * Basic mandatory fields
9975 */
Hanno Becker88440552019-07-03 14:16:13 +01009976 {
9977 size_t const ciphersuite_len = 2;
9978#if defined(MBEDTLS_ZLIB_SUPPORT)
9979 size_t const compression_len = 1;
9980#else
9981 size_t const compression_len = 0;
9982#endif
9983 size_t const id_len_len = 1;
9984 size_t const id_len = 32;
9985 size_t const master_len = 48;
9986 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009987
Hanno Becker88440552019-07-03 14:16:13 +01009988 size_t const basic_len =
9989 ciphersuite_len +
9990 compression_len +
9991 id_len_len +
9992 id_len +
9993 master_len +
9994 verif_result_len;
9995
9996 if( basic_len > (size_t)( end - p ) )
9997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9998 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009999
Hanno Beckere02758c2019-06-26 15:31:31 +010010000 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010001 p += 2;
10002
Hanno Becker73f4cb12019-06-27 13:51:07 +010010003#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010004 session->ciphersuite = ciphersuite;
10005#else
10006 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010007 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010008 {
10009 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10010 }
10011#endif
10012
Hanno Becker88440552019-07-03 14:16:13 +010010013#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010014 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010015#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010016
10017 session->id_len = *p++;
10018 memcpy( session->id, p, 32 );
10019 p += 32;
10020
10021 memcpy( session->master, p, 48 );
10022 p += 48;
10023
10024 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10025 ( (uint32_t) p[1] << 16 ) |
10026 ( (uint32_t) p[2] << 8 ) |
10027 ( (uint32_t) p[3] );
10028 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010029
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010030 /* Immediately clear invalid pointer values that have been read, in case
10031 * we exit early before we replaced them with valid ones. */
10032#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010033#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010034 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010035#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010036 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010037#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010038#endif
10039#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10040 session->ticket = NULL;
10041#endif
10042
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010043 /*
10044 * Peer certificate
10045 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010046#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010047#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010048 if( 3 > (size_t)( end - p ) )
10049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10050
10051 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10052 p += 3;
10053
10054 if( cert_len == 0 )
10055 {
10056 session->peer_cert = NULL;
10057 }
10058 else
10059 {
10060 int ret;
10061
10062 if( cert_len > (size_t)( end - p ) )
10063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10064
10065 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10066
10067 if( session->peer_cert == NULL )
10068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10069
10070 mbedtls_x509_crt_init( session->peer_cert );
10071
10072 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10073 p, cert_len ) ) != 0 )
10074 {
10075 mbedtls_x509_crt_free( session->peer_cert );
10076 mbedtls_free( session->peer_cert );
10077 session->peer_cert = NULL;
10078 return( ret );
10079 }
10080
10081 p += cert_len;
10082 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010083#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010084 /* Deserialize CRT digest from the end of the ticket. */
10085 if( 2 > (size_t)( end - p ) )
10086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10087
10088 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10089 session->peer_cert_digest_len = (size_t) *p++;
10090
10091 if( session->peer_cert_digest_len != 0 )
10092 {
Hanno Becker2326d202019-06-06 14:54:55 +010010093 const mbedtls_md_info_t *md_info =
10094 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10095 if( md_info == NULL )
10096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10097 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10099
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010100 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10101 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10102
10103 session->peer_cert_digest =
10104 mbedtls_calloc( 1, session->peer_cert_digest_len );
10105 if( session->peer_cert_digest == NULL )
10106 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10107
10108 memcpy( session->peer_cert_digest, p,
10109 session->peer_cert_digest_len );
10110 p += session->peer_cert_digest_len;
10111 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010112#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010113#endif /* MBEDTLS_X509_CRT_PARSE_C */
10114
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010115 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010116 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010117 */
10118#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10119 if( 3 > (size_t)( end - p ) )
10120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10121
10122 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10123 p += 3;
10124
10125 if( session->ticket_len != 0 )
10126 {
10127 if( session->ticket_len > (size_t)( end - p ) )
10128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10129
10130 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10131 if( session->ticket == NULL )
10132 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10133
10134 memcpy( session->ticket, p, session->ticket_len );
10135 p += session->ticket_len;
10136 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137
10138 if( 4 > (size_t)( end - p ) )
10139 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10140
10141 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10142 ( (uint32_t) p[1] << 16 ) |
10143 ( (uint32_t) p[2] << 8 ) |
10144 ( (uint32_t) p[3] );
10145 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010146#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10147
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010148 /*
10149 * Misc extension-related info
10150 */
10151#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10152 if( 1 > (size_t)( end - p ) )
10153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10154
10155 session->mfl_code = *p++;
10156#endif
10157
10158#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10159 if( 1 > (size_t)( end - p ) )
10160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10161
10162 session->trunc_hmac = *p++;
10163#endif
10164
10165#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10166 if( 1 > (size_t)( end - p ) )
10167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10168
10169 session->encrypt_then_mac = *p++;
10170#endif
10171
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010172 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010173 if( p != end )
10174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10175
10176 return( 0 );
10177}
10178
10179/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010180 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010181 */
10182int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10183 const unsigned char *buf,
10184 size_t len )
10185{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010186 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010187
10188 if( ret != 0 )
10189 mbedtls_ssl_session_free( session );
10190
10191 return( ret );
10192}
10193
10194/*
Paul Bakker1961b702013-01-25 14:49:24 +010010195 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010200
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010201 if( ssl == NULL || ssl->conf == NULL )
10202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010205 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010209 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010211#endif
10212
Hanno Beckerb82350b2019-07-26 07:24:05 +010010213 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010214 return( ret );
10215}
10216
10217/*
10218 * Perform the SSL handshake
10219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010221{
10222 int ret = 0;
10223
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010224 if( ssl == NULL || ssl->conf == NULL )
10225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010232
10233 if( ret != 0 )
10234 break;
10235 }
10236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010238
10239 return( ret );
10240}
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242#if defined(MBEDTLS_SSL_RENEGOTIATION)
10243#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010244/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010245 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010248{
10249 int ret;
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010252
10253 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10255 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010256
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010257 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010258 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010260 return( ret );
10261 }
10262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010264
10265 return( 0 );
10266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010268
10269/*
10270 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 * - any side: calling mbedtls_ssl_renegotiate(),
10272 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10273 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010274 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010275 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010279{
10280 int ret;
10281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010283
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010284 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10285 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010286
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010287 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10288 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010290 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010292 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010293 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10294 MBEDTLS_SSL_IS_SERVER )
10295 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010296 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010297 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010298 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010299 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010300 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010301 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010302 }
10303#endif
10304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10306 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010311 return( ret );
10312 }
10313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010315
10316 return( 0 );
10317}
10318
10319/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010320 * Renegotiate current connection on client,
10321 * or request renegotiation on server
10322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010326
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010327 if( ssl == NULL || ssl->conf == NULL )
10328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010331 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010332 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010338
10339 /* Did we already try/start sending HelloRequest? */
10340 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010342
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010343 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010348 /*
10349 * On client, either start the renegotiation process or,
10350 * if already in progress, continue the handshake
10351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010356
10357 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360 return( ret );
10361 }
10362 }
10363 else
10364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010368 return( ret );
10369 }
10370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010372
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010373 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010374}
10375
10376/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010377 * Check record counters and renegotiate if they're above the limit.
10378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010380{
Andres AG2196c7f2016-12-15 17:01:16 +000010381 size_t ep_len = ssl_ep_len( ssl );
10382 int in_ctr_cmp;
10383 int out_ctr_cmp;
10384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10386 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010387 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010388 {
10389 return( 0 );
10390 }
10391
Andres AG2196c7f2016-12-15 17:01:16 +000010392 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10393 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010394 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010395 ssl->conf->renego_period + ep_len, 8 - ep_len );
10396
10397 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010398 {
10399 return( 0 );
10400 }
10401
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010406
10407/*
10408 * Receive application data decrypted from the SSL layer
10409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010411{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010412 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010413 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010414
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010415 if( ssl == NULL || ssl->conf == NULL )
10416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010421 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010424 return( ret );
10425
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010426 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010428 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010429 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010430 return( ret );
10431 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010432 }
10433#endif
10434
Hanno Becker4a810fb2017-05-24 16:27:30 +010010435 /*
10436 * Check if renegotiation is necessary and/or handshake is
10437 * in process. If yes, perform/continue, and fall through
10438 * if an unexpected packet is received while the client
10439 * is waiting for the ServerHello.
10440 *
10441 * (There is no equivalent to the last condition on
10442 * the server-side as it is not treated as within
10443 * a handshake while waiting for the ClientHello
10444 * after a renegotiation request.)
10445 */
10446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010448 ret = ssl_check_ctr_renegotiate( ssl );
10449 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10450 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010453 return( ret );
10454 }
10455#endif
10456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010460 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10461 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010464 return( ret );
10465 }
10466 }
10467
Hanno Beckere41158b2017-10-23 13:30:32 +010010468 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010469 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010470 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010471 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010472 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10473 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010474 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010475 ssl_set_timer( ssl,
10476 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010477 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010478
Hanno Becker327c93b2018-08-15 13:56:18 +010010479 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010480 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010481 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10482 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010483
Hanno Becker4a810fb2017-05-24 16:27:30 +010010484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10485 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010486 }
10487
10488 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010490 {
10491 /*
10492 * OpenSSL sends empty messages to randomize the IV
10493 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010494 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010497 return( 0 );
10498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010500 return( ret );
10501 }
10502 }
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010507
Hanno Becker4a810fb2017-05-24 16:27:30 +010010508 /*
10509 * - For client-side, expect SERVER_HELLO_REQUEST.
10510 * - For server-side, expect CLIENT_HELLO.
10511 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10512 */
10513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010515 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10516 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010518 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010521
10522 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010524 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010525 {
10526 continue;
10527 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010528 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010529#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010530#if defined(MBEDTLS_SSL_PROTO_TLS)
10531 {
10532 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10533 }
10534#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010535 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010536#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010537
Hanno Becker4a810fb2017-05-24 16:27:30 +010010538#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010539 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10540 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010541 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010544
10545 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010547 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010548 {
10549 continue;
10550 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010551 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010552#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010553#if defined(MBEDTLS_SSL_PROTO_TLS)
10554 {
10555 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10556 }
10557#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010558 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010559#endif /* MBEDTLS_SSL_SRV_C */
10560
Hanno Becker21df7f92017-10-17 11:03:26 +010010561#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010562 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010563 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10564 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010565 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010566 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10567 {
10568 /*
10569 * Accept renegotiation request
10570 */
Paul Bakker48916f92012-09-16 19:57:18 +000010571
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010572 /* DTLS clients need to know renego is server-initiated */
10573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010574 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010575 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10576 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010577 {
10578 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10579 }
10580#endif
10581 ret = ssl_start_renegotiation( ssl );
10582 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10583 ret != 0 )
10584 {
10585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10586 return( ret );
10587 }
10588 }
10589 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010590#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010591 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010592 /*
10593 * Refuse renegotiation
10594 */
10595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010599 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010600 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010601 /* SSLv3 does not have a "no_renegotiation" warning, so
10602 we send a fatal alert and abort the connection. */
10603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10604 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10605 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010606 }
10607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10609#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10610 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010611 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010612 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010613 ret = mbedtls_ssl_send_alert_message( ssl,
10614 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10615 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10616 if( ret != 0 )
10617 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010618 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10621 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010625 }
Paul Bakker48916f92012-09-16 19:57:18 +000010626 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010627
Hanno Becker90333da2017-10-10 11:27:13 +010010628 /* At this point, we don't know whether the renegotiation has been
10629 * completed or not. The cases to consider are the following:
10630 * 1) The renegotiation is complete. In this case, no new record
10631 * has been read yet.
10632 * 2) The renegotiation is incomplete because the client received
10633 * an application data record while awaiting the ServerHello.
10634 * 3) The renegotiation is incomplete because the client received
10635 * a non-handshake, non-application data message while awaiting
10636 * the ServerHello.
10637 * In each of these case, looping will be the proper action:
10638 * - For 1), the next iteration will read a new record and check
10639 * if it's application data.
10640 * - For 2), the loop condition isn't satisfied as application data
10641 * is present, hence continue is the same as break
10642 * - For 3), the loop condition is satisfied and read_record
10643 * will re-deliver the message that was held back by the client
10644 * when expecting the ServerHello.
10645 */
10646 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010647 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010648#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010650 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010651 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010652 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010653 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010656 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010658 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010659 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10664 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010667 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010668 }
10669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10673 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010674 }
10675
10676 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010677
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010678 /* We're going to return something now, cancel timer,
10679 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010681 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010682
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010684 /* If we requested renego but received AppData, resend HelloRequest.
10685 * Do it now, after setting in_offt, to avoid taking this branch
10686 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010688 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10689 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010691 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010692 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010695 return( ret );
10696 }
10697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010699#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010700 }
10701
10702 n = ( len < ssl->in_msglen )
10703 ? len : ssl->in_msglen;
10704
10705 memcpy( buf, ssl->in_offt, n );
10706 ssl->in_msglen -= n;
10707
10708 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010709 {
10710 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010711 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010712 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010713 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010714 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010715 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010716 /* more data available */
10717 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010718 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010721
Paul Bakker23986e52011-04-24 08:57:21 +000010722 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010723}
10724
10725/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010726 * Send application data to be encrypted by the SSL layer, taking care of max
10727 * fragment length and buffer size.
10728 *
10729 * According to RFC 5246 Section 6.2.1:
10730 *
10731 * Zero-length fragments of Application data MAY be sent as they are
10732 * potentially useful as a traffic analysis countermeasure.
10733 *
10734 * Therefore, it is possible that the input message length is 0 and the
10735 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010736 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010737static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010738 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010739{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010740 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10741 const size_t max_len = (size_t) ret;
10742
10743 if( ret < 0 )
10744 {
10745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10746 return( ret );
10747 }
10748
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010749 if( len > max_len )
10750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010752 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010755 "maximum fragment length: %d > %d",
10756 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010758 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010759 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010760#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010761#if defined(MBEDTLS_SSL_PROTO_TLS)
10762 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010763 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010764 }
10765#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010766 }
Paul Bakker887bd502011-06-08 13:10:54 +000010767
Paul Bakker5121ce52009-01-03 21:22:43 +000010768 if( ssl->out_left != 0 )
10769 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010770 /*
10771 * The user has previously tried to send the data and
10772 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10773 * written. In this case, we expect the high-level write function
10774 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010779 return( ret );
10780 }
10781 }
Paul Bakker887bd502011-06-08 13:10:54 +000010782 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010783 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010784 /*
10785 * The user is trying to send a message the first time, so we need to
10786 * copy the data into the internal buffers and setup the data structure
10787 * to keep track of partial writes
10788 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010789 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010791 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010792
Hanno Becker67bc7c32018-08-06 11:33:50 +010010793 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010796 return( ret );
10797 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010798 }
10799
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010800 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010801}
10802
10803/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010804 * Write application data, doing 1/n-1 splitting if necessary.
10805 *
10806 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010807 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010808 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010811static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010812 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010813{
10814 int ret;
10815
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010816 if( ssl->conf->cbc_record_splitting ==
10817 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010818 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010819 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10821 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010822 {
10823 return( ssl_write_real( ssl, buf, len ) );
10824 }
10825
10826 if( ssl->split_done == 0 )
10827 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010828 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010829 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010830 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010831 }
10832
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010833 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10834 return( ret );
10835 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010836
10837 return( ret + 1 );
10838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010840
10841/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010842 * Write application data (public-facing wrapper)
10843 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010844int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010845{
10846 int ret;
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
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010850 if( ssl == NULL || ssl->conf == NULL )
10851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10852
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010853#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010854 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10855 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010857 return( ret );
10858 }
10859#endif
10860
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010861 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010862 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010863 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010864 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010866 return( ret );
10867 }
10868 }
10869
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010870#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010871 ret = ssl_write_split( ssl, buf, len );
10872#else
10873 ret = ssl_write_real( ssl, buf, len );
10874#endif
10875
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010877
10878 return( ret );
10879}
10880
10881/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010882 * Notify the peer that the connection is being closed
10883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010885{
10886 int ret;
10887
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010888 if( ssl == NULL || ssl->conf == NULL )
10889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010892
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010893 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10899 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10900 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010903 return( ret );
10904 }
10905 }
10906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010908
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010909 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010910}
10911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010913{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010914 if( transform == NULL )
10915 return;
10916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010918 deflateEnd( &transform->ctx_deflate );
10919 inflateEnd( &transform->ctx_inflate );
10920#endif
10921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10923 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010924
Hanno Becker92231322018-01-03 15:32:51 +000010925#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926 mbedtls_md_free( &transform->md_ctx_enc );
10927 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010928#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010929
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010930 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010931}
10932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933#if defined(MBEDTLS_X509_CRT_PARSE_C)
10934static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010937
10938 while( cur != NULL )
10939 {
10940 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010942 cur = next;
10943 }
10944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010946
Hanno Becker0271f962018-08-16 13:23:47 +010010947#if defined(MBEDTLS_SSL_PROTO_DTLS)
10948
10949static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10950{
10951 unsigned offset;
10952 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10953
10954 if( hs == NULL )
10955 return;
10956
Hanno Becker283f5ef2018-08-24 09:34:47 +010010957 ssl_free_buffered_record( ssl );
10958
Hanno Becker0271f962018-08-16 13:23:47 +010010959 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010960 ssl_buffering_free_slot( ssl, offset );
10961}
10962
10963static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10964 uint8_t slot )
10965{
10966 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10967 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010968
10969 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10970 return;
10971
Hanno Beckere605b192018-08-21 15:59:07 +010010972 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010973 {
Hanno Beckere605b192018-08-21 15:59:07 +010010974 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010975 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010976 mbedtls_free( hs_buf->data );
10977 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010978 }
10979}
10980
10981#endif /* MBEDTLS_SSL_PROTO_DTLS */
10982
Gilles Peskine9b562d52018-04-25 20:32:43 +020010983void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010984{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010985 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10986
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010987 if( handshake == NULL )
10988 return;
10989
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010990#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10991 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10992 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010993 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010994 handshake->async_in_progress = 0;
10995 }
10996#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10997
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010998#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10999 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11000 mbedtls_md5_free( &handshake->fin_md5 );
11001 mbedtls_sha1_free( &handshake->fin_sha1 );
11002#endif
11003#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11004#if defined(MBEDTLS_SHA256_C)
11005 mbedtls_sha256_free( &handshake->fin_sha256 );
11006#endif
11007#if defined(MBEDTLS_SHA512_C)
11008 mbedtls_sha512_free( &handshake->fin_sha512 );
11009#endif
11010#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_DHM_C)
11013 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#if defined(MBEDTLS_ECDH_C)
11016 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011017#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011018#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011019 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011020#if defined(MBEDTLS_SSL_CLI_C)
11021 mbedtls_free( handshake->ecjpake_cache );
11022 handshake->ecjpake_cache = NULL;
11023 handshake->ecjpake_cache_len = 0;
11024#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011025#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011026
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011027#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11028 if( handshake->psk != NULL )
11029 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011030 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011031 mbedtls_free( handshake->psk );
11032 }
11033#endif
11034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11036 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011037 /*
11038 * Free only the linked list wrapper, not the keys themselves
11039 * since the belong to the SNI callback
11040 */
11041 if( handshake->sni_key_cert != NULL )
11042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011044
11045 while( cur != NULL )
11046 {
11047 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011049 cur = next;
11050 }
11051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011053
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011054#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011055 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011056 if( handshake->ecrs_peer_cert != NULL )
11057 {
11058 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11059 mbedtls_free( handshake->ecrs_peer_cert );
11060 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011061#endif
11062
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011063#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11064 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11065 mbedtls_pk_free( &handshake->peer_pubkey );
11066#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068#if defined(MBEDTLS_SSL_PROTO_DTLS)
11069 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011070 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011071 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011072#endif
11073
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011074 mbedtls_platform_zeroize( handshake,
11075 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011076}
11077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011079{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011080 if( session == NULL )
11081 return;
11082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011084 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011085#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011086
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011087#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011088 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011089#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011090
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011091 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011092}
11093
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011094#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011095
11096#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11098#else
11099#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11101
11102#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11103#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11104#else
11105#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11106#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11107
11108#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11109#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11110#else
11111#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11112#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11113
11114#if defined(MBEDTLS_SSL_ALPN)
11115#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11116#else
11117#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11118#endif /* MBEDTLS_SSL_ALPN */
11119
11120#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11121#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11122#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11123#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11124
11125#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11126 ( (uint32_t) ( \
11127 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11128 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11129 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11130 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11131 0u ) )
11132
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011133static unsigned char ssl_serialized_context_header[] = {
11134 MBEDTLS_VERSION_MAJOR,
11135 MBEDTLS_VERSION_MINOR,
11136 MBEDTLS_VERSION_PATCH,
11137 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11138 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011139 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11140 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11141 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011142};
11143
Paul Bakker5121ce52009-01-03 21:22:43 +000011144/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011145 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011146 *
11147 * The format of the serialized data is:
11148 * (in the presentation language of TLS, RFC 8446 section 3)
11149 *
11150 * // header
11151 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011152 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011153 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011154 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011155 * Note: When updating the format, remember to keep these
11156 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011157 *
11158 * // session sub-structure
11159 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11160 * // transform sub-structure
11161 * uint8 random[64]; // ServerHello.random+ClientHello.random
11162 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11163 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11164 * // fields from ssl_context
11165 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11166 * uint64 in_window_top; // DTLS: last validated record seq_num
11167 * uint64 in_window; // DTLS: bitmask for replay protection
11168 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11169 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11170 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11171 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11172 *
11173 * Note that many fields of the ssl_context or sub-structures are not
11174 * serialized, as they fall in one of the following categories:
11175 *
11176 * 1. forced value (eg in_left must be 0)
11177 * 2. pointer to dynamically-allocated memory (eg session, transform)
11178 * 3. value can be re-derived from other data (eg session keys from MS)
11179 * 4. value was temporary (eg content of input buffer)
11180 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011181 */
11182int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11183 unsigned char *buf,
11184 size_t buf_len,
11185 size_t *olen )
11186{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011187 unsigned char *p = buf;
11188 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011189 size_t session_len;
11190 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011191
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011192 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011193 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11194 * this function's documentation.
11195 *
11196 * These are due to assumptions/limitations in the implementation. Some of
11197 * them are likely to stay (no handshake in progress) some might go away
11198 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011199 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011200 /* The initial handshake must be over */
11201 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011203 if( ssl->handshake != NULL )
11204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11205 /* Double-check that sub-structures are indeed ready */
11206 if( ssl->transform == NULL || ssl->session == NULL )
11207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11208 /* There must be no pending incoming or outgoing data */
11209 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11211 if( ssl->out_left != 0 )
11212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11213 /* Protocol must be DLTS, not TLS */
11214 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11216 /* Version must be 1.2 */
11217 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11219 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11221 /* We must be using an AEAD ciphersuite */
11222 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11224 /* Renegotiation must not be enabled */
11225 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011227
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011228 /*
11229 * Version and format identifier
11230 */
11231 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011232
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011233 if( used <= buf_len )
11234 {
11235 memcpy( p, ssl_serialized_context_header,
11236 sizeof( ssl_serialized_context_header ) );
11237 p += sizeof( ssl_serialized_context_header );
11238 }
11239
11240 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011241 * Session (length + data)
11242 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011243 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011244 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11245 return( ret );
11246
11247 used += 4 + session_len;
11248 if( used <= buf_len )
11249 {
11250 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11251 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11252 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11253 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11254
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011255 ret = ssl_session_save( ssl->session, 1,
11256 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011257 if( ret != 0 )
11258 return( ret );
11259
11260 p += session_len;
11261 }
11262
11263 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011264 * Transform
11265 */
11266 used += sizeof( ssl->transform->randbytes );
11267 if( used <= buf_len )
11268 {
11269 memcpy( p, ssl->transform->randbytes,
11270 sizeof( ssl->transform->randbytes ) );
11271 p += sizeof( ssl->transform->randbytes );
11272 }
11273
11274#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11275 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11276 if( used <= buf_len )
11277 {
11278 *p++ = ssl->transform->in_cid_len;
11279 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11280 p += ssl->transform->in_cid_len;
11281
11282 *p++ = ssl->transform->out_cid_len;
11283 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11284 p += ssl->transform->out_cid_len;
11285 }
11286#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11287
11288 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011289 * Saved fields from top-level ssl_context structure
11290 */
11291#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11292 used += 4;
11293 if( used <= buf_len )
11294 {
11295 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11296 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11297 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11298 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11299 }
11300#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11301
11302#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11303 used += 16;
11304 if( used <= buf_len )
11305 {
11306 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11307 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11308 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11309 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11310 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11311 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11312 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11313 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11314
11315 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11316 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11317 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11318 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11319 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11320 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11321 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11322 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11323 }
11324#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11325
11326#if defined(MBEDTLS_SSL_PROTO_DTLS)
11327 used += 1;
11328 if( used <= buf_len )
11329 {
11330 *p++ = ssl->disable_datagram_packing;
11331 }
11332#endif /* MBEDTLS_SSL_PROTO_DTLS */
11333
11334 used += 8;
11335 if( used <= buf_len )
11336 {
11337 memcpy( p, ssl->cur_out_ctr, 8 );
11338 p += 8;
11339 }
11340
11341#if defined(MBEDTLS_SSL_PROTO_DTLS)
11342 used += 2;
11343 if( used <= buf_len )
11344 {
11345 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11346 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11347 }
11348#endif /* MBEDTLS_SSL_PROTO_DTLS */
11349
11350#if defined(MBEDTLS_SSL_ALPN)
11351 {
11352 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011353 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011354 : 0;
11355
11356 used += 1 + alpn_len;
11357 if( used <= buf_len )
11358 {
11359 *p++ = alpn_len;
11360
11361 if( ssl->alpn_chosen != NULL )
11362 {
11363 memcpy( p, ssl->alpn_chosen, alpn_len );
11364 p += alpn_len;
11365 }
11366 }
11367 }
11368#endif /* MBEDTLS_SSL_ALPN */
11369
11370 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011371 * Done
11372 */
11373 *olen = used;
11374
11375 if( used > buf_len )
11376 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011377
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011378 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11379
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011380 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011381}
11382
11383/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011384 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011385 *
11386 * This internal version is wrapped by a public function that cleans up in
11387 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011388 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011389static int ssl_context_load( mbedtls_ssl_context *ssl,
11390 const unsigned char *buf,
11391 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011392{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011393 const unsigned char *p = buf;
11394 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011395 size_t session_len;
11396 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011397
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011398 /*
11399 * The context should have been freshly setup or reset.
11400 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011401 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011402 * renegotiating, or if the user mistakenly loaded a session first.)
11403 */
11404 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11405 ssl->session != NULL )
11406 {
11407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11408 }
11409
11410 /*
11411 * We can't check that the config matches the initial one, but we can at
11412 * least check it matches the requirements for serializing.
11413 */
11414 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011415 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11416 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11417 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11418 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11419 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11420 MBEDTLS_SSL_MINOR_VERSION_3 ||
11421 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11422 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011423 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011424 {
11425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11426 }
11427
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011428 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11429
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011430 /*
11431 * Check version identifier
11432 */
11433 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11435
11436 if( memcmp( p, ssl_serialized_context_header,
11437 sizeof( ssl_serialized_context_header ) ) != 0 )
11438 {
11439 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11440 }
11441 p += sizeof( ssl_serialized_context_header );
11442
11443 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011444 * Session
11445 */
11446 if( (size_t)( end - p ) < 4 )
11447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11448
11449 session_len = ( (size_t) p[0] << 24 ) |
11450 ( (size_t) p[1] << 16 ) |
11451 ( (size_t) p[2] << 8 ) |
11452 ( (size_t) p[3] );
11453 p += 4;
11454
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011455 /* This has been allocated by ssl_handshake_init(), called by
11456 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11457 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011458 ssl->session_in = ssl->session;
11459 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011460 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011461
11462 if( (size_t)( end - p ) < session_len )
11463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11464
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011465 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011466 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011467 {
11468 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011469 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011470 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011471
11472 p += session_len;
11473
11474 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011475 * Transform
11476 */
11477
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011478 /* This has been allocated by ssl_handshake_init(), called by
11479 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11480 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011481 ssl->transform_in = ssl->transform;
11482 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011483 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011484
11485 /* Read random bytes and populate structure */
11486 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11488
11489 ret = ssl_populate_transform( ssl->transform,
11490 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11491 ssl->session->master,
11492#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11493#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11494 ssl->session->encrypt_then_mac,
11495#endif
11496#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11497 ssl->session->trunc_hmac,
11498#endif
11499#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11500#if defined(MBEDTLS_ZLIB_SUPPORT)
11501 ssl->session->compression,
11502#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011503 p, /* currently pointing to randbytes */
11504 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11505 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11506 ssl );
11507 if( ret != 0 )
11508 return( ret );
11509
11510 p += sizeof( ssl->transform->randbytes );
11511
11512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11513 /* Read connection IDs and store them */
11514 if( (size_t)( end - p ) < 1 )
11515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11516
11517 ssl->transform->in_cid_len = *p++;
11518
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011519 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11521
11522 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11523 p += ssl->transform->in_cid_len;
11524
11525 ssl->transform->out_cid_len = *p++;
11526
11527 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11529
11530 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11531 p += ssl->transform->out_cid_len;
11532#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11533
11534 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011535 * Saved fields from top-level ssl_context structure
11536 */
11537#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11538 if( (size_t)( end - p ) < 4 )
11539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11540
11541 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11542 ( (uint32_t) p[1] << 16 ) |
11543 ( (uint32_t) p[2] << 8 ) |
11544 ( (uint32_t) p[3] );
11545 p += 4;
11546#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11547
11548#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11549 if( (size_t)( end - p ) < 16 )
11550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11551
11552 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11553 ( (uint64_t) p[1] << 48 ) |
11554 ( (uint64_t) p[2] << 40 ) |
11555 ( (uint64_t) p[3] << 32 ) |
11556 ( (uint64_t) p[4] << 24 ) |
11557 ( (uint64_t) p[5] << 16 ) |
11558 ( (uint64_t) p[6] << 8 ) |
11559 ( (uint64_t) p[7] );
11560 p += 8;
11561
11562 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11563 ( (uint64_t) p[1] << 48 ) |
11564 ( (uint64_t) p[2] << 40 ) |
11565 ( (uint64_t) p[3] << 32 ) |
11566 ( (uint64_t) p[4] << 24 ) |
11567 ( (uint64_t) p[5] << 16 ) |
11568 ( (uint64_t) p[6] << 8 ) |
11569 ( (uint64_t) p[7] );
11570 p += 8;
11571#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11572
11573#if defined(MBEDTLS_SSL_PROTO_DTLS)
11574 if( (size_t)( end - p ) < 1 )
11575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11576
11577 ssl->disable_datagram_packing = *p++;
11578#endif /* MBEDTLS_SSL_PROTO_DTLS */
11579
11580 if( (size_t)( end - p ) < 8 )
11581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11582
11583 memcpy( ssl->cur_out_ctr, p, 8 );
11584 p += 8;
11585
11586#if defined(MBEDTLS_SSL_PROTO_DTLS)
11587 if( (size_t)( end - p ) < 2 )
11588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11589
11590 ssl->mtu = ( p[0] << 8 ) | p[1];
11591 p += 2;
11592#endif /* MBEDTLS_SSL_PROTO_DTLS */
11593
11594#if defined(MBEDTLS_SSL_ALPN)
11595 {
11596 uint8_t alpn_len;
11597 const char **cur;
11598
11599 if( (size_t)( end - p ) < 1 )
11600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11601
11602 alpn_len = *p++;
11603
11604 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11605 {
11606 /* alpn_chosen should point to an item in the configured list */
11607 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11608 {
11609 if( strlen( *cur ) == alpn_len &&
11610 memcmp( p, cur, alpn_len ) == 0 )
11611 {
11612 ssl->alpn_chosen = *cur;
11613 break;
11614 }
11615 }
11616 }
11617
11618 /* can only happen on conf mismatch */
11619 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11621
11622 p += alpn_len;
11623 }
11624#endif /* MBEDTLS_SSL_ALPN */
11625
11626 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011627 * Forced fields from top-level ssl_context structure
11628 *
11629 * Most of them already set to the correct value by mbedtls_ssl_init() and
11630 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11631 */
11632 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11633
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011634#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011635 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011636#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11637#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011638 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011639#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011640
Hanno Becker83985822019-08-30 10:42:49 +010011641 /* Adjust pointers for header fields of outgoing records to
11642 * the given transform, accounting for explicit IV and CID. */
11643 ssl_update_out_pointers( ssl, ssl->transform );
11644
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011645#if defined(MBEDTLS_SSL_PROTO_DTLS)
11646 ssl->in_epoch = 1;
11647#endif
11648
11649 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11650 * which we don't want - otherwise we'd end up freeing the wrong transform
11651 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11652 if( ssl->handshake != NULL )
11653 {
11654 mbedtls_ssl_handshake_free( ssl );
11655 mbedtls_free( ssl->handshake );
11656 ssl->handshake = NULL;
11657 }
11658
11659 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011660 * Done - should have consumed entire buffer
11661 */
11662 if( p != end )
11663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011664
11665 return( 0 );
11666}
11667
11668/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011669 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011670 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011671int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011672 const unsigned char *buf,
11673 size_t len )
11674{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011675 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011676
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011677 if( ret != 0 )
11678 mbedtls_ssl_free( context );
11679
11680 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011681}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011682#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011683
11684/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011685 * Free an SSL context
11686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011687void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011688{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011689 if( ssl == NULL )
11690 return;
11691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011693
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011694 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011695 {
Angus Grattond8213d02016-05-25 20:56:48 +100011696 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011697 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011698 }
11699
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011700 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011701 {
Angus Grattond8213d02016-05-25 20:56:48 +100011702 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011703 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011704 }
11705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011706#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011707 if( ssl->compress_buf != NULL )
11708 {
Angus Grattond8213d02016-05-25 20:56:48 +100011709 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011711 }
11712#endif
11713
Paul Bakker48916f92012-09-16 19:57:18 +000011714 if( ssl->transform )
11715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011716 mbedtls_ssl_transform_free( ssl->transform );
11717 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011718 }
11719
11720 if( ssl->handshake )
11721 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011722 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011723 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11724 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011726 mbedtls_free( ssl->handshake );
11727 mbedtls_free( ssl->transform_negotiate );
11728 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011729 }
11730
Paul Bakkerc0463502013-02-14 11:19:38 +010011731 if( ssl->session )
11732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011733 mbedtls_ssl_session_free( ssl->session );
11734 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011735 }
11736
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011737#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011738 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011739 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011740 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011741 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011742 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011743#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011745#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11746 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11749 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011750 }
11751#endif
11752
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011753#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011754 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011755#endif
11756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011758
Paul Bakker86f04f42013-02-14 11:20:09 +010011759 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011760 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011761}
11762
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011763/*
11764 * Initialze mbedtls_ssl_config
11765 */
11766void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11767{
11768 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011769
11770#if !defined(MBEDTLS_SSL_PROTO_TLS)
11771 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11772#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011773}
11774
Simon Butcherc97b6972015-12-27 23:48:17 +000011775#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011776#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011777static int ssl_preset_default_hashes[] = {
11778#if defined(MBEDTLS_SHA512_C)
11779 MBEDTLS_MD_SHA512,
11780 MBEDTLS_MD_SHA384,
11781#endif
11782#if defined(MBEDTLS_SHA256_C)
11783 MBEDTLS_MD_SHA256,
11784 MBEDTLS_MD_SHA224,
11785#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011786#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011787 MBEDTLS_MD_SHA1,
11788#endif
11789 MBEDTLS_MD_NONE
11790};
Simon Butcherc97b6972015-12-27 23:48:17 +000011791#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011792#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011793
Hanno Becker73f4cb12019-06-27 13:51:07 +010011794#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011795static int ssl_preset_suiteb_ciphersuites[] = {
11796 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11797 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11798 0
11799};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011800#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011801
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011802#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011803#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011804static int ssl_preset_suiteb_hashes[] = {
11805 MBEDTLS_MD_SHA256,
11806 MBEDTLS_MD_SHA384,
11807 MBEDTLS_MD_NONE
11808};
11809#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011810#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011811
Hanno Beckerc1096e72019-06-19 12:30:41 +010011812#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011813static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011814#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011815 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011816#endif
11817#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011818 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011819#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011820 MBEDTLS_ECP_DP_NONE
11821};
11822#endif
11823
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011824/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011825 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011826 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011827int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011828 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011829{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011830#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011831 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011832#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011833
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011834 /* Use the functions here so that they are covered in tests,
11835 * but otherwise access member directly for efficiency */
11836 mbedtls_ssl_conf_endpoint( conf, endpoint );
11837 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011838
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011839 /*
11840 * Things that are common to all presets
11841 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011842#if defined(MBEDTLS_SSL_CLI_C)
11843 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11844 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011845#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011846 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011847#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011848#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11849 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11850#endif
11851 }
11852#endif
11853
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011854#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011855 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011856#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011857
11858#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11859 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11860#endif
11861
11862#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011863#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011864 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011865#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11866#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011867 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011868 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011869#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011870#endif
11871
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011872#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11873 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11874#endif
11875
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011876#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011877 conf->f_cookie_write = ssl_cookie_write_dummy;
11878 conf->f_cookie_check = ssl_cookie_check_dummy;
11879#endif
11880
Hanno Becker7f376f42019-06-12 16:20:48 +010011881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11882 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011883 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11884#endif
11885
Janos Follath088ce432017-04-10 12:42:31 +010011886#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011887#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011888 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011889#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11890#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011891
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011893#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011894 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011895#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11896#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011897 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011898#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11899#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011900
11901#if defined(MBEDTLS_SSL_RENEGOTIATION)
11902 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011903 memset( conf->renego_period, 0x00, 2 );
11904 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011905#endif
11906
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011907#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11908 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11909 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011910 const unsigned char dhm_p[] =
11911 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11912 const unsigned char dhm_g[] =
11913 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11914
Hanno Beckera90658f2017-10-04 15:29:08 +010011915 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11916 dhm_p, sizeof( dhm_p ),
11917 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011918 {
11919 return( ret );
11920 }
11921 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011922#endif
11923
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011924 /*
11925 * Preset-specific defaults
11926 */
11927 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011928 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011929 /*
11930 * NSA Suite B
11931 */
11932 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011933#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011934 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011935#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11936#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011937 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011938#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11939#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011940 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011941#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11942#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011943 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011944#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011945
Hanno Becker73f4cb12019-06-27 13:51:07 +010011946#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11948 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11950 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11951 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011952#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011953
11954#if defined(MBEDTLS_X509_CRT_PARSE_C)
11955 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011956#endif
11957
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011958#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011959#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011960 conf->sig_hashes = ssl_preset_suiteb_hashes;
11961#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011962#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011963
11964#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011965#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011966 conf->curve_list = ssl_preset_suiteb_curves;
11967#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011968#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011969 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011970
11971 /*
11972 * Default
11973 */
11974 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011975#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011976 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11977 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11978 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11979 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011980#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11981#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011982 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11983 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11984 MBEDTLS_SSL_MIN_MINOR_VERSION :
11985 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011987 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011988 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11989#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011990#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11991#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11992 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11993#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11994#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11995 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11996#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011997
Hanno Becker73f4cb12019-06-27 13:51:07 +010011998#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011999 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
12000 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
12001 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
12002 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
12003 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012004#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012005
12006#if defined(MBEDTLS_X509_CRT_PARSE_C)
12007 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12008#endif
12009
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012010#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012011#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012012 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012013#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012014#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012015
12016#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012017#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012018 conf->curve_list = mbedtls_ecp_grp_id_list();
12019#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012020#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012021
12022#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12023 conf->dhm_min_bitlen = 1024;
12024#endif
12025 }
12026
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012027 return( 0 );
12028}
12029
12030/*
12031 * Free mbedtls_ssl_config
12032 */
12033void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12034{
12035#if defined(MBEDTLS_DHM_C)
12036 mbedtls_mpi_free( &conf->dhm_P );
12037 mbedtls_mpi_free( &conf->dhm_G );
12038#endif
12039
12040#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12041 if( conf->psk != NULL )
12042 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012043 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012044 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012045 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012046 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012047 }
12048
12049 if( conf->psk_identity != NULL )
12050 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012051 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012052 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012053 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012054 conf->psk_identity_len = 0;
12055 }
12056#endif
12057
12058#if defined(MBEDTLS_X509_CRT_PARSE_C)
12059 ssl_key_cert_free( conf->key_cert );
12060#endif
12061
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012062 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012063}
12064
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012065#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012066 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12067 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012068/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012069 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012071unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012073#if defined(MBEDTLS_RSA_C)
12074 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12075 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012076#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012077#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012078 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12079 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012081 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012082}
12083
Hanno Becker7e5437a2017-04-28 17:15:26 +010012084unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12085{
12086 switch( type ) {
12087 case MBEDTLS_PK_RSA:
12088 return( MBEDTLS_SSL_SIG_RSA );
12089 case MBEDTLS_PK_ECDSA:
12090 case MBEDTLS_PK_ECKEY:
12091 return( MBEDTLS_SSL_SIG_ECDSA );
12092 default:
12093 return( MBEDTLS_SSL_SIG_ANON );
12094 }
12095}
12096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012097mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012098{
12099 switch( sig )
12100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012101#if defined(MBEDTLS_RSA_C)
12102 case MBEDTLS_SSL_SIG_RSA:
12103 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012104#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012105#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012106 case MBEDTLS_SSL_SIG_ECDSA:
12107 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012108#endif
12109 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012110 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012111 }
12112}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012113#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012114
Hanno Becker7e5437a2017-04-28 17:15:26 +010012115#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12116 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12117
12118/* Find an entry in a signature-hash set matching a given hash algorithm. */
12119mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12120 mbedtls_pk_type_t sig_alg )
12121{
12122 switch( sig_alg )
12123 {
12124 case MBEDTLS_PK_RSA:
12125 return( set->rsa );
12126 case MBEDTLS_PK_ECDSA:
12127 return( set->ecdsa );
12128 default:
12129 return( MBEDTLS_MD_NONE );
12130 }
12131}
12132
12133/* Add a signature-hash-pair to a signature-hash set */
12134void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12135 mbedtls_pk_type_t sig_alg,
12136 mbedtls_md_type_t md_alg )
12137{
12138 switch( sig_alg )
12139 {
12140 case MBEDTLS_PK_RSA:
12141 if( set->rsa == MBEDTLS_MD_NONE )
12142 set->rsa = md_alg;
12143 break;
12144
12145 case MBEDTLS_PK_ECDSA:
12146 if( set->ecdsa == MBEDTLS_MD_NONE )
12147 set->ecdsa = md_alg;
12148 break;
12149
12150 default:
12151 break;
12152 }
12153}
12154
12155/* Allow exactly one hash algorithm for each signature. */
12156void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12157 mbedtls_md_type_t md_alg )
12158{
12159 set->rsa = md_alg;
12160 set->ecdsa = md_alg;
12161}
12162
12163#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12164 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12165
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012166/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012167 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012169mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012170{
12171 switch( hash )
12172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012173#if defined(MBEDTLS_MD5_C)
12174 case MBEDTLS_SSL_HASH_MD5:
12175 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177#if defined(MBEDTLS_SHA1_C)
12178 case MBEDTLS_SSL_HASH_SHA1:
12179 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012181#if defined(MBEDTLS_SHA256_C)
12182 case MBEDTLS_SSL_HASH_SHA224:
12183 return( MBEDTLS_MD_SHA224 );
12184 case MBEDTLS_SSL_HASH_SHA256:
12185 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012187#if defined(MBEDTLS_SHA512_C)
12188 case MBEDTLS_SSL_HASH_SHA384:
12189 return( MBEDTLS_MD_SHA384 );
12190 case MBEDTLS_SSL_HASH_SHA512:
12191 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012192#endif
12193 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012194 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012195 }
12196}
12197
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012198/*
12199 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12200 */
12201unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12202{
12203 switch( md )
12204 {
12205#if defined(MBEDTLS_MD5_C)
12206 case MBEDTLS_MD_MD5:
12207 return( MBEDTLS_SSL_HASH_MD5 );
12208#endif
12209#if defined(MBEDTLS_SHA1_C)
12210 case MBEDTLS_MD_SHA1:
12211 return( MBEDTLS_SSL_HASH_SHA1 );
12212#endif
12213#if defined(MBEDTLS_SHA256_C)
12214 case MBEDTLS_MD_SHA224:
12215 return( MBEDTLS_SSL_HASH_SHA224 );
12216 case MBEDTLS_MD_SHA256:
12217 return( MBEDTLS_SSL_HASH_SHA256 );
12218#endif
12219#if defined(MBEDTLS_SHA512_C)
12220 case MBEDTLS_MD_SHA384:
12221 return( MBEDTLS_SSL_HASH_SHA384 );
12222 case MBEDTLS_MD_SHA512:
12223 return( MBEDTLS_SSL_HASH_SHA512 );
12224#endif
12225 default:
12226 return( MBEDTLS_SSL_HASH_NONE );
12227 }
12228}
12229
Hanno Beckeree902df2019-08-23 13:47:47 +010012230#if defined(MBEDTLS_USE_TINYCRYPT)
12231/*
12232 * Check if a curve proposed by the peer is in our list.
12233 * Return 0 if we're willing to use it, -1 otherwise.
12234 */
12235int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12236 mbedtls_uecc_group_id grp_id )
12237{
12238 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12239 if( own_ec_id == grp_id )
12240 return( 0 );
12241 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12242
12243 return( -1 );
12244}
12245#endif /* MBEDTLS_USE_TINYCRYPT */
12246
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012247#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012248/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012249 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012250 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012251 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012252int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12253 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012254{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012255 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12256 if( own_ec_id == grp_id )
12257 return( 0 );
12258 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012259
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012260 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012261}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012262#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012263
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012264#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012265/*
12266 * Check if a hash proposed by the peer is in our list.
12267 * Return 0 if we're willing to use it, -1 otherwise.
12268 */
12269int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12270 mbedtls_md_type_t md )
12271{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012272 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12273 if( md_alg == md )
12274 return( 0 );
12275 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012276
12277 return( -1 );
12278}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012279#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012281#if defined(MBEDTLS_X509_CRT_PARSE_C)
12282int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012283 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012284 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012285 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012286{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012287 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012288#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012289 int usage = 0;
12290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012291#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012292 const char *ext_oid;
12293 size_t ext_len;
12294#endif
12295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012296#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12297 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012298 ((void) cert);
12299 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012300 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012301#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012303#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12304 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012305 {
12306 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012307 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012309 case MBEDTLS_KEY_EXCHANGE_RSA:
12310 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012311 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012312 break;
12313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012314 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12315 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12316 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12317 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012318 break;
12319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012320 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12321 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012322 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012323 break;
12324
12325 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012326 case MBEDTLS_KEY_EXCHANGE_NONE:
12327 case MBEDTLS_KEY_EXCHANGE_PSK:
12328 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12329 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012330 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012331 usage = 0;
12332 }
12333 }
12334 else
12335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012336 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12337 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012338 }
12339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012340 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012341 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012342 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012343 ret = -1;
12344 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012345#else
12346 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012347#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012349#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12350 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012352 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12353 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012354 }
12355 else
12356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012357 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12358 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012359 }
12360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012361 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012362 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012363 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012364 ret = -1;
12365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012366#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012367
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012368 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012370#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012371
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012372#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12373 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12374int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12375 unsigned char *output,
12376 unsigned char *data, size_t data_len )
12377{
12378 int ret = 0;
12379 mbedtls_md5_context mbedtls_md5;
12380 mbedtls_sha1_context mbedtls_sha1;
12381
12382 mbedtls_md5_init( &mbedtls_md5 );
12383 mbedtls_sha1_init( &mbedtls_sha1 );
12384
12385 /*
12386 * digitally-signed struct {
12387 * opaque md5_hash[16];
12388 * opaque sha_hash[20];
12389 * };
12390 *
12391 * md5_hash
12392 * MD5(ClientHello.random + ServerHello.random
12393 * + ServerParams);
12394 * sha_hash
12395 * SHA(ClientHello.random + ServerHello.random
12396 * + ServerParams);
12397 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012398 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012399 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012401 goto exit;
12402 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012403 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012404 ssl->handshake->randbytes, 64 ) ) != 0 )
12405 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012407 goto exit;
12408 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012409 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012410 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012412 goto exit;
12413 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012414 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012415 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012417 goto exit;
12418 }
12419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012420 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012421 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012423 goto exit;
12424 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012425 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012426 ssl->handshake->randbytes, 64 ) ) != 0 )
12427 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012429 goto exit;
12430 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012431 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012432 data_len ) ) != 0 )
12433 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012435 goto exit;
12436 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012437 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012438 output + 16 ) ) != 0 )
12439 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012441 goto exit;
12442 }
12443
12444exit:
12445 mbedtls_md5_free( &mbedtls_md5 );
12446 mbedtls_sha1_free( &mbedtls_sha1 );
12447
12448 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012449 mbedtls_ssl_pend_fatal_alert( ssl,
12450 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012451
12452 return( ret );
12453
12454}
12455#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12456 MBEDTLS_SSL_PROTO_TLS1_1 */
12457
12458#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12459 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12460int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012461 unsigned char *hash, size_t *hashlen,
12462 unsigned char *data, size_t data_len,
12463 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012464{
12465 int ret = 0;
12466 mbedtls_md_context_t ctx;
12467 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012468 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012469
12470 mbedtls_md_init( &ctx );
12471
12472 /*
12473 * digitally-signed struct {
12474 * opaque client_random[32];
12475 * opaque server_random[32];
12476 * ServerDHParams params;
12477 * };
12478 */
12479 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12480 {
12481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12482 goto exit;
12483 }
12484 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12485 {
12486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12487 goto exit;
12488 }
12489 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12490 {
12491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12492 goto exit;
12493 }
12494 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12495 {
12496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12497 goto exit;
12498 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012499 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012500 {
12501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12502 goto exit;
12503 }
12504
12505exit:
12506 mbedtls_md_free( &ctx );
12507
12508 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012509 mbedtls_ssl_pend_fatal_alert( ssl,
12510 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012511
12512 return( ret );
12513}
12514#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12515 MBEDTLS_SSL_PROTO_TLS1_2 */
12516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012517#endif /* MBEDTLS_SSL_TLS_C */