blob: 8d08ce1200fd7bab13146992da73aca8a14880b0 [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)" ) );
78 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
79 }
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] ) );
89 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
90 }
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)
751static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752 const unsigned char *secret, size_t slen,
753 const char *label,
754 const unsigned char *random, size_t rlen,
755 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756{
757 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100758 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000759 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
761 const mbedtls_md_info_t *md_info;
762 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100763 int ret;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771
772 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774
775 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776 memcpy( tmp + md_len, label, nb );
777 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000778 nb += rlen;
779
780 /*
781 * Compute P_<hash>(secret, label + random)[0..dlen]
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100784 return( ret );
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
787 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
788 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100789
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_md_hmac_reset ( &md_ctx );
793 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
794 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_reset ( &md_ctx );
797 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801
802 for( j = 0; j < k; j++ )
803 dstbuf[i + j] = h_i[j];
804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100807
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500808 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
809 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810
811 return( 0 );
812}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100815MBEDTLS_NO_INLINE static int tls_prf_sha256(
816 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100817 const char *label,
818 const unsigned char *random, size_t rlen,
819 unsigned char *dstbuf, size_t dlen )
820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 label, random, rlen, dstbuf, dlen ) );
823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100827MBEDTLS_NO_INLINE static int tls_prf_sha384(
828 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200829 const char *label,
830 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA512_C */
837#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100839/*
840 * Call the appropriate PRF function
841 */
Hanno Becker2793f742019-08-16 14:28:43 +0100842MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100843 mbedtls_md_type_t hash,
844 const unsigned char *secret, size_t slen,
845 const char *label,
846 const unsigned char *random, size_t rlen,
847 unsigned char *dstbuf, size_t dlen )
848{
849#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
850 (void) hash;
851#endif
852
853#if defined(MBEDTLS_SSL_PROTO_SSL3)
854 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
855 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
856 else
857#endif
858#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
859 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
860 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
864#if defined(MBEDTLS_SHA512_C)
865 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
866 hash == MBEDTLS_MD_SHA384 )
867 {
868 return( tls_prf_sha384( secret, slen, label, random, rlen,
869 dstbuf, dlen ) );
870 }
871 else
872#endif
873#if defined(MBEDTLS_SHA256_C)
874 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
875 {
876 return( tls_prf_sha256( secret, slen, label, random, rlen,
877 dstbuf, dlen ) );
878 }
879#endif
880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
881
882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
883}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200884
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100885#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100886MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100887 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
888{
889 const char *sender;
890 mbedtls_md5_context md5;
891 mbedtls_sha1_context sha1;
892
893 unsigned char padbuf[48];
894 unsigned char md5sum[16];
895 unsigned char sha1sum[20];
896
897 mbedtls_ssl_session *session = ssl->session_negotiate;
898 if( !session )
899 session = ssl->session;
900
901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
902
903 mbedtls_md5_init( &md5 );
904 mbedtls_sha1_init( &sha1 );
905
906 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
907 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
908
909 /*
910 * SSLv3:
911 * hash =
912 * MD5( master + pad2 +
913 * MD5( handshake + sender + master + pad1 ) )
914 * + SHA1( master + pad2 +
915 * SHA1( handshake + sender + master + pad1 ) )
916 */
917
918#if !defined(MBEDTLS_MD5_ALT)
919 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
920 md5.state, sizeof( md5.state ) );
921#endif
922
923#if !defined(MBEDTLS_SHA1_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
925 sha1.state, sizeof( sha1.state ) );
926#endif
927
928 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
929 : "SRVR";
930
931 memset( padbuf, 0x36, 48 );
932
933 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
934 mbedtls_md5_update_ret( &md5, session->master, 48 );
935 mbedtls_md5_update_ret( &md5, padbuf, 48 );
936 mbedtls_md5_finish_ret( &md5, md5sum );
937
938 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
939 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
940 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
941 mbedtls_sha1_finish_ret( &sha1, sha1sum );
942
943 memset( padbuf, 0x5C, 48 );
944
945 mbedtls_md5_starts_ret( &md5 );
946 mbedtls_md5_update_ret( &md5, session->master, 48 );
947 mbedtls_md5_update_ret( &md5, padbuf, 48 );
948 mbedtls_md5_update_ret( &md5, md5sum, 16 );
949 mbedtls_md5_finish_ret( &md5, buf );
950
951 mbedtls_sha1_starts_ret( &sha1 );
952 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
953 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
954 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
955 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
956
957 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
958
959 mbedtls_md5_free( &md5 );
960 mbedtls_sha1_free( &sha1 );
961
962 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
963 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
964 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
965
966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
967}
968#endif /* MBEDTLS_SSL_PROTO_SSL3 */
969
970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100971MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100972 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
973{
974 int len = 12;
975 const char *sender;
976 mbedtls_md5_context md5;
977 mbedtls_sha1_context sha1;
978 unsigned char padbuf[36];
979
980 mbedtls_ssl_session *session = ssl->session_negotiate;
981 if( !session )
982 session = ssl->session;
983
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
985
986 mbedtls_md5_init( &md5 );
987 mbedtls_sha1_init( &sha1 );
988
989 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
990 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
991
992 /*
993 * TLSv1:
994 * hash = PRF( master, finished_label,
995 * MD5( handshake ) + SHA1( handshake ) )[0..11]
996 */
997
998#if !defined(MBEDTLS_MD5_ALT)
999 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1000 md5.state, sizeof( md5.state ) );
1001#endif
1002
1003#if !defined(MBEDTLS_SHA1_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1005 sha1.state, sizeof( sha1.state ) );
1006#endif
1007
1008 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1009 ? "client finished"
1010 : "server finished";
1011
1012 mbedtls_md5_finish_ret( &md5, padbuf );
1013 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1014
1015 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1016 mbedtls_ssl_suite_get_mac(
1017 mbedtls_ssl_ciphersuite_from_id(
1018 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1019 session->master, 48, sender,
1020 padbuf, 36, buf, len );
1021
1022 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1023
1024 mbedtls_md5_free( &md5 );
1025 mbedtls_sha1_free( &sha1 );
1026
1027 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1028
1029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1030}
1031#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1032
1033#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1034#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001035MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001036 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1037{
1038 int len = 12;
1039 const char *sender;
1040 mbedtls_sha256_context sha256;
1041 unsigned char padbuf[32];
1042
1043 mbedtls_ssl_session *session = ssl->session_negotiate;
1044 if( !session )
1045 session = ssl->session;
1046
1047 mbedtls_sha256_init( &sha256 );
1048
1049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1050
1051 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1052
1053 /*
1054 * TLSv1.2:
1055 * hash = PRF( master, finished_label,
1056 * Hash( handshake ) )[0.11]
1057 */
1058
1059#if !defined(MBEDTLS_SHA256_ALT)
1060 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1061 sha256.state, sizeof( sha256.state ) );
1062#endif
1063
1064 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1065 ? "client finished"
1066 : "server finished";
1067
1068 mbedtls_sha256_finish_ret( &sha256, padbuf );
1069
1070 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1071 mbedtls_ssl_suite_get_mac(
1072 mbedtls_ssl_ciphersuite_from_id(
1073 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1074 session->master, 48, sender,
1075 padbuf, 32, buf, len );
1076
1077 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1078
1079 mbedtls_sha256_free( &sha256 );
1080
1081 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1082
1083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1084}
1085#endif /* MBEDTLS_SHA256_C */
1086
1087#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001088MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001089 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1090{
1091 int len = 12;
1092 const char *sender;
1093 mbedtls_sha512_context sha512;
1094 unsigned char padbuf[48];
1095
1096 mbedtls_ssl_session *session = ssl->session_negotiate;
1097 if( !session )
1098 session = ssl->session;
1099
1100 mbedtls_sha512_init( &sha512 );
1101
1102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1103
1104 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1105
1106 /*
1107 * TLSv1.2:
1108 * hash = PRF( master, finished_label,
1109 * Hash( handshake ) )[0.11]
1110 */
1111
1112#if !defined(MBEDTLS_SHA512_ALT)
1113 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1114 sha512.state, sizeof( sha512.state ) );
1115#endif
1116
1117 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1118 ? "client finished"
1119 : "server finished";
1120
1121 mbedtls_sha512_finish_ret( &sha512, padbuf );
1122
1123 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1124 mbedtls_ssl_suite_get_mac(
1125 mbedtls_ssl_ciphersuite_from_id(
1126 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1127 session->master, 48, sender,
1128 padbuf, 48, buf, len );
1129
1130 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1131
1132 mbedtls_sha512_free( &sha512 );
1133
1134 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1135
1136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1137}
1138#endif /* MBEDTLS_SHA512_C */
1139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1140
Hanno Becker2793f742019-08-16 14:28:43 +01001141MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1142 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001143 mbedtls_md_type_t hash,
1144 mbedtls_ssl_context *ssl,
1145 unsigned char *buf,
1146 int from )
1147{
1148#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1149 (void) hash;
1150#endif
1151
1152#if defined(MBEDTLS_SSL_PROTO_SSL3)
1153 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1154 ssl_calc_finished_ssl( ssl, buf, from );
1155 else
1156#endif
1157#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1158 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1159 ssl_calc_finished_tls( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1163#if defined(MBEDTLS_SHA512_C)
1164 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1165 hash == MBEDTLS_MD_SHA384 )
1166 {
1167 ssl_calc_finished_tls_sha384( ssl, buf, from );
1168 }
1169 else
1170#endif
1171#if defined(MBEDTLS_SHA256_C)
1172 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1173 ssl_calc_finished_tls_sha256( ssl, buf, from );
1174 else
1175#endif
1176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1178
1179 return( 0 );
1180}
1181
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001182/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001183 * Populate a transform structure with session keys and all the other
1184 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001185 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001186 * Parameters:
1187 * - [in/out]: transform: structure to populate
1188 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001189 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001190 * - [in] ciphersuite
1191 * - [in] master
1192 * - [in] encrypt_then_mac
1193 * - [in] trunc_hmac
1194 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001195 * - [in] tls_prf: pointer to PRF to use for key derivation
1196 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001197 * - [in] minor_ver: SSL/TLS minor version
1198 * - [in] endpoint: client or server
1199 * - [in] ssl: optionally used for:
1200 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1201 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1202 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001203 */
Hanno Becker298a4702019-08-16 10:21:32 +01001204/* Force compilers to inline this function if it's used only
1205 * from one place, because at least ARMC5 doesn't do that
1206 * automatically. */
1207#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1208MBEDTLS_ALWAYS_INLINE static inline
1209#else
1210static
1211#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1212int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001213 int ciphersuite,
1214 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001215#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1217 int encrypt_then_mac,
1218#endif
1219#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1220 int trunc_hmac,
1221#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001222#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001223#if defined(MBEDTLS_ZLIB_SUPPORT)
1224 int compression,
1225#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001226 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001227 int minor_ver,
1228 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001229 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001230{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001231 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 unsigned char keyblk[256];
1233 unsigned char *key1;
1234 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001235 unsigned char *mac_enc;
1236 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001237 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001238 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001239 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001240 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 const mbedtls_cipher_info_t *cipher_info;
1242 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001243
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001244#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1245 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1246 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001247 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001248 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001249#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001250
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001251 /*
1252 * Some data just needs copying into the structure
1253 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001254#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1255 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001256 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001258
1259#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001260 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001261#else
1262 ((void) minor_ver);
1263#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001265#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1266 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1267#endif
1268
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001269 /*
1270 * Get various info structures
1271 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001272 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001273 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 {
1275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001276 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1278 }
1279
Hanno Becker473f98f2019-06-26 10:27:32 +01001280 cipher_info = mbedtls_cipher_info_from_type(
1281 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001282 if( cipher_info == NULL )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 }
1288
Hanno Becker473f98f2019-06-26 10:27:32 +01001289 md_info = mbedtls_md_info_from_type(
1290 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001291 if( md_info == NULL )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 }
1297
Hanno Beckera5a2b082019-05-15 14:03:01 +01001298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001299 /* Copy own and peer's CID if the use of the CID
1300 * extension has been negotiated. */
1301 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1302 {
1303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001304
Hanno Becker4932f9f2019-05-03 15:23:51 +01001305 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001306 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001307 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001308 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001309
1310 transform->out_cid_len = ssl->handshake->peer_cid_len;
1311 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1312 ssl->handshake->peer_cid_len );
1313 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1314 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001315 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001316#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001319 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001320 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001321 ret = ssl_prf( minor_ver,
1322 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1323 master, 48, "key expansion", randbytes, 64,
1324 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001325 if( ret != 0 )
1326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001328 return( ret );
1329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001332 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001333 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001334 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 /*
1338 * Determine the appropriate key, IV and MAC length.
1339 */
Paul Bakker68884e32013-01-07 18:20:04 +01001340
Hanno Beckere7f2df02017-12-27 08:17:40 +00001341 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001342
Hanno Beckerf1229442018-01-03 15:32:31 +00001343#if defined(MBEDTLS_GCM_C) || \
1344 defined(MBEDTLS_CCM_C) || \
1345 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001347 cipher_info->mode == MBEDTLS_MODE_CCM ||
1348 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001350 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001351 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001352 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1353 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001354
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001355 /* All modes haves 96-bit IVs;
1356 * GCM and CCM has 4 implicit and 8 explicit bytes
1357 * ChachaPoly has all 12 bytes implicit
1358 */
Paul Bakker68884e32013-01-07 18:20:04 +01001359 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1361 transform->fixed_ivlen = 12;
1362 else
1363 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001364 }
1365 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001366#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1367#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1368 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1369 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001370 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001371 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1373 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001379 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 mac_key_len = mbedtls_md_get_size( md_info );
1381 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /*
1385 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1386 * (rfc 6066 page 13 or rfc 2104 section 4),
1387 * so we only need to adjust the length here.
1388 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001389 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001392
1393#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1394 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001395 * HMAC implementation which also truncates the key
1396 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001397 mac_key_len = transform->maclen;
1398#endif
1399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001401
1402 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001403 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001405 else
1406#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Hanno Beckera9d5c452019-07-25 16:47:12 +01001412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001413 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001414 (unsigned) transform->ivlen,
1415 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
1417 /*
1418 * Finally setup the cipher contexts, IVs and MAC secrets.
1419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001421 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001423 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001424 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Paul Bakker68884e32013-01-07 18:20:04 +01001426 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001427 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001429 /*
1430 * This is not used in TLS v1.1.
1431 */
Paul Bakker48916f92012-09-16 19:57:18 +00001432 iv_copy_len = ( transform->fixed_ivlen ) ?
1433 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001434 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1435 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
1438 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_SSL_CLI_C */
1440#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001441 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001443 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001444 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Hanno Becker81c7b182017-11-09 18:39:33 +00001446 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001447 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001449 /*
1450 * This is not used in TLS v1.1.
1451 */
Paul Bakker48916f92012-09-16 19:57:18 +00001452 iv_copy_len = ( transform->fixed_ivlen ) ?
1453 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001454 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1455 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001456 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Hanno Becker92231322018-01-03 15:32:51 +00001465#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001467 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001468 {
Hanno Becker92231322018-01-03 15:32:51 +00001469 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001473 }
1474
Hanno Becker81c7b182017-11-09 18:39:33 +00001475 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1476 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001477 }
1478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1480#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1481 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001482 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001483 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001484 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1485 For AEAD-based ciphersuites, there is nothing to do here. */
1486 if( mac_key_len != 0 )
1487 {
1488 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1489 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1490 }
Paul Bakker68884e32013-01-07 18:20:04 +01001491 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001492 else
1493#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001497 }
Hanno Becker92231322018-01-03 15:32:51 +00001498#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1501 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001502 {
1503 int ret = 0;
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001506
Hanno Beckere7f2df02017-12-27 08:17:40 +00001507 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001508 transform->iv_enc, transform->iv_dec,
1509 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001510 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001511 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1514 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001515 }
1516 }
Hanno Becker92231322018-01-03 15:32:51 +00001517#else
1518 ((void) mac_dec);
1519 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001521
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001522#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1523 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001524 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001525 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001526 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001527 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001528 iv_copy_len );
1529 }
1530#endif
1531
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001532 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001533 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001536 return( ret );
1537 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001538
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001539 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001540 cipher_info ) ) != 0 )
1541 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001543 return( ret );
1544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001547 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001551 return( ret );
1552 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001555 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559 return( ret );
1560 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_CIPHER_MODE_CBC)
1563 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1566 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001570 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1573 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 return( ret );
1577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001581 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001583 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001585 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588
Paul Bakker48916f92012-09-16 19:57:18 +00001589 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1590 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001592 if( deflateInit( &transform->ctx_deflate,
1593 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001594 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1597 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001598 }
1599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 return( 0 );
1603}
1604
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001605#if defined(MBEDTLS_SSL_PROTO_SSL3)
1606static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1607 unsigned char hash[36],
1608 size_t *hlen )
1609{
1610 mbedtls_md5_context md5;
1611 mbedtls_sha1_context sha1;
1612 unsigned char pad_1[48];
1613 unsigned char pad_2[48];
1614
1615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1616
1617 mbedtls_md5_init( &md5 );
1618 mbedtls_sha1_init( &sha1 );
1619
1620 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1621 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1622
1623 memset( pad_1, 0x36, 48 );
1624 memset( pad_2, 0x5C, 48 );
1625
1626 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1627 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1628 mbedtls_md5_finish_ret( &md5, hash );
1629
1630 mbedtls_md5_starts_ret( &md5 );
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1633 mbedtls_md5_update_ret( &md5, hash, 16 );
1634 mbedtls_md5_finish_ret( &md5, hash );
1635
1636 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1637 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1638 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1639
1640 mbedtls_sha1_starts_ret( &sha1 );
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1643 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1644 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1645
1646 *hlen = 36;
1647
1648 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1650
1651 mbedtls_md5_free( &md5 );
1652 mbedtls_sha1_free( &sha1 );
1653
1654 return;
1655}
1656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1657
1658#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1659static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1660 unsigned char hash[36],
1661 size_t *hlen )
1662{
1663 mbedtls_md5_context md5;
1664 mbedtls_sha1_context sha1;
1665
1666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1667
1668 mbedtls_md5_init( &md5 );
1669 mbedtls_sha1_init( &sha1 );
1670
1671 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1672 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1673
1674 mbedtls_md5_finish_ret( &md5, hash );
1675 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1676
1677 *hlen = 36;
1678
1679 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1681
1682 mbedtls_md5_free( &md5 );
1683 mbedtls_sha1_free( &sha1 );
1684
1685 return;
1686}
1687#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1688
1689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1690#if defined(MBEDTLS_SHA256_C)
1691static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1692 unsigned char hash[32],
1693 size_t *hlen )
1694{
1695 mbedtls_sha256_context sha256;
1696
1697 mbedtls_sha256_init( &sha256 );
1698
1699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1700
1701 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1702 mbedtls_sha256_finish_ret( &sha256, hash );
1703
1704 *hlen = 32;
1705
1706 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1708
1709 mbedtls_sha256_free( &sha256 );
1710
1711 return;
1712}
1713#endif /* MBEDTLS_SHA256_C */
1714
1715#if defined(MBEDTLS_SHA512_C)
1716static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1717 unsigned char hash[48],
1718 size_t *hlen )
1719{
1720 mbedtls_sha512_context sha512;
1721
1722 mbedtls_sha512_init( &sha512 );
1723
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1725
1726 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1727 mbedtls_sha512_finish_ret( &sha512, hash );
1728
1729 *hlen = 48;
1730
1731 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1733
1734 mbedtls_sha512_free( &sha512 );
1735
1736 return;
1737}
1738#endif /* MBEDTLS_SHA512_C */
1739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1740
Hanno Becker2f41b242019-08-15 17:29:43 +01001741int mbedtls_ssl_calc_verify( int minor_ver,
1742 mbedtls_md_type_t hash,
1743 mbedtls_ssl_context const *ssl,
1744 unsigned char *dst,
1745 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001746{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001747#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1748 (void) hash;
1749#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001750
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001753 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001754 else
1755#endif
1756#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1762#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001763 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1764 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001765 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001766 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001767 }
1768 else
1769#endif
1770#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001771 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001773 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001774 }
1775 else
1776#endif
1777#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1778 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1780 }
1781
1782 return( 0 );
1783}
1784
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001785/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001786 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001787 *
1788 * Parameters:
1789 * [in/out] handshake
1790 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1791 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 * [out] master
1793 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001794 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001795static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001796 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001797 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001798{
1799 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001801/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1802/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1803/* (void) ssl; */
1804/* #endif */
1805
1806 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1807 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001808
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001809#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001810 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001811 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1813 return( 0 );
1814 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001815#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001816
1817 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1818 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001819
1820#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001821 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1822 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001823 {
1824 unsigned char session_hash[48];
1825 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001826
Hanno Becker2f41b242019-08-15 17:29:43 +01001827 mbedtls_ssl_calc_verify(
1828 mbedtls_ssl_get_minor_ver( ssl ),
1829 mbedtls_ssl_suite_get_mac( ciphersuite ),
1830 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001831
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001832 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1833 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001834
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001835 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1836 mbedtls_ssl_suite_get_mac( ciphersuite ),
1837 handshake->premaster, handshake->pmslen,
1838 "extended master secret",
1839 session_hash, hash_len,
1840 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001841 }
1842 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001844 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001845 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1846 mbedtls_ssl_suite_get_mac( ciphersuite ),
1847 handshake->premaster, handshake->pmslen,
1848 "master secret",
1849 handshake->randbytes, 64,
1850 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001851 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001852 if( ret != 0 )
1853 {
1854 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1855 return( ret );
1856 }
1857
1858 mbedtls_platform_zeroize( handshake->premaster,
1859 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001860
1861 return( 0 );
1862}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001863
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001864int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1865{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001866 int ret;
1867
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1869
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001870 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001871 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001872 ssl->session_negotiate->master,
1873 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001874 if( ret != 0 )
1875 {
1876 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1877 return( ret );
1878 }
1879
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001880 /* Swap the client and server random values:
1881 * - MS derivation wanted client+server (RFC 5246 8.1)
1882 * - key derivation wants server+client (RFC 5246 6.3) */
1883 {
1884 unsigned char tmp[64];
1885 memcpy( tmp, ssl->handshake->randbytes, 64 );
1886 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1887 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1888 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1889 }
1890
1891 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001892 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001893 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1894 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001895#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001896#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001897 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001898#endif
1899#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001900 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001902#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001904 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001905#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001906 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001907 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001908 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1909 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001910 if( ret != 0 )
1911 {
1912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1913 return( ret );
1914 }
1915
1916 /* We no longer need Server/ClientHello.random values */
1917 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1918 sizeof( ssl->handshake->randbytes ) );
1919
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001920 /* Allocate compression buffer */
1921#if defined(MBEDTLS_ZLIB_SUPPORT)
1922 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1923 ssl->compress_buf == NULL )
1924 {
1925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1926 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1927 if( ssl->compress_buf == NULL )
1928 {
1929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001930 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001931 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1932 }
1933 }
1934#endif
1935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1937
1938 return( 0 );
1939}
1940
Hanno Becker09d23642019-07-22 17:18:18 +01001941int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1942{
1943 int ret;
1944
1945 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1946 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1947
Hanno Beckera3c2c172019-07-23 16:51:57 +01001948#if defined(MBEDTLS_USE_TINYCRYPT)
1949 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1950 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1951 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1952 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA )
1953 {
1954 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001955 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956
1957 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1958 ssl->handshake->ecdh_privkey,
1959 ssl->handshake->premaster,
1960 uecc_curve ) )
1961 {
1962 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1963 }
1964
1965 ssl->handshake->pmslen = NUM_ECC_BYTES;
1966 }
1967 else
1968#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001969#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1970 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1971 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1972 {
1973 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1974 ssl->handshake->premaster,
1975 MBEDTLS_PREMASTER_SIZE,
1976 &ssl->handshake->pmslen,
1977 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001978 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001979 {
1980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1981 return( ret );
1982 }
1983
1984 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1985 }
1986 else
1987#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001988#if defined(MBEDTLS_ECDH_C) && \
1989 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1990 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1991 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1992 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01001993 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1994 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
1995 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1996 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1997 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1998 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1999 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2000 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2001 {
2002 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2003 &ssl->handshake->pmslen,
2004 ssl->handshake->premaster,
2005 MBEDTLS_MPI_MAX_SIZE,
2006 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002007 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002008 {
2009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2010 return( ret );
2011 }
2012
2013 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2014 }
2015 else
2016#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2017 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2018 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2019 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2020#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2021 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2022 {
2023 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002024 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002025 {
2026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2027 return( ret );
2028 }
2029 }
2030 else
2031#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2032#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2033 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2034 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2035 {
2036 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2037 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2038 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002039 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002040 if( ret != 0 )
2041 {
2042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2043 return( ret );
2044 }
2045 }
2046 else
2047#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2048#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2049 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2050 == MBEDTLS_KEY_EXCHANGE_RSA )
2051 {
2052 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002053 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002054 * ssl_rsa_generate_partial_pms(). Only the
2055 * PMS length needs to be set. */
2056 ssl->handshake->pmslen = 48;
2057 }
2058 else
2059#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2060 {
2061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2063 }
2064
2065 return( 0 );
2066}
2067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2069int 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 +02002070{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002071 unsigned char *p = ssl->handshake->premaster;
2072 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002073 const unsigned char *psk = ssl->conf->psk;
2074 size_t psk_len = ssl->conf->psk_len;
2075
2076 /* If the psk callback was called, use its result */
2077 if( ssl->handshake->psk != NULL )
2078 {
2079 psk = ssl->handshake->psk;
2080 psk_len = ssl->handshake->psk_len;
2081 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002082
2083 /*
2084 * PMS = struct {
2085 * opaque other_secret<0..2^16-1>;
2086 * opaque psk<0..2^16-1>;
2087 * };
2088 * with "other_secret" depending on the particular key exchange
2089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2091 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002092 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002093 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002095
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002096 *(p++) = (unsigned char)( psk_len >> 8 );
2097 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002098
2099 if( end < p || (size_t)( end - p ) < psk_len )
2100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2101
2102 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002103 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104 }
2105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2107#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2108 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002109 {
2110 /*
2111 * other_secret already set by the ClientKeyExchange message,
2112 * and is 48 bytes long
2113 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002114 if( end - p < 2 )
2115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2116
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002117 *p++ = 0;
2118 *p++ = 48;
2119 p += 48;
2120 }
2121 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2123#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2124 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002125 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002126 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002127 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002128
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002129 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002131 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002132 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002133 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002136 return( ret );
2137 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002138 *(p++) = (unsigned char)( len >> 8 );
2139 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002140 p += len;
2141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002143 }
2144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2146#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2147 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002148 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002149 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002150 size_t zlen;
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002153 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002154 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002155 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002158 return( ret );
2159 }
2160
2161 *(p++) = (unsigned char)( zlen >> 8 );
2162 *(p++) = (unsigned char)( zlen );
2163 p += zlen;
2164
Janos Follath3fbdada2018-08-15 10:26:53 +01002165 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2166 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002167 }
2168 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002173 }
2174
2175 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002176 if( end - p < 2 )
2177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002178
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002179 *(p++) = (unsigned char)( psk_len >> 8 );
2180 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181
2182 if( end < p || (size_t)( end - p ) < psk_len )
2183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2184
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002185 memcpy( p, psk, psk_len );
2186 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002187
2188 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2189
2190 return( 0 );
2191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002195/*
2196 * SSLv3.0 MAC functions
2197 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002198#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002199static void ssl_mac( mbedtls_md_context_t *md_ctx,
2200 const unsigned char *secret,
2201 const unsigned char *buf, size_t len,
2202 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002203 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204{
2205 unsigned char header[11];
2206 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002207 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2209 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002210
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002211 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002213 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002214 else
Paul Bakker68884e32013-01-07 18:20:04 +01002215 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 memcpy( header, ctr, 8 );
2218 header[ 8] = (unsigned char) type;
2219 header[ 9] = (unsigned char)( len >> 8 );
2220 header[10] = (unsigned char)( len );
2221
Paul Bakker68884e32013-01-07 18:20:04 +01002222 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 mbedtls_md_starts( md_ctx );
2224 mbedtls_md_update( md_ctx, secret, md_size );
2225 mbedtls_md_update( md_ctx, padding, padlen );
2226 mbedtls_md_update( md_ctx, header, 11 );
2227 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002228 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Paul Bakker68884e32013-01-07 18:20:04 +01002230 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_md_starts( md_ctx );
2232 mbedtls_md_update( md_ctx, secret, md_size );
2233 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002234 mbedtls_md_update( md_ctx, out, md_size );
2235 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002238
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002239/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002240 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002241#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002242 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2243 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2244 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2245/* This function makes sure every byte in the memory region is accessed
2246 * (in ascending addresses order) */
2247static void ssl_read_memory( unsigned char *p, size_t len )
2248{
2249 unsigned char acc = 0;
2250 volatile unsigned char force;
2251
2252 for( ; len != 0; p++, len-- )
2253 acc ^= *p;
2254
2255 force = acc;
2256 (void) force;
2257}
2258#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2259
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002260/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002262 */
Hanno Becker3307b532017-12-27 21:37:21 +00002263
Hanno Beckera5a2b082019-05-15 14:03:01 +01002264#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002265/* This functions transforms a DTLS plaintext fragment and a record content
2266 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002267 *
2268 * struct {
2269 * opaque content[DTLSPlaintext.length];
2270 * ContentType real_type;
2271 * uint8 zeros[length_of_padding];
2272 * } DTLSInnerPlaintext;
2273 *
2274 * Input:
2275 * - `content`: The beginning of the buffer holding the
2276 * plaintext to be wrapped.
2277 * - `*content_size`: The length of the plaintext in Bytes.
2278 * - `max_len`: The number of Bytes available starting from
2279 * `content`. This must be `>= *content_size`.
2280 * - `rec_type`: The desired record content type.
2281 *
2282 * Output:
2283 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2284 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2285 *
2286 * Returns:
2287 * - `0` on success.
2288 * - A negative error code if `max_len` didn't offer enough space
2289 * for the expansion.
2290 */
2291static int ssl_cid_build_inner_plaintext( unsigned char *content,
2292 size_t *content_size,
2293 size_t remaining,
2294 uint8_t rec_type )
2295{
2296 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002297 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2298 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2299 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002300
2301 /* Write real content type */
2302 if( remaining == 0 )
2303 return( -1 );
2304 content[ len ] = rec_type;
2305 len++;
2306 remaining--;
2307
2308 if( remaining < pad )
2309 return( -1 );
2310 memset( content + len, 0, pad );
2311 len += pad;
2312 remaining -= pad;
2313
2314 *content_size = len;
2315 return( 0 );
2316}
2317
Hanno Becker7dc25772019-05-20 15:08:01 +01002318/* This function parses a DTLSInnerPlaintext structure.
2319 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002320static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2321 size_t *content_size,
2322 uint8_t *rec_type )
2323{
2324 size_t remaining = *content_size;
2325
2326 /* Determine length of padding by skipping zeroes from the back. */
2327 do
2328 {
2329 if( remaining == 0 )
2330 return( -1 );
2331 remaining--;
2332 } while( content[ remaining ] == 0 );
2333
2334 *content_size = remaining;
2335 *rec_type = content[ remaining ];
2336
2337 return( 0 );
2338}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002339#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002340
Hanno Becker99abf512019-05-20 14:50:53 +01002341/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002342 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002343static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002344 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002345 mbedtls_record *rec )
2346{
Hanno Becker99abf512019-05-20 14:50:53 +01002347 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002348 *
2349 * additional_data = seq_num + TLSCompressed.type +
2350 * TLSCompressed.version + TLSCompressed.length;
2351 *
Hanno Becker99abf512019-05-20 14:50:53 +01002352 * For the CID extension, this is extended as follows
2353 * (quoting draft-ietf-tls-dtls-connection-id-05,
2354 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002355 *
2356 * additional_data = seq_num + DTLSPlaintext.type +
2357 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002358 * cid +
2359 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002360 * length_of_DTLSInnerPlaintext;
2361 */
2362
Hanno Becker3307b532017-12-27 21:37:21 +00002363 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2364 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002365 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002366
Hanno Beckera5a2b082019-05-15 14:03:01 +01002367#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002368 if( rec->cid_len != 0 )
2369 {
2370 memcpy( add_data + 11, rec->cid, rec->cid_len );
2371 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2372 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2373 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2374 *add_data_len = 13 + 1 + rec->cid_len;
2375 }
2376 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002377#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002378 {
2379 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2380 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2381 *add_data_len = 13;
2382 }
Hanno Becker3307b532017-12-27 21:37:21 +00002383}
2384
Hanno Becker611a83b2018-01-03 14:27:32 +00002385int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2386 mbedtls_ssl_transform *transform,
2387 mbedtls_record *rec,
2388 int (*f_rng)(void *, unsigned char *, size_t),
2389 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002392 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002393 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002394 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002395 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002396 size_t post_avail;
2397
2398 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002399#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002400 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002401 ((void) ssl);
2402#endif
2403
2404 /* The PRNG is used for dynamic IV generation that's used
2405 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2406#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2407 ( defined(MBEDTLS_AES_C) || \
2408 defined(MBEDTLS_ARIA_C) || \
2409 defined(MBEDTLS_CAMELLIA_C) ) && \
2410 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2411 ((void) f_rng);
2412 ((void) p_rng);
2413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Hanno Becker3307b532017-12-27 21:37:21 +00002417 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002418 {
Hanno Becker3307b532017-12-27 21:37:21 +00002419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2421 }
Hanno Becker505089d2019-05-01 09:45:57 +01002422 if( rec == NULL
2423 || rec->buf == NULL
2424 || rec->buf_len < rec->data_offset
2425 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002426#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002427 || rec->cid_len != 0
2428#endif
2429 )
Hanno Becker3307b532017-12-27 21:37:21 +00002430 {
2431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002433 }
2434
Hanno Becker3307b532017-12-27 21:37:21 +00002435 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002436 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002438 data, rec->data_len );
2439
2440 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2441
2442 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2443 {
2444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2445 (unsigned) rec->data_len,
2446 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2447 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2448 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002449
Hanno Beckera5a2b082019-05-15 14:03:01 +01002450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002451 /*
2452 * Add CID information
2453 */
2454 rec->cid_len = transform->out_cid_len;
2455 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2456 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002457
2458 if( rec->cid_len != 0 )
2459 {
2460 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002461 * Wrap plaintext into DTLSInnerPlaintext structure.
2462 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002463 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002464 * Note that this changes `rec->data_len`, and hence
2465 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002466 */
2467 if( ssl_cid_build_inner_plaintext( data,
2468 &rec->data_len,
2469 post_avail,
2470 rec->type ) != 0 )
2471 {
2472 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2473 }
2474
2475 rec->type = MBEDTLS_SSL_MSG_CID;
2476 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002478
Hanno Becker92c930f2019-04-29 17:31:37 +01002479 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2480
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002482 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002484#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 if( mode == MBEDTLS_MODE_STREAM ||
2486 ( mode == MBEDTLS_MODE_CBC
2487#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002488 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002489#endif
2490 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 {
Hanno Becker3307b532017-12-27 21:37:21 +00002492 if( post_avail < transform->maclen )
2493 {
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2495 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2496 }
2497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002499 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2500 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002501 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002502 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002503 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2504 data, rec->data_len, rec->ctr, rec->type, mac );
2505 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002506 }
2507 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2510 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002511 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2512 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002513 {
Hanno Becker992b6872017-11-09 18:57:39 +00002514 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2515
Hanno Beckere83efe62019-04-29 13:52:53 +01002516 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002517
Hanno Becker3307b532017-12-27 21:37:21 +00002518 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002519 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002520 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2521 data, rec->data_len );
2522 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2523 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2524
2525 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 }
2527 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002528#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002532 }
2533
Hanno Becker3307b532017-12-27 21:37:21 +00002534 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2535 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002536
Hanno Becker3307b532017-12-27 21:37:21 +00002537 rec->data_len += transform->maclen;
2538 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002539 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002540 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002541#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002543 /*
2544 * Encrypt
2545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2547 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002549 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002550 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002552 "including %d bytes of padding",
2553 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Hanno Becker3307b532017-12-27 21:37:21 +00002555 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2556 transform->iv_enc, transform->ivlen,
2557 data, rec->data_len,
2558 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002561 return( ret );
2562 }
2563
Hanno Becker3307b532017-12-27 21:37:21 +00002564 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
Paul Bakker68884e32013-01-07 18:20:04 +01002570 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002573#if defined(MBEDTLS_GCM_C) || \
2574 defined(MBEDTLS_CCM_C) || \
2575 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002577 mode == MBEDTLS_MODE_CCM ||
2578 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002579 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002580 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002581 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002582 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002583
Hanno Becker3307b532017-12-27 21:37:21 +00002584 /* Check that there's space for both the authentication tag
2585 * and the explicit IV before and after the record content. */
2586 if( post_avail < transform->taglen ||
2587 rec->data_offset < explicit_iv_len )
2588 {
2589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2590 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2591 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592
Paul Bakker68884e32013-01-07 18:20:04 +01002593 /*
2594 * Generate IV
2595 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002596 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2597 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002598 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002600 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2601 explicit_iv_len );
2602 /* Prefix record content with explicit IV. */
2603 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 }
2605 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2606 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002607 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 unsigned char i;
2609
2610 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2611
2612 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002613 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 }
2615 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002616 {
2617 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2619 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002620 }
2621
Hanno Beckere83efe62019-04-29 13:52:53 +01002622 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002623
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2625 iv, transform->ivlen );
2626 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002627 data - explicit_iv_len, explicit_iv_len );
2628 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002629 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002631 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002632 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002633
Paul Bakker68884e32013-01-07 18:20:04 +01002634 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002635 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002636 */
Hanno Becker3307b532017-12-27 21:37:21 +00002637
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002639 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002640 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002641 data, rec->data_len, /* source */
2642 data, &rec->data_len, /* destination */
2643 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002646 return( ret );
2647 }
2648
Hanno Becker3307b532017-12-27 21:37:21 +00002649 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2650 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002651
Hanno Becker3307b532017-12-27 21:37:21 +00002652 rec->data_len += transform->taglen + explicit_iv_len;
2653 rec->data_offset -= explicit_iv_len;
2654 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002655 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2659#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002660 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002663 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002664 size_t padlen, i;
2665 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002666
Hanno Becker3307b532017-12-27 21:37:21 +00002667 /* Currently we're always using minimal padding
2668 * (up to 255 bytes would be allowed). */
2669 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2670 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 padlen = 0;
2672
Hanno Becker3307b532017-12-27 21:37:21 +00002673 /* Check there's enough space in the buffer for the padding. */
2674 if( post_avail < padlen + 1 )
2675 {
2676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2677 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2678 }
2679
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002681 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Hanno Becker3307b532017-12-27 21:37:21 +00002683 rec->data_len += padlen + 1;
2684 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002687 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002688 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2689 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002690 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002691 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2692 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002693 {
Hanno Becker3307b532017-12-27 21:37:21 +00002694 if( f_rng == NULL )
2695 {
2696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2698 }
2699
2700 if( rec->data_offset < transform->ivlen )
2701 {
2702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2703 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2704 }
2705
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002706 /*
2707 * Generate IV
2708 */
Hanno Becker3307b532017-12-27 21:37:21 +00002709 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002710 if( ret != 0 )
2711 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002712
Hanno Becker3307b532017-12-27 21:37:21 +00002713 memcpy( data - transform->ivlen, transform->iv_enc,
2714 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002720 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002721 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002722 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Hanno Becker3307b532017-12-27 21:37:21 +00002724 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2725 transform->iv_enc,
2726 transform->ivlen,
2727 data, rec->data_len,
2728 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002731 return( ret );
2732 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002733
Hanno Becker3307b532017-12-27 21:37:21 +00002734 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002738 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002741 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2742 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002743 {
2744 /*
2745 * Save IV in SSL3 and TLS1
2746 */
Hanno Becker3307b532017-12-27 21:37:21 +00002747 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2748 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
Hanno Becker3307b532017-12-27 21:37:21 +00002750 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002751#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002752 {
2753 data -= transform->ivlen;
2754 rec->data_offset -= transform->ivlen;
2755 rec->data_len += transform->ivlen;
2756 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002759 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002760 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002761 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2762
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002763 /*
2764 * MAC(MAC_write_key, seq_num +
2765 * TLSCipherText.type +
2766 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002767 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768 * IV + // except for TLS 1.0
2769 * ENC(content + padding + padding_length));
2770 */
Hanno Becker3307b532017-12-27 21:37:21 +00002771
2772 if( post_avail < transform->maclen)
2773 {
2774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2775 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2776 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002781 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002782 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002783
Hanno Becker3307b532017-12-27 21:37:21 +00002784 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002785 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002786 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2787 data, rec->data_len );
2788 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2789 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002790
Hanno Becker3307b532017-12-27 21:37:21 +00002791 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002792
Hanno Becker3307b532017-12-27 21:37:21 +00002793 rec->data_len += transform->maclen;
2794 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002795 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002799 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002801 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002807 /* Make extra sure authentication was performed, exactly once */
2808 if( auth_done != 1 )
2809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002812 }
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
2816 return( 0 );
2817}
2818
Hanno Becker40478be2019-07-12 08:23:59 +01002819int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002820 mbedtls_ssl_transform *transform,
2821 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002823 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002825 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002826#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002827 size_t padlen = 0, correct = 1;
2828#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002829 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002830 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002831 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002832
Hanno Becker611a83b2018-01-03 14:27:32 +00002833#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002834 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002835 ((void) ssl);
2836#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002839 if( rec == NULL ||
2840 rec->buf == NULL ||
2841 rec->buf_len < rec->data_offset ||
2842 rec->buf_len - rec->data_offset < rec->data_len )
2843 {
2844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002846 }
2847
Hanno Becker4c6876b2017-12-27 21:28:58 +00002848 data = rec->buf + rec->data_offset;
2849 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Hanno Beckera5a2b082019-05-15 14:03:01 +01002851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002852 /*
2853 * Match record's CID with incoming CID.
2854 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002855 if( rec->cid_len != transform->in_cid_len ||
2856 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2857 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002858 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002859 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2863 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002864 {
2865 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002866 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2867 transform->iv_dec,
2868 transform->ivlen,
2869 data, rec->data_len,
2870 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002873 return( ret );
2874 }
2875
Hanno Becker4c6876b2017-12-27 21:28:58 +00002876 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 }
Paul Bakker68884e32013-01-07 18:20:04 +01002882 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002884#if defined(MBEDTLS_GCM_C) || \
2885 defined(MBEDTLS_CCM_C) || \
2886 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002888 mode == MBEDTLS_MODE_CCM ||
2889 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002890 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002891 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002892 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002893
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002894 /*
2895 * Prepare IV from explicit and implicit data.
2896 */
2897
2898 /* Check that there's enough space for the explicit IV
2899 * (at the beginning of the record) and the MAC (at the
2900 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002901 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002904 "+ taglen (%d)", rec->data_len,
2905 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002907 }
Paul Bakker68884e32013-01-07 18:20:04 +01002908
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002909#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002910 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2911 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002912 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002913
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002914 /* Fixed */
2915 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2916 /* Explicit */
2917 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002918 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002919 else
2920#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2921#if defined(MBEDTLS_CHACHAPOLY_C)
2922 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002923 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002924 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002925 unsigned char i;
2926
2927 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2928
2929 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002930 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002931 }
2932 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002933#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002934 {
2935 /* Reminder if we ever add an AEAD mode with a different size */
2936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2937 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2938 }
2939
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002940 /* Group changes to data, data_len, and add_data, because
2941 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002942 data += explicit_iv_len;
2943 rec->data_offset += explicit_iv_len;
2944 rec->data_len -= explicit_iv_len + transform->taglen;
2945
Hanno Beckere83efe62019-04-29 13:52:53 +01002946 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002948 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002949
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002950 /* Because of the check above, we know that there are
2951 * explicit_iv_len Bytes preceeding data, and taglen
2952 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002953 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002954 * mbedtls_cipher_auth_decrypt() below. */
2955
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002956 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002957 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002958 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002959
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002960 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002961 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002962 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002963 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2964 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002965 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 data, rec->data_len,
2967 data, &olen,
2968 data + rec->data_len,
2969 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2974 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002975
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002976 return( ret );
2977 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002978 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002979
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002980 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002981 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002985 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002986 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2989#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002990 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002993 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 /*
Paul Bakker45829992013-01-03 14:52:21 +01002996 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002999 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3000 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003001 {
3002 /* The ciphertext is prefixed with the CBC IV. */
3003 minlen += transform->ivlen;
3004 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif
Paul Bakker45829992013-01-03 14:52:21 +01003006
Hanno Becker4c6876b2017-12-27 21:28:58 +00003007 /* Size considerations:
3008 *
3009 * - The CBC cipher text must not be empty and hence
3010 * at least of size transform->ivlen.
3011 *
3012 * Together with the potential IV-prefix, this explains
3013 * the first of the two checks below.
3014 *
3015 * - The record must contain a MAC, either in plain or
3016 * encrypted, depending on whether Encrypt-then-MAC
3017 * is used or not.
3018 * - If it is, the message contains the IV-prefix,
3019 * the CBC ciphertext, and the MAC.
3020 * - If it is not, the padded plaintext, and hence
3021 * the CBC ciphertext, has at least length maclen + 1
3022 * because there is at least the padding length byte.
3023 *
3024 * As the CBC ciphertext is not empty, both cases give the
3025 * lower bound minlen + maclen + 1 on the record size, which
3026 * we test for in the second check below.
3027 */
3028 if( rec->data_len < minlen + transform->ivlen ||
3029 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 "+ 1 ) ( + expl IV )", rec->data_len,
3033 transform->ivlen,
3034 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003036 }
3037
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003038 /*
3039 * Authenticate before decrypt if enabled
3040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003042 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003043 {
Hanno Becker992b6872017-11-09 18:57:39 +00003044 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003047
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003048 /* Update data_len in tandem with add_data.
3049 *
3050 * The subtraction is safe because of the previous check
3051 * data_len >= minlen + maclen + 1.
3052 *
3053 * Afterwards, we know that data + data_len is followed by at
3054 * least maclen Bytes, which justifies the call to
3055 * mbedtls_ssl_safer_memcmp() below.
3056 *
3057 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003058 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003059 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003060
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003061 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003062 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3063 add_data_len );
3064 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3065 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3067 data, rec->data_len );
3068 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3069 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003070
Hanno Becker4c6876b2017-12-27 21:28:58 +00003071 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3072 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003073 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003074 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003075
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003076 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003077 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3078 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003082 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003083 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003086
3087 /*
3088 * Check length sanity
3089 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003090
3091 /* We know from above that data_len > minlen >= 0,
3092 * so the following check in particular implies that
3093 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003094 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003097 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003099 }
3100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003102 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003103 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003104 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003105 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3106 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003107 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003108 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003109 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003110
Hanno Becker4c6876b2017-12-27 21:28:58 +00003111 data += transform->ivlen;
3112 rec->data_offset += transform->ivlen;
3113 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003116
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003117 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3118
Hanno Becker4c6876b2017-12-27 21:28:58 +00003119 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3120 transform->iv_dec, transform->ivlen,
3121 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003124 return( ret );
3125 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003126
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003127 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3131 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003132 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003135 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3136 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003137 {
3138 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003139 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3140 * records is equivalent to CBC decryption of the concatenation
3141 * of the records; in other words, IVs are maintained across
3142 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003143 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003144 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3145 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Hanno Becker4c6876b2017-12-27 21:28:58 +00003149 /* Safe since data_len >= minlen + maclen + 1, so after having
3150 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003151 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3152 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003154
Hanno Becker4c6876b2017-12-27 21:28:58 +00003155 if( auth_done == 1 )
3156 {
3157 correct *= ( rec->data_len >= padlen + 1 );
3158 padlen *= ( rec->data_len >= padlen + 1 );
3159 }
3160 else
Paul Bakker45829992013-01-03 14:52:21 +01003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003163 if( rec->data_len < transform->maclen + padlen + 1 )
3164 {
3165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3166 rec->data_len,
3167 transform->maclen,
3168 padlen + 1 ) );
3169 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003170#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003171
3172 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3173 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176 padlen++;
3177
3178 /* Regardless of the validity of the padding,
3179 * we have data_len >= padlen here. */
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003182 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3183 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003185 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_SSL_DEBUG_ALL)
3188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003189 "should be no more than %d",
3190 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003191#endif
Paul Bakker45829992013-01-03 14:52:21 +01003192 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194 }
3195 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3197#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3198 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003199 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3200 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003202 /* The padding check involves a series of up to 256
3203 * consecutive memory reads at the end of the record
3204 * plaintext buffer. In order to hide the length and
3205 * validity of the padding, always perform exactly
3206 * `min(256,plaintext_len)` reads (but take into account
3207 * only the last `padlen` bytes for the padding check). */
3208 size_t pad_count = 0;
3209 size_t real_count = 0;
3210 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003211
Hanno Becker4c6876b2017-12-27 21:28:58 +00003212 /* Index of first padding byte; it has been ensured above
3213 * that the subtraction is safe. */
3214 size_t const padding_idx = rec->data_len - padlen;
3215 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3216 size_t const start_idx = rec->data_len - num_checks;
3217 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003218
Hanno Becker4c6876b2017-12-27 21:28:58 +00003219 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003220 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 real_count |= ( idx >= padding_idx );
3222 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003223 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003224 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003227 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003229#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003230 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003232 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3234 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003238 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003239
Hanno Becker4c6876b2017-12-27 21:28:58 +00003240 /* If the padding was found to be invalid, padlen == 0
3241 * and the subtraction is safe. If the padding was found valid,
3242 * padlen hasn't been changed and the previous assertion
3243 * data_len >= padlen still holds. */
3244 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003248 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003254#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003257#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003260 * Authenticate if not done yet.
3261 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003263#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003264 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003265 {
Hanno Becker992b6872017-11-09 18:57:39 +00003266 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003267
Hanno Becker4c6876b2017-12-27 21:28:58 +00003268 /* If the initial value of padlen was such that
3269 * data_len < maclen + padlen + 1, then padlen
3270 * got reset to 1, and the initial check
3271 * data_len >= minlen + maclen + 1
3272 * guarantees that at this point we still
3273 * have at least data_len >= maclen.
3274 *
3275 * If the initial value of padlen was such that
3276 * data_len >= maclen + padlen + 1, then we have
3277 * subtracted either padlen + 1 (if the padding was correct)
3278 * or 0 (if the padding was incorrect) since then,
3279 * hence data_len >= maclen in any case.
3280 */
3281 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003282 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003285 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3286 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003287 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003288 ssl_mac( &transform->md_ctx_dec,
3289 transform->mac_dec,
3290 data, rec->data_len,
3291 rec->ctr, rec->type,
3292 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003293 }
3294 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3296#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3297 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003298 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3299 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003300 {
3301 /*
3302 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003303 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003304 *
3305 * Known timing attacks:
3306 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3307 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003308 * To compensate for different timings for the MAC calculation
3309 * depending on how much padding was removed (which is determined
3310 * by padlen), process extra_run more blocks through the hash
3311 * function.
3312 *
3313 * The formula in the paper is
3314 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3315 * where L1 is the size of the header plus the decrypted message
3316 * plus CBC padding and L2 is the size of the header plus the
3317 * decrypted message. This is for an underlying hash function
3318 * with 64-byte blocks.
3319 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3320 * correctly. We round down instead of up, so -56 is the correct
3321 * value for our calculations instead of -55.
3322 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003323 * Repeat the formula rather than defining a block_size variable.
3324 * This avoids requiring division by a variable at runtime
3325 * (which would be marginally less efficient and would require
3326 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003327 */
3328 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003329 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003330
3331 /*
3332 * The next two sizes are the minimum and maximum values of
3333 * in_msglen over all padlen values.
3334 *
3335 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003336 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003337 *
3338 * Note that max_len + maclen is never more than the buffer
3339 * length, as we previously did in_msglen -= maclen too.
3340 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003341 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003342 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3343
Hanno Becker4c6876b2017-12-27 21:28:58 +00003344 memset( tmp, 0, sizeof( tmp ) );
3345
3346 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003347 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003348#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3349 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003350 case MBEDTLS_MD_MD5:
3351 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003352 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003353 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003354 extra_run =
3355 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3356 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003357 break;
3358#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003359#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003360 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003361 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003362 extra_run =
3363 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3364 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003365 break;
3366#endif
3367 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003369 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3370 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003371
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003372 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003373
Hanno Beckere83efe62019-04-29 13:52:53 +01003374 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3375 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003376 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3377 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003378 /* Make sure we access everything even when padlen > 0. This
3379 * makes the synchronisation requirements for just-in-time
3380 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003381 ssl_read_memory( data + rec->data_len, padlen );
3382 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003383
3384 /* Call mbedtls_md_process at least once due to cache attacks
3385 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003386 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003387 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003388
Hanno Becker4c6876b2017-12-27 21:28:58 +00003389 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003390
3391 /* Make sure we access all the memory that could contain the MAC,
3392 * before we check it in the next code block. This makes the
3393 * synchronisation requirements for just-in-time Prime+Probe
3394 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003395 ssl_read_memory( data + min_len,
3396 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 }
3398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3400 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003405
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003406#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003407 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3408 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003409#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Hanno Becker4c6876b2017-12-27 21:28:58 +00003411 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3412 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#if defined(MBEDTLS_SSL_DEBUG_ALL)
3415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003416#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003417 correct = 0;
3418 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003419 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003420 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003421
3422 /*
3423 * Finally check the correct flag
3424 */
3425 if( correct == 0 )
3426 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003427#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003428
3429 /* Make extra sure authentication was performed, exactly once */
3430 if( auth_done != 1 )
3431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
Hanno Beckera5a2b082019-05-15 14:03:01 +01003436#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003437 if( rec->cid_len != 0 )
3438 {
3439 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3440 &rec->type );
3441 if( ret != 0 )
3442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3443 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003444#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
3448 return( 0 );
3449}
3450
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003451#undef MAC_NONE
3452#undef MAC_PLAINTEXT
3453#undef MAC_CIPHERTEXT
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456/*
3457 * Compression/decompression functions
3458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003460{
3461 int ret;
3462 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003463 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003464 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003465 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003468
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003469 if( len_pre == 0 )
3470 return( 0 );
3471
Paul Bakker2770fbd2012-07-03 13:30:23 +00003472 memcpy( msg_pre, ssl->out_msg, len_pre );
3473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003475 ssl->out_msglen ) );
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003478 ssl->out_msg, ssl->out_msglen );
3479
Paul Bakker48916f92012-09-16 19:57:18 +00003480 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3481 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3482 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003483 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003484
Paul Bakker48916f92012-09-16 19:57:18 +00003485 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003486 if( ret != Z_OK )
3487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3489 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003490 }
3491
Angus Grattond8213d02016-05-25 20:56:48 +10003492 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003493 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003496 ssl->out_msglen ) );
3497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499 ssl->out_msg, ssl->out_msglen );
3500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502
3503 return( 0 );
3504}
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507{
3508 int ret;
3509 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003510 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003512 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003515
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003516 if( len_pre == 0 )
3517 return( 0 );
3518
Paul Bakker2770fbd2012-07-03 13:30:23 +00003519 memcpy( msg_pre, ssl->in_msg, len_pre );
3520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003522 ssl->in_msglen ) );
3523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525 ssl->in_msg, ssl->in_msglen );
3526
Paul Bakker48916f92012-09-16 19:57:18 +00003527 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3528 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3529 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003530 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003531 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532
Paul Bakker48916f92012-09-16 19:57:18 +00003533 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003534 if( ret != Z_OK )
3535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3537 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538 }
3539
Angus Grattond8213d02016-05-25 20:56:48 +10003540 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003541 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003544 ssl->in_msglen ) );
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 ssl->in_msg, ssl->in_msglen );
3548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550
3551 return( 0 );
3552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3556static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558#if defined(MBEDTLS_SSL_PROTO_DTLS)
3559static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003560{
3561 /* If renegotiation is not enforced, retransmit until we would reach max
3562 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003563 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003564 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003565 uint32_t ratio =
3566 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3567 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003568 unsigned char doublings = 1;
3569
3570 while( ratio != 0 )
3571 {
3572 ++doublings;
3573 ratio >>= 1;
3574 }
3575
3576 if( ++ssl->renego_records_seen > doublings )
3577 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003579 return( 0 );
3580 }
3581 }
3582
3583 return( ssl_write_hello_request( ssl ) );
3584}
3585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003587
Paul Bakker5121ce52009-01-03 21:22:43 +00003588/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003589 * Fill the input message buffer by appending data to it.
3590 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003591 *
3592 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3593 * available (from this read and/or a previous one). Otherwise, an error code
3594 * is returned (possibly EOF or WANT_READ).
3595 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003596 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3597 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3598 * since we always read a whole datagram at once.
3599 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003600 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003601 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604{
Paul Bakker23986e52011-04-24 08:57:21 +00003605 int ret;
3606 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Hanno Beckera58a8962019-06-13 16:11:15 +01003610 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3611 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003614 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003616 }
3617
Angus Grattond8213d02016-05-25 20:56:48 +10003618 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003622 }
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003625 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003627 uint32_t timeout;
3628
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003629 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003630 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3631 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003632 {
3633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3634 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3636 }
3637
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003638 /*
3639 * The point is, we need to always read a full datagram at once, so we
3640 * sometimes read more then requested, and handle the additional data.
3641 * It could be the rest of the current record (while fetching the
3642 * header) and/or some other records in the same datagram.
3643 */
3644
3645 /*
3646 * Move to the next record in the already read datagram if applicable
3647 */
3648 if( ssl->next_record_offset != 0 )
3649 {
3650 if( ssl->in_left < ssl->next_record_offset )
3651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003654 }
3655
3656 ssl->in_left -= ssl->next_record_offset;
3657
3658 if( ssl->in_left != 0 )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003661 ssl->next_record_offset ) );
3662 memmove( ssl->in_hdr,
3663 ssl->in_hdr + ssl->next_record_offset,
3664 ssl->in_left );
3665 }
3666
3667 ssl->next_record_offset = 0;
3668 }
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003672
3673 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003674 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003675 */
3676 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003679 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003680 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003681
3682 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003683 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003684 * are not at the beginning of a new record, the caller did something
3685 * wrong.
3686 */
3687 if( ssl->in_left != 0 )
3688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003691 }
3692
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003693 /*
3694 * Don't even try to read if time's out already.
3695 * This avoids by-passing the timer when repeatedly receiving messages
3696 * that will end up being dropped.
3697 */
3698 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003699 {
3700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003701 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003702 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003703 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003704 {
Angus Grattond8213d02016-05-25 20:56:48 +10003705 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003708 timeout = ssl->handshake->retransmit_timeout;
3709 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003710 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003713
Hanno Beckera58a8962019-06-13 16:11:15 +01003714 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3715 {
3716 ret = mbedtls_ssl_get_recv_timeout( ssl )
3717 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3718 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003719 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003720 {
3721 ret = mbedtls_ssl_get_recv( ssl )
3722 ( ssl->p_bio, ssl->in_hdr, len );
3723 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003726
3727 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729 }
3730
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003731 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003734 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003737 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003738 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003741 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003742 }
3743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003747 return( ret );
3748 }
3749
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003750 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003753 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3754 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003756 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003757 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003760 return( ret );
3761 }
3762
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003763 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003766 }
3767
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 if( ret < 0 )
3769 return( ret );
3770
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003771 ssl->in_left = ret;
3772 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003773 MBEDTLS_SSL_TRANSPORT_ELSE
3774#endif /* MBEDTLS_SSL_PROTO_DTLS */
3775#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003778 ssl->in_left, nb_want ) );
3779
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003780 while( ssl->in_left < nb_want )
3781 {
3782 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003783
3784 if( ssl_check_timer( ssl ) != 0 )
3785 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3786 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003787 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003788 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003789 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003790 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3791 ssl->in_hdr + ssl->in_left, len,
3792 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003793 }
3794 else
3795 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003796 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003797 ssl->in_hdr + ssl->in_left, len );
3798 }
3799 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003802 ssl->in_left, nb_want ) );
3803 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003804
3805 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003807
3808 if( ret < 0 )
3809 return( ret );
3810
mohammad160352aecb92018-03-28 23:41:40 -07003811 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003812 {
Darryl Green11999bb2018-03-13 15:22:58 +00003813 MBEDTLS_SSL_DEBUG_MSG( 1,
3814 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003815 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3817 }
3818
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003819 ssl->in_left += ret;
3820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003822#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003825
3826 return( 0 );
3827}
3828
3829/*
3830 * Flush any data not yet written
3831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003833{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003834 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003835 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Hanno Beckera58a8962019-06-13 16:11:15 +01003839 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003842 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003844 }
3845
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003846 /* Avoid incrementing counter if data is flushed */
3847 if( ssl->out_left == 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003850 return( 0 );
3851 }
3852
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 while( ssl->out_left > 0 )
3854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003856 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857
Hanno Becker2b1e3542018-08-06 11:19:13 +01003858 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003859 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
3863 if( ret <= 0 )
3864 return( ret );
3865
mohammad160352aecb92018-03-28 23:41:40 -07003866 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003867 {
Darryl Green11999bb2018-03-13 15:22:58 +00003868 MBEDTLS_SSL_DEBUG_MSG( 1,
3869 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003870 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003871 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3872 }
3873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 ssl->out_left -= ret;
3875 }
3876
Hanno Becker2b1e3542018-08-06 11:19:13 +01003877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003878 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003879 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003880 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003881 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003882 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003883#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003884#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003885 {
3886 ssl->out_hdr = ssl->out_buf + 8;
3887 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003888#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003889 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
3893 return( 0 );
3894}
3895
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003896/*
3897 * Functions to handle the DTLS retransmission state machine
3898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900/*
3901 * Append current handshake message to current outgoing flight
3902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3907 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3908 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909
3910 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003911 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003912 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003915 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003916 }
3917
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003918 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003919 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003922 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003923 }
3924
3925 /* Copy current handshake message with headers */
3926 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3927 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003928 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929 msg->next = NULL;
3930
3931 /* Append to the current flight */
3932 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003933 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 else
3935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 while( cur->next != NULL )
3938 cur = cur->next;
3939 cur->next = msg;
3940 }
3941
Hanno Becker3b235902018-08-06 09:54:53 +01003942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943 return( 0 );
3944}
3945
3946/*
3947 * Free the current flight of handshake messages
3948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 mbedtls_ssl_flight_item *cur = flight;
3952 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003953
3954 while( cur != NULL )
3955 {
3956 next = cur->next;
3957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 mbedtls_free( cur->p );
3959 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003960
3961 cur = next;
3962 }
3963}
3964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3966static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003967#endif
3968
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003970 * Swap transform_out and out_ctr with the alternative ones
3971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003973{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975 unsigned char tmp_out_ctr[8];
3976
3977 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980 return;
3981 }
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003984
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003985 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003986 tmp_transform = ssl->transform_out;
3987 ssl->transform_out = ssl->handshake->alt_transform_out;
3988 ssl->handshake->alt_transform_out = tmp_transform;
3989
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003990 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003991 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3992 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003993 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003994
3995 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003996 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3999 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4004 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004005 }
4006 }
4007#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004008}
4009
4010/*
4011 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004012 */
4013int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4014{
4015 int ret = 0;
4016
4017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4018
4019 ret = mbedtls_ssl_flight_transmit( ssl );
4020
4021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4022
4023 return( ret );
4024}
4025
4026/*
4027 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028 *
4029 * Need to remember the current message in case flush_output returns
4030 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004031 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004032 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004033int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004034{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004035 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004039 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004041
4042 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004043 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044 ssl_swap_epochs( ssl );
4045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004047 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004048
4049 while( ssl->handshake->cur_msg != NULL )
4050 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004051 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004052 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004053
Hanno Beckere1dcb032018-08-17 16:47:58 +01004054 int const is_finished =
4055 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4056 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4057
Hanno Becker04da1892018-08-14 13:22:10 +01004058 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4059 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4060
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004061 /* Swap epochs before sending Finished: we can't do it after
4062 * sending ChangeCipherSpec, in case write returns WANT_READ.
4063 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004064 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004065 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004067 ssl_swap_epochs( ssl );
4068 }
4069
Hanno Becker67bc7c32018-08-06 11:33:50 +01004070 ret = ssl_get_remaining_payload_in_datagram( ssl );
4071 if( ret < 0 )
4072 return( ret );
4073 max_frag_len = (size_t) ret;
4074
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004075 /* CCS is copied as is, while HS messages may need fragmentation */
4076 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4077 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004078 if( max_frag_len == 0 )
4079 {
4080 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4081 return( ret );
4082
4083 continue;
4084 }
4085
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004086 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004088 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004089
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004090 /* Update position inside current message */
4091 ssl->handshake->cur_msg_p += cur->len;
4092 }
4093 else
4094 {
4095 const unsigned char * const p = ssl->handshake->cur_msg_p;
4096 const size_t hs_len = cur->len - 12;
4097 const size_t frag_off = p - ( cur->p + 12 );
4098 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004099 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004100
Hanno Beckere1dcb032018-08-17 16:47:58 +01004101 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004102 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004103 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004104 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004105
Hanno Becker67bc7c32018-08-06 11:33:50 +01004106 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4107 return( ret );
4108
4109 continue;
4110 }
4111 max_hs_frag_len = max_frag_len - 12;
4112
4113 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4114 max_hs_frag_len : rem_len;
4115
4116 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004117 {
4118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004119 (unsigned) cur_hs_frag_len,
4120 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004121 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004122
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004123 /* Messages are stored with handshake headers as if not fragmented,
4124 * copy beginning of headers then fill fragmentation fields.
4125 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4126 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004127
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004128 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4129 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4130 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4131
Hanno Becker67bc7c32018-08-06 11:33:50 +01004132 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4133 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4134 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004135
4136 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4137
Hanno Becker3f7b9732018-08-28 09:53:25 +01004138 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004139 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4140 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004141 ssl->out_msgtype = cur->type;
4142
4143 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004144 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004145 }
4146
4147 /* If done with the current message move to the next one if any */
4148 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4149 {
4150 if( cur->next != NULL )
4151 {
4152 ssl->handshake->cur_msg = cur->next;
4153 ssl->handshake->cur_msg_p = cur->next->p + 12;
4154 }
4155 else
4156 {
4157 ssl->handshake->cur_msg = NULL;
4158 ssl->handshake->cur_msg_p = NULL;
4159 }
4160 }
4161
4162 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004163 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004166 return( ret );
4167 }
4168 }
4169
Hanno Becker67bc7c32018-08-06 11:33:50 +01004170 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4171 return( ret );
4172
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004173 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4175 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004176 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004179 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4180 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004181
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004183
4184 return( 0 );
4185}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004186
4187/*
4188 * To be called when the last message of an incoming flight is received.
4189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004191{
4192 /* We won't need to resend that one any more */
4193 ssl_flight_free( ssl->handshake->flight );
4194 ssl->handshake->flight = NULL;
4195 ssl->handshake->cur_msg = NULL;
4196
4197 /* The next incoming flight will start with this msg_seq */
4198 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4199
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004200 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004201 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004202
Hanno Becker0271f962018-08-16 13:23:47 +01004203 /* Clear future message buffering structure. */
4204 ssl_buffering_free( ssl );
4205
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004206 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004207 ssl_set_timer( ssl, 0 );
4208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4210 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004213 }
4214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004216}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004217
4218/*
4219 * To be called when the last message of an outgoing flight is send.
4220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004222{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004223 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004224 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4227 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004230 }
4231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004233}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004235
Paul Bakker5121ce52009-01-03 21:22:43 +00004236/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004237 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004239
4240/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004241 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004242 *
4243 * - fill in handshake headers
4244 * - update handshake checksum
4245 * - DTLS: save message for resending
4246 * - then pass to the record layer
4247 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004248 * DTLS: except for HelloRequest, messages are only queued, and will only be
4249 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004250 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004251 * Inputs:
4252 * - ssl->out_msglen: 4 + actual handshake message len
4253 * (4 is the size of handshake headers for TLS)
4254 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4255 * - ssl->out_msg + 4: the handshake message body
4256 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004257 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004258 * - ssl->out_msglen: the length of the record contents
4259 * (including handshake headers but excluding record headers)
4260 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004261 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004262int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004263{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004264 int ret;
4265 const size_t hs_len = ssl->out_msglen - 4;
4266 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004267
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4269
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004270 /*
4271 * Sanity checks
4272 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004273 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004274 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4275 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004276 /* In SSLv3, the client might send a NoCertificate alert. */
4277#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004278 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004279 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004280 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4281 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004282#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4283 {
4284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4286 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004288
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004289 /* Whenever we send anything different from a
4290 * HelloRequest we should be in a handshake - double check. */
4291 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4292 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004293 ssl->handshake == NULL )
4294 {
4295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004300 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004301 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004303 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004307#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004308
Hanno Beckerb50a2532018-08-06 11:52:54 +01004309 /* Double-check that we did not exceed the bounds
4310 * of the outgoing record buffer.
4311 * This should never fail as the various message
4312 * writing functions must obey the bounds of the
4313 * outgoing record buffer, but better be safe.
4314 *
4315 * Note: We deliberately do not check for the MTU or MFL here.
4316 */
4317 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4318 {
4319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4320 "size %u, maximum %u",
4321 (unsigned) ssl->out_msglen,
4322 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4324 }
4325
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004326 /*
4327 * Fill handshake headers
4328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004329 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004331 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4332 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4333 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004334
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004335 /*
4336 * DTLS has additional fields in the Handshake layer,
4337 * between the length field and the actual payload:
4338 * uint16 message_seq;
4339 * uint24 fragment_offset;
4340 * uint24 fragment_length;
4341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004343 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004344 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004345 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004346 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004347 {
4348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4349 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004350 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004351 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4353 }
4354
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004355 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004356 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004357
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004358 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004359 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004360 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004361 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4362 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4363 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004364 }
4365 else
4366 {
4367 ssl->out_msg[4] = 0;
4368 ssl->out_msg[5] = 0;
4369 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004370
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004371 /* Handshake hashes are computed without fragmentation,
4372 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004373 memset( ssl->out_msg + 6, 0x00, 3 );
4374 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004377
Hanno Becker0207e532018-08-28 10:28:28 +01004378 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004379 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004380 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 }
4382
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004383 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004385 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004386 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4387 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004388 {
4389 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004392 return( ret );
4393 }
4394 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004395 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004396#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004397 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004398 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004399 {
4400 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4401 return( ret );
4402 }
4403 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004404
4405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4406
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004407 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004408}
4409
4410/*
4411 * Record layer functions
4412 */
4413
4414/*
4415 * Write current record.
4416 *
4417 * Uses:
4418 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4419 * - ssl->out_msglen: length of the record content (excl headers)
4420 * - ssl->out_msg: record content
4421 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004422int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004423{
4424 int ret, done = 0;
4425 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004426 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004427
4428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004431 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004433 {
4434 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004437 return( ret );
4438 }
4439
4440 len = ssl->out_msglen;
4441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4445 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 ret = mbedtls_ssl_hw_record_write( ssl );
4450 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4453 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004454 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004455
4456 if( ret == 0 )
4457 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004460 if( !done )
4461 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004462 unsigned i;
4463 size_t protected_record_size;
4464
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004465 /* Skip writing the record content type to after the encryption,
4466 * as it may change when using the CID extension. */
4467
Hanno Becker2881d802019-05-22 14:44:53 +01004468 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4469 mbedtls_ssl_get_minor_ver( ssl ),
4470 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004471
Hanno Becker19859472018-08-06 09:40:20 +01004472 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004473 ssl->out_len[0] = (unsigned char)( len >> 8 );
4474 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004475
Paul Bakker48916f92012-09-16 19:57:18 +00004476 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004477 {
Hanno Becker3307b532017-12-27 21:37:21 +00004478 mbedtls_record rec;
4479
4480 rec.buf = ssl->out_iv;
4481 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4482 ( ssl->out_iv - ssl->out_buf );
4483 rec.data_len = ssl->out_msglen;
4484 rec.data_offset = ssl->out_msg - rec.buf;
4485
4486 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004487 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4488 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004489 ssl->conf->transport, rec.ver );
4490 rec.type = ssl->out_msgtype;
4491
Hanno Beckera5a2b082019-05-15 14:03:01 +01004492#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004493 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004494 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004495#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004496
Hanno Becker611a83b2018-01-03 14:27:32 +00004497 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004498 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004499 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004502 return( ret );
4503 }
4504
Hanno Becker3307b532017-12-27 21:37:21 +00004505 if( rec.data_offset != 0 )
4506 {
4507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4509 }
4510
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004511 /* Update the record content type and CID. */
4512 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004513#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004514 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004515#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004516 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004517 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4518 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004519 }
4520
Hanno Becker43395762019-05-03 14:46:38 +01004521 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004522
4523#if defined(MBEDTLS_SSL_PROTO_DTLS)
4524 /* In case of DTLS, double-check that we don't exceed
4525 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004526 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004527 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004528 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004529 if( ret < 0 )
4530 return( ret );
4531
4532 if( protected_record_size > (size_t) ret )
4533 {
4534 /* Should never happen */
4535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4536 }
4537 }
4538#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004539
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004540 /* Now write the potentially updated record content type. */
4541 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004544 "version = [%d:%d], msglen = %d",
4545 ssl->out_hdr[0], ssl->out_hdr[1],
4546 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004549 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004550
4551 ssl->out_left += protected_record_size;
4552 ssl->out_hdr += protected_record_size;
4553 ssl_update_out_pointers( ssl, ssl->transform_out );
4554
Hanno Becker04484622018-08-06 09:49:38 +01004555 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4556 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4557 break;
4558
4559 /* The loop goes to its end iff the counter is wrapping */
4560 if( i == ssl_ep_len( ssl ) )
4561 {
4562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4563 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 }
4566
Hanno Becker67bc7c32018-08-06 11:33:50 +01004567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004568 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004569 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004570 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004571 size_t remaining;
4572 ret = ssl_get_remaining_payload_in_datagram( ssl );
4573 if( ret < 0 )
4574 {
4575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4576 ret );
4577 return( ret );
4578 }
4579
4580 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004581 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004582 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004583 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004584 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004585 else
4586 {
Hanno Becker513815a2018-08-20 11:56:09 +01004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004588 }
4589 }
4590#endif /* MBEDTLS_SSL_PROTO_DTLS */
4591
4592 if( ( flush == SSL_FORCE_FLUSH ) &&
4593 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 return( ret );
4597 }
4598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600
4601 return( 0 );
4602}
4603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004605
4606static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4607{
4608 if( ssl->in_msglen < ssl->in_hslen ||
4609 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4610 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4611 {
4612 return( 1 );
4613 }
4614 return( 0 );
4615}
Hanno Becker44650b72018-08-16 12:51:11 +01004616
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004617static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004618{
4619 return( ( ssl->in_msg[9] << 16 ) |
4620 ( ssl->in_msg[10] << 8 ) |
4621 ssl->in_msg[11] );
4622}
4623
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004624static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004625{
4626 return( ( ssl->in_msg[6] << 16 ) |
4627 ( ssl->in_msg[7] << 8 ) |
4628 ssl->in_msg[8] );
4629}
4630
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004631static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004632{
4633 uint32_t msg_len, frag_off, frag_len;
4634
4635 msg_len = ssl_get_hs_total_len( ssl );
4636 frag_off = ssl_get_hs_frag_off( ssl );
4637 frag_len = ssl_get_hs_frag_len( ssl );
4638
4639 if( frag_off > msg_len )
4640 return( -1 );
4641
4642 if( frag_len > msg_len - frag_off )
4643 return( -1 );
4644
4645 if( frag_len + 12 > ssl->in_msglen )
4646 return( -1 );
4647
4648 return( 0 );
4649}
4650
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004651/*
4652 * Mark bits in bitmask (used for DTLS HS reassembly)
4653 */
4654static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4655{
4656 unsigned int start_bits, end_bits;
4657
4658 start_bits = 8 - ( offset % 8 );
4659 if( start_bits != 8 )
4660 {
4661 size_t first_byte_idx = offset / 8;
4662
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004663 /* Special case */
4664 if( len <= start_bits )
4665 {
4666 for( ; len != 0; len-- )
4667 mask[first_byte_idx] |= 1 << ( start_bits - len );
4668
4669 /* Avoid potential issues with offset or len becoming invalid */
4670 return;
4671 }
4672
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004673 offset += start_bits; /* Now offset % 8 == 0 */
4674 len -= start_bits;
4675
4676 for( ; start_bits != 0; start_bits-- )
4677 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4678 }
4679
4680 end_bits = len % 8;
4681 if( end_bits != 0 )
4682 {
4683 size_t last_byte_idx = ( offset + len ) / 8;
4684
4685 len -= end_bits; /* Now len % 8 == 0 */
4686
4687 for( ; end_bits != 0; end_bits-- )
4688 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4689 }
4690
4691 memset( mask + offset / 8, 0xFF, len / 8 );
4692}
4693
4694/*
4695 * Check that bitmask is full
4696 */
4697static int ssl_bitmask_check( unsigned char *mask, size_t len )
4698{
4699 size_t i;
4700
4701 for( i = 0; i < len / 8; i++ )
4702 if( mask[i] != 0xFF )
4703 return( -1 );
4704
4705 for( i = 0; i < len % 8; i++ )
4706 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4707 return( -1 );
4708
4709 return( 0 );
4710}
4711
Hanno Becker56e205e2018-08-16 09:06:12 +01004712/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004713static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004714 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004715{
Hanno Becker56e205e2018-08-16 09:06:12 +01004716 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004717
Hanno Becker56e205e2018-08-16 09:06:12 +01004718 alloc_len = 12; /* Handshake header */
4719 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004720
Hanno Beckerd07df862018-08-16 09:14:58 +01004721 if( add_bitmap )
4722 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004723
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004724 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004725}
Hanno Becker56e205e2018-08-16 09:06:12 +01004726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004728
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004729static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004730{
4731 return( ( ssl->in_msg[1] << 16 ) |
4732 ( ssl->in_msg[2] << 8 ) |
4733 ssl->in_msg[3] );
4734}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004735
Simon Butcher99000142016-10-13 17:21:01 +01004736int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004737{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004741 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004743 }
4744
Hanno Becker12555c62018-08-16 12:47:53 +01004745 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004748 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004749 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004752 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004753 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004754 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004755 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004756
Hanno Becker44650b72018-08-16 12:51:11 +01004757 if( ssl_check_hs_header( ssl ) != 0 )
4758 {
4759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4760 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4761 }
4762
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004763 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004764 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4765 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4766 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4767 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004768 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004769 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4770 {
4771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4772 recv_msg_seq,
4773 ssl->handshake->in_msg_seq ) );
4774 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4775 }
4776
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004777 /* Retransmit only on last message from previous flight, to avoid
4778 * too many retransmissions.
4779 * Besides, No sane server ever retransmits HelloVerifyRequest */
4780 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004784 "message_seq = %d, start_of_flight = %d",
4785 recv_msg_seq,
4786 ssl->handshake->in_flight_start_seq ) );
4787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004791 return( ret );
4792 }
4793 }
4794 else
4795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004797 "message_seq = %d, expected = %d",
4798 recv_msg_seq,
4799 ssl->handshake->in_msg_seq ) );
4800 }
4801
Hanno Becker90333da2017-10-10 11:27:13 +01004802 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004803 }
4804 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004805
Hanno Becker6d97ef52018-08-16 13:09:04 +01004806 /* Message reassembly is handled alongside buffering of future
4807 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004808 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004809 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004810 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004813 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004814 }
4815 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004816 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004818#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004819 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004820 /* With TLS we don't handle fragmentation (for now) */
4821 if( ssl->in_msglen < ssl->in_hslen )
4822 {
4823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4824 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4825 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004826 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004827#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004828
Simon Butcher99000142016-10-13 17:21:01 +01004829 return( 0 );
4830}
4831
4832void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4833{
Hanno Becker0271f962018-08-16 13:23:47 +01004834 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004835
Hanno Becker0271f962018-08-16 13:23:47 +01004836 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004837 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004838
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004839 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004841 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004842 ssl->handshake != NULL )
4843 {
Hanno Becker0271f962018-08-16 13:23:47 +01004844 unsigned offset;
4845 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004846
Hanno Becker0271f962018-08-16 13:23:47 +01004847 /* Increment handshake sequence number */
4848 hs->in_msg_seq++;
4849
4850 /*
4851 * Clear up handshake buffering and reassembly structure.
4852 */
4853
4854 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004855 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004856
4857 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004858 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4859 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004860 offset++, hs_buf++ )
4861 {
4862 *hs_buf = *(hs_buf + 1);
4863 }
4864
4865 /* Create a fresh last entry */
4866 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004867 }
4868#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004869}
4870
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004871/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004872 * DTLS anti-replay: RFC 6347 4.1.2.6
4873 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004874 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4875 * Bit n is set iff record number in_window_top - n has been seen.
4876 *
4877 * Usually, in_window_top is the last record number seen and the lsb of
4878 * in_window is set. The only exception is the initial state (record number 0
4879 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4882static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004883{
4884 ssl->in_window_top = 0;
4885 ssl->in_window = 0;
4886}
4887
4888static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4889{
4890 return( ( (uint64_t) buf[0] << 40 ) |
4891 ( (uint64_t) buf[1] << 32 ) |
4892 ( (uint64_t) buf[2] << 24 ) |
4893 ( (uint64_t) buf[3] << 16 ) |
4894 ( (uint64_t) buf[4] << 8 ) |
4895 ( (uint64_t) buf[5] ) );
4896}
4897
4898/*
4899 * Return 0 if sequence number is acceptable, -1 otherwise
4900 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004901int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004902{
4903 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4904 uint64_t bit;
4905
Hanno Becker7f376f42019-06-12 16:20:48 +01004906 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4907 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4908 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004909 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004910 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004911
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004912 if( rec_seqnum > ssl->in_window_top )
4913 return( 0 );
4914
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004915 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004916
4917 if( bit >= 64 )
4918 return( -1 );
4919
4920 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4921 return( -1 );
4922
4923 return( 0 );
4924}
4925
4926/*
4927 * Update replay window on new validated record
4928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004930{
4931 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4932
Hanno Becker7f376f42019-06-12 16:20:48 +01004933 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4934 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4935 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004936 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004937 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004938
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004939 if( rec_seqnum > ssl->in_window_top )
4940 {
4941 /* Update window_top and the contents of the window */
4942 uint64_t shift = rec_seqnum - ssl->in_window_top;
4943
4944 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004945 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004946 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004947 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004948 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004949 ssl->in_window |= 1;
4950 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004951
4952 ssl->in_window_top = rec_seqnum;
4953 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004954 else
4955 {
4956 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004957 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004958
4959 if( bit < 64 ) /* Always true, but be extra sure */
4960 ssl->in_window |= (uint64_t) 1 << bit;
4961 }
4962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004964
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004965#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004966/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004967static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4968
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004969/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004970 * Without any SSL context, check if a datagram looks like a ClientHello with
4971 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004972 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004973 *
4974 * - if cookie is valid, return 0
4975 * - if ClientHello looks superficially valid but cookie is not,
4976 * fill obuf and set olen, then
4977 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4978 * - otherwise return a specific error code
4979 */
4980static int ssl_check_dtls_clihlo_cookie(
4981 mbedtls_ssl_cookie_write_t *f_cookie_write,
4982 mbedtls_ssl_cookie_check_t *f_cookie_check,
4983 void *p_cookie,
4984 const unsigned char *cli_id, size_t cli_id_len,
4985 const unsigned char *in, size_t in_len,
4986 unsigned char *obuf, size_t buf_len, size_t *olen )
4987{
4988 size_t sid_len, cookie_len;
4989 unsigned char *p;
4990
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004991 /*
4992 * Structure of ClientHello with record and handshake headers,
4993 * and expected values. We don't need to check a lot, more checks will be
4994 * done when actually parsing the ClientHello - skipping those checks
4995 * avoids code duplication and does not make cookie forging any easier.
4996 *
4997 * 0-0 ContentType type; copied, must be handshake
4998 * 1-2 ProtocolVersion version; copied
4999 * 3-4 uint16 epoch; copied, must be 0
5000 * 5-10 uint48 sequence_number; copied
5001 * 11-12 uint16 length; (ignored)
5002 *
5003 * 13-13 HandshakeType msg_type; (ignored)
5004 * 14-16 uint24 length; (ignored)
5005 * 17-18 uint16 message_seq; copied
5006 * 19-21 uint24 fragment_offset; copied, must be 0
5007 * 22-24 uint24 fragment_length; (ignored)
5008 *
5009 * 25-26 ProtocolVersion client_version; (ignored)
5010 * 27-58 Random random; (ignored)
5011 * 59-xx SessionID session_id; 1 byte len + sid_len content
5012 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5013 * ...
5014 *
5015 * Minimum length is 61 bytes.
5016 */
5017 if( in_len < 61 ||
5018 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5019 in[3] != 0 || in[4] != 0 ||
5020 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5021 {
5022 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5023 }
5024
5025 sid_len = in[59];
5026 if( sid_len > in_len - 61 )
5027 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5028
5029 cookie_len = in[60 + sid_len];
5030 if( cookie_len > in_len - 60 )
5031 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5032
5033 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5034 cli_id, cli_id_len ) == 0 )
5035 {
5036 /* Valid cookie */
5037 return( 0 );
5038 }
5039
5040 /*
5041 * If we get here, we've got an invalid cookie, let's prepare HVR.
5042 *
5043 * 0-0 ContentType type; copied
5044 * 1-2 ProtocolVersion version; copied
5045 * 3-4 uint16 epoch; copied
5046 * 5-10 uint48 sequence_number; copied
5047 * 11-12 uint16 length; olen - 13
5048 *
5049 * 13-13 HandshakeType msg_type; hello_verify_request
5050 * 14-16 uint24 length; olen - 25
5051 * 17-18 uint16 message_seq; copied
5052 * 19-21 uint24 fragment_offset; copied
5053 * 22-24 uint24 fragment_length; olen - 25
5054 *
5055 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5056 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5057 *
5058 * Minimum length is 28.
5059 */
5060 if( buf_len < 28 )
5061 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5062
5063 /* Copy most fields and adapt others */
5064 memcpy( obuf, in, 25 );
5065 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5066 obuf[25] = 0xfe;
5067 obuf[26] = 0xff;
5068
5069 /* Generate and write actual cookie */
5070 p = obuf + 28;
5071 if( f_cookie_write( p_cookie,
5072 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5073 {
5074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5075 }
5076
5077 *olen = p - obuf;
5078
5079 /* Go back and fill length fields */
5080 obuf[27] = (unsigned char)( *olen - 28 );
5081
5082 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5083 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5084 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5085
5086 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5087 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5088
5089 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5090}
5091
5092/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005093 * Handle possible client reconnect with the same UDP quadruplet
5094 * (RFC 6347 Section 4.2.8).
5095 *
5096 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5097 * that looks like a ClientHello.
5098 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005099 * - if the input looks like a ClientHello without cookies,
5100 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005101 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005102 * - if the input looks like a ClientHello with a valid cookie,
5103 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005104 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005105 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005106 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005107 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005108 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5109 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005110 */
5111static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5112{
5113 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005114 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005115
Hanno Becker87b56262019-07-10 14:37:41 +01005116 if( ssl->conf->f_cookie_write == NULL ||
5117 ssl->conf->f_cookie_check == NULL )
5118 {
5119 /* If we can't use cookies to verify reachability of the peer,
5120 * drop the record. */
5121 return( 0 );
5122 }
5123
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005124 ret = ssl_check_dtls_clihlo_cookie(
5125 ssl->conf->f_cookie_write,
5126 ssl->conf->f_cookie_check,
5127 ssl->conf->p_cookie,
5128 ssl->cli_id, ssl->cli_id_len,
5129 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005130 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005131
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005132 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5133
5134 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005135 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005136 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 * If the error is permanent we'll catch it later,
5138 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005139 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005140 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005141 }
5142
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005143 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005144 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005145 /* Got a valid cookie, partially reset context */
5146 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5147 {
5148 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5149 return( ret );
5150 }
5151
5152 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005153 }
5154
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005155 return( ret );
5156}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005157#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158
Hanno Becker46483f12019-05-03 13:25:54 +01005159static int ssl_check_record_type( uint8_t record_type )
5160{
5161 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5162 record_type != MBEDTLS_SSL_MSG_ALERT &&
5163 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5164 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5165 {
5166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5167 }
5168
5169 return( 0 );
5170}
5171
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005172/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005173 * ContentType type;
5174 * ProtocolVersion version;
5175 * uint16 epoch; // DTLS only
5176 * uint48 sequence_number; // DTLS only
5177 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005178 *
5179 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005180 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005181 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5182 *
5183 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005184 * 1. proceed with the record if this function returns 0
5185 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5186 * 3. return CLIENT_RECONNECT if this function return that value
5187 * 4. drop the whole datagram if this function returns anything else.
5188 * Point 2 is needed when the peer is resending, and we have already received
5189 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005190 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005191static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005192 unsigned char *buf,
5193 size_t len,
5194 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005195{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005196 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005197
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005198 size_t const rec_hdr_type_offset = 0;
5199 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005200
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005201 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5202 rec_hdr_type_len;
5203 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005204
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005205 size_t const rec_hdr_ctr_len = 8;
5206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005207 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005208 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5209 rec_hdr_version_len;
5210
Hanno Beckera5a2b082019-05-15 14:03:01 +01005211#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005212 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5213 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005214 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005215#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5216#endif /* MBEDTLS_SSL_PROTO_DTLS */
5217
5218 size_t rec_hdr_len_offset; /* To be determined */
5219 size_t const rec_hdr_len_len = 2;
5220
5221 /*
5222 * Check minimum lengths for record header.
5223 */
5224
5225#if defined(MBEDTLS_SSL_PROTO_DTLS)
5226 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5227 {
5228 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5229 }
5230 MBEDTLS_SSL_TRANSPORT_ELSE
5231#endif /* MBEDTLS_SSL_PROTO_DTLS */
5232#if defined(MBEDTLS_SSL_PROTO_TLS)
5233 {
5234 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5235 }
5236#endif /* MBEDTLS_SSL_PROTO_DTLS */
5237
5238 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5241 (unsigned) len,
5242 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5243 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5244 }
5245
5246 /*
5247 * Parse and validate record content type
5248 */
5249
5250 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005251
5252 /* Check record content type */
5253#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5254 rec->cid_len = 0;
5255
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005256 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005257 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5258 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005259 {
5260 /* Shift pointers to account for record header including CID
5261 * struct {
5262 * ContentType special_type = tls12_cid;
5263 * ProtocolVersion version;
5264 * uint16 epoch;
5265 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005266 * opaque cid[cid_length]; // Additional field compared to
5267 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005268 * uint16 length;
5269 * opaque enc_content[DTLSCiphertext.length];
5270 * } DTLSCiphertext;
5271 */
5272
5273 /* So far, we only support static CID lengths
5274 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005275 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5276 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005277
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005278 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005279 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5281 (unsigned) len,
5282 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005283 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005284 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005285
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005286 /* configured CID len is guaranteed at most 255, see
5287 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5288 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005289 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005290 }
5291 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005292#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005293 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005294 if( ssl_check_record_type( rec->type ) )
5295 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5297 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005298 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5299 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005300 }
5301
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005302 /*
5303 * Parse and validate record version
5304 */
5305
Hanno Becker8061c6e2019-07-26 08:07:03 +01005306 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5307 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005308 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5309 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005310 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005311
Hanno Becker2881d802019-05-22 14:44:53 +01005312 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5315 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005316 }
5317
Hanno Beckere965bd32019-06-12 14:04:34 +01005318 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5321 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 }
5323
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005324 /*
5325 * Parse/Copy record sequence number.
5326 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005327
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005328#if defined(MBEDTLS_SSL_PROTO_DTLS)
5329 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5330 {
5331 /* Copy explicit record sequence number from input buffer. */
5332 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5333 rec_hdr_ctr_len );
5334 }
5335 MBEDTLS_SSL_TRANSPORT_ELSE
5336#endif /* MBEDTLS_SSL_PROTO_DTLS */
5337#if defined(MBEDTLS_SSL_PROTO_TLS)
5338 {
5339 /* Copy implicit record sequence number from SSL context structure. */
5340 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5341 }
5342#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005343
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005344 /*
5345 * Parse record length.
5346 */
5347
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005348 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005349 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5350 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005351 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5352
Hanno Becker8b09b732019-05-08 12:03:28 +01005353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005354 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005355 rec->type,
5356 major_ver, minor_ver, rec->data_len ) );
5357
5358 rec->buf = buf;
5359 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005360
Hanno Beckerec014082019-07-26 08:20:27 +01005361 if( rec->data_len == 0 )
5362 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5363
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005364 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005365 * DTLS-related tests.
5366 * Check epoch before checking length constraint because
5367 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5368 * message gets duplicated before the corresponding Finished message,
5369 * the second ChangeCipherSpec should be discarded because it belongs
5370 * to an old epoch, but not because its length is shorter than
5371 * the minimum record length for packets using the new record transform.
5372 * Note that these two kinds of failures are handled differently,
5373 * as an unexpected record is silently skipped but an invalid
5374 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005375 */
5376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005378 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005379 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005380
Hanno Beckere0452772019-07-10 17:12:07 +01005381 /* Check that the datagram is large enough to contain a record
5382 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005383 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005384 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5386 (unsigned) len,
5387 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005388 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5389 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005390
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005391 /* Records from other, non-matching epochs are silently discarded.
5392 * (The case of same-port Client reconnects must be considered in
5393 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005394 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005395 {
5396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5397 "expected %d, received %d",
5398 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005399
5400 /* Records from the next epoch are considered for buffering
5401 * (concretely: early Finished messages). */
5402 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5403 {
5404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5405 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5406 }
5407
Hanno Becker87b56262019-07-10 14:37:41 +01005408 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005409 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005410#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005411 /* For records from the correct epoch, check whether their
5412 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005413 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005414 {
5415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5416 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5417 }
5418#endif
5419 }
5420#endif /* MBEDTLS_SSL_PROTO_DTLS */
5421
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005422 return( 0 );
5423}
Paul Bakker5121ce52009-01-03 21:22:43 +00005424
Hanno Becker87b56262019-07-10 14:37:41 +01005425
5426#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5427static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5428{
5429 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5430
5431 /*
5432 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5433 * access the first byte of record content (handshake type), as we
5434 * have an active transform (possibly iv_len != 0), so use the
5435 * fact that the record header len is 13 instead.
5436 */
5437 if( rec_epoch == 0 &&
5438 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5439 MBEDTLS_SSL_IS_SERVER &&
5440 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5441 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5442 ssl->in_left > 13 &&
5443 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5444 {
5445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5446 "from the same port" ) );
5447 return( ssl_handle_possible_reconnect( ssl ) );
5448 }
5449
5450 return( 0 );
5451}
5452#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5453
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454/*
5455 * If applicable, decrypt (and decompress) record content
5456 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005457static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5458 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459{
5460 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005463 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5466 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 ret = mbedtls_ssl_hw_record_read( ssl );
5471 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5474 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005475 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005476
5477 if( ret == 0 )
5478 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005481 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005483 unsigned char const old_msg_type = rec->type;
5484
Hanno Becker611a83b2018-01-03 14:27:32 +00005485 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005486 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005489
Hanno Beckera5a2b082019-05-15 14:03:01 +01005490#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005491 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005492 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5493 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005494 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005496 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005497 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005498#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005499
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 return( ret );
5501 }
5502
Hanno Becker106f3da2019-07-12 09:35:58 +01005503 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005504 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005505 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005506 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005510 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005511
Hanno Beckera5a2b082019-05-15 14:03:01 +01005512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005513 /* We have already checked the record content type
5514 * in ssl_parse_record_header(), failing or silently
5515 * dropping the record in the case of an unknown type.
5516 *
5517 * Since with the use of CIDs, the record content type
5518 * might change during decryption, re-check the record
5519 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005520 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005521 {
5522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5523 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5524 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005525#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005526
Hanno Becker106f3da2019-07-12 09:35:58 +01005527 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005528 {
5529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005530 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005531 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005532 {
5533 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5535 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5536 }
5537#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5538
5539 ssl->nb_zero++;
5540
5541 /*
5542 * Three or more empty messages may be a DoS attack
5543 * (excessive CPU consumption).
5544 */
5545 if( ssl->nb_zero > 3 )
5546 {
5547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005548 "messages, possible DoS attack" ) );
5549 /* Treat the records as if they were not properly authenticated,
5550 * thereby failing the connection if we see more than allowed
5551 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005552 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5553 }
5554 }
5555 else
5556 ssl->nb_zero = 0;
5557
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005558 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5559#if defined(MBEDTLS_SSL_PROTO_TLS)
5560 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005561 {
5562 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005563 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005564 if( ++ssl->in_ctr[i - 1] != 0 )
5565 break;
5566
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005567 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005568 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5571 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5572 }
5573 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005574#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005575
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 }
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005579 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005581 {
5582 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005585 return( ret );
5586 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005591 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005594 }
5595#endif
5596
Hanno Beckerf0242852019-07-09 17:30:02 +01005597 /* Check actual (decrypted) record content length against
5598 * configured maximum. */
5599 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5602 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5603 }
5604
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005605 return( 0 );
5606}
5607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005609
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005610/*
5611 * Read a record.
5612 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005613 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5614 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5615 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005616 */
Hanno Becker1097b342018-08-15 14:09:41 +01005617
5618/* Helper functions for mbedtls_ssl_read_record(). */
5619static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005620static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5621static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005622
Hanno Becker327c93b2018-08-15 13:56:18 +01005623int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005624 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005625{
5626 int ret;
5627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005629
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005630 if( ssl->keep_current_message == 0 )
5631 {
5632 do {
Simon Butcher99000142016-10-13 17:21:01 +01005633
Hanno Becker26994592018-08-15 14:14:59 +01005634 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005635 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005636 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005637
Hanno Beckere74d5562018-08-15 14:26:08 +01005638 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005639 {
Hanno Becker40f50842018-08-15 14:48:01 +01005640#if defined(MBEDTLS_SSL_PROTO_DTLS)
5641 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005642
Hanno Becker40f50842018-08-15 14:48:01 +01005643 /* We only check for buffered messages if the
5644 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005645 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005646 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005647 {
Hanno Becker40f50842018-08-15 14:48:01 +01005648 if( ssl_load_buffered_message( ssl ) == 0 )
5649 have_buffered = 1;
5650 }
5651
5652 if( have_buffered == 0 )
5653#endif /* MBEDTLS_SSL_PROTO_DTLS */
5654 {
5655 ret = ssl_get_next_record( ssl );
5656 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5657 continue;
5658
5659 if( ret != 0 )
5660 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005661 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005662 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005663 return( ret );
5664 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005665 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005666 }
5667
5668 ret = mbedtls_ssl_handle_message_type( ssl );
5669
Hanno Becker40f50842018-08-15 14:48:01 +01005670#if defined(MBEDTLS_SSL_PROTO_DTLS)
5671 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5672 {
5673 /* Buffer future message */
5674 ret = ssl_buffer_message( ssl );
5675 if( ret != 0 )
5676 return( ret );
5677
5678 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5679 }
5680#endif /* MBEDTLS_SSL_PROTO_DTLS */
5681
Hanno Becker90333da2017-10-10 11:27:13 +01005682 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5683 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005684
5685 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005686 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005687 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005688 return( ret );
5689 }
5690
Hanno Becker327c93b2018-08-15 13:56:18 +01005691 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005692 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005693 {
5694 mbedtls_ssl_update_handshake_status( ssl );
5695 }
Simon Butcher99000142016-10-13 17:21:01 +01005696 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005697 else
Simon Butcher99000142016-10-13 17:21:01 +01005698 {
Hanno Becker02f59072018-08-15 14:00:24 +01005699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005700 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005701 }
5702
5703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5704
5705 return( 0 );
5706}
5707
Hanno Becker40f50842018-08-15 14:48:01 +01005708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005709static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005710{
Hanno Becker40f50842018-08-15 14:48:01 +01005711 if( ssl->in_left > ssl->next_record_offset )
5712 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005713
Hanno Becker40f50842018-08-15 14:48:01 +01005714 return( 0 );
5715}
5716
5717static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5718{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005719 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005720 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005721 int ret = 0;
5722
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005723 if( hs == NULL )
5724 return( -1 );
5725
Hanno Beckere00ae372018-08-20 09:39:42 +01005726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5727
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005728 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5729 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5730 {
5731 /* Check if we have seen a ChangeCipherSpec before.
5732 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005733 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005734 {
5735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5736 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005737 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005738 }
5739
Hanno Becker39b8bc92018-08-28 17:17:13 +01005740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005741 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5742 ssl->in_msglen = 1;
5743 ssl->in_msg[0] = 1;
5744
5745 /* As long as they are equal, the exact value doesn't matter. */
5746 ssl->in_left = 0;
5747 ssl->next_record_offset = 0;
5748
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005749 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005750 goto exit;
5751 }
Hanno Becker37f95322018-08-16 13:55:32 +01005752
Hanno Beckerb8f50142018-08-28 10:01:34 +01005753#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005754 /* Debug only */
5755 {
5756 unsigned offset;
5757 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5758 {
5759 hs_buf = &hs->buffering.hs[offset];
5760 if( hs_buf->is_valid == 1 )
5761 {
5762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5763 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005764 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005765 }
5766 }
5767 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005768#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005769
5770 /* Check if we have buffered and/or fully reassembled the
5771 * next handshake message. */
5772 hs_buf = &hs->buffering.hs[0];
5773 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5774 {
5775 /* Synthesize a record containing the buffered HS message. */
5776 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5777 ( hs_buf->data[2] << 8 ) |
5778 hs_buf->data[3];
5779
5780 /* Double-check that we haven't accidentally buffered
5781 * a message that doesn't fit into the input buffer. */
5782 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5783 {
5784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5786 }
5787
5788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5789 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5790 hs_buf->data, msg_len + 12 );
5791
5792 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5793 ssl->in_hslen = msg_len + 12;
5794 ssl->in_msglen = msg_len + 12;
5795 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5796
5797 ret = 0;
5798 goto exit;
5799 }
5800 else
5801 {
5802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5803 hs->in_msg_seq ) );
5804 }
5805
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005806 ret = -1;
5807
5808exit:
5809
5810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5811 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005812}
5813
Hanno Beckera02b0b42018-08-21 17:20:27 +01005814static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5815 size_t desired )
5816{
5817 int offset;
5818 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5820 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005821
Hanno Becker01315ea2018-08-21 17:22:17 +01005822 /* Get rid of future records epoch first, if such exist. */
5823 ssl_free_buffered_record( ssl );
5824
5825 /* Check if we have enough space available now. */
5826 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5827 hs->buffering.total_bytes_buffered ) )
5828 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005830 return( 0 );
5831 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005832
Hanno Becker4f432ad2018-08-28 10:02:32 +01005833 /* We don't have enough space to buffer the next expected handshake
5834 * message. Remove buffers used for future messages to gain space,
5835 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005836 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5837 offset >= 0; offset-- )
5838 {
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5840 offset ) );
5841
Hanno Beckerb309b922018-08-23 13:18:05 +01005842 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005843
5844 /* Check if we have enough space available now. */
5845 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5846 hs->buffering.total_bytes_buffered ) )
5847 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005849 return( 0 );
5850 }
5851 }
5852
5853 return( -1 );
5854}
5855
Hanno Becker40f50842018-08-15 14:48:01 +01005856static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5857{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 int ret = 0;
5859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5860
5861 if( hs == NULL )
5862 return( 0 );
5863
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5865
5866 switch( ssl->in_msgtype )
5867 {
5868 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005870
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005871 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872 break;
5873
5874 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005875 {
5876 unsigned recv_msg_seq_offset;
5877 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5878 mbedtls_ssl_hs_buffer *hs_buf;
5879 size_t msg_len = ssl->in_hslen - 12;
5880
5881 /* We should never receive an old handshake
5882 * message - double-check nonetheless. */
5883 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5884 {
5885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5886 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5887 }
5888
5889 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5890 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5891 {
5892 /* Silently ignore -- message too far in the future */
5893 MBEDTLS_SSL_DEBUG_MSG( 2,
5894 ( "Ignore future HS message with sequence number %u, "
5895 "buffering window %u - %u",
5896 recv_msg_seq, ssl->handshake->in_msg_seq,
5897 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5898
5899 goto exit;
5900 }
5901
5902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5903 recv_msg_seq, recv_msg_seq_offset ) );
5904
5905 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5906
5907 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005908 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005909 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005910 size_t reassembly_buf_sz;
5911
Hanno Becker37f95322018-08-16 13:55:32 +01005912 hs_buf->is_fragmented =
5913 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5914
5915 /* We copy the message back into the input buffer
5916 * after reassembly, so check that it's not too large.
5917 * This is an implementation-specific limitation
5918 * and not one from the standard, hence it is not
5919 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005920 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005921 {
5922 /* Ignore message */
5923 goto exit;
5924 }
5925
Hanno Beckere0b150f2018-08-21 15:51:03 +01005926 /* Check if we have enough space to buffer the message. */
5927 if( hs->buffering.total_bytes_buffered >
5928 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5932 }
5933
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005934 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5935 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005936
5937 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5938 hs->buffering.total_bytes_buffered ) )
5939 {
5940 if( recv_msg_seq_offset > 0 )
5941 {
5942 /* If we can't buffer a future message because
5943 * of space limitations -- ignore. */
5944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5945 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5946 (unsigned) hs->buffering.total_bytes_buffered ) );
5947 goto exit;
5948 }
Hanno Beckere1801392018-08-21 16:51:05 +01005949 else
5950 {
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5952 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5953 (unsigned) hs->buffering.total_bytes_buffered ) );
5954 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005955
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005957 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5959 (unsigned) msg_len,
5960 (unsigned) reassembly_buf_sz,
5961 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005962 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005963 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5964 goto exit;
5965 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005966 }
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5969 msg_len ) );
5970
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005971 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5972 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005973 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005974 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005975 goto exit;
5976 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005977 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005978
5979 /* Prepare final header: copy msg_type, length and message_seq,
5980 * then add standardised fragment_offset and fragment_length */
5981 memcpy( hs_buf->data, ssl->in_msg, 6 );
5982 memset( hs_buf->data + 6, 0, 3 );
5983 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5984
5985 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005986
5987 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005988 }
5989 else
5990 {
5991 /* Make sure msg_type and length are consistent */
5992 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5993 {
5994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5995 /* Ignore */
5996 goto exit;
5997 }
5998 }
5999
Hanno Becker4422bbb2018-08-20 09:40:19 +01006000 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006001 {
6002 size_t frag_len, frag_off;
6003 unsigned char * const msg = hs_buf->data + 12;
6004
6005 /*
6006 * Check and copy current fragment
6007 */
6008
6009 /* Validation of header fields already done in
6010 * mbedtls_ssl_prepare_handshake_record(). */
6011 frag_off = ssl_get_hs_frag_off( ssl );
6012 frag_len = ssl_get_hs_frag_len( ssl );
6013
6014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6015 frag_off, frag_len ) );
6016 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6017
6018 if( hs_buf->is_fragmented )
6019 {
6020 unsigned char * const bitmask = msg + msg_len;
6021 ssl_bitmask_set( bitmask, frag_off, frag_len );
6022 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6023 msg_len ) == 0 );
6024 }
6025 else
6026 {
6027 hs_buf->is_complete = 1;
6028 }
6029
6030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6031 hs_buf->is_complete ? "" : "not yet " ) );
6032 }
6033
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006034 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006035 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006036
6037 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006038 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006039 break;
6040 }
6041
6042exit:
6043
6044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6045 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006046}
6047#endif /* MBEDTLS_SSL_PROTO_DTLS */
6048
Hanno Becker1097b342018-08-15 14:09:41 +01006049static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006050{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006051 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006052 * Consume last content-layer message and potentially
6053 * update in_msglen which keeps track of the contents'
6054 * consumption state.
6055 *
6056 * (1) Handshake messages:
6057 * Remove last handshake message, move content
6058 * and adapt in_msglen.
6059 *
6060 * (2) Alert messages:
6061 * Consume whole record content, in_msglen = 0.
6062 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006063 * (3) Change cipher spec:
6064 * Consume whole record content, in_msglen = 0.
6065 *
6066 * (4) Application data:
6067 * Don't do anything - the record layer provides
6068 * the application data as a stream transport
6069 * and consumes through mbedtls_ssl_read only.
6070 *
6071 */
6072
6073 /* Case (1): Handshake messages */
6074 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006075 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006076 /* Hard assertion to be sure that no application data
6077 * is in flight, as corrupting ssl->in_msglen during
6078 * ssl->in_offt != NULL is fatal. */
6079 if( ssl->in_offt != NULL )
6080 {
6081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6083 }
6084
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006085 /*
6086 * Get next Handshake message in the current record
6087 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006088
Hanno Becker4a810fb2017-05-24 16:27:30 +01006089 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006090 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006091 * current handshake content: If DTLS handshake
6092 * fragmentation is used, that's the fragment
6093 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006094 * size here is faulty and should be changed at
6095 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 * (2) While it doesn't seem to cause problems, one
6097 * has to be very careful not to assume that in_hslen
6098 * is always <= in_msglen in a sensible communication.
6099 * Again, it's wrong for DTLS handshake fragmentation.
6100 * The following check is therefore mandatory, and
6101 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006102 * Additionally, ssl->in_hslen might be arbitrarily out of
6103 * bounds after handling a DTLS message with an unexpected
6104 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006105 */
6106 if( ssl->in_hslen < ssl->in_msglen )
6107 {
6108 ssl->in_msglen -= ssl->in_hslen;
6109 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6110 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006111
Hanno Becker4a810fb2017-05-24 16:27:30 +01006112 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6113 ssl->in_msg, ssl->in_msglen );
6114 }
6115 else
6116 {
6117 ssl->in_msglen = 0;
6118 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006119
Hanno Becker4a810fb2017-05-24 16:27:30 +01006120 ssl->in_hslen = 0;
6121 }
6122 /* Case (4): Application data */
6123 else if( ssl->in_offt != NULL )
6124 {
6125 return( 0 );
6126 }
6127 /* Everything else (CCS & Alerts) */
6128 else
6129 {
6130 ssl->in_msglen = 0;
6131 }
6132
Hanno Becker1097b342018-08-15 14:09:41 +01006133 return( 0 );
6134}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006135
Hanno Beckere74d5562018-08-15 14:26:08 +01006136static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6137{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006138 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006139 return( 1 );
6140
6141 return( 0 );
6142}
6143
Hanno Becker5f066e72018-08-16 14:56:31 +01006144#if defined(MBEDTLS_SSL_PROTO_DTLS)
6145
6146static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6147{
6148 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6149 if( hs == NULL )
6150 return;
6151
Hanno Becker01315ea2018-08-21 17:22:17 +01006152 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006153 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006154 hs->buffering.total_bytes_buffered -=
6155 hs->buffering.future_record.len;
6156
6157 mbedtls_free( hs->buffering.future_record.data );
6158 hs->buffering.future_record.data = NULL;
6159 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006160}
6161
6162static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6163{
6164 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6165 unsigned char * rec;
6166 size_t rec_len;
6167 unsigned rec_epoch;
6168
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006169 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006170 return( 0 );
6171
6172 if( hs == NULL )
6173 return( 0 );
6174
Hanno Becker5f066e72018-08-16 14:56:31 +01006175 rec = hs->buffering.future_record.data;
6176 rec_len = hs->buffering.future_record.len;
6177 rec_epoch = hs->buffering.future_record.epoch;
6178
6179 if( rec == NULL )
6180 return( 0 );
6181
Hanno Becker4cb782d2018-08-20 11:19:05 +01006182 /* Only consider loading future records if the
6183 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006184 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006185 return( 0 );
6186
Hanno Becker5f066e72018-08-16 14:56:31 +01006187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6188
6189 if( rec_epoch != ssl->in_epoch )
6190 {
6191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6192 goto exit;
6193 }
6194
6195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6196
6197 /* Double-check that the record is not too large */
6198 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6199 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6200 {
6201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6203 }
6204
6205 memcpy( ssl->in_hdr, rec, rec_len );
6206 ssl->in_left = rec_len;
6207 ssl->next_record_offset = 0;
6208
6209 ssl_free_buffered_record( ssl );
6210
6211exit:
6212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6213 return( 0 );
6214}
6215
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006216static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6217 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006218{
6219 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006220
6221 /* Don't buffer future records outside handshakes. */
6222 if( hs == NULL )
6223 return( 0 );
6224
6225 /* Only buffer handshake records (we are only interested
6226 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006227 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006228 return( 0 );
6229
6230 /* Don't buffer more than one future epoch record. */
6231 if( hs->buffering.future_record.data != NULL )
6232 return( 0 );
6233
Hanno Becker01315ea2018-08-21 17:22:17 +01006234 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006235 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006236 hs->buffering.total_bytes_buffered ) )
6237 {
6238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006239 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006240 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006241 return( 0 );
6242 }
6243
Hanno Becker5f066e72018-08-16 14:56:31 +01006244 /* Buffer record */
6245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6246 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006247 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006248
6249 /* ssl_parse_record_header() only considers records
6250 * of the next epoch as candidates for buffering. */
6251 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006253
6254 hs->buffering.future_record.data =
6255 mbedtls_calloc( 1, hs->buffering.future_record.len );
6256 if( hs->buffering.future_record.data == NULL )
6257 {
6258 /* If we run out of RAM trying to buffer a
6259 * record from the next epoch, just ignore. */
6260 return( 0 );
6261 }
6262
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006263 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006264
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006265 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006266 return( 0 );
6267}
6268
6269#endif /* MBEDTLS_SSL_PROTO_DTLS */
6270
Hanno Beckere74d5562018-08-15 14:26:08 +01006271static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006272{
6273 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006274 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006275
Hanno Becker5f066e72018-08-16 14:56:31 +01006276#if defined(MBEDTLS_SSL_PROTO_DTLS)
6277 /* We might have buffered a future record; if so,
6278 * and if the epoch matches now, load it.
6279 * On success, this call will set ssl->in_left to
6280 * the length of the buffered record, so that
6281 * the calls to ssl_fetch_input() below will
6282 * essentially be no-ops. */
6283 ret = ssl_load_buffered_record( ssl );
6284 if( ret != 0 )
6285 return( ret );
6286#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006287
Hanno Becker8b09b732019-05-08 12:03:28 +01006288 /* Ensure that we have enough space available for the default form
6289 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6290 * with no space for CIDs counted in). */
6291 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6292 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006295 return( ret );
6296 }
6297
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006298 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6299 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006302 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006303 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006304 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6305 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006306 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006307 if( ret != 0 )
6308 return( ret );
6309
6310 /* Fall through to handling of unexpected records */
6311 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6312 }
6313
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006314 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6315 {
Hanno Becker87b56262019-07-10 14:37:41 +01006316#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006317 /* Reset in pointers to default state for TLS/DTLS records,
6318 * assuming no CID and no offset between record content and
6319 * record plaintext. */
6320 ssl_update_in_pointers( ssl );
6321
Hanno Becker69412452019-07-12 08:33:49 +01006322 /* Setup internal message pointers from record structure. */
6323 ssl->in_msgtype = rec.type;
6324#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6325 ssl->in_len = ssl->in_cid + rec.cid_len;
6326#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006327 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006328 ssl->in_msglen = rec.data_len;
6329
Hanno Becker87b56262019-07-10 14:37:41 +01006330 ret = ssl_check_client_reconnect( ssl );
6331 if( ret != 0 )
6332 return( ret );
6333#endif
6334
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006335 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006336 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006337
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6339 "(header)" ) );
6340 }
6341 else
6342 {
6343 /* Skip invalid record and the rest of the datagram */
6344 ssl->next_record_offset = 0;
6345 ssl->in_left = 0;
6346
6347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6348 "(header)" ) );
6349 }
6350
6351 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006352 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006353 }
Hanno Becker87b56262019-07-10 14:37:41 +01006354 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006355#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006356 {
6357 return( ret );
6358 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006359 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006362 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006363 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006364 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006365 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006366 if( ssl->next_record_offset < ssl->in_left )
6367 {
6368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6369 }
6370 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006371 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006372#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006373#if defined(MBEDTLS_SSL_PROTO_TLS)
6374 {
Hanno Beckere0452772019-07-10 17:12:07 +01006375 /*
6376 * Fetch record contents from underlying transport.
6377 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006378 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006379 if( ret != 0 )
6380 {
6381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6382 return( ret );
6383 }
6384
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006385 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006386 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006387#endif /* MBEDTLS_SSL_PROTO_TLS */
6388
6389 /*
6390 * Decrypt record contents.
6391 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006392
Hanno Beckera89610a2019-07-11 13:07:45 +01006393 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006396 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006397 {
6398 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006400 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006401 /* Except when waiting for Finished as a bad mac here
6402 * probably means something went wrong in the handshake
6403 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6404 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6405 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6406 {
6407#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6408 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6409 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006410 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006411 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6412 }
6413#endif
6414 return( ret );
6415 }
6416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006418 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6419 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6422 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006423 }
6424#endif
6425
Hanno Becker4a810fb2017-05-24 16:27:30 +01006426 /* As above, invalid records cause
6427 * dismissal of the whole datagram. */
6428
6429 ssl->next_record_offset = 0;
6430 ssl->in_left = 0;
6431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006433 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006434 }
6435
6436 return( ret );
6437 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006438 MBEDTLS_SSL_TRANSPORT_ELSE
6439#endif /* MBEDTLS_SSL_PROTO_DTLS */
6440#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006441 {
6442 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6444 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006445 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006446 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006448 }
6449#endif
6450 return( ret );
6451 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006452#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006453 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006454
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006455
6456 /* Reset in pointers to default state for TLS/DTLS records,
6457 * assuming no CID and no offset between record content and
6458 * record plaintext. */
6459 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006460#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6461 ssl->in_len = ssl->in_cid + rec.cid_len;
6462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006463 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006464
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006465 /* The record content type may change during decryption,
6466 * so re-read it. */
6467 ssl->in_msgtype = rec.type;
6468 /* Also update the input buffer, because unfortunately
6469 * the server-side ssl_parse_client_hello() reparses the
6470 * record header when receiving a ClientHello initiating
6471 * a renegotiation. */
6472 ssl->in_hdr[0] = rec.type;
6473 ssl->in_msg = rec.buf + rec.data_offset;
6474 ssl->in_msglen = rec.data_len;
6475 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6476 ssl->in_len[1] = (unsigned char)( rec.data_len );
6477
Simon Butcher99000142016-10-13 17:21:01 +01006478 return( 0 );
6479}
6480
6481int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6482{
6483 int ret;
6484
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006485 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006486 * Handle particular types of records
6487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 {
Simon Butcher99000142016-10-13 17:21:01 +01006490 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6491 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006492 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 }
6495
Hanno Beckere678eaa2018-08-21 14:57:46 +01006496 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006497 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006498 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006499 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6501 ssl->in_msglen ) );
6502 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006503 }
6504
Hanno Beckere678eaa2018-08-21 14:57:46 +01006505 if( ssl->in_msg[0] != 1 )
6506 {
6507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6508 ssl->in_msg[0] ) );
6509 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6510 }
6511
6512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006513 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006514 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6515 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6516 {
6517 if( ssl->handshake == NULL )
6518 {
6519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6520 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6521 }
6522
6523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6524 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6525 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006526#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006527 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006531 if( ssl->in_msglen != 2 )
6532 {
6533 /* Note: Standard allows for more than one 2 byte alert
6534 to be packed in a single message, but Mbed TLS doesn't
6535 currently support this. */
6536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6537 ssl->in_msglen ) );
6538 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6539 }
6540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 ssl->in_msg[0], ssl->in_msg[1] ) );
6543
6544 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006545 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006550 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 }
6553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6555 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6558 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006559 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006560
6561#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6562 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6563 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6564 {
Hanno Becker90333da2017-10-10 11:27:13 +01006565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006566 /* Will be handled when trying to parse ServerHello */
6567 return( 0 );
6568 }
6569#endif
6570
6571#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006572 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006573 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6574 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006575 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6576 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6577 {
6578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6579 /* Will be handled in mbedtls_ssl_parse_certificate() */
6580 return( 0 );
6581 }
6582#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6583
6584 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006585 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 }
6587
Hanno Beckerc76c6192017-06-06 10:03:17 +01006588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006589 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006590 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006591 /* Drop unexpected ApplicationData records,
6592 * except at the beginning of renegotiations */
6593 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6594 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6595#if defined(MBEDTLS_SSL_RENEGOTIATION)
6596 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6597 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006598#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006599 )
6600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6602 return( MBEDTLS_ERR_SSL_NON_FATAL );
6603 }
6604
6605 if( ssl->handshake != NULL &&
6606 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6607 {
6608 ssl_handshake_wrapup_free_hs_transform( ssl );
6609 }
6610 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006611#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006612
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 return( 0 );
6614}
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006617{
6618 int ret;
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6621 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6622 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006623 {
6624 return( ret );
6625 }
6626
6627 return( 0 );
6628}
6629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006631 unsigned char level,
6632 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006633{
6634 int ret;
6635
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006636 if( ssl == NULL || ssl->conf == NULL )
6637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006643 ssl->out_msglen = 2;
6644 ssl->out_msg[0] = level;
6645 ssl->out_msg[1] = message;
6646
Hanno Becker67bc7c32018-08-06 11:33:50 +01006647 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006650 return( ret );
6651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006653
6654 return( 0 );
6655}
6656
Hanno Becker17572472019-02-08 07:19:04 +00006657#if defined(MBEDTLS_X509_CRT_PARSE_C)
6658static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6659{
6660#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6661 if( session->peer_cert != NULL )
6662 {
6663 mbedtls_x509_crt_free( session->peer_cert );
6664 mbedtls_free( session->peer_cert );
6665 session->peer_cert = NULL;
6666 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006667#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006668 if( session->peer_cert_digest != NULL )
6669 {
6670 /* Zeroization is not necessary. */
6671 mbedtls_free( session->peer_cert_digest );
6672 session->peer_cert_digest = NULL;
6673 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6674 session->peer_cert_digest_len = 0;
6675 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006676#else
6677 ((void) session);
6678#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006679}
6680#endif /* MBEDTLS_X509_CRT_PARSE_C */
6681
Paul Bakker5121ce52009-01-03 21:22:43 +00006682/*
6683 * Handshake functions
6684 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006685#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006686/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006688{
Hanno Beckerdf645962019-06-26 13:02:22 +01006689 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6690 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Hanno Becker5097cba2019-02-05 13:36:46 +00006694 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006697 ssl->state++;
6698 return( 0 );
6699 }
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006703}
6704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006706{
Hanno Beckerdf645962019-06-26 13:02:22 +01006707 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6708 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006711
Hanno Becker5097cba2019-02-05 13:36:46 +00006712 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006715 ssl->state++;
6716 return( 0 );
6717 }
6718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006721}
Gilles Peskinef9828522017-05-03 12:28:43 +02006722
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006723#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006724/* Some certificate support -> implement write and parse */
6725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006727{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006729 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006731 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6732 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006735
Hanno Becker5097cba2019-02-05 13:36:46 +00006736 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006739 ssl->state++;
6740 return( 0 );
6741 }
6742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006744 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6745 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 {
6747 if( ssl->client_auth == 0 )
6748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 ssl->state++;
6751 return( 0 );
6752 }
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 /*
6756 * If using SSLv3 and got no cert, send an Alert message
6757 * (otherwise an empty Certificate message will be sent).
6758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006760 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 {
6762 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6764 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6765 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 goto write_msg;
6769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772#endif /* MBEDTLS_SSL_CLI_C */
6773#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006774 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6779 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 }
6781 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006782#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785
6786 /*
6787 * 0 . 0 handshake type
6788 * 1 . 3 handshake length
6789 * 4 . 6 length of all certs
6790 * 7 . 9 length of cert. 1
6791 * 10 . n-1 peer certificate
6792 * n . n+2 length of cert. 2
6793 * n+3 . ... upper level cert, etc.
6794 */
6795 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006797
Paul Bakker29087132010-03-21 21:03:34 +00006798 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006799 {
6800 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006801 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006804 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806 }
6807
6808 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6809 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6810 ssl->out_msg[i + 2] = (unsigned char)( n );
6811
6812 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6813 i += n; crt = crt->next;
6814 }
6815
6816 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6817 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6818 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6819
6820 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6822 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006824#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006825write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006826#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
6828 ssl->state++;
6829
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006830 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 return( ret );
6834 }
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Paul Bakkered27a042013-04-18 22:46:23 +02006838 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839}
6840
Hanno Becker285ff0c2019-01-31 07:44:03 +00006841#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006842
6843#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006844static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6845 unsigned char *crt_buf,
6846 size_t crt_buf_len )
6847{
6848 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6849
6850 if( peer_crt == NULL )
6851 return( -1 );
6852
6853 if( peer_crt->raw.len != crt_buf_len )
6854 return( -1 );
6855
Hanno Becker68b856d2019-02-08 14:00:04 +00006856 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006857}
Hanno Becker5882dd02019-06-06 16:25:57 +01006858#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006859static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6860 unsigned char *crt_buf,
6861 size_t crt_buf_len )
6862{
6863 int ret;
6864 unsigned char const * const peer_cert_digest =
6865 ssl->session->peer_cert_digest;
6866 mbedtls_md_type_t const peer_cert_digest_type =
6867 ssl->session->peer_cert_digest_type;
6868 mbedtls_md_info_t const * const digest_info =
6869 mbedtls_md_info_from_type( peer_cert_digest_type );
6870 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6871 size_t digest_len;
6872
6873 if( peer_cert_digest == NULL || digest_info == NULL )
6874 return( -1 );
6875
6876 digest_len = mbedtls_md_get_size( digest_info );
6877 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6878 return( -1 );
6879
6880 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6881 if( ret != 0 )
6882 return( -1 );
6883
6884 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6885}
Hanno Becker5882dd02019-06-06 16:25:57 +01006886#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006887#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006888
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006889/*
6890 * Once the certificate message is read, parse it into a cert chain and
6891 * perform basic checks, but leave actual verification to the caller
6892 */
Hanno Becker35e41772019-02-05 15:37:23 +00006893static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6894 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006895{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006896 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006897#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6898 int crt_cnt=0;
6899#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006900 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006901 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006906 mbedtls_ssl_pend_fatal_alert( ssl,
6907 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 }
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6912 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006915 mbedtls_ssl_pend_fatal_alert( ssl,
6916 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 }
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006921
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006925 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006926
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006927 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006931 mbedtls_ssl_pend_fatal_alert( ssl,
6932 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 }
6935
Hanno Becker33c3dc82019-01-30 14:46:46 +00006936 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6937 i += 3;
6938
Hanno Becker33c3dc82019-01-30 14:46:46 +00006939 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 while( i < ssl->in_hslen )
6941 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006942 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006943 if ( i + 3 > ssl->in_hslen ) {
6944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006945 mbedtls_ssl_pend_fatal_alert( ssl,
6946 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006947 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6948 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006949 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6950 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 if( ssl->in_msg[i] != 0 )
6952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006954 mbedtls_ssl_pend_fatal_alert( ssl,
6955 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006957 }
6958
Hanno Becker33c3dc82019-01-30 14:46:46 +00006959 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6961 | (unsigned int) ssl->in_msg[i + 2];
6962 i += 3;
6963
6964 if( n < 128 || i + n > ssl->in_hslen )
6965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006967 mbedtls_ssl_pend_fatal_alert( ssl,
6968 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 }
6971
Hanno Becker33c3dc82019-01-30 14:46:46 +00006972 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006973#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6974 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006975 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6976 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006977 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006978 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006979 /* During client-side renegotiation, check that the server's
6980 * end-CRTs hasn't changed compared to the initial handshake,
6981 * mitigating the triple handshake attack. On success, reuse
6982 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6984 if( ssl_check_peer_crt_unchanged( ssl,
6985 &ssl->in_msg[i],
6986 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006987 {
Hanno Becker35e41772019-02-05 15:37:23 +00006988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006989 mbedtls_ssl_pend_fatal_alert( ssl,
6990 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006991 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006992 }
Hanno Becker35e41772019-02-05 15:37:23 +00006993
6994 /* Now we can safely free the original chain. */
6995 ssl_clear_peer_cert( ssl->session );
6996 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6998
Hanno Becker33c3dc82019-01-30 14:46:46 +00006999 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007000#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007001 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007002#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007003 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007004 * it in-place from the input buffer instead of making a copy. */
7005 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7006#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007007 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007008 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007009 case 0: /*ok*/
7010 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7011 /* Ignore certificate with an unknown algorithm: maybe a
7012 prior certificate was already trusted. */
7013 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007014
Hanno Becker33c3dc82019-01-30 14:46:46 +00007015 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7016 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7017 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007018
Hanno Becker33c3dc82019-01-30 14:46:46 +00007019 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7020 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7021 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007022
Hanno Becker33c3dc82019-01-30 14:46:46 +00007023 default:
7024 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7025 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007026 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007027 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7028 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 }
7030
7031 i += n;
7032 }
7033
Hanno Becker35e41772019-02-05 15:37:23 +00007034 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007035 return( 0 );
7036}
7037
Hanno Beckerb8a08572019-02-05 12:49:06 +00007038#if defined(MBEDTLS_SSL_SRV_C)
7039static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7040{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007041 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007042 return( -1 );
7043
7044#if defined(MBEDTLS_SSL_PROTO_SSL3)
7045 /*
7046 * Check if the client sent an empty certificate
7047 */
Hanno Becker2881d802019-05-22 14:44:53 +01007048 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007049 {
7050 if( ssl->in_msglen == 2 &&
7051 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7052 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7053 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7054 {
7055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7056 return( 0 );
7057 }
7058
7059 return( -1 );
7060 }
7061#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7062
7063#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7064 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7065 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7066 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7067 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7068 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7069 {
7070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7071 return( 0 );
7072 }
7073
Hanno Beckerb8a08572019-02-05 12:49:06 +00007074#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7075 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007076
7077 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007078}
7079#endif /* MBEDTLS_SSL_SRV_C */
7080
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007081/* Check if a certificate message is expected.
7082 * Return either
7083 * - SSL_CERTIFICATE_EXPECTED, or
7084 * - SSL_CERTIFICATE_SKIP
7085 * indicating whether a Certificate message is expected or not.
7086 */
7087#define SSL_CERTIFICATE_EXPECTED 0
7088#define SSL_CERTIFICATE_SKIP 1
7089static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7090 int authmode )
7091{
Hanno Becker473f98f2019-06-26 10:27:32 +01007092 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007093 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007094
7095 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7096 return( SSL_CERTIFICATE_SKIP );
7097
7098#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007099 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007100 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007101 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7102 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7103 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007104 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007105 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007106
7107 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7108 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007109 ssl->session_negotiate->verify_result =
7110 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7111 return( SSL_CERTIFICATE_SKIP );
7112 }
7113 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007114#else
7115 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007116#endif /* MBEDTLS_SSL_SRV_C */
7117
7118 return( SSL_CERTIFICATE_EXPECTED );
7119}
7120
Hanno Becker3cf50612019-02-05 14:36:34 +00007121static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7122 int authmode,
7123 mbedtls_x509_crt *chain,
7124 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007125{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007126 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007127 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007128 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007129 mbedtls_x509_crt *ca_chain;
7130 mbedtls_x509_crl *ca_crl;
7131
7132 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7133 return( 0 );
7134
7135#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7136 if( ssl->handshake->sni_ca_chain != NULL )
7137 {
7138 ca_chain = ssl->handshake->sni_ca_chain;
7139 ca_crl = ssl->handshake->sni_ca_crl;
7140 }
7141 else
7142#endif
7143 {
7144 ca_chain = ssl->conf->ca_chain;
7145 ca_crl = ssl->conf->ca_crl;
7146 }
7147
7148 /*
7149 * Main check: verify certificate
7150 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007151 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007152 chain,
7153 ca_chain, ca_crl,
7154 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007155#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007156 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007157#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007158 &ssl->session_negotiate->verify_result,
7159 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7160
Hanno Becker8c13ee62019-02-26 16:48:17 +00007161 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007162 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007163 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007164 }
7165
7166#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007167 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007168 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7169#endif
7170
7171 /*
7172 * Secondary checks: always done, but change 'ret' only if it was 0
7173 */
7174
7175#if defined(MBEDTLS_ECP_C)
7176 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007177 int ret;
Hanno Becker8c13ee62019-02-26 16:48:17 +00007178 mbedtls_pk_context *pk;
7179 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7180 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007181 {
7182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007183 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007184 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007185
7186 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007187 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
7188 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
7189
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007190 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191
7192 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007193 {
7194 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7195
7196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007197 if( verify_ret == 0 )
7198 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007199 }
7200 }
7201#endif /* MBEDTLS_ECP_C */
7202
7203 if( mbedtls_ssl_check_cert_usage( chain,
7204 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007205 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7206 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007207 &ssl->session_negotiate->verify_result ) != 0 )
7208 {
7209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007210 if( verify_ret == 0 )
7211 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007212 }
7213
7214 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7215 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7216 * with details encoded in the verification flags. All other kinds
7217 * of error codes, including those from the user provided f_vrfy
7218 * functions, are treated as fatal and lead to a failure of
7219 * ssl_parse_certificate even if verification was optional. */
7220 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7222 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007223 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007224 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007225 }
7226
7227 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7228 {
7229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007230 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007231 }
7232
Hanno Becker8c13ee62019-02-26 16:48:17 +00007233 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007234 {
7235 uint8_t alert;
7236
7237 /* The certificate may have been rejected for several reasons.
7238 Pick one and send the corresponding alert. Which alert to send
7239 may be a subject of debate in some cases. */
7240 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7241 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7242 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7243 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7244 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7245 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7246 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7247 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7248 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7249 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7250 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7251 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7252 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7253 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7254 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7255 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7256 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7257 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7258 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7259 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7260 else
7261 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007262 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007263 }
7264
7265#if defined(MBEDTLS_DEBUG_C)
7266 if( ssl->session_negotiate->verify_result != 0 )
7267 {
7268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7269 ssl->session_negotiate->verify_result ) );
7270 }
7271 else
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7274 }
7275#endif /* MBEDTLS_DEBUG_C */
7276
Hanno Becker8c13ee62019-02-26 16:48:17 +00007277 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007278}
7279
Hanno Becker34106f62019-02-08 14:59:05 +00007280#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007281
7282#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007283static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7284 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007285{
7286 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007287 /* Remember digest of the peer's end-CRT. */
7288 ssl->session_negotiate->peer_cert_digest =
7289 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7290 if( ssl->session_negotiate->peer_cert_digest == NULL )
7291 {
7292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7293 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007294 mbedtls_ssl_pend_fatal_alert( ssl,
7295 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007296
7297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7298 }
7299
7300 ret = mbedtls_md( mbedtls_md_info_from_type(
7301 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7302 start, len,
7303 ssl->session_negotiate->peer_cert_digest );
7304
7305 ssl->session_negotiate->peer_cert_digest_type =
7306 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7307 ssl->session_negotiate->peer_cert_digest_len =
7308 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7309
7310 return( ret );
7311}
Hanno Becker5882dd02019-06-06 16:25:57 +01007312#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007313
7314static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7315 unsigned char *start, size_t len )
7316{
7317 unsigned char *end = start + len;
7318 int ret;
7319
7320 /* Make a copy of the peer's raw public key. */
7321 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7322 ret = mbedtls_pk_parse_subpubkey( &start, end,
7323 &ssl->handshake->peer_pubkey );
7324 if( ret != 0 )
7325 {
7326 /* We should have parsed the public key before. */
7327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7328 }
7329
7330 return( 0 );
7331}
7332#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7333
Hanno Becker3cf50612019-02-05 14:36:34 +00007334int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7335{
7336 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007337 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007338#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7339 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7340 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007341 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007342#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007343 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007344#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007345 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007346 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007347
7348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7349
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007350 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7351 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007352 {
7353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007354 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007355 }
7356
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007357#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7358 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007359 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007360 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007361 chain = ssl->handshake->ecrs_peer_cert;
7362 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007363 goto crt_verify;
7364 }
7365#endif
7366
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007367 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007368 {
7369 /* mbedtls_ssl_read_record may have sent an alert already. We
7370 let it decide whether to alert. */
7371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007372 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373 }
7374
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007375#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007376 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7377 {
7378 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007379
Hanno Beckerb8a08572019-02-05 12:49:06 +00007380 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007381 ret = 0;
7382 else
7383 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007384
Hanno Becker613d4902019-02-05 13:11:17 +00007385 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007387#endif /* MBEDTLS_SSL_SRV_C */
7388
Hanno Becker35e41772019-02-05 15:37:23 +00007389 /* Clear existing peer CRT structure in case we tried to
7390 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007391 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007392
7393 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7394 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007395 {
7396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7397 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007398 mbedtls_ssl_pend_fatal_alert( ssl,
7399 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007400
Hanno Beckere4aeb762019-02-05 17:19:52 +00007401 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7402 goto exit;
7403 }
7404 mbedtls_x509_crt_init( chain );
7405
7406 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007407 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007408 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007409
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007410#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7411 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007412 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007413
7414crt_verify:
7415 if( ssl->handshake->ecrs_enabled)
7416 rs_ctx = &ssl->handshake->ecrs_ctx;
7417#endif
7418
Hanno Becker3cf50612019-02-05 14:36:34 +00007419 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007420 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007421 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007422 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
Hanno Becker3008d282019-02-05 17:02:28 +00007424#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007425 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007426 size_t pk_len;
7427 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007428
Hanno Becker34106f62019-02-08 14:59:05 +00007429 /* We parse the CRT chain without copying, so
7430 * these pointers point into the input buffer,
7431 * and are hence still valid after freeing the
7432 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Hanno Becker5882dd02019-06-06 16:25:57 +01007434#if defined(MBEDTLS_SSL_RENEGOTIATION)
7435 unsigned char *crt_start;
7436 size_t crt_len;
7437
Hanno Becker34106f62019-02-08 14:59:05 +00007438 crt_start = chain->raw.p;
7439 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007440#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007441
Hanno Becker8c13ee62019-02-26 16:48:17 +00007442 pk_start = chain->cache->pk_raw.p;
7443 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007444
7445 /* Free the CRT structures before computing
7446 * digest and copying the peer's public key. */
7447 mbedtls_x509_crt_free( chain );
7448 mbedtls_free( chain );
7449 chain = NULL;
7450
Hanno Becker5882dd02019-06-06 16:25:57 +01007451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007452 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007453 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007454 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007455#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007456
Hanno Becker34106f62019-02-08 14:59:05 +00007457 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007459 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007460 }
Hanno Becker34106f62019-02-08 14:59:05 +00007461#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7462 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007463 ssl->session_negotiate->peer_cert = chain;
7464 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007465#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007468
Hanno Becker613d4902019-02-05 13:11:17 +00007469exit:
7470
Hanno Beckere4aeb762019-02-05 17:19:52 +00007471 if( ret == 0 )
7472 ssl->state++;
7473
7474#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7475 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7476 {
7477 ssl->handshake->ecrs_peer_cert = chain;
7478 chain = NULL;
7479 }
7480#endif
7481
7482 if( chain != NULL )
7483 {
7484 mbedtls_x509_crt_free( chain );
7485 mbedtls_free( chain );
7486 }
7487
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 return( ret );
7489}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007490#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007493{
7494 int ret;
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 ssl->out_msglen = 1;
7500 ssl->out_msg[0] = 1;
7501
Paul Bakker5121ce52009-01-03 21:22:43 +00007502 ssl->state++;
7503
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007504 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 return( ret );
7508 }
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
7512 return( 0 );
7513}
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007516{
7517 int ret;
7518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520
Hanno Becker327c93b2018-08-15 13:56:18 +01007521 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 return( ret );
7525 }
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007530 mbedtls_ssl_pend_fatal_alert( ssl,
7531 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 }
7534
Hanno Beckere678eaa2018-08-21 14:57:46 +01007535 /* CCS records are only accepted if they have length 1 and content '1',
7536 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007537
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007538 /*
7539 * Switch to our negotiated transform and session parameters for inbound
7540 * data.
7541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007543 ssl->transform_in = ssl->transform_negotiate;
7544 ssl->session_in = ssl->session_negotiate;
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007547 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007550 ssl_dtls_replay_reset( ssl );
7551#endif
7552
7553 /* Increment epoch */
7554 if( ++ssl->in_epoch == 0 )
7555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007557 /* This is highly unlikely to happen for legitimate reasons, so
7558 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007560 }
7561 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007562 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007564#if defined(MBEDTLS_SSL_PROTO_TLS)
7565 {
7566 memset( ssl->in_ctr, 0, 8 );
7567 }
7568#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007569
Hanno Beckerf5970a02019-05-08 09:38:41 +01007570 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7573 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007578 mbedtls_ssl_pend_fatal_alert( ssl,
7579 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 }
7582 }
7583#endif
7584
Paul Bakker5121ce52009-01-03 21:22:43 +00007585 ssl->state++;
7586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
7589 return( 0 );
7590}
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7595 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007596 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007597 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7600#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007601 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007604 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007607}
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007612
7613 /*
7614 * Free our handshake params
7615 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007616 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007618 ssl->handshake = NULL;
7619
7620 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007622 */
7623 if( ssl->transform )
7624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 mbedtls_ssl_transform_free( ssl->transform );
7626 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007627 }
7628 ssl->transform = ssl->transform_negotiate;
7629 ssl->transform_negotiate = NULL;
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007632}
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_RENEGOTIATION)
7639 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642 ssl->renego_records_seen = 0;
7643 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007644#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645
7646 /*
7647 * Free the previous session and switch in the current one
7648 */
Paul Bakker0a597072012-09-25 21:55:46 +00007649 if( ssl->session )
7650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007652 /* RFC 7366 3.1: keep the EtM state */
7653 ssl->session_negotiate->encrypt_then_mac =
7654 ssl->session->encrypt_then_mac;
7655#endif
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 mbedtls_ssl_session_free( ssl->session );
7658 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007659 }
7660 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007661 ssl->session_negotiate = NULL;
7662
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007663#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007664 /*
7665 * Add cache entry
7666 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007667 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007668 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007669 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007670 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007671 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007673 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007674#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007678 ssl->handshake->flight != NULL )
7679 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007680 /* Cancel handshake timer */
7681 ssl_set_timer( ssl, 0 );
7682
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007683 /* Keep last flight around in case we need to resend it:
7684 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007686 }
7687 else
7688#endif
7689 ssl_handshake_wrapup_free_hs_transform( ssl );
7690
Paul Bakker48916f92012-09-16 19:57:18 +00007691 ssl->state++;
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007694}
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007698 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007702 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007703
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007704 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7705 mbedtls_ssl_suite_get_mac(
7706 mbedtls_ssl_ciphersuite_from_id(
7707 mbedtls_ssl_session_get_ciphersuite(
7708 ssl->session_negotiate ) ) ),
7709 ssl, ssl->out_msg + 4,
7710 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007712 /*
7713 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7714 * may define some other value. Currently (early 2016), no defined
7715 * ciphersuite does this (and this is unlikely to change as activity has
7716 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7717 */
Hanno Becker2881d802019-05-22 14:44:53 +01007718 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007721 ssl->verify_data_len = hash_len;
7722 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007723#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007724
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7727 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007729#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 /*
7731 * In case of session resuming, invert the client and server
7732 * ChangeCipherSpec messages order.
7733 */
Paul Bakker0a597072012-09-25 21:55:46 +00007734 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007737 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7738 MBEDTLS_SSL_IS_CLIENT )
7739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007741 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007744 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7745 MBEDTLS_SSL_IS_SERVER )
7746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007748 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 }
7751 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007752#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 ssl->state++;
7754
Paul Bakker48916f92012-09-16 19:57:18 +00007755 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007756 * Switch to our negotiated transform and session parameters for outbound
7757 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007762 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007763 {
7764 unsigned char i;
7765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007766 /* Remember current epoch settings for resending */
7767 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007768 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007769
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007770 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007771 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007772
7773 /* Increment epoch */
7774 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007775 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007776 break;
7777
7778 /* The loop goes to its end iff the counter is wrapping */
7779 if( i == 0 )
7780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7782 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007783 }
7784 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007785 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007787#if defined(MBEDTLS_SSL_PROTO_TLS)
7788 {
7789 memset( ssl->cur_out_ctr, 0, 8 );
7790 }
7791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007792
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007793 ssl->transform_out = ssl->transform_negotiate;
7794 ssl->session_out = ssl->session_negotiate;
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7797 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7802 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007803 }
7804 }
7805#endif
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007808 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007810#endif
7811
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007812 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007813 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 return( ret );
7816 }
7817
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007818#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007819 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007820 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7821 {
7822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7823 return( ret );
7824 }
7825#endif
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007828
7829 return( 0 );
7830}
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007833#define SSL_MAX_HASH_LEN 36
7834#else
7835#define SSL_MAX_HASH_LEN 12
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007839{
Paul Bakker23986e52011-04-24 08:57:21 +00007840 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007841 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007842 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007846 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7847 mbedtls_ssl_suite_get_mac(
7848 mbedtls_ssl_ciphersuite_from_id(
7849 mbedtls_ssl_session_get_ciphersuite(
7850 ssl->session_negotiate ) ) ),
7851 ssl, buf,
7852 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007853
Hanno Becker327c93b2018-08-15 13:56:18 +01007854 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 return( ret );
7858 }
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007863 mbedtls_ssl_pend_fatal_alert( ssl,
7864 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 }
7867
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007868 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007870 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007871 hash_len = 36;
7872 else
7873#endif
7874 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7877 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007880 mbedtls_ssl_pend_fatal_alert( ssl,
7881 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 }
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007886 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007889 mbedtls_ssl_pend_fatal_alert( ssl,
7890 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 }
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007895 ssl->verify_data_len = hash_len;
7896 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007897#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007898
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007899#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007900 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007903 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007907 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007909#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007910 }
7911 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007912#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 ssl->state++;
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007916 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007918#endif
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
7922 return( 0 );
7923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7930 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7931 mbedtls_md5_init( &handshake->fin_md5 );
7932 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007933 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7934 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7937#if defined(MBEDTLS_SHA256_C)
7938 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007939 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SHA512_C)
7942 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007943 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007946
Hanno Becker7e5437a2017-04-28 17:15:26 +01007947#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7948 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7949 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7950#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952#if defined(MBEDTLS_DHM_C)
7953 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_ECDH_C)
7956 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007958#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007959 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007960#if defined(MBEDTLS_SSL_CLI_C)
7961 handshake->ecjpake_cache = NULL;
7962 handshake->ecjpake_cache_len = 0;
7963#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007964#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007965
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007966#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007967 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007968#endif
7969
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007970#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7971 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7972#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007973
7974#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7975 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7976 mbedtls_pk_init( &handshake->peer_pubkey );
7977#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978}
7979
Hanno Becker611a83b2018-01-03 14:27:32 +00007980void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7985 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007986
Hanno Becker92231322018-01-03 15:32:51 +00007987#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 mbedtls_md_init( &transform->md_ctx_enc );
7989 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007990#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991}
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996}
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007999{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008000 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008001 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008003 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008006 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008007
8008 /*
8009 * Either the pointers are now NULL or cleared properly and can be freed.
8010 * Now allocate missing structures.
8011 */
8012 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008013 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008014 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008015 }
Paul Bakker48916f92012-09-16 19:57:18 +00008016
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008017 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008018 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008019 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008020 }
Paul Bakker48916f92012-09-16 19:57:18 +00008021
Paul Bakker82788fb2014-10-20 13:59:19 +02008022 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008023 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008024 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008025 }
Paul Bakker48916f92012-09-16 19:57:18 +00008026
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008028 if( ssl->handshake == NULL ||
8029 ssl->transform_negotiate == NULL ||
8030 ssl->session_negotiate == NULL )
8031 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 mbedtls_free( ssl->handshake );
8035 mbedtls_free( ssl->transform_negotiate );
8036 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008037
8038 ssl->handshake = NULL;
8039 ssl->transform_negotiate = NULL;
8040 ssl->session_negotiate = NULL;
8041
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008042 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008043 }
8044
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008045 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008047 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008048 ssl_handshake_params_init( ssl->handshake );
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008051 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008052 {
8053 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008054
Hanno Becker2d9623f2019-06-13 12:07:05 +01008055 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008056 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8057 else
8058 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8059 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008060#endif
8061
Paul Bakker48916f92012-09-16 19:57:18 +00008062 return( 0 );
8063}
8064
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008065#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008066/* Dummy cookie callbacks for defaults */
8067static int ssl_cookie_write_dummy( void *ctx,
8068 unsigned char **p, unsigned char *end,
8069 const unsigned char *cli_id, size_t cli_id_len )
8070{
8071 ((void) ctx);
8072 ((void) p);
8073 ((void) end);
8074 ((void) cli_id);
8075 ((void) cli_id_len);
8076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008078}
8079
8080static int ssl_cookie_check_dummy( void *ctx,
8081 const unsigned char *cookie, size_t cookie_len,
8082 const unsigned char *cli_id, size_t cli_id_len )
8083{
8084 ((void) ctx);
8085 ((void) cookie);
8086 ((void) cookie_len);
8087 ((void) cli_id);
8088 ((void) cli_id_len);
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008091}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008092#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008093
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008094/* Once ssl->out_hdr as the address of the beginning of the
8095 * next outgoing record is set, deduce the other pointers.
8096 *
8097 * Note: For TLS, we save the implicit record sequence number
8098 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8099 * and the caller has to make sure there's space for this.
8100 */
8101
8102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8103 mbedtls_ssl_transform *transform )
8104{
8105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008106 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008107 {
8108 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008110 ssl->out_cid = ssl->out_ctr + 8;
8111 ssl->out_len = ssl->out_cid;
8112 if( transform != NULL )
8113 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008114#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008115 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008116#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008117 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008118 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008119 MBEDTLS_SSL_TRANSPORT_ELSE
8120#endif /* MBEDTLS_SSL_PROTO_DTLS */
8121#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008122 {
8123 ssl->out_ctr = ssl->out_hdr - 8;
8124 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008125#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008126 ssl->out_cid = ssl->out_len;
8127#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128 ssl->out_iv = ssl->out_hdr + 5;
8129 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008130#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008131
8132 /* Adjust out_msg to make space for explicit IV, if used. */
8133 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008134 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008135 {
8136 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8137 }
8138 else
8139 ssl->out_msg = ssl->out_iv;
8140}
8141
8142/* Once ssl->in_hdr as the address of the beginning of the
8143 * next incoming record is set, deduce the other pointers.
8144 *
8145 * Note: For TLS, we save the implicit record sequence number
8146 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8147 * and the caller has to make sure there's space for this.
8148 */
8149
Hanno Beckerf5970a02019-05-08 09:38:41 +01008150static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008151{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008152 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008153 * of unprotected TLS/DTLS records, with ssl->in_msg
8154 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008155 *
8156 * When decrypting a protected record, ssl->in_msg
8157 * will be shifted to point to the beginning of the
8158 * record plaintext.
8159 */
8160
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163 {
Hanno Becker70e79282019-05-03 14:34:53 +01008164 /* This sets the header pointers to match records
8165 * without CID. When we receive a record containing
8166 * a CID, the fields are shifted accordingly in
8167 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008169#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008170 ssl->in_cid = ssl->in_ctr + 8;
8171 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008172#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008173 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008175 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008176 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008177 MBEDTLS_SSL_TRANSPORT_ELSE
8178#endif /* MBEDTLS_SSL_PROTO_DTLS */
8179#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008180 {
8181 ssl->in_ctr = ssl->in_hdr - 8;
8182 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008184 ssl->in_cid = ssl->in_len;
8185#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008186 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008187 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008188#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008189}
8190
Paul Bakker5121ce52009-01-03 21:22:43 +00008191/*
8192 * Initialize an SSL context
8193 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008194void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8195{
8196 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8197}
8198
8199/*
8200 * Setup an SSL context
8201 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008202
8203static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8204{
8205 /* Set the incoming and outgoing record pointers. */
8206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008207 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008208 {
8209 ssl->out_hdr = ssl->out_buf;
8210 ssl->in_hdr = ssl->in_buf;
8211 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008214#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215 {
8216 ssl->out_hdr = ssl->out_buf + 8;
8217 ssl->in_hdr = ssl->in_buf + 8;
8218 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008220
8221 /* Derive other internal pointers. */
8222 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008223 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008224}
8225
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008226int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008227 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008228{
Paul Bakker48916f92012-09-16 19:57:18 +00008229 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008230
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008231 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008232
Hanno Beckeref982d52019-07-23 15:56:18 +01008233#if defined(MBEDTLS_USE_TINYCRYPT)
8234 uECC_set_rng( &uecc_rng_wrapper );
8235#endif
8236
Paul Bakker62f2dee2012-09-28 07:31:51 +00008237 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008238 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008239 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008240
8241 /* Set to NULL in case of an error condition */
8242 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008243
Angus Grattond8213d02016-05-25 20:56:48 +10008244 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8245 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008246 {
Angus Grattond8213d02016-05-25 20:56:48 +10008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008248 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008249 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008250 }
8251
8252 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8253 if( ssl->out_buf == NULL )
8254 {
8255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008256 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008257 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008258 }
8259
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008260 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008261
Paul Bakker48916f92012-09-16 19:57:18 +00008262 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008263 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008264
Hanno Beckerc8f52992019-07-25 11:15:08 +01008265 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008266
Paul Bakker5121ce52009-01-03 21:22:43 +00008267 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008268
8269error:
8270 mbedtls_free( ssl->in_buf );
8271 mbedtls_free( ssl->out_buf );
8272
8273 ssl->conf = NULL;
8274
8275 ssl->in_buf = NULL;
8276 ssl->out_buf = NULL;
8277
8278 ssl->in_hdr = NULL;
8279 ssl->in_ctr = NULL;
8280 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008281 ssl->in_msg = NULL;
8282
8283 ssl->out_hdr = NULL;
8284 ssl->out_ctr = NULL;
8285 ssl->out_len = NULL;
8286 ssl->out_iv = NULL;
8287 ssl->out_msg = NULL;
8288
k-stachowiak9f7798e2018-07-31 16:52:32 +02008289 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008290}
8291
8292/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008293 * Reset an initialized and used SSL context for re-use while retaining
8294 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008295 *
8296 * If partial is non-zero, keep data in the input buffer and client ID.
8297 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008299static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008300{
Paul Bakker48916f92012-09-16 19:57:18 +00008301 int ret;
8302
Hanno Becker7e772132018-08-10 12:38:21 +01008303#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8304 !defined(MBEDTLS_SSL_SRV_C)
8305 ((void) partial);
8306#endif
8307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008309
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008310 /* Cancel any possibly running timer */
8311 ssl_set_timer( ssl, 0 );
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313#if defined(MBEDTLS_SSL_RENEGOTIATION)
8314 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008315 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008316
8317 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8319 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008322
Paul Bakker7eb013f2011-10-06 12:37:39 +00008323 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008324 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325
8326 ssl->in_msgtype = 0;
8327 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008329 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008330 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008333 ssl_dtls_replay_reset( ssl );
8334#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008335
8336 ssl->in_hslen = 0;
8337 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008338
8339 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008340
8341 ssl->out_msgtype = 0;
8342 ssl->out_msglen = 0;
8343 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8345 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008346 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008347#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008348
Hanno Becker19859472018-08-06 09:40:20 +01008349 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8350
Paul Bakker48916f92012-09-16 19:57:18 +00008351 ssl->transform_in = NULL;
8352 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008353
Hanno Becker78640902018-08-13 16:35:15 +01008354 ssl->session_in = NULL;
8355 ssl->session_out = NULL;
8356
Angus Grattond8213d02016-05-25 20:56:48 +10008357 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008358
8359#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008360 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008361#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8362 {
8363 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008364 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008365 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8368 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8371 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8374 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008375 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008376 }
8377#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008378
Paul Bakker48916f92012-09-16 19:57:18 +00008379 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 mbedtls_ssl_transform_free( ssl->transform );
8382 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008383 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008384 }
Paul Bakker48916f92012-09-16 19:57:18 +00008385
Paul Bakkerc0463502013-02-14 11:19:38 +01008386 if( ssl->session )
8387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 mbedtls_ssl_session_free( ssl->session );
8389 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008390 ssl->session = NULL;
8391 }
8392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008394 ssl->alpn_chosen = NULL;
8395#endif
8396
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008397#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008398#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008399 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008400#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008401 {
8402 mbedtls_free( ssl->cli_id );
8403 ssl->cli_id = NULL;
8404 ssl->cli_id_len = 0;
8405 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008406#endif
8407
Paul Bakker48916f92012-09-16 19:57:18 +00008408 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8409 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008410
8411 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008412}
8413
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008414/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008415 * Reset an initialized and used SSL context for re-use while retaining
8416 * all application-set variables, function pointers and data.
8417 */
8418int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8419{
8420 return( ssl_session_reset_int( ssl, 0 ) );
8421}
8422
8423/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 * SSL set accessors
8425 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008426#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008427void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008429 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008430}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008431#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008432
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008433void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008435 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008436}
8437
Hanno Becker7f376f42019-06-12 16:20:48 +01008438#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8439 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008440void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008441{
Hanno Becker7f376f42019-06-12 16:20:48 +01008442 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008443}
Hanno Becker7f376f42019-06-12 16:20:48 +01008444#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008445
Hanno Beckerde671542019-06-12 16:30:46 +01008446#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8447 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8448void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8449 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008451 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008452}
Hanno Beckerde671542019-06-12 16:30:46 +01008453#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008456
Hanno Becker1841b0a2018-08-24 11:13:57 +01008457void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8458 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008459{
8460 ssl->disable_datagram_packing = !allow_packing;
8461}
8462
Hanno Becker1f835fa2019-06-13 10:14:59 +01008463#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8464 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008465void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8466 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->hs_timeout_min = min;
8469 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008470}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008471#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8472 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8473void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8474 uint32_t min, uint32_t max )
8475{
8476 ((void) conf);
8477 ((void) min);
8478 ((void) max);
8479}
8480#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8481 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8482
8483#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008484
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008485void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008486{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008487#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8488 conf->authmode = authmode;
8489#else
8490 ((void) conf);
8491 ((void) authmode);
8492#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008493}
8494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008496void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008497 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008498 void *p_vrfy )
8499{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008500 conf->f_vrfy = f_vrfy;
8501 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008504
Hanno Beckerece325c2019-06-13 15:39:27 +01008505#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008507 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008508 void *p_rng )
8509{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008510 conf->f_rng = f_rng;
8511 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008512}
Hanno Beckerece325c2019-06-13 15:39:27 +01008513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008514
Hanno Becker14a4a442019-07-02 17:00:34 +01008515#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008517 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008518 void *p_dbg )
8519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008520 conf->f_dbg = f_dbg;
8521 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008522}
Hanno Becker14a4a442019-07-02 17:00:34 +01008523#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
Hanno Beckera58a8962019-06-13 16:11:15 +01008525#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8526 !defined(MBEDTLS_SSL_CONF_SEND) && \
8527 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008529 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008530 mbedtls_ssl_send_t *f_send,
8531 mbedtls_ssl_recv_t *f_recv,
8532 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008533{
Hanno Beckera58a8962019-06-13 16:11:15 +01008534 ssl->p_bio = p_bio;
8535 ssl->f_send = f_send;
8536 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008537 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008538}
Hanno Beckera58a8962019-06-13 16:11:15 +01008539#else
8540void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8541 void *p_bio )
8542{
8543 ssl->p_bio = p_bio;
8544}
8545#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008546
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008547#if defined(MBEDTLS_SSL_PROTO_DTLS)
8548void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8549{
8550 ssl->mtu = mtu;
8551}
8552#endif
8553
Hanno Becker1f835fa2019-06-13 10:14:59 +01008554#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008556{
8557 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008558}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008559#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008560
Hanno Becker0ae6b242019-06-13 16:45:36 +01008561#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8562 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008563void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8564 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008565 mbedtls_ssl_set_timer_t *f_set_timer,
8566 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008567{
8568 ssl->p_timer = p_timer;
8569 ssl->f_set_timer = f_set_timer;
8570 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008571 /* Make sure we start with no timer running */
8572 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008573}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008574#else
8575void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8576 void *p_timer )
8577{
8578 ssl->p_timer = p_timer;
8579 /* Make sure we start with no timer running */
8580 ssl_set_timer( ssl, 0 );
8581}
8582#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008583
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008584#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008585void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008586 void *p_cache,
8587 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8588 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008589{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008590 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008592 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008593}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008594#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008595
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008596#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008598{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008599 int ret;
8600
8601 if( ssl == NULL ||
8602 session == NULL ||
8603 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008604 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008607 }
8608
Hanno Becker58fccf22019-02-06 14:30:46 +00008609 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8610 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008611 return( ret );
8612
Paul Bakker0a597072012-09-25 21:55:46 +00008613 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008614
8615 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008616}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008617#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008618
Hanno Becker73f4cb12019-06-27 13:51:07 +01008619#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008621 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8624 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8625 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8626 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008627}
8628
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008630 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008631 int major, int minor )
8632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008634 return;
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008637 return;
8638
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008639 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008640}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008641#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008644void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008645 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008646{
8647 conf->cert_profile = profile;
8648}
8649
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008650/* Append a new keycert entry to a (possibly empty) list */
8651static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8652 mbedtls_x509_crt *cert,
8653 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008654{
niisato8ee24222018-06-25 19:05:48 +09008655 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008656
niisato8ee24222018-06-25 19:05:48 +09008657 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8658 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008659 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008660
niisato8ee24222018-06-25 19:05:48 +09008661 new_cert->cert = cert;
8662 new_cert->key = key;
8663 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008664
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008665 /* Update head is the list was null, else add to the end */
8666 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008667 {
niisato8ee24222018-06-25 19:05:48 +09008668 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008669 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008670 else
8671 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008672 mbedtls_ssl_key_cert *cur = *head;
8673 while( cur->next != NULL )
8674 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008675 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008676 }
8677
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008678 return( 0 );
8679}
8680
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008681int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008682 mbedtls_x509_crt *own_cert,
8683 mbedtls_pk_context *pk_key )
8684{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008685 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008686}
8687
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008688void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008689 mbedtls_x509_crt *ca_chain,
8690 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008691{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008692 conf->ca_chain = ca_chain;
8693 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008696
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008697#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8698int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8699 mbedtls_x509_crt *own_cert,
8700 mbedtls_pk_context *pk_key )
8701{
8702 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8703 own_cert, pk_key ) );
8704}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008705
8706void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8707 mbedtls_x509_crt *ca_chain,
8708 mbedtls_x509_crl *ca_crl )
8709{
8710 ssl->handshake->sni_ca_chain = ca_chain;
8711 ssl->handshake->sni_ca_crl = ca_crl;
8712}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008713
8714void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8715 int authmode )
8716{
8717 ssl->handshake->sni_authmode = authmode;
8718}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008719#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8720
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008721#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008722/*
8723 * Set EC J-PAKE password for current handshake
8724 */
8725int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8726 const unsigned char *pw,
8727 size_t pw_len )
8728{
8729 mbedtls_ecjpake_role role;
8730
Janos Follath8eb64132016-06-03 15:40:57 +01008731 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8733
Hanno Becker2d9623f2019-06-13 12:07:05 +01008734 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008735 role = MBEDTLS_ECJPAKE_SERVER;
8736 else
8737 role = MBEDTLS_ECJPAKE_CLIENT;
8738
8739 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8740 role,
8741 MBEDTLS_MD_SHA256,
8742 MBEDTLS_ECP_DP_SECP256R1,
8743 pw, pw_len ) );
8744}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008745#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008748int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008749 const unsigned char *psk, size_t psk_len,
8750 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008751{
Paul Bakker6db455e2013-09-18 17:29:31 +02008752 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008757
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008758 /* Identity len will be encoded on two bytes */
8759 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008760 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008761 {
8762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8763 }
8764
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008765 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008766 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008767 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008768
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008769 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008770 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008771 conf->psk_len = 0;
8772 }
8773 if( conf->psk_identity != NULL )
8774 {
8775 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008776 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008777 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008778 }
8779
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008780 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8781 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008782 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008783 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008784 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008785 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008786 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008787 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008788 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008789
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008790 conf->psk_len = psk_len;
8791 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008792
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008793 memcpy( conf->psk, psk, conf->psk_len );
8794 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008795
8796 return( 0 );
8797}
8798
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008799int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8800 const unsigned char *psk, size_t psk_len )
8801{
8802 if( psk == NULL || ssl->handshake == NULL )
8803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8804
8805 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8807
8808 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008809 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008810 mbedtls_platform_zeroize( ssl->handshake->psk,
8811 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008812 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008813 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008814 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008815
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008816 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008818
8819 ssl->handshake->psk_len = psk_len;
8820 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8821
8822 return( 0 );
8823}
8824
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008825void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008827 size_t),
8828 void *p_psk )
8829{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008830 conf->f_psk = f_psk;
8831 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008834
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008836
8837#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008838int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008839{
8840 int ret;
8841
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008842 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8843 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8844 {
8845 mbedtls_mpi_free( &conf->dhm_P );
8846 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008847 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008849
8850 return( 0 );
8851}
Hanno Becker470a8c42017-10-04 15:28:46 +01008852#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008853
Hanno Beckera90658f2017-10-04 15:29:08 +01008854int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8855 const unsigned char *dhm_P, size_t P_len,
8856 const unsigned char *dhm_G, size_t G_len )
8857{
8858 int ret;
8859
8860 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8861 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8862 {
8863 mbedtls_mpi_free( &conf->dhm_P );
8864 mbedtls_mpi_free( &conf->dhm_G );
8865 return( ret );
8866 }
8867
8868 return( 0 );
8869}
Paul Bakker5121ce52009-01-03 21:22:43 +00008870
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008871int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008872{
8873 int ret;
8874
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008875 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8876 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8877 {
8878 mbedtls_mpi_free( &conf->dhm_P );
8879 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008880 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008881 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008882
8883 return( 0 );
8884}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008885#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008886
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008887#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8888/*
8889 * Set the minimum length for Diffie-Hellman parameters
8890 */
8891void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8892 unsigned int bitlen )
8893{
8894 conf->dhm_min_bitlen = bitlen;
8895}
8896#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8897
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008898#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008899/*
8900 * Set allowed/preferred hashes for handshake signatures
8901 */
8902void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8903 const int *hashes )
8904{
Hanno Becker56595f42019-06-19 16:31:38 +01008905#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008906 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008907#else
8908 ((void) conf);
8909 ((void) hashes);
8910#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008911}
Hanno Becker947194e2017-04-07 13:25:49 +01008912#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008913
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008914#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008915#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008916/*
8917 * Set the allowed elliptic curves
8918 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008919void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008920 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008922 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008923}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008924#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008925#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008926
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008927#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008929{
Hanno Becker947194e2017-04-07 13:25:49 +01008930 /* Initialize to suppress unnecessary compiler warning */
8931 size_t hostname_len = 0;
8932
8933 /* Check if new hostname is valid before
8934 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008935 if( hostname != NULL )
8936 {
8937 hostname_len = strlen( hostname );
8938
8939 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8941 }
8942
8943 /* Now it's clear that we will overwrite the old hostname,
8944 * so we can free it safely */
8945
8946 if( ssl->hostname != NULL )
8947 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008948 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008949 mbedtls_free( ssl->hostname );
8950 }
8951
8952 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008953
Paul Bakker5121ce52009-01-03 21:22:43 +00008954 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008955 {
8956 ssl->hostname = NULL;
8957 }
8958 else
8959 {
8960 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008961 if( ssl->hostname == NULL )
8962 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008963
Hanno Becker947194e2017-04-07 13:25:49 +01008964 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008965
Hanno Becker947194e2017-04-07 13:25:49 +01008966 ssl->hostname[hostname_len] = '\0';
8967 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008968
8969 return( 0 );
8970}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008971#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008972
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008973#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008974void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008976 const unsigned char *, size_t),
8977 void *p_sni )
8978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008979 conf->f_sni = f_sni;
8980 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008985int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008986{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008987 size_t cur_len, tot_len;
8988 const char **p;
8989
8990 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008991 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8992 * MUST NOT be truncated."
8993 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008994 */
8995 tot_len = 0;
8996 for( p = protos; *p != NULL; p++ )
8997 {
8998 cur_len = strlen( *p );
8999 tot_len += cur_len;
9000
9001 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009003 }
9004
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009005 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009006
9007 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009008}
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009011{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009012 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009015
Hanno Becker33b9b252019-07-05 11:23:25 +01009016#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9017 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9018void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9019 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009020{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009021 conf->max_major_ver = major;
9022 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009023}
Hanno Becker33b9b252019-07-05 11:23:25 +01009024#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9025 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009026
Hanno Becker33b9b252019-07-05 11:23:25 +01009027#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9028 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9029void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9030 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->min_major_ver = major;
9033 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009034}
Hanno Becker33b9b252019-07-05 11:23:25 +01009035#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9036 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009039void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009040{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009041 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009042}
9043#endif
9044
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009045#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009046void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9047 char cert_req_ca_list )
9048{
9049 conf->cert_req_ca_list = cert_req_ca_list;
9050}
9051#endif
9052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009054void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009057}
9058#endif
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009061#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009062void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009064 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009065}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009066#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9067#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009068void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009069 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009070{
9071 conf->enforce_extended_master_secret = ems_enf;
9072}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009073#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009074#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009075
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009076#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009080}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009084int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009087 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090 }
9091
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009092 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093
9094 return( 0 );
9095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009099void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009101 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009107{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009108 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009109}
9110#endif
9111
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009112#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009113void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009115 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009116}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009117#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009120void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009122 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009123}
9124
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009128}
9129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009131 const unsigned char period[8] )
9132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009137#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009138#if defined(MBEDTLS_SSL_CLI_C)
9139void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009140{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009141 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009142}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009144
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009145#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009146void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9147 mbedtls_ssl_ticket_write_t *f_ticket_write,
9148 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9149 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009150{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009151 conf->f_ticket_write = f_ticket_write;
9152 conf->f_ticket_parse = f_ticket_parse;
9153 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009154}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009157
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009158#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9159void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9160 mbedtls_ssl_export_keys_t *f_export_keys,
9161 void *p_export_keys )
9162{
9163 conf->f_export_keys = f_export_keys;
9164 conf->p_export_keys = p_export_keys;
9165}
9166#endif
9167
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009168#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009169void mbedtls_ssl_conf_async_private_cb(
9170 mbedtls_ssl_config *conf,
9171 mbedtls_ssl_async_sign_t *f_async_sign,
9172 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9173 mbedtls_ssl_async_resume_t *f_async_resume,
9174 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009175 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009176{
9177 conf->f_async_sign_start = f_async_sign;
9178 conf->f_async_decrypt_start = f_async_decrypt;
9179 conf->f_async_resume = f_async_resume;
9180 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009181 conf->p_async_config_data = async_config_data;
9182}
9183
Gilles Peskine8f97af72018-04-26 11:46:10 +02009184void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9185{
9186 return( conf->p_async_config_data );
9187}
9188
Gilles Peskine1febfef2018-04-30 11:54:39 +02009189void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190{
9191 if( ssl->handshake == NULL )
9192 return( NULL );
9193 else
9194 return( ssl->handshake->user_async_ctx );
9195}
9196
Gilles Peskine1febfef2018-04-30 11:54:39 +02009197void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009198 void *ctx )
9199{
9200 if( ssl->handshake != NULL )
9201 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009202}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009203#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204
Paul Bakker5121ce52009-01-03 21:22:43 +00009205/*
9206 * SSL get accessors
9207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009209{
9210 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9211}
9212
Hanno Becker8b170a02017-10-10 11:51:19 +01009213int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9214{
9215 /*
9216 * Case A: We're currently holding back
9217 * a message for further processing.
9218 */
9219
9220 if( ssl->keep_current_message == 1 )
9221 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009223 return( 1 );
9224 }
9225
9226 /*
9227 * Case B: Further records are pending in the current datagram.
9228 */
9229
9230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009232 ssl->in_left > ssl->next_record_offset )
9233 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009235 return( 1 );
9236 }
9237#endif /* MBEDTLS_SSL_PROTO_DTLS */
9238
9239 /*
9240 * Case C: A handshake message is being processed.
9241 */
9242
Hanno Becker8b170a02017-10-10 11:51:19 +01009243 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9244 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 return( 1 );
9247 }
9248
9249 /*
9250 * Case D: An application data message is being processed
9251 */
9252 if( ssl->in_offt != NULL )
9253 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009255 return( 1 );
9256 }
9257
9258 /*
9259 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009260 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 * we implement support for multiple alerts in single records.
9262 */
9263
9264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9265 return( 0 );
9266}
9267
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009268uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009269{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009270 if( ssl->session != NULL )
9271 return( ssl->session->verify_result );
9272
9273 if( ssl->session_negotiate != NULL )
9274 return( ssl->session_negotiate->verify_result );
9275
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009276 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009277}
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009280{
Hanno Beckere02758c2019-06-26 15:31:31 +01009281 int suite;
9282
Paul Bakker926c8e42013-03-06 10:23:34 +01009283 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009284 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009285
Hanno Beckere02758c2019-06-26 15:31:31 +01009286 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9287 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009294 {
Hanno Becker2881d802019-05-22 14:44:53 +01009295 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009298 return( "DTLSv1.0" );
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009301 return( "DTLSv1.2" );
9302
9303 default:
9304 return( "unknown (DTLS)" );
9305 }
9306 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009307 MBEDTLS_SSL_TRANSPORT_ELSE
9308#endif /* MBEDTLS_SSL_PROTO_DTLS */
9309#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009310 {
Hanno Becker2881d802019-05-22 14:44:53 +01009311 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009312 {
9313 case MBEDTLS_SSL_MINOR_VERSION_0:
9314 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009316 case MBEDTLS_SSL_MINOR_VERSION_1:
9317 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009319 case MBEDTLS_SSL_MINOR_VERSION_2:
9320 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009322 case MBEDTLS_SSL_MINOR_VERSION_3:
9323 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009324
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009325 default:
9326 return( "unknown" );
9327 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009329#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330}
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009333{
Hanno Becker3136ede2018-08-17 15:28:19 +01009334 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009336
Hanno Becker43395762019-05-03 14:46:38 +01009337 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9338
Hanno Becker78640902018-08-13 16:35:15 +01009339 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009340 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_ZLIB_SUPPORT)
9343 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9344 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345#endif
9346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009348 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009349#if defined(MBEDTLS_GCM_C) || \
9350 defined(MBEDTLS_CCM_C) || \
9351 defined(MBEDTLS_CHACHAPOLY_C)
9352#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009354#endif
9355#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009357#endif
9358#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009359 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009360#endif
9361 transform_expansion =
9362 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009363 break;
9364
Hanno Beckera9d5c452019-07-25 16:47:12 +01009365#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9366 MBEDTLS_CHACHAPOLY_C */
9367
9368#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9369 case MBEDTLS_MODE_STREAM:
9370 transform_expansion = transform->maclen;
9371 break;
9372#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9373
9374#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009376 {
9377 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009378
9379 block_size = mbedtls_cipher_get_block_size(
9380 &transform->cipher_ctx_enc );
9381
Hanno Becker3136ede2018-08-17 15:28:19 +01009382 /* Expansion due to the addition of the MAC. */
9383 transform_expansion += transform->maclen;
9384
9385 /* Expansion due to the addition of CBC padding;
9386 * Theoretically up to 256 bytes, but we never use
9387 * more than the block size of the underlying cipher. */
9388 transform_expansion += block_size;
9389
9390 /* For TLS 1.1 or higher, an explicit IV is added
9391 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009392#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009393 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009394 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009395#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009396
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009397 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009398 }
9399#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009400
9401 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009404 }
9405
Hanno Beckera5a2b082019-05-15 14:03:01 +01009406#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009407 if( transform->out_cid_len != 0 )
9408 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009410
Hanno Becker43395762019-05-03 14:46:38 +01009411 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009412}
9413
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9415size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9416{
9417 size_t max_len;
9418
9419 /*
9420 * Assume mfl_code is correct since it was checked when set
9421 */
Angus Grattond8213d02016-05-25 20:56:48 +10009422 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009423
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009424 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009425 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009426 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009427 {
Angus Grattond8213d02016-05-25 20:56:48 +10009428 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009429 }
9430
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009431 /* During a handshake, use the value being negotiated */
9432 if( ssl->session_negotiate != NULL &&
9433 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9434 {
9435 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9436 }
9437
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009438 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009439}
9440#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9441
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009442#if defined(MBEDTLS_SSL_PROTO_DTLS)
9443static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9444{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009445 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009446 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009447 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9448 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9449 return ( 0 );
9450
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009451 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9452 return( ssl->mtu );
9453
9454 if( ssl->mtu == 0 )
9455 return( ssl->handshake->mtu );
9456
9457 return( ssl->mtu < ssl->handshake->mtu ?
9458 ssl->mtu : ssl->handshake->mtu );
9459}
9460#endif /* MBEDTLS_SSL_PROTO_DTLS */
9461
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009462int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9463{
9464 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9465
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009466#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9467 !defined(MBEDTLS_SSL_PROTO_DTLS)
9468 (void) ssl;
9469#endif
9470
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009471#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9472 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9473
9474 if( max_len > mfl )
9475 max_len = mfl;
9476#endif
9477
9478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009479 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009480 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009481 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9483 const size_t overhead = (size_t) ret;
9484
9485 if( ret < 0 )
9486 return( ret );
9487
9488 if( mtu <= overhead )
9489 {
9490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9491 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9492 }
9493
9494 if( max_len > mtu - overhead )
9495 max_len = mtu - overhead;
9496 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009497#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009498
Hanno Becker0defedb2018-08-10 12:35:02 +01009499#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9500 !defined(MBEDTLS_SSL_PROTO_DTLS)
9501 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009502#endif
9503
9504 return( (int) max_len );
9505}
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_X509_CRT_PARSE_C)
9508const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009509{
9510 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009511 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009512
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009513#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009514 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009515#else
9516 return( NULL );
9517#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009522int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9523 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009524{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009525 if( ssl == NULL ||
9526 dst == NULL ||
9527 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009528 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009531 }
9532
Hanno Becker58fccf22019-02-06 14:30:46 +00009533 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009537const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9538{
9539 if( ssl == NULL )
9540 return( NULL );
9541
9542 return( ssl->session );
9543}
9544
Paul Bakker5121ce52009-01-03 21:22:43 +00009545/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009546 * Define ticket header determining Mbed TLS version
9547 * and structure of the ticket.
9548 */
9549
Hanno Becker41527622019-05-16 12:50:45 +01009550/*
Hanno Becker26829e92019-05-28 14:30:45 +01009551 * Define bitflag determining compile-time settings influencing
9552 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009553 */
9554
Hanno Becker26829e92019-05-28 14:30:45 +01009555#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009556#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009557#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009558#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009559#endif /* MBEDTLS_HAVE_TIME */
9560
9561#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009562#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009563#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009564#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009565#endif /* MBEDTLS_X509_CRT_PARSE_C */
9566
9567#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009568#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009569#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009570#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009571#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9572
9573#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009574#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009575#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009576#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009577#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9578
9579#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009580#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009581#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009582#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009583#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9584
9585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009586#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009587#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009588#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009589#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9590
Hanno Becker41527622019-05-16 12:50:45 +01009591#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9592#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9593#else
9594#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9595#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9596
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009597#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9598#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9599#else
9600#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9601#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9602
Hanno Becker88440552019-07-03 14:16:13 +01009603#if defined(MBEDTLS_ZLIB_SUPPORT)
9604#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9605#else
9606#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9607#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9608
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009609#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9610#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9611#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9612#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9613#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9614#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9615#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009616#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009617#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009618
Hanno Becker26829e92019-05-28 14:30:45 +01009619#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009620 ( (uint16_t) ( \
9621 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9622 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9623 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9624 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9625 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9626 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009627 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009628 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009629 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009630
Hanno Becker557fe9f2019-05-16 12:41:07 +01009631static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009632 MBEDTLS_VERSION_MAJOR,
9633 MBEDTLS_VERSION_MINOR,
9634 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009635 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9636 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009637};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009638
9639/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009640 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009641 * (in the presentation language of TLS, RFC 8446 section 3)
9642 *
Hanno Becker26829e92019-05-28 14:30:45 +01009643 * opaque mbedtls_version[3]; // major, minor, patch
9644 * opaque session_format[2]; // version-specific 16-bit field determining
9645 * // the format of the remaining
9646 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009647 *
9648 * Note: When updating the format, remember to keep
9649 * these version+format bytes.
9650 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009651 * // In this version, `session_format` determines
9652 * // the setting of those compile-time
9653 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009654 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009655 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009656 * uint8 ciphersuite[2]; // defined by the standard
9657 * uint8 compression; // 0 or 1
9658 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009659 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009660 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009661 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009662 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9663 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9664 * case disabled: uint8_t peer_cert_digest_type;
9665 * opaque peer_cert_digest<0..2^8-1>;
9666 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009667 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009668 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009669 * uint8 mfl_code; // up to 255 according to standard
9670 * uint8 trunc_hmac; // 0 or 1
9671 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009672 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009673 * The order is the same as in the definition of the structure, except
9674 * verify_result is put before peer_cert so that all mandatory fields come
9675 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009676 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009677static int ssl_session_save( const mbedtls_ssl_session *session,
9678 unsigned char omit_header,
9679 unsigned char *buf,
9680 size_t buf_len,
9681 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009682{
9683 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009684 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009685#if defined(MBEDTLS_HAVE_TIME)
9686 uint64_t start;
9687#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009688#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009689#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009690 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009691#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009692#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009693
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009694 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009695 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009696 /*
9697 * Add version identifier
9698 */
9699
9700 used += sizeof( ssl_serialized_session_header );
9701
9702 if( used <= buf_len )
9703 {
9704 memcpy( p, ssl_serialized_session_header,
9705 sizeof( ssl_serialized_session_header ) );
9706 p += sizeof( ssl_serialized_session_header );
9707 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009708 }
9709
9710 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009711 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009712 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009713#if defined(MBEDTLS_HAVE_TIME)
9714 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009715
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009716 if( used <= buf_len )
9717 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009718 start = (uint64_t) session->start;
9719
9720 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9721 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9722 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9723 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9724 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9725 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9727 *p++ = (unsigned char)( ( start ) & 0xFF );
9728 }
9729#endif /* MBEDTLS_HAVE_TIME */
9730
9731 /*
9732 * Basic mandatory fields
9733 */
Hanno Becker88440552019-07-03 14:16:13 +01009734 {
9735 size_t const ciphersuite_len = 2;
9736#if defined(MBEDTLS_ZLIB_SUPPORT)
9737 size_t const compression_len = 1;
9738#else
9739 size_t const compression_len = 0;
9740#endif
9741 size_t const id_len_len = 1;
9742 size_t const id_len = 32;
9743 size_t const master_len = 48;
9744 size_t const verif_result_len = 4;
9745
9746 size_t const basic_len =
9747 ciphersuite_len +
9748 compression_len +
9749 id_len_len +
9750 id_len +
9751 master_len +
9752 verif_result_len;
9753
9754 used += basic_len;
9755 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009756
9757 if( used <= buf_len )
9758 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009759 const int ciphersuite =
9760 mbedtls_ssl_session_get_ciphersuite( session );
9761 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9762 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009763
Hanno Becker88440552019-07-03 14:16:13 +01009764#if defined(MBEDTLS_ZLIB_SUPPORT)
9765 *p++ = (unsigned char)(
9766 mbedtls_ssl_session_get_compression( session ) );
9767#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009768
9769 *p++ = (unsigned char)( session->id_len & 0xFF );
9770 memcpy( p, session->id, 32 );
9771 p += 32;
9772
9773 memcpy( p, session->master, 48 );
9774 p += 48;
9775
9776 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9777 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9778 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9779 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009780 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009781
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009782 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009783 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009784 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009785#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009786#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009787 if( session->peer_cert == NULL )
9788 cert_len = 0;
9789 else
9790 cert_len = session->peer_cert->raw.len;
9791
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009792 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009793
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009794 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009795 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009796 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9797 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9798 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9799
9800 if( session->peer_cert != NULL )
9801 {
9802 memcpy( p, session->peer_cert->raw.p, cert_len );
9803 p += cert_len;
9804 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009805 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009806
Hanno Becker5882dd02019-06-06 16:25:57 +01009807#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009808 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009809 if( session->peer_cert_digest != NULL )
9810 {
9811 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9812 if( used <= buf_len )
9813 {
9814 *p++ = (unsigned char) session->peer_cert_digest_type;
9815 *p++ = (unsigned char) session->peer_cert_digest_len;
9816 memcpy( p, session->peer_cert_digest,
9817 session->peer_cert_digest_len );
9818 p += session->peer_cert_digest_len;
9819 }
9820 }
9821 else
9822 {
9823 used += 2;
9824 if( used <= buf_len )
9825 {
9826 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9827 *p++ = 0;
9828 }
9829 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009830#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009831#endif /* MBEDTLS_X509_CRT_PARSE_C */
9832
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009833 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009834 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009835 */
9836#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009837 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009838
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009839 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009840 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009841 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9842 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9843 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9844
9845 if( session->ticket != NULL )
9846 {
9847 memcpy( p, session->ticket, session->ticket_len );
9848 p += session->ticket_len;
9849 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009850
9851 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9852 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9853 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9854 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009855 }
9856#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9857
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009858 /*
9859 * Misc extension-related info
9860 */
9861#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9862 used += 1;
9863
9864 if( used <= buf_len )
9865 *p++ = session->mfl_code;
9866#endif
9867
9868#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9869 used += 1;
9870
9871 if( used <= buf_len )
9872 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9873#endif
9874
9875#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9876 used += 1;
9877
9878 if( used <= buf_len )
9879 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9880#endif
9881
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009882 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009883 *olen = used;
9884
9885 if( used > buf_len )
9886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009887
9888 return( 0 );
9889}
9890
9891/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009892 * Public wrapper for ssl_session_save()
9893 */
9894int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9895 unsigned char *buf,
9896 size_t buf_len,
9897 size_t *olen )
9898{
9899 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9900}
9901
9902/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009903 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009904 *
9905 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009906 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009907 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009908static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009909 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009910 const unsigned char *buf,
9911 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009912{
9913 const unsigned char *p = buf;
9914 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009915 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009916#if defined(MBEDTLS_HAVE_TIME)
9917 uint64_t start;
9918#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009919#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009920#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009921 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009922#endif
9923#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009924
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009925 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009926 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009927 /*
9928 * Check version identifier
9929 */
9930
9931 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9933
9934 if( memcmp( p, ssl_serialized_session_header,
9935 sizeof( ssl_serialized_session_header ) ) != 0 )
9936 {
9937 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9938 }
9939 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009940 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009941
9942 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009943 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009944 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009945#if defined(MBEDTLS_HAVE_TIME)
9946 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9948
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009949 start = ( (uint64_t) p[0] << 56 ) |
9950 ( (uint64_t) p[1] << 48 ) |
9951 ( (uint64_t) p[2] << 40 ) |
9952 ( (uint64_t) p[3] << 32 ) |
9953 ( (uint64_t) p[4] << 24 ) |
9954 ( (uint64_t) p[5] << 16 ) |
9955 ( (uint64_t) p[6] << 8 ) |
9956 ( (uint64_t) p[7] );
9957 p += 8;
9958
9959 session->start = (time_t) start;
9960#endif /* MBEDTLS_HAVE_TIME */
9961
9962 /*
9963 * Basic mandatory fields
9964 */
Hanno Becker88440552019-07-03 14:16:13 +01009965 {
9966 size_t const ciphersuite_len = 2;
9967#if defined(MBEDTLS_ZLIB_SUPPORT)
9968 size_t const compression_len = 1;
9969#else
9970 size_t const compression_len = 0;
9971#endif
9972 size_t const id_len_len = 1;
9973 size_t const id_len = 32;
9974 size_t const master_len = 48;
9975 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009976
Hanno Becker88440552019-07-03 14:16:13 +01009977 size_t const basic_len =
9978 ciphersuite_len +
9979 compression_len +
9980 id_len_len +
9981 id_len +
9982 master_len +
9983 verif_result_len;
9984
9985 if( basic_len > (size_t)( end - p ) )
9986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9987 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009988
Hanno Beckere02758c2019-06-26 15:31:31 +01009989 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009990 p += 2;
9991
Hanno Becker73f4cb12019-06-27 13:51:07 +01009992#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009993 session->ciphersuite = ciphersuite;
9994#else
9995 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009996 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009997 {
9998 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9999 }
10000#endif
10001
Hanno Becker88440552019-07-03 14:16:13 +010010002#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010003 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010004#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010005
10006 session->id_len = *p++;
10007 memcpy( session->id, p, 32 );
10008 p += 32;
10009
10010 memcpy( session->master, p, 48 );
10011 p += 48;
10012
10013 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10014 ( (uint32_t) p[1] << 16 ) |
10015 ( (uint32_t) p[2] << 8 ) |
10016 ( (uint32_t) p[3] );
10017 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010018
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010019 /* Immediately clear invalid pointer values that have been read, in case
10020 * we exit early before we replaced them with valid ones. */
10021#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010022#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010023 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010024#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010025 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010026#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010027#endif
10028#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10029 session->ticket = NULL;
10030#endif
10031
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010032 /*
10033 * Peer certificate
10034 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010036#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010037 if( 3 > (size_t)( end - p ) )
10038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10039
10040 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10041 p += 3;
10042
10043 if( cert_len == 0 )
10044 {
10045 session->peer_cert = NULL;
10046 }
10047 else
10048 {
10049 int ret;
10050
10051 if( cert_len > (size_t)( end - p ) )
10052 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10053
10054 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10055
10056 if( session->peer_cert == NULL )
10057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10058
10059 mbedtls_x509_crt_init( session->peer_cert );
10060
10061 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10062 p, cert_len ) ) != 0 )
10063 {
10064 mbedtls_x509_crt_free( session->peer_cert );
10065 mbedtls_free( session->peer_cert );
10066 session->peer_cert = NULL;
10067 return( ret );
10068 }
10069
10070 p += cert_len;
10071 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010072#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010073 /* Deserialize CRT digest from the end of the ticket. */
10074 if( 2 > (size_t)( end - p ) )
10075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10076
10077 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10078 session->peer_cert_digest_len = (size_t) *p++;
10079
10080 if( session->peer_cert_digest_len != 0 )
10081 {
Hanno Becker2326d202019-06-06 14:54:55 +010010082 const mbedtls_md_info_t *md_info =
10083 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10084 if( md_info == NULL )
10085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10086 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10088
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010089 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10091
10092 session->peer_cert_digest =
10093 mbedtls_calloc( 1, session->peer_cert_digest_len );
10094 if( session->peer_cert_digest == NULL )
10095 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10096
10097 memcpy( session->peer_cert_digest, p,
10098 session->peer_cert_digest_len );
10099 p += session->peer_cert_digest_len;
10100 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010101#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010102#endif /* MBEDTLS_X509_CRT_PARSE_C */
10103
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010104 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010105 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010106 */
10107#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10108 if( 3 > (size_t)( end - p ) )
10109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10110
10111 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10112 p += 3;
10113
10114 if( session->ticket_len != 0 )
10115 {
10116 if( session->ticket_len > (size_t)( end - p ) )
10117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10118
10119 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10120 if( session->ticket == NULL )
10121 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10122
10123 memcpy( session->ticket, p, session->ticket_len );
10124 p += session->ticket_len;
10125 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010126
10127 if( 4 > (size_t)( end - p ) )
10128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10129
10130 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10131 ( (uint32_t) p[1] << 16 ) |
10132 ( (uint32_t) p[2] << 8 ) |
10133 ( (uint32_t) p[3] );
10134 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010135#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10136
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137 /*
10138 * Misc extension-related info
10139 */
10140#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10141 if( 1 > (size_t)( end - p ) )
10142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10143
10144 session->mfl_code = *p++;
10145#endif
10146
10147#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10148 if( 1 > (size_t)( end - p ) )
10149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10150
10151 session->trunc_hmac = *p++;
10152#endif
10153
10154#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10155 if( 1 > (size_t)( end - p ) )
10156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10157
10158 session->encrypt_then_mac = *p++;
10159#endif
10160
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010161 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010162 if( p != end )
10163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10164
10165 return( 0 );
10166}
10167
10168/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010169 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010170 */
10171int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10172 const unsigned char *buf,
10173 size_t len )
10174{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010175 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010176
10177 if( ret != 0 )
10178 mbedtls_ssl_session_free( session );
10179
10180 return( ret );
10181}
10182
10183/*
Paul Bakker1961b702013-01-25 14:49:24 +010010184 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010189
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010190 if( ssl == NULL || ssl->conf == NULL )
10191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010194 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010198 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010200#endif
10201
Hanno Beckerb82350b2019-07-26 07:24:05 +010010202 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010203 return( ret );
10204}
10205
10206/*
10207 * Perform the SSL handshake
10208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010210{
10211 int ret = 0;
10212
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010213 if( ssl == NULL || ssl->conf == NULL )
10214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010221
10222 if( ret != 0 )
10223 break;
10224 }
10225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010227
10228 return( ret );
10229}
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231#if defined(MBEDTLS_SSL_RENEGOTIATION)
10232#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010233/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010234 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010237{
10238 int ret;
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010241
10242 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10244 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010245
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010246 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010247 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010249 return( ret );
10250 }
10251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010253
10254 return( 0 );
10255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010257
10258/*
10259 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260 * - any side: calling mbedtls_ssl_renegotiate(),
10261 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10262 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010263 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010264 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010268{
10269 int ret;
10270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010272
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010273 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10274 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010275
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010276 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10277 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010279 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010281 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010282 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10283 MBEDTLS_SSL_IS_SERVER )
10284 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010285 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010286 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010287 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010288 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010289 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010290 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010291 }
10292#endif
10293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10295 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010300 return( ret );
10301 }
10302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010304
10305 return( 0 );
10306}
10307
10308/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010309 * Renegotiate current connection on client,
10310 * or request renegotiation on server
10311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010315
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010316 if( ssl == NULL || ssl->conf == NULL )
10317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010320 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010321 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010327
10328 /* Did we already try/start sending HelloRequest? */
10329 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010331
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010332 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010337 /*
10338 * On client, either start the renegotiation process or,
10339 * if already in progress, continue the handshake
10340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010345
10346 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010349 return( ret );
10350 }
10351 }
10352 else
10353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010357 return( ret );
10358 }
10359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010361
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010362 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010363}
10364
10365/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010366 * Check record counters and renegotiate if they're above the limit.
10367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010369{
Andres AG2196c7f2016-12-15 17:01:16 +000010370 size_t ep_len = ssl_ep_len( ssl );
10371 int in_ctr_cmp;
10372 int out_ctr_cmp;
10373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10375 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010376 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010377 {
10378 return( 0 );
10379 }
10380
Andres AG2196c7f2016-12-15 17:01:16 +000010381 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10382 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010383 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010384 ssl->conf->renego_period + ep_len, 8 - ep_len );
10385
10386 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010387 {
10388 return( 0 );
10389 }
10390
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010395
10396/*
10397 * Receive application data decrypted from the SSL layer
10398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010400{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010401 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010402 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010403
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010404 if( ssl == NULL || ssl->conf == NULL )
10405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010410 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010413 return( ret );
10414
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010415 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010417 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010418 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010419 return( ret );
10420 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010421 }
10422#endif
10423
Hanno Becker4a810fb2017-05-24 16:27:30 +010010424 /*
10425 * Check if renegotiation is necessary and/or handshake is
10426 * in process. If yes, perform/continue, and fall through
10427 * if an unexpected packet is received while the client
10428 * is waiting for the ServerHello.
10429 *
10430 * (There is no equivalent to the last condition on
10431 * the server-side as it is not treated as within
10432 * a handshake while waiting for the ClientHello
10433 * after a renegotiation request.)
10434 */
10435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010437 ret = ssl_check_ctr_renegotiate( ssl );
10438 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10439 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010442 return( ret );
10443 }
10444#endif
10445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010449 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10450 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010453 return( ret );
10454 }
10455 }
10456
Hanno Beckere41158b2017-10-23 13:30:32 +010010457 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010458 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010459 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010460 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010461 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10462 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010463 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010464 ssl_set_timer( ssl,
10465 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010466 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010467
Hanno Becker327c93b2018-08-15 13:56:18 +010010468 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010469 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010470 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10471 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010472
Hanno Becker4a810fb2017-05-24 16:27:30 +010010473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10474 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 }
10476
10477 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010479 {
10480 /*
10481 * OpenSSL sends empty messages to randomize the IV
10482 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010483 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010486 return( 0 );
10487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010489 return( ret );
10490 }
10491 }
10492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010496
Hanno Becker4a810fb2017-05-24 16:27:30 +010010497 /*
10498 * - For client-side, expect SERVER_HELLO_REQUEST.
10499 * - For server-side, expect CLIENT_HELLO.
10500 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10501 */
10502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010504 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10505 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010507 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010510
10511 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010513 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010514 {
10515 continue;
10516 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010517 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010518#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010519#if defined(MBEDTLS_SSL_PROTO_TLS)
10520 {
10521 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10522 }
10523#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010524 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010525#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010526
Hanno Becker4a810fb2017-05-24 16:27:30 +010010527#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010528 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10529 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010533
10534 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010536 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010537 {
10538 continue;
10539 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010540 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010541#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010542#if defined(MBEDTLS_SSL_PROTO_TLS)
10543 {
10544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10545 }
10546#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010547 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010548#endif /* MBEDTLS_SSL_SRV_C */
10549
Hanno Becker21df7f92017-10-17 11:03:26 +010010550#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010551 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010552 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10553 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010554 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010555 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10556 {
10557 /*
10558 * Accept renegotiation request
10559 */
Paul Bakker48916f92012-09-16 19:57:18 +000010560
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010561 /* DTLS clients need to know renego is server-initiated */
10562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010563 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010564 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10565 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010566 {
10567 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10568 }
10569#endif
10570 ret = ssl_start_renegotiation( ssl );
10571 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10572 ret != 0 )
10573 {
10574 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10575 return( ret );
10576 }
10577 }
10578 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010579#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010580 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010581 /*
10582 * Refuse renegotiation
10583 */
10584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010588 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010589 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010590 /* SSLv3 does not have a "no_renegotiation" warning, so
10591 we send a fatal alert and abort the connection. */
10592 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10593 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10594 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010595 }
10596 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10598#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10599 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010600 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010601 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010602 ret = mbedtls_ssl_send_alert_message( ssl,
10603 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10604 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10605 if( ret != 0 )
10606 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010607 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010608 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10610 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010614 }
Paul Bakker48916f92012-09-16 19:57:18 +000010615 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010616
Hanno Becker90333da2017-10-10 11:27:13 +010010617 /* At this point, we don't know whether the renegotiation has been
10618 * completed or not. The cases to consider are the following:
10619 * 1) The renegotiation is complete. In this case, no new record
10620 * has been read yet.
10621 * 2) The renegotiation is incomplete because the client received
10622 * an application data record while awaiting the ServerHello.
10623 * 3) The renegotiation is incomplete because the client received
10624 * a non-handshake, non-application data message while awaiting
10625 * the ServerHello.
10626 * In each of these case, looping will be the proper action:
10627 * - For 1), the next iteration will read a new record and check
10628 * if it's application data.
10629 * - For 2), the loop condition isn't satisfied as application data
10630 * is present, hence continue is the same as break
10631 * - For 3), the loop condition is satisfied and read_record
10632 * will re-deliver the message that was held back by the client
10633 * when expecting the ServerHello.
10634 */
10635 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010636 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010637#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010639 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010640 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010641 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010642 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010645 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010647 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010648 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10653 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010656 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010657 }
10658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10662 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010663 }
10664
10665 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010666
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010667 /* We're going to return something now, cancel timer,
10668 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010670 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010671
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010673 /* If we requested renego but received AppData, resend HelloRequest.
10674 * Do it now, after setting in_offt, to avoid taking this branch
10675 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010677 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10678 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010680 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010681 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010684 return( ret );
10685 }
10686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010688#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010689 }
10690
10691 n = ( len < ssl->in_msglen )
10692 ? len : ssl->in_msglen;
10693
10694 memcpy( buf, ssl->in_offt, n );
10695 ssl->in_msglen -= n;
10696
10697 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010698 {
10699 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010700 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010701 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010702 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010703 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010704 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010705 /* more data available */
10706 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010707 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010710
Paul Bakker23986e52011-04-24 08:57:21 +000010711 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010712}
10713
10714/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010715 * Send application data to be encrypted by the SSL layer, taking care of max
10716 * fragment length and buffer size.
10717 *
10718 * According to RFC 5246 Section 6.2.1:
10719 *
10720 * Zero-length fragments of Application data MAY be sent as they are
10721 * potentially useful as a traffic analysis countermeasure.
10722 *
10723 * Therefore, it is possible that the input message length is 0 and the
10724 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010725 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010726static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010727 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010728{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010729 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10730 const size_t max_len = (size_t) ret;
10731
10732 if( ret < 0 )
10733 {
10734 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10735 return( ret );
10736 }
10737
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010738 if( len > max_len )
10739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010744 "maximum fragment length: %d > %d",
10745 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010747 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010748 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010749#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010750#if defined(MBEDTLS_SSL_PROTO_TLS)
10751 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010752 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010753 }
10754#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010755 }
Paul Bakker887bd502011-06-08 13:10:54 +000010756
Paul Bakker5121ce52009-01-03 21:22:43 +000010757 if( ssl->out_left != 0 )
10758 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010759 /*
10760 * The user has previously tried to send the data and
10761 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10762 * written. In this case, we expect the high-level write function
10763 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010768 return( ret );
10769 }
10770 }
Paul Bakker887bd502011-06-08 13:10:54 +000010771 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010772 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010773 /*
10774 * The user is trying to send a message the first time, so we need to
10775 * copy the data into the internal buffers and setup the data structure
10776 * to keep track of partial writes
10777 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010778 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010780 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010781
Hanno Becker67bc7c32018-08-06 11:33:50 +010010782 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010785 return( ret );
10786 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010787 }
10788
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010789 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010790}
10791
10792/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010793 * Write application data, doing 1/n-1 splitting if necessary.
10794 *
10795 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010796 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010797 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010800static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010801 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010802{
10803 int ret;
10804
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010805 if( ssl->conf->cbc_record_splitting ==
10806 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010807 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010808 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10810 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010811 {
10812 return( ssl_write_real( ssl, buf, len ) );
10813 }
10814
10815 if( ssl->split_done == 0 )
10816 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010817 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010818 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010819 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010820 }
10821
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010822 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10823 return( ret );
10824 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010825
10826 return( ret + 1 );
10827}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010829
10830/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010831 * Write application data (public-facing wrapper)
10832 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010833int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010834{
10835 int ret;
10836
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010838
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010839 if( ssl == NULL || ssl->conf == NULL )
10840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10841
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010842#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010843 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10844 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010845 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010846 return( ret );
10847 }
10848#endif
10849
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010850 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010851 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010852 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010853 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010855 return( ret );
10856 }
10857 }
10858
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010859#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010860 ret = ssl_write_split( ssl, buf, len );
10861#else
10862 ret = ssl_write_real( ssl, buf, len );
10863#endif
10864
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010866
10867 return( ret );
10868}
10869
10870/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010871 * Notify the peer that the connection is being closed
10872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010874{
10875 int ret;
10876
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010877 if( ssl == NULL || ssl->conf == NULL )
10878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010881
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010882 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010883 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10888 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10889 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010892 return( ret );
10893 }
10894 }
10895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010897
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010898 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010899}
10900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010902{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010903 if( transform == NULL )
10904 return;
10905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010907 deflateEnd( &transform->ctx_deflate );
10908 inflateEnd( &transform->ctx_inflate );
10909#endif
10910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10912 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010913
Hanno Becker92231322018-01-03 15:32:51 +000010914#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 mbedtls_md_free( &transform->md_ctx_enc );
10916 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010917#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010918
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010919 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010920}
10921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922#if defined(MBEDTLS_X509_CRT_PARSE_C)
10923static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010926
10927 while( cur != NULL )
10928 {
10929 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010931 cur = next;
10932 }
10933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010935
Hanno Becker0271f962018-08-16 13:23:47 +010010936#if defined(MBEDTLS_SSL_PROTO_DTLS)
10937
10938static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10939{
10940 unsigned offset;
10941 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10942
10943 if( hs == NULL )
10944 return;
10945
Hanno Becker283f5ef2018-08-24 09:34:47 +010010946 ssl_free_buffered_record( ssl );
10947
Hanno Becker0271f962018-08-16 13:23:47 +010010948 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010949 ssl_buffering_free_slot( ssl, offset );
10950}
10951
10952static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10953 uint8_t slot )
10954{
10955 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10956 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010957
10958 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10959 return;
10960
Hanno Beckere605b192018-08-21 15:59:07 +010010961 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010962 {
Hanno Beckere605b192018-08-21 15:59:07 +010010963 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010964 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010965 mbedtls_free( hs_buf->data );
10966 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010967 }
10968}
10969
10970#endif /* MBEDTLS_SSL_PROTO_DTLS */
10971
Gilles Peskine9b562d52018-04-25 20:32:43 +020010972void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010973{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010974 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10975
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010976 if( handshake == NULL )
10977 return;
10978
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010979#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10980 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10981 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010982 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010983 handshake->async_in_progress = 0;
10984 }
10985#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10986
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010987#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10988 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10989 mbedtls_md5_free( &handshake->fin_md5 );
10990 mbedtls_sha1_free( &handshake->fin_sha1 );
10991#endif
10992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10993#if defined(MBEDTLS_SHA256_C)
10994 mbedtls_sha256_free( &handshake->fin_sha256 );
10995#endif
10996#if defined(MBEDTLS_SHA512_C)
10997 mbedtls_sha512_free( &handshake->fin_sha512 );
10998#endif
10999#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001#if defined(MBEDTLS_DHM_C)
11002 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004#if defined(MBEDTLS_ECDH_C)
11005 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011006#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011007#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011008 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011009#if defined(MBEDTLS_SSL_CLI_C)
11010 mbedtls_free( handshake->ecjpake_cache );
11011 handshake->ecjpake_cache = NULL;
11012 handshake->ecjpake_cache_len = 0;
11013#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011014#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011015
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011016#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11017 if( handshake->psk != NULL )
11018 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011019 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011020 mbedtls_free( handshake->psk );
11021 }
11022#endif
11023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11025 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011026 /*
11027 * Free only the linked list wrapper, not the keys themselves
11028 * since the belong to the SNI callback
11029 */
11030 if( handshake->sni_key_cert != NULL )
11031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011033
11034 while( cur != NULL )
11035 {
11036 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011038 cur = next;
11039 }
11040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011042
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011043#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011044 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011045 if( handshake->ecrs_peer_cert != NULL )
11046 {
11047 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11048 mbedtls_free( handshake->ecrs_peer_cert );
11049 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011050#endif
11051
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011052#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11053 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11054 mbedtls_pk_free( &handshake->peer_pubkey );
11055#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#if defined(MBEDTLS_SSL_PROTO_DTLS)
11058 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011059 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011060 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011061#endif
11062
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011063 mbedtls_platform_zeroize( handshake,
11064 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011065}
11066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011067void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011068{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011069 if( session == NULL )
11070 return;
11071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011073 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011074#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011075
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011076#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011078#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011079
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011080 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011081}
11082
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011083#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011084
11085#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11086#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11087#else
11088#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11089#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11090
11091#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11092#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11093#else
11094#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11095#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11096
11097#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11098#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11099#else
11100#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11101#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11102
11103#if defined(MBEDTLS_SSL_ALPN)
11104#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11105#else
11106#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11107#endif /* MBEDTLS_SSL_ALPN */
11108
11109#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11110#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11111#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11112#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11113
11114#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11115 ( (uint32_t) ( \
11116 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11117 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11118 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11119 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11120 0u ) )
11121
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011122static unsigned char ssl_serialized_context_header[] = {
11123 MBEDTLS_VERSION_MAJOR,
11124 MBEDTLS_VERSION_MINOR,
11125 MBEDTLS_VERSION_PATCH,
11126 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11127 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011128 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11129 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11130 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011131};
11132
Paul Bakker5121ce52009-01-03 21:22:43 +000011133/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011134 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011135 *
11136 * The format of the serialized data is:
11137 * (in the presentation language of TLS, RFC 8446 section 3)
11138 *
11139 * // header
11140 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011141 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011142 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011143 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011144 * Note: When updating the format, remember to keep these
11145 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011146 *
11147 * // session sub-structure
11148 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11149 * // transform sub-structure
11150 * uint8 random[64]; // ServerHello.random+ClientHello.random
11151 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11152 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11153 * // fields from ssl_context
11154 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11155 * uint64 in_window_top; // DTLS: last validated record seq_num
11156 * uint64 in_window; // DTLS: bitmask for replay protection
11157 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11158 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11159 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11160 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11161 *
11162 * Note that many fields of the ssl_context or sub-structures are not
11163 * serialized, as they fall in one of the following categories:
11164 *
11165 * 1. forced value (eg in_left must be 0)
11166 * 2. pointer to dynamically-allocated memory (eg session, transform)
11167 * 3. value can be re-derived from other data (eg session keys from MS)
11168 * 4. value was temporary (eg content of input buffer)
11169 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011170 */
11171int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11172 unsigned char *buf,
11173 size_t buf_len,
11174 size_t *olen )
11175{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011176 unsigned char *p = buf;
11177 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011178 size_t session_len;
11179 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011180
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011181 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011182 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11183 * this function's documentation.
11184 *
11185 * These are due to assumptions/limitations in the implementation. Some of
11186 * them are likely to stay (no handshake in progress) some might go away
11187 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011188 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011189 /* The initial handshake must be over */
11190 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011192 if( ssl->handshake != NULL )
11193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11194 /* Double-check that sub-structures are indeed ready */
11195 if( ssl->transform == NULL || ssl->session == NULL )
11196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11197 /* There must be no pending incoming or outgoing data */
11198 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11200 if( ssl->out_left != 0 )
11201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11202 /* Protocol must be DLTS, not TLS */
11203 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11205 /* Version must be 1.2 */
11206 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11208 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11210 /* We must be using an AEAD ciphersuite */
11211 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11213 /* Renegotiation must not be enabled */
11214 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011216
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011217 /*
11218 * Version and format identifier
11219 */
11220 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011221
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011222 if( used <= buf_len )
11223 {
11224 memcpy( p, ssl_serialized_context_header,
11225 sizeof( ssl_serialized_context_header ) );
11226 p += sizeof( ssl_serialized_context_header );
11227 }
11228
11229 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011230 * Session (length + data)
11231 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011232 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011233 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11234 return( ret );
11235
11236 used += 4 + session_len;
11237 if( used <= buf_len )
11238 {
11239 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11240 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11241 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11242 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11243
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011244 ret = ssl_session_save( ssl->session, 1,
11245 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011246 if( ret != 0 )
11247 return( ret );
11248
11249 p += session_len;
11250 }
11251
11252 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011253 * Transform
11254 */
11255 used += sizeof( ssl->transform->randbytes );
11256 if( used <= buf_len )
11257 {
11258 memcpy( p, ssl->transform->randbytes,
11259 sizeof( ssl->transform->randbytes ) );
11260 p += sizeof( ssl->transform->randbytes );
11261 }
11262
11263#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11264 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11265 if( used <= buf_len )
11266 {
11267 *p++ = ssl->transform->in_cid_len;
11268 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11269 p += ssl->transform->in_cid_len;
11270
11271 *p++ = ssl->transform->out_cid_len;
11272 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11273 p += ssl->transform->out_cid_len;
11274 }
11275#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11276
11277 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011278 * Saved fields from top-level ssl_context structure
11279 */
11280#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11281 used += 4;
11282 if( used <= buf_len )
11283 {
11284 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11285 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11286 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11287 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11288 }
11289#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11290
11291#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11292 used += 16;
11293 if( used <= buf_len )
11294 {
11295 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11296 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11297 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11298 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11299 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11300 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11301 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11302 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11303
11304 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11305 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11306 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11307 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11308 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11309 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11310 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11311 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11312 }
11313#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11314
11315#if defined(MBEDTLS_SSL_PROTO_DTLS)
11316 used += 1;
11317 if( used <= buf_len )
11318 {
11319 *p++ = ssl->disable_datagram_packing;
11320 }
11321#endif /* MBEDTLS_SSL_PROTO_DTLS */
11322
11323 used += 8;
11324 if( used <= buf_len )
11325 {
11326 memcpy( p, ssl->cur_out_ctr, 8 );
11327 p += 8;
11328 }
11329
11330#if defined(MBEDTLS_SSL_PROTO_DTLS)
11331 used += 2;
11332 if( used <= buf_len )
11333 {
11334 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11335 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11336 }
11337#endif /* MBEDTLS_SSL_PROTO_DTLS */
11338
11339#if defined(MBEDTLS_SSL_ALPN)
11340 {
11341 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011342 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011343 : 0;
11344
11345 used += 1 + alpn_len;
11346 if( used <= buf_len )
11347 {
11348 *p++ = alpn_len;
11349
11350 if( ssl->alpn_chosen != NULL )
11351 {
11352 memcpy( p, ssl->alpn_chosen, alpn_len );
11353 p += alpn_len;
11354 }
11355 }
11356 }
11357#endif /* MBEDTLS_SSL_ALPN */
11358
11359 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011360 * Done
11361 */
11362 *olen = used;
11363
11364 if( used > buf_len )
11365 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011366
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011367 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11368
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011369 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011370}
11371
11372/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011373 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011374 *
11375 * This internal version is wrapped by a public function that cleans up in
11376 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011377 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011378static int ssl_context_load( mbedtls_ssl_context *ssl,
11379 const unsigned char *buf,
11380 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011381{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011382 const unsigned char *p = buf;
11383 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011384 size_t session_len;
11385 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011386
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011387 /*
11388 * The context should have been freshly setup or reset.
11389 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011390 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011391 * renegotiating, or if the user mistakenly loaded a session first.)
11392 */
11393 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11394 ssl->session != NULL )
11395 {
11396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11397 }
11398
11399 /*
11400 * We can't check that the config matches the initial one, but we can at
11401 * least check it matches the requirements for serializing.
11402 */
11403 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011404 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11405 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11406 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11407 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11408 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11409 MBEDTLS_SSL_MINOR_VERSION_3 ||
11410 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11411 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011412 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011413 {
11414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11415 }
11416
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011417 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11418
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011419 /*
11420 * Check version identifier
11421 */
11422 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11424
11425 if( memcmp( p, ssl_serialized_context_header,
11426 sizeof( ssl_serialized_context_header ) ) != 0 )
11427 {
11428 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11429 }
11430 p += sizeof( ssl_serialized_context_header );
11431
11432 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011433 * Session
11434 */
11435 if( (size_t)( end - p ) < 4 )
11436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11437
11438 session_len = ( (size_t) p[0] << 24 ) |
11439 ( (size_t) p[1] << 16 ) |
11440 ( (size_t) p[2] << 8 ) |
11441 ( (size_t) p[3] );
11442 p += 4;
11443
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011444 /* This has been allocated by ssl_handshake_init(), called by
11445 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11446 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011447 ssl->session_in = ssl->session;
11448 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011449 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011450
11451 if( (size_t)( end - p ) < session_len )
11452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11453
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011454 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011455 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011456 {
11457 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011458 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011459 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011460
11461 p += session_len;
11462
11463 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011464 * Transform
11465 */
11466
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011467 /* This has been allocated by ssl_handshake_init(), called by
11468 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11469 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011470 ssl->transform_in = ssl->transform;
11471 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011472 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011473
11474 /* Read random bytes and populate structure */
11475 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11477
11478 ret = ssl_populate_transform( ssl->transform,
11479 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11480 ssl->session->master,
11481#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11482#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11483 ssl->session->encrypt_then_mac,
11484#endif
11485#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11486 ssl->session->trunc_hmac,
11487#endif
11488#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11489#if defined(MBEDTLS_ZLIB_SUPPORT)
11490 ssl->session->compression,
11491#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011492 p, /* currently pointing to randbytes */
11493 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11494 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11495 ssl );
11496 if( ret != 0 )
11497 return( ret );
11498
11499 p += sizeof( ssl->transform->randbytes );
11500
11501#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11502 /* Read connection IDs and store them */
11503 if( (size_t)( end - p ) < 1 )
11504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11505
11506 ssl->transform->in_cid_len = *p++;
11507
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011508 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11510
11511 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11512 p += ssl->transform->in_cid_len;
11513
11514 ssl->transform->out_cid_len = *p++;
11515
11516 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11518
11519 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11520 p += ssl->transform->out_cid_len;
11521#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11522
11523 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011524 * Saved fields from top-level ssl_context structure
11525 */
11526#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11527 if( (size_t)( end - p ) < 4 )
11528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11529
11530 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11531 ( (uint32_t) p[1] << 16 ) |
11532 ( (uint32_t) p[2] << 8 ) |
11533 ( (uint32_t) p[3] );
11534 p += 4;
11535#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11536
11537#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11538 if( (size_t)( end - p ) < 16 )
11539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11540
11541 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11542 ( (uint64_t) p[1] << 48 ) |
11543 ( (uint64_t) p[2] << 40 ) |
11544 ( (uint64_t) p[3] << 32 ) |
11545 ( (uint64_t) p[4] << 24 ) |
11546 ( (uint64_t) p[5] << 16 ) |
11547 ( (uint64_t) p[6] << 8 ) |
11548 ( (uint64_t) p[7] );
11549 p += 8;
11550
11551 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11552 ( (uint64_t) p[1] << 48 ) |
11553 ( (uint64_t) p[2] << 40 ) |
11554 ( (uint64_t) p[3] << 32 ) |
11555 ( (uint64_t) p[4] << 24 ) |
11556 ( (uint64_t) p[5] << 16 ) |
11557 ( (uint64_t) p[6] << 8 ) |
11558 ( (uint64_t) p[7] );
11559 p += 8;
11560#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11561
11562#if defined(MBEDTLS_SSL_PROTO_DTLS)
11563 if( (size_t)( end - p ) < 1 )
11564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11565
11566 ssl->disable_datagram_packing = *p++;
11567#endif /* MBEDTLS_SSL_PROTO_DTLS */
11568
11569 if( (size_t)( end - p ) < 8 )
11570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11571
11572 memcpy( ssl->cur_out_ctr, p, 8 );
11573 p += 8;
11574
11575#if defined(MBEDTLS_SSL_PROTO_DTLS)
11576 if( (size_t)( end - p ) < 2 )
11577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11578
11579 ssl->mtu = ( p[0] << 8 ) | p[1];
11580 p += 2;
11581#endif /* MBEDTLS_SSL_PROTO_DTLS */
11582
11583#if defined(MBEDTLS_SSL_ALPN)
11584 {
11585 uint8_t alpn_len;
11586 const char **cur;
11587
11588 if( (size_t)( end - p ) < 1 )
11589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11590
11591 alpn_len = *p++;
11592
11593 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11594 {
11595 /* alpn_chosen should point to an item in the configured list */
11596 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11597 {
11598 if( strlen( *cur ) == alpn_len &&
11599 memcmp( p, cur, alpn_len ) == 0 )
11600 {
11601 ssl->alpn_chosen = *cur;
11602 break;
11603 }
11604 }
11605 }
11606
11607 /* can only happen on conf mismatch */
11608 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11610
11611 p += alpn_len;
11612 }
11613#endif /* MBEDTLS_SSL_ALPN */
11614
11615 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011616 * Forced fields from top-level ssl_context structure
11617 *
11618 * Most of them already set to the correct value by mbedtls_ssl_init() and
11619 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11620 */
11621 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11622
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011623#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011624 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011625#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11626#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011627 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011628#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011629
11630#if defined(MBEDTLS_SSL_PROTO_DTLS)
11631 ssl->in_epoch = 1;
11632#endif
11633
11634 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11635 * which we don't want - otherwise we'd end up freeing the wrong transform
11636 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11637 if( ssl->handshake != NULL )
11638 {
11639 mbedtls_ssl_handshake_free( ssl );
11640 mbedtls_free( ssl->handshake );
11641 ssl->handshake = NULL;
11642 }
11643
11644 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011645 * Done - should have consumed entire buffer
11646 */
11647 if( p != end )
11648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011649
11650 return( 0 );
11651}
11652
11653/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011654 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011655 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011656int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011657 const unsigned char *buf,
11658 size_t len )
11659{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011660 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011661
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011662 if( ret != 0 )
11663 mbedtls_ssl_free( context );
11664
11665 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011666}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011667#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011668
11669/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011670 * Free an SSL context
11671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011672void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011673{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011674 if( ssl == NULL )
11675 return;
11676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011678
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011679 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011680 {
Angus Grattond8213d02016-05-25 20:56:48 +100011681 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011682 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011683 }
11684
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011685 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011686 {
Angus Grattond8213d02016-05-25 20:56:48 +100011687 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011688 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011689 }
11690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011691#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011692 if( ssl->compress_buf != NULL )
11693 {
Angus Grattond8213d02016-05-25 20:56:48 +100011694 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011695 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011696 }
11697#endif
11698
Paul Bakker48916f92012-09-16 19:57:18 +000011699 if( ssl->transform )
11700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011701 mbedtls_ssl_transform_free( ssl->transform );
11702 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011703 }
11704
11705 if( ssl->handshake )
11706 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011707 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011708 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11709 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011711 mbedtls_free( ssl->handshake );
11712 mbedtls_free( ssl->transform_negotiate );
11713 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011714 }
11715
Paul Bakkerc0463502013-02-14 11:19:38 +010011716 if( ssl->session )
11717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011718 mbedtls_ssl_session_free( ssl->session );
11719 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011720 }
11721
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011722#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011723 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011724 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011725 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011726 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011727 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011728#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11731 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11734 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011735 }
11736#endif
11737
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011738#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011739 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011740#endif
11741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011743
Paul Bakker86f04f42013-02-14 11:20:09 +010011744 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011745 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011746}
11747
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011748/*
11749 * Initialze mbedtls_ssl_config
11750 */
11751void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11752{
11753 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011754
11755#if !defined(MBEDTLS_SSL_PROTO_TLS)
11756 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11757#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011758}
11759
Simon Butcherc97b6972015-12-27 23:48:17 +000011760#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011761#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011762static int ssl_preset_default_hashes[] = {
11763#if defined(MBEDTLS_SHA512_C)
11764 MBEDTLS_MD_SHA512,
11765 MBEDTLS_MD_SHA384,
11766#endif
11767#if defined(MBEDTLS_SHA256_C)
11768 MBEDTLS_MD_SHA256,
11769 MBEDTLS_MD_SHA224,
11770#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011771#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011772 MBEDTLS_MD_SHA1,
11773#endif
11774 MBEDTLS_MD_NONE
11775};
Simon Butcherc97b6972015-12-27 23:48:17 +000011776#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011777#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011778
Hanno Becker73f4cb12019-06-27 13:51:07 +010011779#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011780static int ssl_preset_suiteb_ciphersuites[] = {
11781 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11782 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11783 0
11784};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011785#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011786
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011787#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011788#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011789static int ssl_preset_suiteb_hashes[] = {
11790 MBEDTLS_MD_SHA256,
11791 MBEDTLS_MD_SHA384,
11792 MBEDTLS_MD_NONE
11793};
11794#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011795#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011796
Hanno Beckerc1096e72019-06-19 12:30:41 +010011797#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011798static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011799#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011800 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011801#endif
11802#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011803 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011804#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011805 MBEDTLS_ECP_DP_NONE
11806};
11807#endif
11808
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011809/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011810 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011811 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011812int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011813 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011814{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011815#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011816 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011817#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011818
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011819 /* Use the functions here so that they are covered in tests,
11820 * but otherwise access member directly for efficiency */
11821 mbedtls_ssl_conf_endpoint( conf, endpoint );
11822 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011823
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011824 /*
11825 * Things that are common to all presets
11826 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011827#if defined(MBEDTLS_SSL_CLI_C)
11828 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11829 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011830#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011831 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011832#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11834 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11835#endif
11836 }
11837#endif
11838
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011839#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011840 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011841#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011842
11843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11844 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11845#endif
11846
11847#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011848#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011849 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011850#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11851#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011852 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011853 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011854#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011855#endif
11856
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011857#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11858 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11859#endif
11860
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011861#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011862 conf->f_cookie_write = ssl_cookie_write_dummy;
11863 conf->f_cookie_check = ssl_cookie_check_dummy;
11864#endif
11865
Hanno Becker7f376f42019-06-12 16:20:48 +010011866#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11867 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011868 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11869#endif
11870
Janos Follath088ce432017-04-10 12:42:31 +010011871#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011872#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011873 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011874#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11875#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011876
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011878#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011879 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011880#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11881#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011882 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011883#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11884#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011885
11886#if defined(MBEDTLS_SSL_RENEGOTIATION)
11887 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011888 memset( conf->renego_period, 0x00, 2 );
11889 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011890#endif
11891
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11893 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11894 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011895 const unsigned char dhm_p[] =
11896 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11897 const unsigned char dhm_g[] =
11898 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11899
Hanno Beckera90658f2017-10-04 15:29:08 +010011900 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11901 dhm_p, sizeof( dhm_p ),
11902 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011903 {
11904 return( ret );
11905 }
11906 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011907#endif
11908
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011909 /*
11910 * Preset-specific defaults
11911 */
11912 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011913 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011914 /*
11915 * NSA Suite B
11916 */
11917 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011918#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011919 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011920#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11921#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011922 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011923#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11924#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011925 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011926#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11927#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011928 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011929#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011930
Hanno Becker73f4cb12019-06-27 13:51:07 +010011931#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011932 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11933 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11934 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11935 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11936 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011937#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011938
11939#if defined(MBEDTLS_X509_CRT_PARSE_C)
11940 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011941#endif
11942
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011943#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011944#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011945 conf->sig_hashes = ssl_preset_suiteb_hashes;
11946#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011947#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011948
11949#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011950#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011951 conf->curve_list = ssl_preset_suiteb_curves;
11952#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011953#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011954 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011955
11956 /*
11957 * Default
11958 */
11959 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011960#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011961 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11962 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11963 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11964 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011965#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11966#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011967 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11968 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11969 MBEDTLS_SSL_MIN_MINOR_VERSION :
11970 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011972 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11974#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011975#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11976#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11977 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11978#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11979#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11980 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11981#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011982
Hanno Becker73f4cb12019-06-27 13:51:07 +010011983#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011984 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11985 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11986 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11987 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11988 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011989#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011990
11991#if defined(MBEDTLS_X509_CRT_PARSE_C)
11992 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11993#endif
11994
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011995#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011996#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011997 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011998#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011999#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012000
12001#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012002#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012003 conf->curve_list = mbedtls_ecp_grp_id_list();
12004#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012005#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012006
12007#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12008 conf->dhm_min_bitlen = 1024;
12009#endif
12010 }
12011
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012012 return( 0 );
12013}
12014
12015/*
12016 * Free mbedtls_ssl_config
12017 */
12018void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12019{
12020#if defined(MBEDTLS_DHM_C)
12021 mbedtls_mpi_free( &conf->dhm_P );
12022 mbedtls_mpi_free( &conf->dhm_G );
12023#endif
12024
12025#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12026 if( conf->psk != NULL )
12027 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012028 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012029 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012030 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012031 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012032 }
12033
12034 if( conf->psk_identity != NULL )
12035 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012036 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012037 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012038 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012039 conf->psk_identity_len = 0;
12040 }
12041#endif
12042
12043#if defined(MBEDTLS_X509_CRT_PARSE_C)
12044 ssl_key_cert_free( conf->key_cert );
12045#endif
12046
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012047 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012048}
12049
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012050#if defined(MBEDTLS_PK_C) && \
12051 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012052/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012053 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012055unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012057#if defined(MBEDTLS_RSA_C)
12058 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12059 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012061#if defined(MBEDTLS_ECDSA_C)
12062 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12063 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012065 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012066}
12067
Hanno Becker7e5437a2017-04-28 17:15:26 +010012068unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12069{
12070 switch( type ) {
12071 case MBEDTLS_PK_RSA:
12072 return( MBEDTLS_SSL_SIG_RSA );
12073 case MBEDTLS_PK_ECDSA:
12074 case MBEDTLS_PK_ECKEY:
12075 return( MBEDTLS_SSL_SIG_ECDSA );
12076 default:
12077 return( MBEDTLS_SSL_SIG_ANON );
12078 }
12079}
12080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012081mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012082{
12083 switch( sig )
12084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012085#if defined(MBEDTLS_RSA_C)
12086 case MBEDTLS_SSL_SIG_RSA:
12087 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012089#if defined(MBEDTLS_ECDSA_C)
12090 case MBEDTLS_SSL_SIG_ECDSA:
12091 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012092#endif
12093 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012094 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012095 }
12096}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012097#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012098
Hanno Becker7e5437a2017-04-28 17:15:26 +010012099#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12100 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12101
12102/* Find an entry in a signature-hash set matching a given hash algorithm. */
12103mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12104 mbedtls_pk_type_t sig_alg )
12105{
12106 switch( sig_alg )
12107 {
12108 case MBEDTLS_PK_RSA:
12109 return( set->rsa );
12110 case MBEDTLS_PK_ECDSA:
12111 return( set->ecdsa );
12112 default:
12113 return( MBEDTLS_MD_NONE );
12114 }
12115}
12116
12117/* Add a signature-hash-pair to a signature-hash set */
12118void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12119 mbedtls_pk_type_t sig_alg,
12120 mbedtls_md_type_t md_alg )
12121{
12122 switch( sig_alg )
12123 {
12124 case MBEDTLS_PK_RSA:
12125 if( set->rsa == MBEDTLS_MD_NONE )
12126 set->rsa = md_alg;
12127 break;
12128
12129 case MBEDTLS_PK_ECDSA:
12130 if( set->ecdsa == MBEDTLS_MD_NONE )
12131 set->ecdsa = md_alg;
12132 break;
12133
12134 default:
12135 break;
12136 }
12137}
12138
12139/* Allow exactly one hash algorithm for each signature. */
12140void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12141 mbedtls_md_type_t md_alg )
12142{
12143 set->rsa = md_alg;
12144 set->ecdsa = md_alg;
12145}
12146
12147#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12148 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12149
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012150/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012151 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012153mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012154{
12155 switch( hash )
12156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157#if defined(MBEDTLS_MD5_C)
12158 case MBEDTLS_SSL_HASH_MD5:
12159 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012161#if defined(MBEDTLS_SHA1_C)
12162 case MBEDTLS_SSL_HASH_SHA1:
12163 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012165#if defined(MBEDTLS_SHA256_C)
12166 case MBEDTLS_SSL_HASH_SHA224:
12167 return( MBEDTLS_MD_SHA224 );
12168 case MBEDTLS_SSL_HASH_SHA256:
12169 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012171#if defined(MBEDTLS_SHA512_C)
12172 case MBEDTLS_SSL_HASH_SHA384:
12173 return( MBEDTLS_MD_SHA384 );
12174 case MBEDTLS_SSL_HASH_SHA512:
12175 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012176#endif
12177 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012178 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012179 }
12180}
12181
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012182/*
12183 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12184 */
12185unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12186{
12187 switch( md )
12188 {
12189#if defined(MBEDTLS_MD5_C)
12190 case MBEDTLS_MD_MD5:
12191 return( MBEDTLS_SSL_HASH_MD5 );
12192#endif
12193#if defined(MBEDTLS_SHA1_C)
12194 case MBEDTLS_MD_SHA1:
12195 return( MBEDTLS_SSL_HASH_SHA1 );
12196#endif
12197#if defined(MBEDTLS_SHA256_C)
12198 case MBEDTLS_MD_SHA224:
12199 return( MBEDTLS_SSL_HASH_SHA224 );
12200 case MBEDTLS_MD_SHA256:
12201 return( MBEDTLS_SSL_HASH_SHA256 );
12202#endif
12203#if defined(MBEDTLS_SHA512_C)
12204 case MBEDTLS_MD_SHA384:
12205 return( MBEDTLS_SSL_HASH_SHA384 );
12206 case MBEDTLS_MD_SHA512:
12207 return( MBEDTLS_SSL_HASH_SHA512 );
12208#endif
12209 default:
12210 return( MBEDTLS_SSL_HASH_NONE );
12211 }
12212}
12213
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012214#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012215/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012216 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012217 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012218 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012219int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012220{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012221 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12222 if( own_ec_id == grp_id )
12223 return( 0 );
12224 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012225
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012226 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012227}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012228#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012229
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012230#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012231/*
12232 * Check if a hash 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_sig_hash( const mbedtls_ssl_context *ssl,
12236 mbedtls_md_type_t md )
12237{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012238 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12239 if( md_alg == md )
12240 return( 0 );
12241 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012242
12243 return( -1 );
12244}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012245#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012247#if defined(MBEDTLS_X509_CRT_PARSE_C)
12248int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012249 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012250 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012251 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012252{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012253 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012254#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012255 int usage = 0;
12256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012257#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012258 const char *ext_oid;
12259 size_t ext_len;
12260#endif
12261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012262#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12263 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012264 ((void) cert);
12265 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012266 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012267#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012269#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12270 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012271 {
12272 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012273 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012275 case MBEDTLS_KEY_EXCHANGE_RSA:
12276 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012277 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012278 break;
12279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012280 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12281 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12282 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12283 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012284 break;
12285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012286 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12287 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012288 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012289 break;
12290
12291 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012292 case MBEDTLS_KEY_EXCHANGE_NONE:
12293 case MBEDTLS_KEY_EXCHANGE_PSK:
12294 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12295 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012296 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012297 usage = 0;
12298 }
12299 }
12300 else
12301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012302 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12303 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012304 }
12305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012306 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012307 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012308 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012309 ret = -1;
12310 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012311#else
12312 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012313#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012315#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12316 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012318 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12319 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012320 }
12321 else
12322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012323 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12324 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012325 }
12326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012327 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012328 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012329 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012330 ret = -1;
12331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012332#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012333
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012334 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012336#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012337
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012338#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12339 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12340int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12341 unsigned char *output,
12342 unsigned char *data, size_t data_len )
12343{
12344 int ret = 0;
12345 mbedtls_md5_context mbedtls_md5;
12346 mbedtls_sha1_context mbedtls_sha1;
12347
12348 mbedtls_md5_init( &mbedtls_md5 );
12349 mbedtls_sha1_init( &mbedtls_sha1 );
12350
12351 /*
12352 * digitally-signed struct {
12353 * opaque md5_hash[16];
12354 * opaque sha_hash[20];
12355 * };
12356 *
12357 * md5_hash
12358 * MD5(ClientHello.random + ServerHello.random
12359 * + ServerParams);
12360 * sha_hash
12361 * SHA(ClientHello.random + ServerHello.random
12362 * + ServerParams);
12363 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012364 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012365 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012367 goto exit;
12368 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012369 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012370 ssl->handshake->randbytes, 64 ) ) != 0 )
12371 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012373 goto exit;
12374 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012375 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012376 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012378 goto exit;
12379 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012380 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012381 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012383 goto exit;
12384 }
12385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012386 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012387 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012389 goto exit;
12390 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012391 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392 ssl->handshake->randbytes, 64 ) ) != 0 )
12393 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012394 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012395 goto exit;
12396 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012397 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012398 data_len ) ) != 0 )
12399 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012404 output + 16 ) ) != 0 )
12405 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012407 goto exit;
12408 }
12409
12410exit:
12411 mbedtls_md5_free( &mbedtls_md5 );
12412 mbedtls_sha1_free( &mbedtls_sha1 );
12413
12414 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012415 mbedtls_ssl_pend_fatal_alert( ssl,
12416 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012417
12418 return( ret );
12419
12420}
12421#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12422 MBEDTLS_SSL_PROTO_TLS1_1 */
12423
12424#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12425 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12426int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012427 unsigned char *hash, size_t *hashlen,
12428 unsigned char *data, size_t data_len,
12429 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012430{
12431 int ret = 0;
12432 mbedtls_md_context_t ctx;
12433 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012434 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012435
12436 mbedtls_md_init( &ctx );
12437
12438 /*
12439 * digitally-signed struct {
12440 * opaque client_random[32];
12441 * opaque server_random[32];
12442 * ServerDHParams params;
12443 * };
12444 */
12445 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12446 {
12447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12448 goto exit;
12449 }
12450 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12451 {
12452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12453 goto exit;
12454 }
12455 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12456 {
12457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12458 goto exit;
12459 }
12460 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12461 {
12462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12463 goto exit;
12464 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012465 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012466 {
12467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12468 goto exit;
12469 }
12470
12471exit:
12472 mbedtls_md_free( &ctx );
12473
12474 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012475 mbedtls_ssl_pend_fatal_alert( ssl,
12476 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012477
12478 return( ret );
12479}
12480#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12481 MBEDTLS_SSL_PROTO_TLS1_2 */
12482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012483#endif /* MBEDTLS_SSL_TLS_C */