blob: 75d199a64f87dd105f03a8e1c6f3f006ac4916c3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001955 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001957 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1958 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1959 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1960 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1961 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962 {
1963 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001964 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965
1966 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1967 ssl->handshake->ecdh_privkey,
1968 ssl->handshake->premaster,
1969 uecc_curve ) )
1970 {
1971 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1972 }
1973
1974 ssl->handshake->pmslen = NUM_ECC_BYTES;
1975 }
1976 else
1977#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001978#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1980 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1981 {
1982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1983 ssl->handshake->premaster,
1984 MBEDTLS_PREMASTER_SIZE,
1985 &ssl->handshake->pmslen,
1986 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001987 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001988 {
1989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1990 return( ret );
1991 }
1992
1993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1994 }
1995 else
1996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001997#if defined(MBEDTLS_ECDH_C) && \
1998 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2001 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2006 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2007 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2008 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2009 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2010 {
2011 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2012 &ssl->handshake->pmslen,
2013 ssl->handshake->premaster,
2014 MBEDTLS_MPI_MAX_SIZE,
2015 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002016 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002017 {
2018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2019 return( ret );
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2023 }
2024 else
2025#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2026 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2027 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2029#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2030 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2031 {
2032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002033 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2036 return( ret );
2037 }
2038 }
2039 else
2040#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2041#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2042 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2043 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2044 {
2045 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2046 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2047 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002048 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( ret != 0 )
2050 {
2051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2052 return( ret );
2053 }
2054 }
2055 else
2056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2057#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_RSA )
2060 {
2061 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002062 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002063 * ssl_rsa_generate_partial_pms(). Only the
2064 * PMS length needs to be set. */
2065 ssl->handshake->pmslen = 48;
2066 }
2067 else
2068#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2072 }
2073
2074 return( 0 );
2075}
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2078int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 unsigned char *p = ssl->handshake->premaster;
2081 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002082 const unsigned char *psk = ssl->conf->psk;
2083 size_t psk_len = ssl->conf->psk_len;
2084
2085 /* If the psk callback was called, use its result */
2086 if( ssl->handshake->psk != NULL )
2087 {
2088 psk = ssl->handshake->psk;
2089 psk_len = ssl->handshake->psk_len;
2090 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091
2092 /*
2093 * PMS = struct {
2094 * opaque other_secret<0..2^16-1>;
2095 * opaque psk<0..2^16-1>;
2096 * };
2097 * with "other_secret" depending on the particular key exchange
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2100 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002101 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002102 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002105 *(p++) = (unsigned char)( psk_len >> 8 );
2106 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002107
2108 if( end < p || (size_t)( end - p ) < psk_len )
2109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2110
2111 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002112 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 }
2114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2116#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2117 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002118 {
2119 /*
2120 * other_secret already set by the ClientKeyExchange message,
2121 * and is 48 bytes long
2122 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002123 if( end - p < 2 )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002126 *p++ = 0;
2127 *p++ = 48;
2128 p += 48;
2129 }
2130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2132#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2133 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002134 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002135 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002136 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002137
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002138 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002140 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002141 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002142 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145 return( ret );
2146 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002147 *(p++) = (unsigned char)( len >> 8 );
2148 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002149 p += len;
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002152 }
2153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2155#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2156 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002158 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 size_t zlen;
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002162 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002163 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002164 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002167 return( ret );
2168 }
2169
2170 *(p++) = (unsigned char)( zlen >> 8 );
2171 *(p++) = (unsigned char)( zlen );
2172 p += zlen;
2173
Janos Follath3fbdada2018-08-15 10:26:53 +01002174 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2175 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002176 }
2177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002182 }
2183
2184 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002185 if( end - p < 2 )
2186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002187
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188 *(p++) = (unsigned char)( psk_len >> 8 );
2189 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002190
2191 if( end < p || (size_t)( end - p ) < psk_len )
2192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2193
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002194 memcpy( p, psk, psk_len );
2195 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002196
2197 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2198
2199 return( 0 );
2200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002204/*
2205 * SSLv3.0 MAC functions
2206 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002207#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002208static void ssl_mac( mbedtls_md_context_t *md_ctx,
2209 const unsigned char *secret,
2210 const unsigned char *buf, size_t len,
2211 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002212 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002213{
2214 unsigned char header[11];
2215 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002216 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2218 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002219
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002220 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002222 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002223 else
Paul Bakker68884e32013-01-07 18:20:04 +01002224 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002225
2226 memcpy( header, ctr, 8 );
2227 header[ 8] = (unsigned char) type;
2228 header[ 9] = (unsigned char)( len >> 8 );
2229 header[10] = (unsigned char)( len );
2230
Paul Bakker68884e32013-01-07 18:20:04 +01002231 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_md_starts( md_ctx );
2233 mbedtls_md_update( md_ctx, secret, md_size );
2234 mbedtls_md_update( md_ctx, padding, padlen );
2235 mbedtls_md_update( md_ctx, header, 11 );
2236 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002237 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Paul Bakker68884e32013-01-07 18:20:04 +01002239 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 mbedtls_md_starts( md_ctx );
2241 mbedtls_md_update( md_ctx, secret, md_size );
2242 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002243 mbedtls_md_update( md_ctx, out, md_size );
2244 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002247
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002248/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002249 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002250#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002251 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2252 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2253 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2254/* This function makes sure every byte in the memory region is accessed
2255 * (in ascending addresses order) */
2256static void ssl_read_memory( unsigned char *p, size_t len )
2257{
2258 unsigned char acc = 0;
2259 volatile unsigned char force;
2260
2261 for( ; len != 0; p++, len-- )
2262 acc ^= *p;
2263
2264 force = acc;
2265 (void) force;
2266}
2267#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2268
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002269/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002271 */
Hanno Becker3307b532017-12-27 21:37:21 +00002272
Hanno Beckera5a2b082019-05-15 14:03:01 +01002273#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002274/* This functions transforms a DTLS plaintext fragment and a record content
2275 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002276 *
2277 * struct {
2278 * opaque content[DTLSPlaintext.length];
2279 * ContentType real_type;
2280 * uint8 zeros[length_of_padding];
2281 * } DTLSInnerPlaintext;
2282 *
2283 * Input:
2284 * - `content`: The beginning of the buffer holding the
2285 * plaintext to be wrapped.
2286 * - `*content_size`: The length of the plaintext in Bytes.
2287 * - `max_len`: The number of Bytes available starting from
2288 * `content`. This must be `>= *content_size`.
2289 * - `rec_type`: The desired record content type.
2290 *
2291 * Output:
2292 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2293 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2294 *
2295 * Returns:
2296 * - `0` on success.
2297 * - A negative error code if `max_len` didn't offer enough space
2298 * for the expansion.
2299 */
2300static int ssl_cid_build_inner_plaintext( unsigned char *content,
2301 size_t *content_size,
2302 size_t remaining,
2303 uint8_t rec_type )
2304{
2305 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002306 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2307 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2308 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002309
2310 /* Write real content type */
2311 if( remaining == 0 )
2312 return( -1 );
2313 content[ len ] = rec_type;
2314 len++;
2315 remaining--;
2316
2317 if( remaining < pad )
2318 return( -1 );
2319 memset( content + len, 0, pad );
2320 len += pad;
2321 remaining -= pad;
2322
2323 *content_size = len;
2324 return( 0 );
2325}
2326
Hanno Becker7dc25772019-05-20 15:08:01 +01002327/* This function parses a DTLSInnerPlaintext structure.
2328 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002329static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2330 size_t *content_size,
2331 uint8_t *rec_type )
2332{
2333 size_t remaining = *content_size;
2334
2335 /* Determine length of padding by skipping zeroes from the back. */
2336 do
2337 {
2338 if( remaining == 0 )
2339 return( -1 );
2340 remaining--;
2341 } while( content[ remaining ] == 0 );
2342
2343 *content_size = remaining;
2344 *rec_type = content[ remaining ];
2345
2346 return( 0 );
2347}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002348#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002349
Hanno Becker99abf512019-05-20 14:50:53 +01002350/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002351 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002352static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002353 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002354 mbedtls_record *rec )
2355{
Hanno Becker99abf512019-05-20 14:50:53 +01002356 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002357 *
2358 * additional_data = seq_num + TLSCompressed.type +
2359 * TLSCompressed.version + TLSCompressed.length;
2360 *
Hanno Becker99abf512019-05-20 14:50:53 +01002361 * For the CID extension, this is extended as follows
2362 * (quoting draft-ietf-tls-dtls-connection-id-05,
2363 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002364 *
2365 * additional_data = seq_num + DTLSPlaintext.type +
2366 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002367 * cid +
2368 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002369 * length_of_DTLSInnerPlaintext;
2370 */
2371
Hanno Becker3307b532017-12-27 21:37:21 +00002372 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2373 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002374 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002375
Hanno Beckera5a2b082019-05-15 14:03:01 +01002376#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002377 if( rec->cid_len != 0 )
2378 {
2379 memcpy( add_data + 11, rec->cid, rec->cid_len );
2380 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2381 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2382 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2383 *add_data_len = 13 + 1 + rec->cid_len;
2384 }
2385 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002386#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002387 {
2388 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2389 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2390 *add_data_len = 13;
2391 }
Hanno Becker3307b532017-12-27 21:37:21 +00002392}
2393
Hanno Becker611a83b2018-01-03 14:27:32 +00002394int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2395 mbedtls_ssl_transform *transform,
2396 mbedtls_record *rec,
2397 int (*f_rng)(void *, unsigned char *, size_t),
2398 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002401 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002402 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002403 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002404 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002405 size_t post_avail;
2406
2407 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002408#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002409 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002410 ((void) ssl);
2411#endif
2412
2413 /* The PRNG is used for dynamic IV generation that's used
2414 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2415#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2416 ( defined(MBEDTLS_AES_C) || \
2417 defined(MBEDTLS_ARIA_C) || \
2418 defined(MBEDTLS_CAMELLIA_C) ) && \
2419 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2420 ((void) f_rng);
2421 ((void) p_rng);
2422#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
Hanno Becker3307b532017-12-27 21:37:21 +00002426 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002427 {
Hanno Becker3307b532017-12-27 21:37:21 +00002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2430 }
Hanno Becker505089d2019-05-01 09:45:57 +01002431 if( rec == NULL
2432 || rec->buf == NULL
2433 || rec->buf_len < rec->data_offset
2434 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002435#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002436 || rec->cid_len != 0
2437#endif
2438 )
Hanno Becker3307b532017-12-27 21:37:21 +00002439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002442 }
2443
Hanno Becker3307b532017-12-27 21:37:21 +00002444 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002445 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002447 data, rec->data_len );
2448
2449 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2450
2451 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2452 {
2453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2454 (unsigned) rec->data_len,
2455 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2457 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002458
Hanno Beckera5a2b082019-05-15 14:03:01 +01002459#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002460 /*
2461 * Add CID information
2462 */
2463 rec->cid_len = transform->out_cid_len;
2464 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2465 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002466
2467 if( rec->cid_len != 0 )
2468 {
2469 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002470 * Wrap plaintext into DTLSInnerPlaintext structure.
2471 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002472 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002473 * Note that this changes `rec->data_len`, and hence
2474 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002475 */
2476 if( ssl_cid_build_inner_plaintext( data,
2477 &rec->data_len,
2478 post_avail,
2479 rec->type ) != 0 )
2480 {
2481 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2482 }
2483
2484 rec->type = MBEDTLS_SSL_MSG_CID;
2485 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002487
Hanno Becker92c930f2019-04-29 17:31:37 +01002488 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2489
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002491 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002493#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 if( mode == MBEDTLS_MODE_STREAM ||
2495 ( mode == MBEDTLS_MODE_CBC
2496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002497 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002498#endif
2499 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 {
Hanno Becker3307b532017-12-27 21:37:21 +00002501 if( post_avail < transform->maclen )
2502 {
2503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2504 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2505 }
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002508 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2509 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002510 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002511 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002512 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2513 data, rec->data_len, rec->ctr, rec->type, mac );
2514 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002515 }
2516 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2519 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002520 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2521 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002522 {
Hanno Becker992b6872017-11-09 18:57:39 +00002523 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2524
Hanno Beckere83efe62019-04-29 13:52:53 +01002525 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002526
Hanno Becker3307b532017-12-27 21:37:21 +00002527 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002528 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002529 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2530 data, rec->data_len );
2531 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2532 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2533
2534 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002535 }
2536 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002541 }
2542
Hanno Becker3307b532017-12-27 21:37:21 +00002543 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2544 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002545
Hanno Becker3307b532017-12-27 21:37:21 +00002546 rec->data_len += transform->maclen;
2547 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002548 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002549 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002550#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002552 /*
2553 * Encrypt
2554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2556 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002558 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002559 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002561 "including %d bytes of padding",
2562 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Hanno Becker3307b532017-12-27 21:37:21 +00002564 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2565 transform->iv_enc, transform->ivlen,
2566 data, rec->data_len,
2567 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002570 return( ret );
2571 }
2572
Hanno Becker3307b532017-12-27 21:37:21 +00002573 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
Paul Bakker68884e32013-01-07 18:20:04 +01002579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002581
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002582#if defined(MBEDTLS_GCM_C) || \
2583 defined(MBEDTLS_CCM_C) || \
2584 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002586 mode == MBEDTLS_MODE_CCM ||
2587 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002588 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002589 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002590 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002591 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592
Hanno Becker3307b532017-12-27 21:37:21 +00002593 /* Check that there's space for both the authentication tag
2594 * and the explicit IV before and after the record content. */
2595 if( post_avail < transform->taglen ||
2596 rec->data_offset < explicit_iv_len )
2597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2599 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2600 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002601
Paul Bakker68884e32013-01-07 18:20:04 +01002602 /*
2603 * Generate IV
2604 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2606 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002607 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002608 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002609 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2610 explicit_iv_len );
2611 /* Prefix record content with explicit IV. */
2612 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 }
2614 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2615 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002616 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002617 unsigned char i;
2618
2619 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2620
2621 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002622 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 }
2624 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002625 {
2626 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002629 }
2630
Hanno Beckere83efe62019-04-29 13:52:53 +01002631 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002632
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2634 iv, transform->ivlen );
2635 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002636 data - explicit_iv_len, explicit_iv_len );
2637 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002638 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002640 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002641 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002642
Paul Bakker68884e32013-01-07 18:20:04 +01002643 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002644 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002645 */
Hanno Becker3307b532017-12-27 21:37:21 +00002646
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002647 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002648 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002649 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002650 data, rec->data_len, /* source */
2651 data, &rec->data_len, /* destination */
2652 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002655 return( ret );
2656 }
2657
Hanno Becker3307b532017-12-27 21:37:21 +00002658 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2659 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002660
Hanno Becker3307b532017-12-27 21:37:21 +00002661 rec->data_len += transform->taglen + explicit_iv_len;
2662 rec->data_offset -= explicit_iv_len;
2663 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002664 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2668#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002669 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002672 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002673 size_t padlen, i;
2674 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002675
Hanno Becker3307b532017-12-27 21:37:21 +00002676 /* Currently we're always using minimal padding
2677 * (up to 255 bytes would be allowed). */
2678 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2679 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 padlen = 0;
2681
Hanno Becker3307b532017-12-27 21:37:21 +00002682 /* Check there's enough space in the buffer for the padding. */
2683 if( post_avail < padlen + 1 )
2684 {
2685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2686 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2687 }
2688
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002690 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Becker3307b532017-12-27 21:37:21 +00002692 rec->data_len += padlen + 1;
2693 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002696 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002697 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2698 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002699 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002700 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2701 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002702 {
Hanno Becker3307b532017-12-27 21:37:21 +00002703 if( f_rng == NULL )
2704 {
2705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2707 }
2708
2709 if( rec->data_offset < transform->ivlen )
2710 {
2711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2712 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2713 }
2714
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 /*
2716 * Generate IV
2717 */
Hanno Becker3307b532017-12-27 21:37:21 +00002718 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002719 if( ret != 0 )
2720 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002721
Hanno Becker3307b532017-12-27 21:37:21 +00002722 memcpy( data - transform->ivlen, transform->iv_enc,
2723 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002724
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002729 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002730 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002731 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Hanno Becker3307b532017-12-27 21:37:21 +00002733 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2734 transform->iv_enc,
2735 transform->ivlen,
2736 data, rec->data_len,
2737 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002740 return( ret );
2741 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002742
Hanno Becker3307b532017-12-27 21:37:21 +00002743 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002747 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002750 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2751 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002752 {
2753 /*
2754 * Save IV in SSL3 and TLS1
2755 */
Hanno Becker3307b532017-12-27 21:37:21 +00002756 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2757 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
Hanno Becker3307b532017-12-27 21:37:21 +00002759 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002760#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002761 {
2762 data -= transform->ivlen;
2763 rec->data_offset -= transform->ivlen;
2764 rec->data_len += transform->ivlen;
2765 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002768 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002769 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002770 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2771
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772 /*
2773 * MAC(MAC_write_key, seq_num +
2774 * TLSCipherText.type +
2775 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002776 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 * IV + // except for TLS 1.0
2778 * ENC(content + padding + padding_length));
2779 */
Hanno Becker3307b532017-12-27 21:37:21 +00002780
2781 if( post_avail < transform->maclen)
2782 {
2783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2784 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2785 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002790 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002791 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002792
Hanno Becker3307b532017-12-27 21:37:21 +00002793 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002794 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002795 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2796 data, rec->data_len );
2797 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2798 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002799
Hanno Becker3307b532017-12-27 21:37:21 +00002800 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801
Hanno Becker3307b532017-12-27 21:37:21 +00002802 rec->data_len += transform->maclen;
2803 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002804 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002808 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002810 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2813 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002816 /* Make extra sure authentication was performed, exactly once */
2817 if( auth_done != 1 )
2818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002821 }
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
2825 return( 0 );
2826}
2827
Hanno Becker40478be2019-07-12 08:23:59 +01002828int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002829 mbedtls_ssl_transform *transform,
2830 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002831{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002832 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002834 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002835#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002836 size_t padlen = 0, correct = 1;
2837#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002838 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002839 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002840 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002841
Hanno Becker611a83b2018-01-03 14:27:32 +00002842#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002843 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 ((void) ssl);
2845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002848 if( rec == NULL ||
2849 rec->buf == NULL ||
2850 rec->buf_len < rec->data_offset ||
2851 rec->buf_len - rec->data_offset < rec->data_len )
2852 {
2853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002855 }
2856
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857 data = rec->buf + rec->data_offset;
2858 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002861 /*
2862 * Match record's CID with incoming CID.
2863 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002864 if( rec->cid_len != transform->in_cid_len ||
2865 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2866 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002867 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2872 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002873 {
2874 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002875 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2876 transform->iv_dec,
2877 transform->ivlen,
2878 data, rec->data_len,
2879 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002882 return( ret );
2883 }
2884
Hanno Becker4c6876b2017-12-27 21:28:58 +00002885 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 }
Paul Bakker68884e32013-01-07 18:20:04 +01002891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002893#if defined(MBEDTLS_GCM_C) || \
2894 defined(MBEDTLS_CCM_C) || \
2895 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002897 mode == MBEDTLS_MODE_CCM ||
2898 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002899 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002900 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002901 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002902
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002903 /*
2904 * Prepare IV from explicit and implicit data.
2905 */
2906
2907 /* Check that there's enough space for the explicit IV
2908 * (at the beginning of the record) and the MAC (at the
2909 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002910 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002913 "+ taglen (%d)", rec->data_len,
2914 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002916 }
Paul Bakker68884e32013-01-07 18:20:04 +01002917
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002918#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002919 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2920 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002921 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002922
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002923 /* Fixed */
2924 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2925 /* Explicit */
2926 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002927 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002928 else
2929#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2930#if defined(MBEDTLS_CHACHAPOLY_C)
2931 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002932 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002933 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002934 unsigned char i;
2935
2936 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2937
2938 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002939 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002940 }
2941 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002942#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002943 {
2944 /* Reminder if we ever add an AEAD mode with a different size */
2945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2947 }
2948
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002949 /* Group changes to data, data_len, and add_data, because
2950 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002951 data += explicit_iv_len;
2952 rec->data_offset += explicit_iv_len;
2953 rec->data_len -= explicit_iv_len + transform->taglen;
2954
Hanno Beckere83efe62019-04-29 13:52:53 +01002955 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002956 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002957 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002958
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002959 /* Because of the check above, we know that there are
2960 * explicit_iv_len Bytes preceeding data, and taglen
2961 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002962 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002963 * mbedtls_cipher_auth_decrypt() below. */
2964
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002965 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002967 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002968
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002969 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002970 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002971 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2973 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002974 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002975 data, rec->data_len,
2976 data, &olen,
2977 data + rec->data_len,
2978 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2983 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002984
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002985 return( ret );
2986 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002987 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002988
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002989 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002990 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002994 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2998#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002999 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003002 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003003
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 /*
Paul Bakker45829992013-01-03 14:52:21 +01003005 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003008 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3009 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003010 {
3011 /* The ciphertext is prefixed with the CBC IV. */
3012 minlen += transform->ivlen;
3013 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif
Paul Bakker45829992013-01-03 14:52:21 +01003015
Hanno Becker4c6876b2017-12-27 21:28:58 +00003016 /* Size considerations:
3017 *
3018 * - The CBC cipher text must not be empty and hence
3019 * at least of size transform->ivlen.
3020 *
3021 * Together with the potential IV-prefix, this explains
3022 * the first of the two checks below.
3023 *
3024 * - The record must contain a MAC, either in plain or
3025 * encrypted, depending on whether Encrypt-then-MAC
3026 * is used or not.
3027 * - If it is, the message contains the IV-prefix,
3028 * the CBC ciphertext, and the MAC.
3029 * - If it is not, the padded plaintext, and hence
3030 * the CBC ciphertext, has at least length maclen + 1
3031 * because there is at least the padding length byte.
3032 *
3033 * As the CBC ciphertext is not empty, both cases give the
3034 * lower bound minlen + maclen + 1 on the record size, which
3035 * we test for in the second check below.
3036 */
3037 if( rec->data_len < minlen + transform->ivlen ||
3038 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003041 "+ 1 ) ( + expl IV )", rec->data_len,
3042 transform->ivlen,
3043 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003045 }
3046
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003047 /*
3048 * Authenticate before decrypt if enabled
3049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003051 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003052 {
Hanno Becker992b6872017-11-09 18:57:39 +00003053 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003056
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003057 /* Update data_len in tandem with add_data.
3058 *
3059 * The subtraction is safe because of the previous check
3060 * data_len >= minlen + maclen + 1.
3061 *
3062 * Afterwards, we know that data + data_len is followed by at
3063 * least maclen Bytes, which justifies the call to
3064 * mbedtls_ssl_safer_memcmp() below.
3065 *
3066 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003067 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003068 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003069
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003070 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003071 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3072 add_data_len );
3073 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3074 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3076 data, rec->data_len );
3077 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3078 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003079
Hanno Becker4c6876b2017-12-27 21:28:58 +00003080 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3081 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003082 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003083 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003084
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003085 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003086 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3087 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003091 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003092 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003095
3096 /*
3097 * Check length sanity
3098 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003099
3100 /* We know from above that data_len > minlen >= 0,
3101 * so the following check in particular implies that
3102 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003103 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003106 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003108 }
3109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003111 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003113 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003114 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3115 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003116 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003117 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003118 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003119
Hanno Becker4c6876b2017-12-27 21:28:58 +00003120 data += transform->ivlen;
3121 rec->data_offset += transform->ivlen;
3122 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003125
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003126 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3127
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3129 transform->iv_dec, transform->ivlen,
3130 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003133 return( ret );
3134 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003135
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003136 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003137 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003141 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003144 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3145 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003146 {
3147 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003148 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3149 * records is equivalent to CBC decryption of the concatenation
3150 * of the records; in other words, IVs are maintained across
3151 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003152 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3154 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003157
Hanno Becker4c6876b2017-12-27 21:28:58 +00003158 /* Safe since data_len >= minlen + maclen + 1, so after having
3159 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003160 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3161 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003163
Hanno Becker4c6876b2017-12-27 21:28:58 +00003164 if( auth_done == 1 )
3165 {
3166 correct *= ( rec->data_len >= padlen + 1 );
3167 padlen *= ( rec->data_len >= padlen + 1 );
3168 }
3169 else
Paul Bakker45829992013-01-03 14:52:21 +01003170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003172 if( rec->data_len < transform->maclen + padlen + 1 )
3173 {
3174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3175 rec->data_len,
3176 transform->maclen,
3177 padlen + 1 ) );
3178 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003179#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180
3181 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3182 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Hanno Becker4c6876b2017-12-27 21:28:58 +00003185 padlen++;
3186
3187 /* Regardless of the validity of the padding,
3188 * we have data_len >= padlen here. */
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003191 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3192 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003194 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196#if defined(MBEDTLS_SSL_DEBUG_ALL)
3197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003198 "should be no more than %d",
3199 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003200#endif
Paul Bakker45829992013-01-03 14:52:21 +01003201 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 }
3203 }
3204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3206#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3207 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003208 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3209 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003211 /* The padding check involves a series of up to 256
3212 * consecutive memory reads at the end of the record
3213 * plaintext buffer. In order to hide the length and
3214 * validity of the padding, always perform exactly
3215 * `min(256,plaintext_len)` reads (but take into account
3216 * only the last `padlen` bytes for the padding check). */
3217 size_t pad_count = 0;
3218 size_t real_count = 0;
3219 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003220
Hanno Becker4c6876b2017-12-27 21:28:58 +00003221 /* Index of first padding byte; it has been ensured above
3222 * that the subtraction is safe. */
3223 size_t const padding_idx = rec->data_len - padlen;
3224 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3225 size_t const start_idx = rec->data_len - num_checks;
3226 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003227
Hanno Becker4c6876b2017-12-27 21:28:58 +00003228 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003229 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 real_count |= ( idx >= padding_idx );
3231 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003232 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003233 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003236 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003238#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003239 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3243 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003247 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003248
Hanno Becker4c6876b2017-12-27 21:28:58 +00003249 /* If the padding was found to be invalid, padlen == 0
3250 * and the subtraction is safe. If the padding was found valid,
3251 * padlen hasn't been changed and the previous assertion
3252 * data_len >= padlen still holds. */
3253 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003257 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003263#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
3268 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003269 * Authenticate if not done yet.
3270 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003272#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003273 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003274 {
Hanno Becker992b6872017-11-09 18:57:39 +00003275 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003276
Hanno Becker4c6876b2017-12-27 21:28:58 +00003277 /* If the initial value of padlen was such that
3278 * data_len < maclen + padlen + 1, then padlen
3279 * got reset to 1, and the initial check
3280 * data_len >= minlen + maclen + 1
3281 * guarantees that at this point we still
3282 * have at least data_len >= maclen.
3283 *
3284 * If the initial value of padlen was such that
3285 * data_len >= maclen + padlen + 1, then we have
3286 * subtracted either padlen + 1 (if the padding was correct)
3287 * or 0 (if the padding was incorrect) since then,
3288 * hence data_len >= maclen in any case.
3289 */
3290 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003291 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003294 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3295 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003296 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003297 ssl_mac( &transform->md_ctx_dec,
3298 transform->mac_dec,
3299 data, rec->data_len,
3300 rec->ctr, rec->type,
3301 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003302 }
3303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3305#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3306 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003307 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3308 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003309 {
3310 /*
3311 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003312 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003313 *
3314 * Known timing attacks:
3315 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3316 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003317 * To compensate for different timings for the MAC calculation
3318 * depending on how much padding was removed (which is determined
3319 * by padlen), process extra_run more blocks through the hash
3320 * function.
3321 *
3322 * The formula in the paper is
3323 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3324 * where L1 is the size of the header plus the decrypted message
3325 * plus CBC padding and L2 is the size of the header plus the
3326 * decrypted message. This is for an underlying hash function
3327 * with 64-byte blocks.
3328 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3329 * correctly. We round down instead of up, so -56 is the correct
3330 * value for our calculations instead of -55.
3331 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003332 * Repeat the formula rather than defining a block_size variable.
3333 * This avoids requiring division by a variable at runtime
3334 * (which would be marginally less efficient and would require
3335 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003336 */
3337 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003338 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003339
3340 /*
3341 * The next two sizes are the minimum and maximum values of
3342 * in_msglen over all padlen values.
3343 *
3344 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003345 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003346 *
3347 * Note that max_len + maclen is never more than the buffer
3348 * length, as we previously did in_msglen -= maclen too.
3349 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003350 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003351 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3352
Hanno Becker4c6876b2017-12-27 21:28:58 +00003353 memset( tmp, 0, sizeof( tmp ) );
3354
3355 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003356 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003357#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3358 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003359 case MBEDTLS_MD_MD5:
3360 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003361 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003362 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003363 extra_run =
3364 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3365 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003366 break;
3367#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003368#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003369 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003371 extra_run =
3372 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3373 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003374 break;
3375#endif
3376 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3379 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003380
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003381 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003382
Hanno Beckere83efe62019-04-29 13:52:53 +01003383 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3384 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003385 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3386 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003387 /* Make sure we access everything even when padlen > 0. This
3388 * makes the synchronisation requirements for just-in-time
3389 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003390 ssl_read_memory( data + rec->data_len, padlen );
3391 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003392
3393 /* Call mbedtls_md_process at least once due to cache attacks
3394 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003395 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003396 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003397
Hanno Becker4c6876b2017-12-27 21:28:58 +00003398 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003399
3400 /* Make sure we access all the memory that could contain the MAC,
3401 * before we check it in the next code block. This makes the
3402 * synchronisation requirements for just-in-time Prime+Probe
3403 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003404 ssl_read_memory( data + min_len,
3405 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003406 }
3407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3409 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003413 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003414
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003415#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003416 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3417 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003419
Hanno Becker4c6876b2017-12-27 21:28:58 +00003420 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3421 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_SSL_DEBUG_ALL)
3424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003425#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003426 correct = 0;
3427 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003428 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003429 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003430
3431 /*
3432 * Finally check the correct flag
3433 */
3434 if( correct == 0 )
3435 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003436#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003437
3438 /* Make extra sure authentication was performed, exactly once */
3439 if( auth_done != 1 )
3440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003444
Hanno Beckera5a2b082019-05-15 14:03:01 +01003445#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003446 if( rec->cid_len != 0 )
3447 {
3448 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3449 &rec->type );
3450 if( ret != 0 )
3451 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3452 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003453#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
3457 return( 0 );
3458}
3459
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003460#undef MAC_NONE
3461#undef MAC_PLAINTEXT
3462#undef MAC_CIPHERTEXT
3463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465/*
3466 * Compression/decompression functions
3467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469{
3470 int ret;
3471 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003472 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003474 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003478 if( len_pre == 0 )
3479 return( 0 );
3480
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481 memcpy( msg_pre, ssl->out_msg, len_pre );
3482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003484 ssl->out_msglen ) );
3485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003487 ssl->out_msg, ssl->out_msglen );
3488
Paul Bakker48916f92012-09-16 19:57:18 +00003489 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3490 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3491 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003492 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493
Paul Bakker48916f92012-09-16 19:57:18 +00003494 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 if( ret != Z_OK )
3496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3498 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499 }
3500
Angus Grattond8213d02016-05-25 20:56:48 +10003501 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003502 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003505 ssl->out_msglen ) );
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 ssl->out_msg, ssl->out_msglen );
3509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511
3512 return( 0 );
3513}
3514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516{
3517 int ret;
3518 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003519 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003521 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003525 if( len_pre == 0 )
3526 return( 0 );
3527
Paul Bakker2770fbd2012-07-03 13:30:23 +00003528 memcpy( msg_pre, ssl->in_msg, len_pre );
3529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531 ssl->in_msglen ) );
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003534 ssl->in_msg, ssl->in_msglen );
3535
Paul Bakker48916f92012-09-16 19:57:18 +00003536 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3537 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3538 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003539 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003540 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003541
Paul Bakker48916f92012-09-16 19:57:18 +00003542 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 if( ret != Z_OK )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3546 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 }
3548
Angus Grattond8213d02016-05-25 20:56:48 +10003549 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003550 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003553 ssl->in_msglen ) );
3554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556 ssl->in_msg, ssl->in_msglen );
3557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
3560 return( 0 );
3561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3565static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567#if defined(MBEDTLS_SSL_PROTO_DTLS)
3568static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003569{
3570 /* If renegotiation is not enforced, retransmit until we would reach max
3571 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003572 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003573 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003574 uint32_t ratio =
3575 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3576 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003577 unsigned char doublings = 1;
3578
3579 while( ratio != 0 )
3580 {
3581 ++doublings;
3582 ratio >>= 1;
3583 }
3584
3585 if( ++ssl->renego_records_seen > doublings )
3586 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003588 return( 0 );
3589 }
3590 }
3591
3592 return( ssl_write_hello_request( ssl ) );
3593}
3594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003596
Paul Bakker5121ce52009-01-03 21:22:43 +00003597/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003598 * Fill the input message buffer by appending data to it.
3599 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003600 *
3601 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3602 * available (from this read and/or a previous one). Otherwise, an error code
3603 * is returned (possibly EOF or WANT_READ).
3604 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003605 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3606 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3607 * since we always read a whole datagram at once.
3608 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003609 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003610 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613{
Paul Bakker23986e52011-04-24 08:57:21 +00003614 int ret;
3615 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Hanno Beckera58a8962019-06-13 16:11:15 +01003619 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3620 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003623 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003625 }
3626
Angus Grattond8213d02016-05-25 20:56:48 +10003627 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003631 }
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003634 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003636 uint32_t timeout;
3637
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003638 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003639 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3640 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003641 {
3642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3643 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3645 }
3646
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003647 /*
3648 * The point is, we need to always read a full datagram at once, so we
3649 * sometimes read more then requested, and handle the additional data.
3650 * It could be the rest of the current record (while fetching the
3651 * header) and/or some other records in the same datagram.
3652 */
3653
3654 /*
3655 * Move to the next record in the already read datagram if applicable
3656 */
3657 if( ssl->next_record_offset != 0 )
3658 {
3659 if( ssl->in_left < ssl->next_record_offset )
3660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003663 }
3664
3665 ssl->in_left -= ssl->next_record_offset;
3666
3667 if( ssl->in_left != 0 )
3668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003670 ssl->next_record_offset ) );
3671 memmove( ssl->in_hdr,
3672 ssl->in_hdr + ssl->next_record_offset,
3673 ssl->in_left );
3674 }
3675
3676 ssl->next_record_offset = 0;
3677 }
3678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003681
3682 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003683 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003684 */
3685 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003688 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003689 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003690
3691 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003692 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003693 * are not at the beginning of a new record, the caller did something
3694 * wrong.
3695 */
3696 if( ssl->in_left != 0 )
3697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003700 }
3701
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003702 /*
3703 * Don't even try to read if time's out already.
3704 * This avoids by-passing the timer when repeatedly receiving messages
3705 * that will end up being dropped.
3706 */
3707 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003710 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003711 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003712 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003713 {
Angus Grattond8213d02016-05-25 20:56:48 +10003714 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003717 timeout = ssl->handshake->retransmit_timeout;
3718 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003719 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003722
Hanno Beckera58a8962019-06-13 16:11:15 +01003723 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3724 {
3725 ret = mbedtls_ssl_get_recv_timeout( ssl )
3726 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3727 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003728 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003729 {
3730 ret = mbedtls_ssl_get_recv( ssl )
3731 ( ssl->p_bio, ssl->in_hdr, len );
3732 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003735
3736 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003738 }
3739
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003740 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003743 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003746 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003747 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003750 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003751 }
3752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003756 return( ret );
3757 }
3758
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003759 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003760 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003762 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3763 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003765 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003766 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003769 return( ret );
3770 }
3771
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003772 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003775 }
3776
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 if( ret < 0 )
3778 return( ret );
3779
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003780 ssl->in_left = ret;
3781 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003782 MBEDTLS_SSL_TRANSPORT_ELSE
3783#endif /* MBEDTLS_SSL_PROTO_DTLS */
3784#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003787 ssl->in_left, nb_want ) );
3788
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003789 while( ssl->in_left < nb_want )
3790 {
3791 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003792
3793 if( ssl_check_timer( ssl ) != 0 )
3794 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3795 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003796 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003797 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003798 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003799 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3800 ssl->in_hdr + ssl->in_left, len,
3801 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003802 }
3803 else
3804 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003805 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003806 ssl->in_hdr + ssl->in_left, len );
3807 }
3808 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003811 ssl->in_left, nb_want ) );
3812 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003813
3814 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003816
3817 if( ret < 0 )
3818 return( ret );
3819
mohammad160352aecb92018-03-28 23:41:40 -07003820 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003821 {
Darryl Green11999bb2018-03-13 15:22:58 +00003822 MBEDTLS_SSL_DEBUG_MSG( 1,
3823 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003824 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3826 }
3827
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003828 ssl->in_left += ret;
3829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003831#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
3835 return( 0 );
3836}
3837
3838/*
3839 * Flush any data not yet written
3840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003842{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003843 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003844 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003847
Hanno Beckera58a8962019-06-13 16:11:15 +01003848 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003851 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003853 }
3854
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003855 /* Avoid incrementing counter if data is flushed */
3856 if( ssl->out_left == 0 )
3857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003859 return( 0 );
3860 }
3861
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 while( ssl->out_left > 0 )
3863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003865 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003866
Hanno Becker2b1e3542018-08-06 11:19:13 +01003867 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003868 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003871
3872 if( ret <= 0 )
3873 return( ret );
3874
mohammad160352aecb92018-03-28 23:41:40 -07003875 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003876 {
Darryl Green11999bb2018-03-13 15:22:58 +00003877 MBEDTLS_SSL_DEBUG_MSG( 1,
3878 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003879 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3881 }
3882
Paul Bakker5121ce52009-01-03 21:22:43 +00003883 ssl->out_left -= ret;
3884 }
3885
Hanno Becker2b1e3542018-08-06 11:19:13 +01003886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003887 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003888 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003889 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003890 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003891 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003892#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003893#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894 {
3895 ssl->out_hdr = ssl->out_buf + 8;
3896 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003897#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003898 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003901
3902 return( 0 );
3903}
3904
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003905/*
3906 * Functions to handle the DTLS retransmission state machine
3907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909/*
3910 * Append current handshake message to current outgoing flight
3911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3916 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3917 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003918
3919 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003920 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003924 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925 }
3926
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003927 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003931 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003932 }
3933
3934 /* Copy current handshake message with headers */
3935 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3936 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003937 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003938 msg->next = NULL;
3939
3940 /* Append to the current flight */
3941 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003942 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943 else
3944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003946 while( cur->next != NULL )
3947 cur = cur->next;
3948 cur->next = msg;
3949 }
3950
Hanno Becker3b235902018-08-06 09:54:53 +01003951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952 return( 0 );
3953}
3954
3955/*
3956 * Free the current flight of handshake messages
3957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 mbedtls_ssl_flight_item *cur = flight;
3961 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962
3963 while( cur != NULL )
3964 {
3965 next = cur->next;
3966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 mbedtls_free( cur->p );
3968 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969
3970 cur = next;
3971 }
3972}
3973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3975static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003976#endif
3977
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003979 * Swap transform_out and out_ctr with the alternative ones
3980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003984 unsigned char tmp_out_ctr[8];
3985
3986 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003989 return;
3990 }
3991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003993
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003994 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003995 tmp_transform = ssl->transform_out;
3996 ssl->transform_out = ssl->handshake->alt_transform_out;
3997 ssl->handshake->alt_transform_out = tmp_transform;
3998
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003999 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004000 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4001 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004003
4004 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004005 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4008 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4013 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004014 }
4015 }
4016#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004017}
4018
4019/*
4020 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004021 */
4022int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4023{
4024 int ret = 0;
4025
4026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4027
4028 ret = mbedtls_ssl_flight_transmit( ssl );
4029
4030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4031
4032 return( ret );
4033}
4034
4035/*
4036 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 *
4038 * Need to remember the current message in case flush_output returns
4039 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004040 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004042int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004044 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004050
4051 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004052 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004053 ssl_swap_epochs( ssl );
4054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004056 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057
4058 while( ssl->handshake->cur_msg != NULL )
4059 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004060 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004061 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004062
Hanno Beckere1dcb032018-08-17 16:47:58 +01004063 int const is_finished =
4064 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4065 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4066
Hanno Becker04da1892018-08-14 13:22:10 +01004067 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4068 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4069
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004070 /* Swap epochs before sending Finished: we can't do it after
4071 * sending ChangeCipherSpec, in case write returns WANT_READ.
4072 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004073 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004074 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004076 ssl_swap_epochs( ssl );
4077 }
4078
Hanno Becker67bc7c32018-08-06 11:33:50 +01004079 ret = ssl_get_remaining_payload_in_datagram( ssl );
4080 if( ret < 0 )
4081 return( ret );
4082 max_frag_len = (size_t) ret;
4083
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004084 /* CCS is copied as is, while HS messages may need fragmentation */
4085 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4086 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087 if( max_frag_len == 0 )
4088 {
4089 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4090 return( ret );
4091
4092 continue;
4093 }
4094
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004095 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004096 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004097 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004098
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004099 /* Update position inside current message */
4100 ssl->handshake->cur_msg_p += cur->len;
4101 }
4102 else
4103 {
4104 const unsigned char * const p = ssl->handshake->cur_msg_p;
4105 const size_t hs_len = cur->len - 12;
4106 const size_t frag_off = p - ( cur->p + 12 );
4107 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004108 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004109
Hanno Beckere1dcb032018-08-17 16:47:58 +01004110 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004111 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004112 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004113 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004114
Hanno Becker67bc7c32018-08-06 11:33:50 +01004115 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4116 return( ret );
4117
4118 continue;
4119 }
4120 max_hs_frag_len = max_frag_len - 12;
4121
4122 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4123 max_hs_frag_len : rem_len;
4124
4125 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004126 {
4127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004128 (unsigned) cur_hs_frag_len,
4129 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004130 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004131
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004132 /* Messages are stored with handshake headers as if not fragmented,
4133 * copy beginning of headers then fill fragmentation fields.
4134 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4135 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004136
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004137 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4138 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4139 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4140
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4142 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4143 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004144
4145 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4146
Hanno Becker3f7b9732018-08-28 09:53:25 +01004147 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004148 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4149 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004150 ssl->out_msgtype = cur->type;
4151
4152 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004153 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004154 }
4155
4156 /* If done with the current message move to the next one if any */
4157 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4158 {
4159 if( cur->next != NULL )
4160 {
4161 ssl->handshake->cur_msg = cur->next;
4162 ssl->handshake->cur_msg_p = cur->next->p + 12;
4163 }
4164 else
4165 {
4166 ssl->handshake->cur_msg = NULL;
4167 ssl->handshake->cur_msg_p = NULL;
4168 }
4169 }
4170
4171 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004172 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004175 return( ret );
4176 }
4177 }
4178
Hanno Becker67bc7c32018-08-06 11:33:50 +01004179 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4180 return( ret );
4181
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004182 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4184 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004185 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004188 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4189 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004190
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004192
4193 return( 0 );
4194}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004195
4196/*
4197 * To be called when the last message of an incoming flight is received.
4198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004200{
4201 /* We won't need to resend that one any more */
4202 ssl_flight_free( ssl->handshake->flight );
4203 ssl->handshake->flight = NULL;
4204 ssl->handshake->cur_msg = NULL;
4205
4206 /* The next incoming flight will start with this msg_seq */
4207 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4208
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004209 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004210 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004211
Hanno Becker0271f962018-08-16 13:23:47 +01004212 /* Clear future message buffering structure. */
4213 ssl_buffering_free( ssl );
4214
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004215 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004216 ssl_set_timer( ssl, 0 );
4217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4219 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004222 }
4223 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004225}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004226
4227/*
4228 * To be called when the last message of an outgoing flight is send.
4229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004231{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004232 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004233 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4236 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004239 }
4240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004244
Paul Bakker5121ce52009-01-03 21:22:43 +00004245/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004246 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004248
4249/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004250 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004251 *
4252 * - fill in handshake headers
4253 * - update handshake checksum
4254 * - DTLS: save message for resending
4255 * - then pass to the record layer
4256 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004257 * DTLS: except for HelloRequest, messages are only queued, and will only be
4258 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004259 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004260 * Inputs:
4261 * - ssl->out_msglen: 4 + actual handshake message len
4262 * (4 is the size of handshake headers for TLS)
4263 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4264 * - ssl->out_msg + 4: the handshake message body
4265 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004266 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267 * - ssl->out_msglen: the length of the record contents
4268 * (including handshake headers but excluding record headers)
4269 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004270 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004271int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004272{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004273 int ret;
4274 const size_t hs_len = ssl->out_msglen - 4;
4275 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4278
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004279 /*
4280 * Sanity checks
4281 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004282 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004283 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4284 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004285 /* In SSLv3, the client might send a NoCertificate alert. */
4286#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004287 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004288 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004289 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4290 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004291#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4292 {
4293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4295 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004298 /* Whenever we send anything different from a
4299 * HelloRequest we should be in a handshake - double check. */
4300 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4301 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004302 ssl->handshake == NULL )
4303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004309 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004310 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004312 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004315 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004316#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004317
Hanno Beckerb50a2532018-08-06 11:52:54 +01004318 /* Double-check that we did not exceed the bounds
4319 * of the outgoing record buffer.
4320 * This should never fail as the various message
4321 * writing functions must obey the bounds of the
4322 * outgoing record buffer, but better be safe.
4323 *
4324 * Note: We deliberately do not check for the MTU or MFL here.
4325 */
4326 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4327 {
4328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4329 "size %u, maximum %u",
4330 (unsigned) ssl->out_msglen,
4331 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4333 }
4334
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004335 /*
4336 * Fill handshake headers
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004340 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4341 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4342 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004344 /*
4345 * DTLS has additional fields in the Handshake layer,
4346 * between the length field and the actual payload:
4347 * uint16 message_seq;
4348 * uint24 fragment_offset;
4349 * uint24 fragment_length;
4350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004352 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004353 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004354 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004355 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004356 {
4357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4358 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004359 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004360 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4362 }
4363
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004364 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004365 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004366
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004367 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004368 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004369 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004370 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4371 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4372 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004373 }
4374 else
4375 {
4376 ssl->out_msg[4] = 0;
4377 ssl->out_msg[5] = 0;
4378 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004379
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 /* Handshake hashes are computed without fragmentation,
4381 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004382 memset( ssl->out_msg + 6, 0x00, 3 );
4383 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004384 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004386
Hanno Becker0207e532018-08-28 10:28:28 +01004387 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004388 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004389 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
4391
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004392 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004394 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004395 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4396 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004397 {
4398 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004401 return( ret );
4402 }
4403 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004404 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004405#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004406 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004407 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004408 {
4409 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4410 return( ret );
4411 }
4412 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004413
4414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4415
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004416 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004417}
4418
4419/*
4420 * Record layer functions
4421 */
4422
4423/*
4424 * Write current record.
4425 *
4426 * Uses:
4427 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4428 * - ssl->out_msglen: length of the record content (excl headers)
4429 * - ssl->out_msg: record content
4430 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004432{
4433 int ret, done = 0;
4434 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004435 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004440 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004442 {
4443 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004446 return( ret );
4447 }
4448
4449 len = ssl->out_msglen;
4450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4454 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 ret = mbedtls_ssl_hw_record_write( ssl );
4459 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4462 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004463 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004464
4465 if( ret == 0 )
4466 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004469 if( !done )
4470 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004471 unsigned i;
4472 size_t protected_record_size;
4473
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004474 /* Skip writing the record content type to after the encryption,
4475 * as it may change when using the CID extension. */
4476
Hanno Becker2881d802019-05-22 14:44:53 +01004477 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4478 mbedtls_ssl_get_minor_ver( ssl ),
4479 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004480
Hanno Becker19859472018-08-06 09:40:20 +01004481 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004482 ssl->out_len[0] = (unsigned char)( len >> 8 );
4483 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004484
Paul Bakker48916f92012-09-16 19:57:18 +00004485 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004486 {
Hanno Becker3307b532017-12-27 21:37:21 +00004487 mbedtls_record rec;
4488
4489 rec.buf = ssl->out_iv;
4490 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4491 ( ssl->out_iv - ssl->out_buf );
4492 rec.data_len = ssl->out_msglen;
4493 rec.data_offset = ssl->out_msg - rec.buf;
4494
4495 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004496 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4497 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004498 ssl->conf->transport, rec.ver );
4499 rec.type = ssl->out_msgtype;
4500
Hanno Beckera5a2b082019-05-15 14:03:01 +01004501#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004502 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004503 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004505
Hanno Becker611a83b2018-01-03 14:27:32 +00004506 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004507 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004508 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004511 return( ret );
4512 }
4513
Hanno Becker3307b532017-12-27 21:37:21 +00004514 if( rec.data_offset != 0 )
4515 {
4516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4518 }
4519
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004520 /* Update the record content type and CID. */
4521 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004522#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004523 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004524#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004525 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004526 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4527 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004528 }
4529
Hanno Becker43395762019-05-03 14:46:38 +01004530 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004531
4532#if defined(MBEDTLS_SSL_PROTO_DTLS)
4533 /* In case of DTLS, double-check that we don't exceed
4534 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004535 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004536 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004537 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004538 if( ret < 0 )
4539 return( ret );
4540
4541 if( protected_record_size > (size_t) ret )
4542 {
4543 /* Should never happen */
4544 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4545 }
4546 }
4547#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004548
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004549 /* Now write the potentially updated record content type. */
4550 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004553 "version = [%d:%d], msglen = %d",
4554 ssl->out_hdr[0], ssl->out_hdr[1],
4555 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004558 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004559
4560 ssl->out_left += protected_record_size;
4561 ssl->out_hdr += protected_record_size;
4562 ssl_update_out_pointers( ssl, ssl->transform_out );
4563
Hanno Becker04484622018-08-06 09:49:38 +01004564 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4565 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4566 break;
4567
4568 /* The loop goes to its end iff the counter is wrapping */
4569 if( i == ssl_ep_len( ssl ) )
4570 {
4571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4572 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 }
4575
Hanno Becker67bc7c32018-08-06 11:33:50 +01004576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004577 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004578 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004579 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004580 size_t remaining;
4581 ret = ssl_get_remaining_payload_in_datagram( ssl );
4582 if( ret < 0 )
4583 {
4584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4585 ret );
4586 return( ret );
4587 }
4588
4589 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004590 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004591 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004592 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004593 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004594 else
4595 {
Hanno Becker513815a2018-08-20 11:56:09 +01004596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004597 }
4598 }
4599#endif /* MBEDTLS_SSL_PROTO_DTLS */
4600
4601 if( ( flush == SSL_FORCE_FLUSH ) &&
4602 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 return( ret );
4606 }
4607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
4610 return( 0 );
4611}
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004614
4615static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4616{
4617 if( ssl->in_msglen < ssl->in_hslen ||
4618 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4619 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4620 {
4621 return( 1 );
4622 }
4623 return( 0 );
4624}
Hanno Becker44650b72018-08-16 12:51:11 +01004625
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004626static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004627{
4628 return( ( ssl->in_msg[9] << 16 ) |
4629 ( ssl->in_msg[10] << 8 ) |
4630 ssl->in_msg[11] );
4631}
4632
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004633static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004634{
4635 return( ( ssl->in_msg[6] << 16 ) |
4636 ( ssl->in_msg[7] << 8 ) |
4637 ssl->in_msg[8] );
4638}
4639
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004640static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004641{
4642 uint32_t msg_len, frag_off, frag_len;
4643
4644 msg_len = ssl_get_hs_total_len( ssl );
4645 frag_off = ssl_get_hs_frag_off( ssl );
4646 frag_len = ssl_get_hs_frag_len( ssl );
4647
4648 if( frag_off > msg_len )
4649 return( -1 );
4650
4651 if( frag_len > msg_len - frag_off )
4652 return( -1 );
4653
4654 if( frag_len + 12 > ssl->in_msglen )
4655 return( -1 );
4656
4657 return( 0 );
4658}
4659
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004660/*
4661 * Mark bits in bitmask (used for DTLS HS reassembly)
4662 */
4663static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4664{
4665 unsigned int start_bits, end_bits;
4666
4667 start_bits = 8 - ( offset % 8 );
4668 if( start_bits != 8 )
4669 {
4670 size_t first_byte_idx = offset / 8;
4671
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004672 /* Special case */
4673 if( len <= start_bits )
4674 {
4675 for( ; len != 0; len-- )
4676 mask[first_byte_idx] |= 1 << ( start_bits - len );
4677
4678 /* Avoid potential issues with offset or len becoming invalid */
4679 return;
4680 }
4681
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004682 offset += start_bits; /* Now offset % 8 == 0 */
4683 len -= start_bits;
4684
4685 for( ; start_bits != 0; start_bits-- )
4686 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4687 }
4688
4689 end_bits = len % 8;
4690 if( end_bits != 0 )
4691 {
4692 size_t last_byte_idx = ( offset + len ) / 8;
4693
4694 len -= end_bits; /* Now len % 8 == 0 */
4695
4696 for( ; end_bits != 0; end_bits-- )
4697 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4698 }
4699
4700 memset( mask + offset / 8, 0xFF, len / 8 );
4701}
4702
4703/*
4704 * Check that bitmask is full
4705 */
4706static int ssl_bitmask_check( unsigned char *mask, size_t len )
4707{
4708 size_t i;
4709
4710 for( i = 0; i < len / 8; i++ )
4711 if( mask[i] != 0xFF )
4712 return( -1 );
4713
4714 for( i = 0; i < len % 8; i++ )
4715 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4716 return( -1 );
4717
4718 return( 0 );
4719}
4720
Hanno Becker56e205e2018-08-16 09:06:12 +01004721/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004722static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004723 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004724{
Hanno Becker56e205e2018-08-16 09:06:12 +01004725 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004726
Hanno Becker56e205e2018-08-16 09:06:12 +01004727 alloc_len = 12; /* Handshake header */
4728 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004729
Hanno Beckerd07df862018-08-16 09:14:58 +01004730 if( add_bitmap )
4731 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004732
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004733 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004734}
Hanno Becker56e205e2018-08-16 09:06:12 +01004735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004737
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004738static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004739{
4740 return( ( ssl->in_msg[1] << 16 ) |
4741 ( ssl->in_msg[2] << 8 ) |
4742 ssl->in_msg[3] );
4743}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004744
Simon Butcher99000142016-10-13 17:21:01 +01004745int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004750 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004752 }
4753
Hanno Becker12555c62018-08-16 12:47:53 +01004754 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004757 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004758 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004761 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004762 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004763 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004764 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004765
Hanno Becker44650b72018-08-16 12:51:11 +01004766 if( ssl_check_hs_header( ssl ) != 0 )
4767 {
4768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4769 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4770 }
4771
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004772 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004773 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4774 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4775 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4776 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004777 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004778 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4781 recv_msg_seq,
4782 ssl->handshake->in_msg_seq ) );
4783 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4784 }
4785
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004786 /* Retransmit only on last message from previous flight, to avoid
4787 * too many retransmissions.
4788 * Besides, No sane server ever retransmits HelloVerifyRequest */
4789 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004793 "message_seq = %d, start_of_flight = %d",
4794 recv_msg_seq,
4795 ssl->handshake->in_flight_start_seq ) );
4796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004800 return( ret );
4801 }
4802 }
4803 else
4804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004806 "message_seq = %d, expected = %d",
4807 recv_msg_seq,
4808 ssl->handshake->in_msg_seq ) );
4809 }
4810
Hanno Becker90333da2017-10-10 11:27:13 +01004811 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004812 }
4813 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004814
Hanno Becker6d97ef52018-08-16 13:09:04 +01004815 /* Message reassembly is handled alongside buffering of future
4816 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004817 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004818 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004819 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004822 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004823 }
4824 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004825 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004827#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004829 /* With TLS we don't handle fragmentation (for now) */
4830 if( ssl->in_msglen < ssl->in_hslen )
4831 {
4832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4833 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4834 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004835 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004836#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004837
Simon Butcher99000142016-10-13 17:21:01 +01004838 return( 0 );
4839}
4840
4841void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4842{
Hanno Becker0271f962018-08-16 13:23:47 +01004843 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004844
Hanno Becker0271f962018-08-16 13:23:47 +01004845 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004846 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004847
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004848 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004851 ssl->handshake != NULL )
4852 {
Hanno Becker0271f962018-08-16 13:23:47 +01004853 unsigned offset;
4854 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004855
Hanno Becker0271f962018-08-16 13:23:47 +01004856 /* Increment handshake sequence number */
4857 hs->in_msg_seq++;
4858
4859 /*
4860 * Clear up handshake buffering and reassembly structure.
4861 */
4862
4863 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004864 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004865
4866 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004867 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4868 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004869 offset++, hs_buf++ )
4870 {
4871 *hs_buf = *(hs_buf + 1);
4872 }
4873
4874 /* Create a fresh last entry */
4875 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004876 }
4877#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004878}
4879
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004880/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004881 * DTLS anti-replay: RFC 6347 4.1.2.6
4882 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004883 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4884 * Bit n is set iff record number in_window_top - n has been seen.
4885 *
4886 * Usually, in_window_top is the last record number seen and the lsb of
4887 * in_window is set. The only exception is the initial state (record number 0
4888 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4891static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004892{
4893 ssl->in_window_top = 0;
4894 ssl->in_window = 0;
4895}
4896
4897static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4898{
4899 return( ( (uint64_t) buf[0] << 40 ) |
4900 ( (uint64_t) buf[1] << 32 ) |
4901 ( (uint64_t) buf[2] << 24 ) |
4902 ( (uint64_t) buf[3] << 16 ) |
4903 ( (uint64_t) buf[4] << 8 ) |
4904 ( (uint64_t) buf[5] ) );
4905}
4906
4907/*
4908 * Return 0 if sequence number is acceptable, -1 otherwise
4909 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004910int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004911{
4912 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4913 uint64_t bit;
4914
Hanno Becker7f376f42019-06-12 16:20:48 +01004915 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4916 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4917 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004918 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004919 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004920
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004921 if( rec_seqnum > ssl->in_window_top )
4922 return( 0 );
4923
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004924 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004925
4926 if( bit >= 64 )
4927 return( -1 );
4928
4929 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4930 return( -1 );
4931
4932 return( 0 );
4933}
4934
4935/*
4936 * Update replay window on new validated record
4937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004939{
4940 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4941
Hanno Becker7f376f42019-06-12 16:20:48 +01004942 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4943 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4944 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004945 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004946 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004947
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004948 if( rec_seqnum > ssl->in_window_top )
4949 {
4950 /* Update window_top and the contents of the window */
4951 uint64_t shift = rec_seqnum - ssl->in_window_top;
4952
4953 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004954 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004955 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004956 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004957 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004958 ssl->in_window |= 1;
4959 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004960
4961 ssl->in_window_top = rec_seqnum;
4962 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004963 else
4964 {
4965 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004966 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004967
4968 if( bit < 64 ) /* Always true, but be extra sure */
4969 ssl->in_window |= (uint64_t) 1 << bit;
4970 }
4971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004973
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004974#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004976static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4977
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004978/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004979 * Without any SSL context, check if a datagram looks like a ClientHello with
4980 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004981 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004982 *
4983 * - if cookie is valid, return 0
4984 * - if ClientHello looks superficially valid but cookie is not,
4985 * fill obuf and set olen, then
4986 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4987 * - otherwise return a specific error code
4988 */
4989static int ssl_check_dtls_clihlo_cookie(
4990 mbedtls_ssl_cookie_write_t *f_cookie_write,
4991 mbedtls_ssl_cookie_check_t *f_cookie_check,
4992 void *p_cookie,
4993 const unsigned char *cli_id, size_t cli_id_len,
4994 const unsigned char *in, size_t in_len,
4995 unsigned char *obuf, size_t buf_len, size_t *olen )
4996{
4997 size_t sid_len, cookie_len;
4998 unsigned char *p;
4999
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005000 /*
5001 * Structure of ClientHello with record and handshake headers,
5002 * and expected values. We don't need to check a lot, more checks will be
5003 * done when actually parsing the ClientHello - skipping those checks
5004 * avoids code duplication and does not make cookie forging any easier.
5005 *
5006 * 0-0 ContentType type; copied, must be handshake
5007 * 1-2 ProtocolVersion version; copied
5008 * 3-4 uint16 epoch; copied, must be 0
5009 * 5-10 uint48 sequence_number; copied
5010 * 11-12 uint16 length; (ignored)
5011 *
5012 * 13-13 HandshakeType msg_type; (ignored)
5013 * 14-16 uint24 length; (ignored)
5014 * 17-18 uint16 message_seq; copied
5015 * 19-21 uint24 fragment_offset; copied, must be 0
5016 * 22-24 uint24 fragment_length; (ignored)
5017 *
5018 * 25-26 ProtocolVersion client_version; (ignored)
5019 * 27-58 Random random; (ignored)
5020 * 59-xx SessionID session_id; 1 byte len + sid_len content
5021 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5022 * ...
5023 *
5024 * Minimum length is 61 bytes.
5025 */
5026 if( in_len < 61 ||
5027 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5028 in[3] != 0 || in[4] != 0 ||
5029 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5030 {
5031 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5032 }
5033
5034 sid_len = in[59];
5035 if( sid_len > in_len - 61 )
5036 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5037
5038 cookie_len = in[60 + sid_len];
5039 if( cookie_len > in_len - 60 )
5040 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5041
5042 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5043 cli_id, cli_id_len ) == 0 )
5044 {
5045 /* Valid cookie */
5046 return( 0 );
5047 }
5048
5049 /*
5050 * If we get here, we've got an invalid cookie, let's prepare HVR.
5051 *
5052 * 0-0 ContentType type; copied
5053 * 1-2 ProtocolVersion version; copied
5054 * 3-4 uint16 epoch; copied
5055 * 5-10 uint48 sequence_number; copied
5056 * 11-12 uint16 length; olen - 13
5057 *
5058 * 13-13 HandshakeType msg_type; hello_verify_request
5059 * 14-16 uint24 length; olen - 25
5060 * 17-18 uint16 message_seq; copied
5061 * 19-21 uint24 fragment_offset; copied
5062 * 22-24 uint24 fragment_length; olen - 25
5063 *
5064 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5065 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5066 *
5067 * Minimum length is 28.
5068 */
5069 if( buf_len < 28 )
5070 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5071
5072 /* Copy most fields and adapt others */
5073 memcpy( obuf, in, 25 );
5074 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5075 obuf[25] = 0xfe;
5076 obuf[26] = 0xff;
5077
5078 /* Generate and write actual cookie */
5079 p = obuf + 28;
5080 if( f_cookie_write( p_cookie,
5081 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5082 {
5083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5084 }
5085
5086 *olen = p - obuf;
5087
5088 /* Go back and fill length fields */
5089 obuf[27] = (unsigned char)( *olen - 28 );
5090
5091 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5092 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5093 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5094
5095 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5096 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5097
5098 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5099}
5100
5101/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005102 * Handle possible client reconnect with the same UDP quadruplet
5103 * (RFC 6347 Section 4.2.8).
5104 *
5105 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5106 * that looks like a ClientHello.
5107 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005108 * - if the input looks like a ClientHello without cookies,
5109 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005111 * - if the input looks like a ClientHello with a valid cookie,
5112 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005113 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005114 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005115 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005116 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005117 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5118 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005119 */
5120static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5121{
5122 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005123 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005124
Hanno Becker87b56262019-07-10 14:37:41 +01005125 if( ssl->conf->f_cookie_write == NULL ||
5126 ssl->conf->f_cookie_check == NULL )
5127 {
5128 /* If we can't use cookies to verify reachability of the peer,
5129 * drop the record. */
5130 return( 0 );
5131 }
5132
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005133 ret = ssl_check_dtls_clihlo_cookie(
5134 ssl->conf->f_cookie_write,
5135 ssl->conf->f_cookie_check,
5136 ssl->conf->p_cookie,
5137 ssl->cli_id, ssl->cli_id_len,
5138 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005139 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005141 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5142
5143 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005144 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005145 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005146 * If the error is permanent we'll catch it later,
5147 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005148 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005149 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005150 }
5151
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005152 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005153 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005154 /* Got a valid cookie, partially reset context */
5155 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5156 {
5157 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5158 return( ret );
5159 }
5160
5161 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005162 }
5163
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005164 return( ret );
5165}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005166#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005167
Hanno Becker46483f12019-05-03 13:25:54 +01005168static int ssl_check_record_type( uint8_t record_type )
5169{
5170 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5171 record_type != MBEDTLS_SSL_MSG_ALERT &&
5172 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5173 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5174 {
5175 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5176 }
5177
5178 return( 0 );
5179}
5180
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005181/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005182 * ContentType type;
5183 * ProtocolVersion version;
5184 * uint16 epoch; // DTLS only
5185 * uint48 sequence_number; // DTLS only
5186 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005187 *
5188 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005189 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005190 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5191 *
5192 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005193 * 1. proceed with the record if this function returns 0
5194 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5195 * 3. return CLIENT_RECONNECT if this function return that value
5196 * 4. drop the whole datagram if this function returns anything else.
5197 * Point 2 is needed when the peer is resending, and we have already received
5198 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005199 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005200static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005201 unsigned char *buf,
5202 size_t len,
5203 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005204{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005205 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005206
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005207 size_t const rec_hdr_type_offset = 0;
5208 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005209
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005210 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5211 rec_hdr_type_len;
5212 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005213
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005214 size_t const rec_hdr_ctr_len = 8;
5215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005216 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5218 rec_hdr_version_len;
5219
Hanno Beckera5a2b082019-05-15 14:03:01 +01005220#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005221 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5222 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005223 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005224#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5225#endif /* MBEDTLS_SSL_PROTO_DTLS */
5226
5227 size_t rec_hdr_len_offset; /* To be determined */
5228 size_t const rec_hdr_len_len = 2;
5229
5230 /*
5231 * Check minimum lengths for record header.
5232 */
5233
5234#if defined(MBEDTLS_SSL_PROTO_DTLS)
5235 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5236 {
5237 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5238 }
5239 MBEDTLS_SSL_TRANSPORT_ELSE
5240#endif /* MBEDTLS_SSL_PROTO_DTLS */
5241#if defined(MBEDTLS_SSL_PROTO_TLS)
5242 {
5243 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5244 }
5245#endif /* MBEDTLS_SSL_PROTO_DTLS */
5246
5247 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5248 {
5249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5250 (unsigned) len,
5251 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5252 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5253 }
5254
5255 /*
5256 * Parse and validate record content type
5257 */
5258
5259 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005260
5261 /* Check record content type */
5262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5263 rec->cid_len = 0;
5264
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005265 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005266 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5267 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005268 {
5269 /* Shift pointers to account for record header including CID
5270 * struct {
5271 * ContentType special_type = tls12_cid;
5272 * ProtocolVersion version;
5273 * uint16 epoch;
5274 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005275 * opaque cid[cid_length]; // Additional field compared to
5276 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005277 * uint16 length;
5278 * opaque enc_content[DTLSCiphertext.length];
5279 * } DTLSCiphertext;
5280 */
5281
5282 /* So far, we only support static CID lengths
5283 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005284 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5285 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005286
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005287 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005288 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5290 (unsigned) len,
5291 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005292 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005293 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005294
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005295 /* configured CID len is guaranteed at most 255, see
5296 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5297 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005298 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005299 }
5300 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005301#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005302 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 if( ssl_check_record_type( rec->type ) )
5304 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5306 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005307 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5308 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005309 }
5310
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005311 /*
5312 * Parse and validate record version
5313 */
5314
Hanno Becker8061c6e2019-07-26 08:07:03 +01005315 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5316 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005317 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5318 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005319 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005320
Hanno Becker2881d802019-05-22 14:44:53 +01005321 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5324 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005325 }
5326
Hanno Beckere965bd32019-06-12 14:04:34 +01005327 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5330 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 }
5332
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 /*
5334 * Parse/Copy record sequence number.
5335 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005336
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005337#if defined(MBEDTLS_SSL_PROTO_DTLS)
5338 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5339 {
5340 /* Copy explicit record sequence number from input buffer. */
5341 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5342 rec_hdr_ctr_len );
5343 }
5344 MBEDTLS_SSL_TRANSPORT_ELSE
5345#endif /* MBEDTLS_SSL_PROTO_DTLS */
5346#if defined(MBEDTLS_SSL_PROTO_TLS)
5347 {
5348 /* Copy implicit record sequence number from SSL context structure. */
5349 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5350 }
5351#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353 /*
5354 * Parse record length.
5355 */
5356
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005357 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005358 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5359 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005360 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5361
Hanno Becker8b09b732019-05-08 12:03:28 +01005362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005363 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005364 rec->type,
5365 major_ver, minor_ver, rec->data_len ) );
5366
5367 rec->buf = buf;
5368 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005369
Hanno Beckerec014082019-07-26 08:20:27 +01005370 if( rec->data_len == 0 )
5371 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5372
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005373 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005374 * DTLS-related tests.
5375 * Check epoch before checking length constraint because
5376 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5377 * message gets duplicated before the corresponding Finished message,
5378 * the second ChangeCipherSpec should be discarded because it belongs
5379 * to an old epoch, but not because its length is shorter than
5380 * the minimum record length for packets using the new record transform.
5381 * Note that these two kinds of failures are handled differently,
5382 * as an unexpected record is silently skipped but an invalid
5383 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005384 */
5385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005386 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005387 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005388 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005389
Hanno Beckere0452772019-07-10 17:12:07 +01005390 /* Check that the datagram is large enough to contain a record
5391 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005392 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005393 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5395 (unsigned) len,
5396 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005397 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5398 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005399
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005400 /* Records from other, non-matching epochs are silently discarded.
5401 * (The case of same-port Client reconnects must be considered in
5402 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005403 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005404 {
5405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5406 "expected %d, received %d",
5407 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005408
5409 /* Records from the next epoch are considered for buffering
5410 * (concretely: early Finished messages). */
5411 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5412 {
5413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5414 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5415 }
5416
Hanno Becker87b56262019-07-10 14:37:41 +01005417 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005418 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005419#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005420 /* For records from the correct epoch, check whether their
5421 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005422 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5425 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5426 }
5427#endif
5428 }
5429#endif /* MBEDTLS_SSL_PROTO_DTLS */
5430
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005431 return( 0 );
5432}
Paul Bakker5121ce52009-01-03 21:22:43 +00005433
Hanno Becker87b56262019-07-10 14:37:41 +01005434
5435#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5436static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5437{
5438 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5439
5440 /*
5441 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5442 * access the first byte of record content (handshake type), as we
5443 * have an active transform (possibly iv_len != 0), so use the
5444 * fact that the record header len is 13 instead.
5445 */
5446 if( rec_epoch == 0 &&
5447 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5448 MBEDTLS_SSL_IS_SERVER &&
5449 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5450 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5451 ssl->in_left > 13 &&
5452 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5453 {
5454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5455 "from the same port" ) );
5456 return( ssl_handle_possible_reconnect( ssl ) );
5457 }
5458
5459 return( 0 );
5460}
5461#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5462
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005463/*
5464 * If applicable, decrypt (and decompress) record content
5465 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005466static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5467 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005468{
5469 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005472 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5475 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 ret = mbedtls_ssl_hw_record_read( ssl );
5480 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005484 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005485
5486 if( ret == 0 )
5487 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005490 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005492 unsigned char const old_msg_type = rec->type;
5493
Hanno Becker611a83b2018-01-03 14:27:32 +00005494 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005495 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005498
Hanno Beckera5a2b082019-05-15 14:03:01 +01005499#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005500 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005501 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5502 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005503 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005505 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005506 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005507#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005508
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 return( ret );
5510 }
5511
Hanno Becker106f3da2019-07-12 09:35:58 +01005512 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005513 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005514 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005515 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005519 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005520
Hanno Beckera5a2b082019-05-15 14:03:01 +01005521#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005522 /* We have already checked the record content type
5523 * in ssl_parse_record_header(), failing or silently
5524 * dropping the record in the case of an unknown type.
5525 *
5526 * Since with the use of CIDs, the record content type
5527 * might change during decryption, re-check the record
5528 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005529 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005530 {
5531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5532 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5533 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005534#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005535
Hanno Becker106f3da2019-07-12 09:35:58 +01005536 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005537 {
5538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005539 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005540 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005541 {
5542 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5544 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5545 }
5546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5547
5548 ssl->nb_zero++;
5549
5550 /*
5551 * Three or more empty messages may be a DoS attack
5552 * (excessive CPU consumption).
5553 */
5554 if( ssl->nb_zero > 3 )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005557 "messages, possible DoS attack" ) );
5558 /* Treat the records as if they were not properly authenticated,
5559 * thereby failing the connection if we see more than allowed
5560 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005561 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5562 }
5563 }
5564 else
5565 ssl->nb_zero = 0;
5566
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005567 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5568#if defined(MBEDTLS_SSL_PROTO_TLS)
5569 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005570 {
5571 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005572 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005573 if( ++ssl->in_ctr[i - 1] != 0 )
5574 break;
5575
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005576 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005577 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5580 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5581 }
5582 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005583#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005584
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 }
5586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005588 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005590 {
5591 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005594 return( ret );
5595 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005600 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005603 }
5604#endif
5605
Hanno Beckerf0242852019-07-09 17:30:02 +01005606 /* Check actual (decrypted) record content length against
5607 * configured maximum. */
5608 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5609 {
5610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5611 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5612 }
5613
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005614 return( 0 );
5615}
5616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005618
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005619/*
5620 * Read a record.
5621 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005622 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5623 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5624 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005625 */
Hanno Becker1097b342018-08-15 14:09:41 +01005626
5627/* Helper functions for mbedtls_ssl_read_record(). */
5628static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005629static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5630static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005631
Hanno Becker327c93b2018-08-15 13:56:18 +01005632int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005633 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005634{
5635 int ret;
5636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005638
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005639 if( ssl->keep_current_message == 0 )
5640 {
5641 do {
Simon Butcher99000142016-10-13 17:21:01 +01005642
Hanno Becker26994592018-08-15 14:14:59 +01005643 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005644 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005645 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005646
Hanno Beckere74d5562018-08-15 14:26:08 +01005647 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005648 {
Hanno Becker40f50842018-08-15 14:48:01 +01005649#if defined(MBEDTLS_SSL_PROTO_DTLS)
5650 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005651
Hanno Becker40f50842018-08-15 14:48:01 +01005652 /* We only check for buffered messages if the
5653 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005654 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005655 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005656 {
Hanno Becker40f50842018-08-15 14:48:01 +01005657 if( ssl_load_buffered_message( ssl ) == 0 )
5658 have_buffered = 1;
5659 }
5660
5661 if( have_buffered == 0 )
5662#endif /* MBEDTLS_SSL_PROTO_DTLS */
5663 {
5664 ret = ssl_get_next_record( ssl );
5665 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5666 continue;
5667
5668 if( ret != 0 )
5669 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005670 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005671 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005672 return( ret );
5673 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005674 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005675 }
5676
5677 ret = mbedtls_ssl_handle_message_type( ssl );
5678
Hanno Becker40f50842018-08-15 14:48:01 +01005679#if defined(MBEDTLS_SSL_PROTO_DTLS)
5680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5681 {
5682 /* Buffer future message */
5683 ret = ssl_buffer_message( ssl );
5684 if( ret != 0 )
5685 return( ret );
5686
5687 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5688 }
5689#endif /* MBEDTLS_SSL_PROTO_DTLS */
5690
Hanno Becker90333da2017-10-10 11:27:13 +01005691 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5692 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005693
5694 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005695 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005696 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005697 return( ret );
5698 }
5699
Hanno Becker327c93b2018-08-15 13:56:18 +01005700 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005701 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005702 {
5703 mbedtls_ssl_update_handshake_status( ssl );
5704 }
Simon Butcher99000142016-10-13 17:21:01 +01005705 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005706 else
Simon Butcher99000142016-10-13 17:21:01 +01005707 {
Hanno Becker02f59072018-08-15 14:00:24 +01005708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005709 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005710 }
5711
5712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5713
5714 return( 0 );
5715}
5716
Hanno Becker40f50842018-08-15 14:48:01 +01005717#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005718static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005719{
Hanno Becker40f50842018-08-15 14:48:01 +01005720 if( ssl->in_left > ssl->next_record_offset )
5721 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005722
Hanno Becker40f50842018-08-15 14:48:01 +01005723 return( 0 );
5724}
5725
5726static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5727{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005728 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005729 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005730 int ret = 0;
5731
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005732 if( hs == NULL )
5733 return( -1 );
5734
Hanno Beckere00ae372018-08-20 09:39:42 +01005735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5736
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005737 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5738 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5739 {
5740 /* Check if we have seen a ChangeCipherSpec before.
5741 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005742 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 {
5744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5745 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005746 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005747 }
5748
Hanno Becker39b8bc92018-08-28 17:17:13 +01005749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005750 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5751 ssl->in_msglen = 1;
5752 ssl->in_msg[0] = 1;
5753
5754 /* As long as they are equal, the exact value doesn't matter. */
5755 ssl->in_left = 0;
5756 ssl->next_record_offset = 0;
5757
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005758 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005759 goto exit;
5760 }
Hanno Becker37f95322018-08-16 13:55:32 +01005761
Hanno Beckerb8f50142018-08-28 10:01:34 +01005762#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005763 /* Debug only */
5764 {
5765 unsigned offset;
5766 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5767 {
5768 hs_buf = &hs->buffering.hs[offset];
5769 if( hs_buf->is_valid == 1 )
5770 {
5771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5772 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005773 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005774 }
5775 }
5776 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005777#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005778
5779 /* Check if we have buffered and/or fully reassembled the
5780 * next handshake message. */
5781 hs_buf = &hs->buffering.hs[0];
5782 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5783 {
5784 /* Synthesize a record containing the buffered HS message. */
5785 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5786 ( hs_buf->data[2] << 8 ) |
5787 hs_buf->data[3];
5788
5789 /* Double-check that we haven't accidentally buffered
5790 * a message that doesn't fit into the input buffer. */
5791 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5792 {
5793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5795 }
5796
5797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5798 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5799 hs_buf->data, msg_len + 12 );
5800
5801 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5802 ssl->in_hslen = msg_len + 12;
5803 ssl->in_msglen = msg_len + 12;
5804 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5805
5806 ret = 0;
5807 goto exit;
5808 }
5809 else
5810 {
5811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5812 hs->in_msg_seq ) );
5813 }
5814
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005815 ret = -1;
5816
5817exit:
5818
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5820 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005821}
5822
Hanno Beckera02b0b42018-08-21 17:20:27 +01005823static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5824 size_t desired )
5825{
5826 int offset;
5827 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5829 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005830
Hanno Becker01315ea2018-08-21 17:22:17 +01005831 /* Get rid of future records epoch first, if such exist. */
5832 ssl_free_buffered_record( ssl );
5833
5834 /* Check if we have enough space available now. */
5835 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5836 hs->buffering.total_bytes_buffered ) )
5837 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005839 return( 0 );
5840 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005841
Hanno Becker4f432ad2018-08-28 10:02:32 +01005842 /* We don't have enough space to buffer the next expected handshake
5843 * message. Remove buffers used for future messages to gain space,
5844 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005845 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5846 offset >= 0; offset-- )
5847 {
5848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5849 offset ) );
5850
Hanno Beckerb309b922018-08-23 13:18:05 +01005851 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005852
5853 /* Check if we have enough space available now. */
5854 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5855 hs->buffering.total_bytes_buffered ) )
5856 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005858 return( 0 );
5859 }
5860 }
5861
5862 return( -1 );
5863}
5864
Hanno Becker40f50842018-08-15 14:48:01 +01005865static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5866{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 int ret = 0;
5868 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5869
5870 if( hs == NULL )
5871 return( 0 );
5872
5873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5874
5875 switch( ssl->in_msgtype )
5876 {
5877 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005879
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005880 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005881 break;
5882
5883 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005884 {
5885 unsigned recv_msg_seq_offset;
5886 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5887 mbedtls_ssl_hs_buffer *hs_buf;
5888 size_t msg_len = ssl->in_hslen - 12;
5889
5890 /* We should never receive an old handshake
5891 * message - double-check nonetheless. */
5892 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5893 {
5894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5896 }
5897
5898 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5899 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5900 {
5901 /* Silently ignore -- message too far in the future */
5902 MBEDTLS_SSL_DEBUG_MSG( 2,
5903 ( "Ignore future HS message with sequence number %u, "
5904 "buffering window %u - %u",
5905 recv_msg_seq, ssl->handshake->in_msg_seq,
5906 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5907
5908 goto exit;
5909 }
5910
5911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5912 recv_msg_seq, recv_msg_seq_offset ) );
5913
5914 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5915
5916 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005917 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005918 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005919 size_t reassembly_buf_sz;
5920
Hanno Becker37f95322018-08-16 13:55:32 +01005921 hs_buf->is_fragmented =
5922 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5923
5924 /* We copy the message back into the input buffer
5925 * after reassembly, so check that it's not too large.
5926 * This is an implementation-specific limitation
5927 * and not one from the standard, hence it is not
5928 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005929 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005930 {
5931 /* Ignore message */
5932 goto exit;
5933 }
5934
Hanno Beckere0b150f2018-08-21 15:51:03 +01005935 /* Check if we have enough space to buffer the message. */
5936 if( hs->buffering.total_bytes_buffered >
5937 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5938 {
5939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5941 }
5942
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005943 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5944 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005945
5946 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5947 hs->buffering.total_bytes_buffered ) )
5948 {
5949 if( recv_msg_seq_offset > 0 )
5950 {
5951 /* If we can't buffer a future message because
5952 * of space limitations -- ignore. */
5953 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",
5954 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5955 (unsigned) hs->buffering.total_bytes_buffered ) );
5956 goto exit;
5957 }
Hanno Beckere1801392018-08-21 16:51:05 +01005958 else
5959 {
5960 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",
5961 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5962 (unsigned) hs->buffering.total_bytes_buffered ) );
5963 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005964
Hanno Beckera02b0b42018-08-21 17:20:27 +01005965 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005966 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005967 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",
5968 (unsigned) msg_len,
5969 (unsigned) reassembly_buf_sz,
5970 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005971 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005972 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5973 goto exit;
5974 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005975 }
5976
5977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5978 msg_len ) );
5979
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005980 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5981 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005982 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005983 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005984 goto exit;
5985 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005986 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005987
5988 /* Prepare final header: copy msg_type, length and message_seq,
5989 * then add standardised fragment_offset and fragment_length */
5990 memcpy( hs_buf->data, ssl->in_msg, 6 );
5991 memset( hs_buf->data + 6, 0, 3 );
5992 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5993
5994 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005995
5996 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005997 }
5998 else
5999 {
6000 /* Make sure msg_type and length are consistent */
6001 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
6002 {
6003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6004 /* Ignore */
6005 goto exit;
6006 }
6007 }
6008
Hanno Becker4422bbb2018-08-20 09:40:19 +01006009 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006010 {
6011 size_t frag_len, frag_off;
6012 unsigned char * const msg = hs_buf->data + 12;
6013
6014 /*
6015 * Check and copy current fragment
6016 */
6017
6018 /* Validation of header fields already done in
6019 * mbedtls_ssl_prepare_handshake_record(). */
6020 frag_off = ssl_get_hs_frag_off( ssl );
6021 frag_len = ssl_get_hs_frag_len( ssl );
6022
6023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6024 frag_off, frag_len ) );
6025 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6026
6027 if( hs_buf->is_fragmented )
6028 {
6029 unsigned char * const bitmask = msg + msg_len;
6030 ssl_bitmask_set( bitmask, frag_off, frag_len );
6031 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6032 msg_len ) == 0 );
6033 }
6034 else
6035 {
6036 hs_buf->is_complete = 1;
6037 }
6038
6039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6040 hs_buf->is_complete ? "" : "not yet " ) );
6041 }
6042
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006043 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006044 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006045
6046 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006047 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006048 break;
6049 }
6050
6051exit:
6052
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6054 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006055}
6056#endif /* MBEDTLS_SSL_PROTO_DTLS */
6057
Hanno Becker1097b342018-08-15 14:09:41 +01006058static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006059{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006060 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006061 * Consume last content-layer message and potentially
6062 * update in_msglen which keeps track of the contents'
6063 * consumption state.
6064 *
6065 * (1) Handshake messages:
6066 * Remove last handshake message, move content
6067 * and adapt in_msglen.
6068 *
6069 * (2) Alert messages:
6070 * Consume whole record content, in_msglen = 0.
6071 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006072 * (3) Change cipher spec:
6073 * Consume whole record content, in_msglen = 0.
6074 *
6075 * (4) Application data:
6076 * Don't do anything - the record layer provides
6077 * the application data as a stream transport
6078 * and consumes through mbedtls_ssl_read only.
6079 *
6080 */
6081
6082 /* Case (1): Handshake messages */
6083 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006084 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006085 /* Hard assertion to be sure that no application data
6086 * is in flight, as corrupting ssl->in_msglen during
6087 * ssl->in_offt != NULL is fatal. */
6088 if( ssl->in_offt != NULL )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6092 }
6093
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006094 /*
6095 * Get next Handshake message in the current record
6096 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006097
Hanno Becker4a810fb2017-05-24 16:27:30 +01006098 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006099 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006100 * current handshake content: If DTLS handshake
6101 * fragmentation is used, that's the fragment
6102 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006103 * size here is faulty and should be changed at
6104 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006105 * (2) While it doesn't seem to cause problems, one
6106 * has to be very careful not to assume that in_hslen
6107 * is always <= in_msglen in a sensible communication.
6108 * Again, it's wrong for DTLS handshake fragmentation.
6109 * The following check is therefore mandatory, and
6110 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006111 * Additionally, ssl->in_hslen might be arbitrarily out of
6112 * bounds after handling a DTLS message with an unexpected
6113 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006114 */
6115 if( ssl->in_hslen < ssl->in_msglen )
6116 {
6117 ssl->in_msglen -= ssl->in_hslen;
6118 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6119 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006120
Hanno Becker4a810fb2017-05-24 16:27:30 +01006121 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6122 ssl->in_msg, ssl->in_msglen );
6123 }
6124 else
6125 {
6126 ssl->in_msglen = 0;
6127 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006128
Hanno Becker4a810fb2017-05-24 16:27:30 +01006129 ssl->in_hslen = 0;
6130 }
6131 /* Case (4): Application data */
6132 else if( ssl->in_offt != NULL )
6133 {
6134 return( 0 );
6135 }
6136 /* Everything else (CCS & Alerts) */
6137 else
6138 {
6139 ssl->in_msglen = 0;
6140 }
6141
Hanno Becker1097b342018-08-15 14:09:41 +01006142 return( 0 );
6143}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006144
Hanno Beckere74d5562018-08-15 14:26:08 +01006145static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6146{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006147 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006148 return( 1 );
6149
6150 return( 0 );
6151}
6152
Hanno Becker5f066e72018-08-16 14:56:31 +01006153#if defined(MBEDTLS_SSL_PROTO_DTLS)
6154
6155static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6156{
6157 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6158 if( hs == NULL )
6159 return;
6160
Hanno Becker01315ea2018-08-21 17:22:17 +01006161 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006162 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006163 hs->buffering.total_bytes_buffered -=
6164 hs->buffering.future_record.len;
6165
6166 mbedtls_free( hs->buffering.future_record.data );
6167 hs->buffering.future_record.data = NULL;
6168 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006169}
6170
6171static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6172{
6173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6174 unsigned char * rec;
6175 size_t rec_len;
6176 unsigned rec_epoch;
6177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006179 return( 0 );
6180
6181 if( hs == NULL )
6182 return( 0 );
6183
Hanno Becker5f066e72018-08-16 14:56:31 +01006184 rec = hs->buffering.future_record.data;
6185 rec_len = hs->buffering.future_record.len;
6186 rec_epoch = hs->buffering.future_record.epoch;
6187
6188 if( rec == NULL )
6189 return( 0 );
6190
Hanno Becker4cb782d2018-08-20 11:19:05 +01006191 /* Only consider loading future records if the
6192 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006193 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006194 return( 0 );
6195
Hanno Becker5f066e72018-08-16 14:56:31 +01006196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6197
6198 if( rec_epoch != ssl->in_epoch )
6199 {
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6201 goto exit;
6202 }
6203
6204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6205
6206 /* Double-check that the record is not too large */
6207 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6208 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6209 {
6210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6212 }
6213
6214 memcpy( ssl->in_hdr, rec, rec_len );
6215 ssl->in_left = rec_len;
6216 ssl->next_record_offset = 0;
6217
6218 ssl_free_buffered_record( ssl );
6219
6220exit:
6221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6222 return( 0 );
6223}
6224
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006225static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6226 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006227{
6228 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006229
6230 /* Don't buffer future records outside handshakes. */
6231 if( hs == NULL )
6232 return( 0 );
6233
6234 /* Only buffer handshake records (we are only interested
6235 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006236 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006237 return( 0 );
6238
6239 /* Don't buffer more than one future epoch record. */
6240 if( hs->buffering.future_record.data != NULL )
6241 return( 0 );
6242
Hanno Becker01315ea2018-08-21 17:22:17 +01006243 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006244 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006245 hs->buffering.total_bytes_buffered ) )
6246 {
6247 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 +01006248 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006249 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006250 return( 0 );
6251 }
6252
Hanno Becker5f066e72018-08-16 14:56:31 +01006253 /* Buffer record */
6254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6255 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006256 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006257
6258 /* ssl_parse_record_header() only considers records
6259 * of the next epoch as candidates for buffering. */
6260 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006261 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006262
6263 hs->buffering.future_record.data =
6264 mbedtls_calloc( 1, hs->buffering.future_record.len );
6265 if( hs->buffering.future_record.data == NULL )
6266 {
6267 /* If we run out of RAM trying to buffer a
6268 * record from the next epoch, just ignore. */
6269 return( 0 );
6270 }
6271
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006272 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006273
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006274 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006275 return( 0 );
6276}
6277
6278#endif /* MBEDTLS_SSL_PROTO_DTLS */
6279
Hanno Beckere74d5562018-08-15 14:26:08 +01006280static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006281{
6282 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006283 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006284
Hanno Becker5f066e72018-08-16 14:56:31 +01006285#if defined(MBEDTLS_SSL_PROTO_DTLS)
6286 /* We might have buffered a future record; if so,
6287 * and if the epoch matches now, load it.
6288 * On success, this call will set ssl->in_left to
6289 * the length of the buffered record, so that
6290 * the calls to ssl_fetch_input() below will
6291 * essentially be no-ops. */
6292 ret = ssl_load_buffered_record( ssl );
6293 if( ret != 0 )
6294 return( ret );
6295#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006296
Hanno Becker8b09b732019-05-08 12:03:28 +01006297 /* Ensure that we have enough space available for the default form
6298 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6299 * with no space for CIDs counted in). */
6300 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6301 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006304 return( ret );
6305 }
6306
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006307 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6308 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006311 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006312 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006313 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6314 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006315 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006316 if( ret != 0 )
6317 return( ret );
6318
6319 /* Fall through to handling of unexpected records */
6320 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6321 }
6322
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006323 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6324 {
Hanno Becker87b56262019-07-10 14:37:41 +01006325#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006326 /* Reset in pointers to default state for TLS/DTLS records,
6327 * assuming no CID and no offset between record content and
6328 * record plaintext. */
6329 ssl_update_in_pointers( ssl );
6330
Hanno Becker69412452019-07-12 08:33:49 +01006331 /* Setup internal message pointers from record structure. */
6332 ssl->in_msgtype = rec.type;
6333#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6334 ssl->in_len = ssl->in_cid + rec.cid_len;
6335#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006336 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006337 ssl->in_msglen = rec.data_len;
6338
Hanno Becker87b56262019-07-10 14:37:41 +01006339 ret = ssl_check_client_reconnect( ssl );
6340 if( ret != 0 )
6341 return( ret );
6342#endif
6343
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006344 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006345 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006346
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6348 "(header)" ) );
6349 }
6350 else
6351 {
6352 /* Skip invalid record and the rest of the datagram */
6353 ssl->next_record_offset = 0;
6354 ssl->in_left = 0;
6355
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6357 "(header)" ) );
6358 }
6359
6360 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006361 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006362 }
Hanno Becker87b56262019-07-10 14:37:41 +01006363 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006364#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006365 {
6366 return( ret );
6367 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006368 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006371 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006372 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006373 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006374 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006375 if( ssl->next_record_offset < ssl->in_left )
6376 {
6377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6378 }
6379 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006380 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006381#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006382#if defined(MBEDTLS_SSL_PROTO_TLS)
6383 {
Hanno Beckere0452772019-07-10 17:12:07 +01006384 /*
6385 * Fetch record contents from underlying transport.
6386 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006387 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006388 if( ret != 0 )
6389 {
6390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6391 return( ret );
6392 }
6393
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006394 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006395 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006396#endif /* MBEDTLS_SSL_PROTO_TLS */
6397
6398 /*
6399 * Decrypt record contents.
6400 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006401
Hanno Beckera89610a2019-07-11 13:07:45 +01006402 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006405 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006406 {
6407 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006408 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006409 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006410 /* Except when waiting for Finished as a bad mac here
6411 * probably means something went wrong in the handshake
6412 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6413 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6414 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6415 {
6416#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6417 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6418 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006419 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006420 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6421 }
6422#endif
6423 return( ret );
6424 }
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006427 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6428 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6431 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006432 }
6433#endif
6434
Hanno Becker4a810fb2017-05-24 16:27:30 +01006435 /* As above, invalid records cause
6436 * dismissal of the whole datagram. */
6437
6438 ssl->next_record_offset = 0;
6439 ssl->in_left = 0;
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006442 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006443 }
6444
6445 return( ret );
6446 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006447 MBEDTLS_SSL_TRANSPORT_ELSE
6448#endif /* MBEDTLS_SSL_PROTO_DTLS */
6449#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006450 {
6451 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6453 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006454 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006455 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006457 }
6458#endif
6459 return( ret );
6460 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006461#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006462 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006463
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006464
6465 /* Reset in pointers to default state for TLS/DTLS records,
6466 * assuming no CID and no offset between record content and
6467 * record plaintext. */
6468 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6470 ssl->in_len = ssl->in_cid + rec.cid_len;
6471#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006472 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006473
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006474 /* The record content type may change during decryption,
6475 * so re-read it. */
6476 ssl->in_msgtype = rec.type;
6477 /* Also update the input buffer, because unfortunately
6478 * the server-side ssl_parse_client_hello() reparses the
6479 * record header when receiving a ClientHello initiating
6480 * a renegotiation. */
6481 ssl->in_hdr[0] = rec.type;
6482 ssl->in_msg = rec.buf + rec.data_offset;
6483 ssl->in_msglen = rec.data_len;
6484 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6485 ssl->in_len[1] = (unsigned char)( rec.data_len );
6486
Simon Butcher99000142016-10-13 17:21:01 +01006487 return( 0 );
6488}
6489
6490int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6491{
6492 int ret;
6493
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006494 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006495 * Handle particular types of records
6496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 {
Simon Butcher99000142016-10-13 17:21:01 +01006499 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6500 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006501 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 }
6504
Hanno Beckere678eaa2018-08-21 14:57:46 +01006505 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006506 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006507 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006508 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6510 ssl->in_msglen ) );
6511 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006512 }
6513
Hanno Beckere678eaa2018-08-21 14:57:46 +01006514 if( ssl->in_msg[0] != 1 )
6515 {
6516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6517 ssl->in_msg[0] ) );
6518 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6519 }
6520
6521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006522 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006523 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6524 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6525 {
6526 if( ssl->handshake == NULL )
6527 {
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6529 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6530 }
6531
6532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6533 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6534 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006535#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006536 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006540 if( ssl->in_msglen != 2 )
6541 {
6542 /* Note: Standard allows for more than one 2 byte alert
6543 to be packed in a single message, but Mbed TLS doesn't
6544 currently support this. */
6545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6546 ssl->in_msglen ) );
6547 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6548 }
6549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 ssl->in_msg[0], ssl->in_msg[1] ) );
6552
6553 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006554 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006559 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006561 }
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6564 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6567 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006568 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006569
6570#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6571 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6572 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6573 {
Hanno Becker90333da2017-10-10 11:27:13 +01006574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006575 /* Will be handled when trying to parse ServerHello */
6576 return( 0 );
6577 }
6578#endif
6579
6580#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006581 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006582 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6583 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006584 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6585 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6586 {
6587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6588 /* Will be handled in mbedtls_ssl_parse_certificate() */
6589 return( 0 );
6590 }
6591#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6592
6593 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006594 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006595 }
6596
Hanno Beckerc76c6192017-06-06 10:03:17 +01006597#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006598 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006599 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006600 /* Drop unexpected ApplicationData records,
6601 * except at the beginning of renegotiations */
6602 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6603 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6604#if defined(MBEDTLS_SSL_RENEGOTIATION)
6605 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6606 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006607#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006608 )
6609 {
6610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6611 return( MBEDTLS_ERR_SSL_NON_FATAL );
6612 }
6613
6614 if( ssl->handshake != NULL &&
6615 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6616 {
6617 ssl_handshake_wrapup_free_hs_transform( ssl );
6618 }
6619 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006620#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006621
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 return( 0 );
6623}
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006626{
6627 int ret;
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6630 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6631 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006632 {
6633 return( ret );
6634 }
6635
6636 return( 0 );
6637}
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006640 unsigned char level,
6641 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006642{
6643 int ret;
6644
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006645 if( ssl == NULL || ssl->conf == NULL )
6646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006652 ssl->out_msglen = 2;
6653 ssl->out_msg[0] = level;
6654 ssl->out_msg[1] = message;
6655
Hanno Becker67bc7c32018-08-06 11:33:50 +01006656 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006659 return( ret );
6660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006662
6663 return( 0 );
6664}
6665
Hanno Becker17572472019-02-08 07:19:04 +00006666#if defined(MBEDTLS_X509_CRT_PARSE_C)
6667static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6668{
6669#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6670 if( session->peer_cert != NULL )
6671 {
6672 mbedtls_x509_crt_free( session->peer_cert );
6673 mbedtls_free( session->peer_cert );
6674 session->peer_cert = NULL;
6675 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006676#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006677 if( session->peer_cert_digest != NULL )
6678 {
6679 /* Zeroization is not necessary. */
6680 mbedtls_free( session->peer_cert_digest );
6681 session->peer_cert_digest = NULL;
6682 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6683 session->peer_cert_digest_len = 0;
6684 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006685#else
6686 ((void) session);
6687#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006688}
6689#endif /* MBEDTLS_X509_CRT_PARSE_C */
6690
Paul Bakker5121ce52009-01-03 21:22:43 +00006691/*
6692 * Handshake functions
6693 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006694#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006695/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697{
Hanno Beckerdf645962019-06-26 13:02:22 +01006698 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6699 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006702
Hanno Becker5097cba2019-02-05 13:36:46 +00006703 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006706 ssl->state++;
6707 return( 0 );
6708 }
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006712}
6713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006715{
Hanno Beckerdf645962019-06-26 13:02:22 +01006716 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6717 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720
Hanno Becker5097cba2019-02-05 13:36:46 +00006721 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006724 ssl->state++;
6725 return( 0 );
6726 }
6727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006730}
Gilles Peskinef9828522017-05-03 12:28:43 +02006731
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006732#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006733/* Some certificate support -> implement write and parse */
6734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006740 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6741 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744
Hanno Becker5097cba2019-02-05 13:36:46 +00006745 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006748 ssl->state++;
6749 return( 0 );
6750 }
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006753 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6754 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 {
6756 if( ssl->client_auth == 0 )
6757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 ssl->state++;
6760 return( 0 );
6761 }
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006764 /*
6765 * If using SSLv3 and got no cert, send an Alert message
6766 * (otherwise an empty Certificate message will be sent).
6767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006769 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 {
6771 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6773 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6774 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 goto write_msg;
6778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#endif /* MBEDTLS_SSL_CLI_C */
6782#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006783 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6788 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006794
6795 /*
6796 * 0 . 0 handshake type
6797 * 1 . 3 handshake length
6798 * 4 . 6 length of all certs
6799 * 7 . 9 length of cert. 1
6800 * 10 . n-1 peer certificate
6801 * n . n+2 length of cert. 2
6802 * n+3 . ... upper level cert, etc.
6803 */
6804 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Paul Bakker29087132010-03-21 21:03:34 +00006807 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006808 {
6809 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006810 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006813 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 }
6816
6817 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6818 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6819 ssl->out_msg[i + 2] = (unsigned char)( n );
6820
6821 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6822 i += n; crt = crt->next;
6823 }
6824
6825 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6826 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6827 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6828
6829 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6831 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006833#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006834write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
6837 ssl->state++;
6838
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006839 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006840 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 return( ret );
6843 }
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
Paul Bakkered27a042013-04-18 22:46:23 +02006847 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848}
6849
Hanno Becker285ff0c2019-01-31 07:44:03 +00006850#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006851
6852#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006853static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6854 unsigned char *crt_buf,
6855 size_t crt_buf_len )
6856{
6857 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6858
6859 if( peer_crt == NULL )
6860 return( -1 );
6861
6862 if( peer_crt->raw.len != crt_buf_len )
6863 return( -1 );
6864
Hanno Becker68b856d2019-02-08 14:00:04 +00006865 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006866}
Hanno Becker5882dd02019-06-06 16:25:57 +01006867#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006868static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6869 unsigned char *crt_buf,
6870 size_t crt_buf_len )
6871{
6872 int ret;
6873 unsigned char const * const peer_cert_digest =
6874 ssl->session->peer_cert_digest;
6875 mbedtls_md_type_t const peer_cert_digest_type =
6876 ssl->session->peer_cert_digest_type;
6877 mbedtls_md_info_t const * const digest_info =
6878 mbedtls_md_info_from_type( peer_cert_digest_type );
6879 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6880 size_t digest_len;
6881
6882 if( peer_cert_digest == NULL || digest_info == NULL )
6883 return( -1 );
6884
6885 digest_len = mbedtls_md_get_size( digest_info );
6886 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6887 return( -1 );
6888
6889 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6890 if( ret != 0 )
6891 return( -1 );
6892
6893 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6894}
Hanno Becker5882dd02019-06-06 16:25:57 +01006895#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006896#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006897
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006898/*
6899 * Once the certificate message is read, parse it into a cert chain and
6900 * perform basic checks, but leave actual verification to the caller
6901 */
Hanno Becker35e41772019-02-05 15:37:23 +00006902static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6903 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006905 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006906#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6907 int crt_cnt=0;
6908#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006909 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006910 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 }
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6921 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006924 mbedtls_ssl_pend_fatal_alert( ssl,
6925 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006927 }
6928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006930
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006933 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006934 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006936 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006940 mbedtls_ssl_pend_fatal_alert( ssl,
6941 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 }
6944
Hanno Becker33c3dc82019-01-30 14:46:46 +00006945 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6946 i += 3;
6947
Hanno Becker33c3dc82019-01-30 14:46:46 +00006948 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 while( i < ssl->in_hslen )
6950 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006951 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006952 if ( i + 3 > ssl->in_hslen ) {
6953 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 );
Philippe Antoine747fd532018-05-30 09:13:21 +02006956 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6957 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006958 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6959 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 if( ssl->in_msg[i] != 0 )
6961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006963 mbedtls_ssl_pend_fatal_alert( ssl,
6964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 }
6967
Hanno Becker33c3dc82019-01-30 14:46:46 +00006968 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6970 | (unsigned int) ssl->in_msg[i + 2];
6971 i += 3;
6972
6973 if( n < 128 || i + n > ssl->in_hslen )
6974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006976 mbedtls_ssl_pend_fatal_alert( ssl,
6977 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 }
6980
Hanno Becker33c3dc82019-01-30 14:46:46 +00006981 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006982#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6983 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006984 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6985 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006986 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006987 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006988 /* During client-side renegotiation, check that the server's
6989 * end-CRTs hasn't changed compared to the initial handshake,
6990 * mitigating the triple handshake attack. On success, reuse
6991 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6993 if( ssl_check_peer_crt_unchanged( ssl,
6994 &ssl->in_msg[i],
6995 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006996 {
Hanno Becker35e41772019-02-05 15:37:23 +00006997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006998 mbedtls_ssl_pend_fatal_alert( ssl,
6999 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007000 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007001 }
Hanno Becker35e41772019-02-05 15:37:23 +00007002
7003 /* Now we can safely free the original chain. */
7004 ssl_clear_peer_cert( ssl->session );
7005 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007006#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7007
Hanno Becker33c3dc82019-01-30 14:46:46 +00007008 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007009#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007010 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007011#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007012 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007013 * it in-place from the input buffer instead of making a copy. */
7014 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7015#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007016 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007017 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018 case 0: /*ok*/
7019 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7020 /* Ignore certificate with an unknown algorithm: maybe a
7021 prior certificate was already trusted. */
7022 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007023
Hanno Becker33c3dc82019-01-30 14:46:46 +00007024 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7025 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7026 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007027
Hanno Becker33c3dc82019-01-30 14:46:46 +00007028 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7029 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7030 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007031
Hanno Becker33c3dc82019-01-30 14:46:46 +00007032 default:
7033 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7034 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007035 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007036 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7037 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007038 }
7039
7040 i += n;
7041 }
7042
Hanno Becker35e41772019-02-05 15:37:23 +00007043 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007044 return( 0 );
7045}
7046
Hanno Beckerb8a08572019-02-05 12:49:06 +00007047#if defined(MBEDTLS_SSL_SRV_C)
7048static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7049{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007050 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007051 return( -1 );
7052
7053#if defined(MBEDTLS_SSL_PROTO_SSL3)
7054 /*
7055 * Check if the client sent an empty certificate
7056 */
Hanno Becker2881d802019-05-22 14:44:53 +01007057 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007058 {
7059 if( ssl->in_msglen == 2 &&
7060 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7061 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7062 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7065 return( 0 );
7066 }
7067
7068 return( -1 );
7069 }
7070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7071
7072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7073 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7074 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7075 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7076 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7077 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7078 {
7079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7080 return( 0 );
7081 }
7082
Hanno Beckerb8a08572019-02-05 12:49:06 +00007083#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7084 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007085
7086 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007087}
7088#endif /* MBEDTLS_SSL_SRV_C */
7089
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007090/* Check if a certificate message is expected.
7091 * Return either
7092 * - SSL_CERTIFICATE_EXPECTED, or
7093 * - SSL_CERTIFICATE_SKIP
7094 * indicating whether a Certificate message is expected or not.
7095 */
7096#define SSL_CERTIFICATE_EXPECTED 0
7097#define SSL_CERTIFICATE_SKIP 1
7098static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7099 int authmode )
7100{
Hanno Becker473f98f2019-06-26 10:27:32 +01007101 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007102 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007103
7104 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7105 return( SSL_CERTIFICATE_SKIP );
7106
7107#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007108 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007109 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007110 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7111 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7112 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007113 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007114 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007115
7116 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7117 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007118 ssl->session_negotiate->verify_result =
7119 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7120 return( SSL_CERTIFICATE_SKIP );
7121 }
7122 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007123#else
7124 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007125#endif /* MBEDTLS_SSL_SRV_C */
7126
7127 return( SSL_CERTIFICATE_EXPECTED );
7128}
7129
Hanno Becker3cf50612019-02-05 14:36:34 +00007130static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7131 int authmode,
7132 mbedtls_x509_crt *chain,
7133 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007134{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007135 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007136 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007137 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007138 mbedtls_x509_crt *ca_chain;
7139 mbedtls_x509_crl *ca_crl;
7140
7141 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7142 return( 0 );
7143
7144#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7145 if( ssl->handshake->sni_ca_chain != NULL )
7146 {
7147 ca_chain = ssl->handshake->sni_ca_chain;
7148 ca_crl = ssl->handshake->sni_ca_crl;
7149 }
7150 else
7151#endif
7152 {
7153 ca_chain = ssl->conf->ca_chain;
7154 ca_crl = ssl->conf->ca_crl;
7155 }
7156
7157 /*
7158 * Main check: verify certificate
7159 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007160 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007161 chain,
7162 ca_chain, ca_crl,
7163 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007164#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007165 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007166#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 &ssl->session_negotiate->verify_result,
7168 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7169
Hanno Becker8c13ee62019-02-26 16:48:17 +00007170 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007171 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007172 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007173 }
7174
7175#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007176 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007177 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7178#endif
7179
7180 /*
7181 * Secondary checks: always done, but change 'ret' only if it was 0
7182 */
7183
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007184#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007185 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007186 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007187#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007188 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007189#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007190 mbedtls_pk_context *pk;
7191 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7192 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007193 {
7194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007195 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007196 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007197
7198 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007199 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007200 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007201 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007202 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007203
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007204 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007205#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007206
7207 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007208 {
7209 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7210
7211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007212 if( verify_ret == 0 )
7213 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007214 }
7215 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007216#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007217
7218 if( mbedtls_ssl_check_cert_usage( chain,
7219 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007220 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7221 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007222 &ssl->session_negotiate->verify_result ) != 0 )
7223 {
7224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007225 if( verify_ret == 0 )
7226 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007227 }
7228
7229 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7230 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7231 * with details encoded in the verification flags. All other kinds
7232 * of error codes, including those from the user provided f_vrfy
7233 * functions, are treated as fatal and lead to a failure of
7234 * ssl_parse_certificate even if verification was optional. */
7235 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007236 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7237 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007238 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007239 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007240 }
7241
7242 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7243 {
7244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007245 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007246 }
7247
Hanno Becker8c13ee62019-02-26 16:48:17 +00007248 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007249 {
7250 uint8_t alert;
7251
7252 /* The certificate may have been rejected for several reasons.
7253 Pick one and send the corresponding alert. Which alert to send
7254 may be a subject of debate in some cases. */
7255 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7256 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7258 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7260 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7262 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7264 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7265 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7266 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7267 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7268 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7269 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7270 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7271 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7272 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7273 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7274 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7275 else
7276 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007277 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007278 }
7279
7280#if defined(MBEDTLS_DEBUG_C)
7281 if( ssl->session_negotiate->verify_result != 0 )
7282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7284 ssl->session_negotiate->verify_result ) );
7285 }
7286 else
7287 {
7288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7289 }
7290#endif /* MBEDTLS_DEBUG_C */
7291
Hanno Becker8c13ee62019-02-26 16:48:17 +00007292 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007293}
7294
Hanno Becker34106f62019-02-08 14:59:05 +00007295#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007296
7297#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007298static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7299 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007300{
7301 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007302 /* Remember digest of the peer's end-CRT. */
7303 ssl->session_negotiate->peer_cert_digest =
7304 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7305 if( ssl->session_negotiate->peer_cert_digest == NULL )
7306 {
7307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7308 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007309 mbedtls_ssl_pend_fatal_alert( ssl,
7310 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007311
7312 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7313 }
7314
7315 ret = mbedtls_md( mbedtls_md_info_from_type(
7316 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7317 start, len,
7318 ssl->session_negotiate->peer_cert_digest );
7319
7320 ssl->session_negotiate->peer_cert_digest_type =
7321 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7322 ssl->session_negotiate->peer_cert_digest_len =
7323 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7324
7325 return( ret );
7326}
Hanno Becker5882dd02019-06-06 16:25:57 +01007327#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007328
7329static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7330 unsigned char *start, size_t len )
7331{
7332 unsigned char *end = start + len;
7333 int ret;
7334
7335 /* Make a copy of the peer's raw public key. */
7336 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7337 ret = mbedtls_pk_parse_subpubkey( &start, end,
7338 &ssl->handshake->peer_pubkey );
7339 if( ret != 0 )
7340 {
7341 /* We should have parsed the public key before. */
7342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7343 }
7344
7345 return( 0 );
7346}
7347#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7348
Hanno Becker3cf50612019-02-05 14:36:34 +00007349int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7350{
7351 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007352 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007353#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7354 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7355 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007356 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007357#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007358 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007359#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007360 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007361 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007362
7363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7364
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007365 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7366 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007367 {
7368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007369 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007370 }
7371
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007372#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7373 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007374 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007375 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007376 chain = ssl->handshake->ecrs_peer_cert;
7377 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007378 goto crt_verify;
7379 }
7380#endif
7381
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007382 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007383 {
7384 /* mbedtls_ssl_read_record may have sent an alert already. We
7385 let it decide whether to alert. */
7386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007387 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007388 }
7389
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007390#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007391 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7392 {
7393 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007394
Hanno Beckerb8a08572019-02-05 12:49:06 +00007395 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007396 ret = 0;
7397 else
7398 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007399
Hanno Becker613d4902019-02-05 13:11:17 +00007400 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007401 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007402#endif /* MBEDTLS_SSL_SRV_C */
7403
Hanno Becker35e41772019-02-05 15:37:23 +00007404 /* Clear existing peer CRT structure in case we tried to
7405 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007406 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007407
7408 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7409 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007410 {
7411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7412 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007413 mbedtls_ssl_pend_fatal_alert( ssl,
7414 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007415
Hanno Beckere4aeb762019-02-05 17:19:52 +00007416 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7417 goto exit;
7418 }
7419 mbedtls_x509_crt_init( chain );
7420
7421 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007422 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007423 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007424
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007425#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7426 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007427 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007428
7429crt_verify:
7430 if( ssl->handshake->ecrs_enabled)
7431 rs_ctx = &ssl->handshake->ecrs_ctx;
7432#endif
7433
Hanno Becker3cf50612019-02-05 14:36:34 +00007434 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007435 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007436 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007437 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007438
Hanno Becker3008d282019-02-05 17:02:28 +00007439#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007440 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007441 size_t pk_len;
7442 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007443
Hanno Becker34106f62019-02-08 14:59:05 +00007444 /* We parse the CRT chain without copying, so
7445 * these pointers point into the input buffer,
7446 * and are hence still valid after freeing the
7447 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
Hanno Becker5882dd02019-06-06 16:25:57 +01007449#if defined(MBEDTLS_SSL_RENEGOTIATION)
7450 unsigned char *crt_start;
7451 size_t crt_len;
7452
Hanno Becker34106f62019-02-08 14:59:05 +00007453 crt_start = chain->raw.p;
7454 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007455#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007456
Hanno Becker8c13ee62019-02-26 16:48:17 +00007457 pk_start = chain->cache->pk_raw.p;
7458 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007459
7460 /* Free the CRT structures before computing
7461 * digest and copying the peer's public key. */
7462 mbedtls_x509_crt_free( chain );
7463 mbedtls_free( chain );
7464 chain = NULL;
7465
Hanno Becker5882dd02019-06-06 16:25:57 +01007466#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007467 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007468 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007469 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007470#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007471
Hanno Becker34106f62019-02-08 14:59:05 +00007472 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007474 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007475 }
Hanno Becker34106f62019-02-08 14:59:05 +00007476#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7477 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007478 ssl->session_negotiate->peer_cert = chain;
7479 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007480#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007483
Hanno Becker613d4902019-02-05 13:11:17 +00007484exit:
7485
Hanno Beckere4aeb762019-02-05 17:19:52 +00007486 if( ret == 0 )
7487 ssl->state++;
7488
7489#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7490 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7491 {
7492 ssl->handshake->ecrs_peer_cert = chain;
7493 chain = NULL;
7494 }
7495#endif
7496
7497 if( chain != NULL )
7498 {
7499 mbedtls_x509_crt_free( chain );
7500 mbedtls_free( chain );
7501 }
7502
Paul Bakker5121ce52009-01-03 21:22:43 +00007503 return( ret );
7504}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007505#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007508{
7509 int ret;
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007514 ssl->out_msglen = 1;
7515 ssl->out_msg[0] = 1;
7516
Paul Bakker5121ce52009-01-03 21:22:43 +00007517 ssl->state++;
7518
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007519 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007522 return( ret );
7523 }
7524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007526
7527 return( 0 );
7528}
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007531{
7532 int ret;
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007535
Hanno Becker327c93b2018-08-15 13:56:18 +01007536 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007539 return( ret );
7540 }
7541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007545 mbedtls_ssl_pend_fatal_alert( ssl,
7546 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007548 }
7549
Hanno Beckere678eaa2018-08-21 14:57:46 +01007550 /* CCS records are only accepted if they have length 1 and content '1',
7551 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007552
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007553 /*
7554 * Switch to our negotiated transform and session parameters for inbound
7555 * data.
7556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007558 ssl->transform_in = ssl->transform_negotiate;
7559 ssl->session_in = ssl->session_negotiate;
7560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007562 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007565 ssl_dtls_replay_reset( ssl );
7566#endif
7567
7568 /* Increment epoch */
7569 if( ++ssl->in_epoch == 0 )
7570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007572 /* This is highly unlikely to happen for legitimate reasons, so
7573 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007575 }
7576 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007577 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007579#if defined(MBEDTLS_SSL_PROTO_TLS)
7580 {
7581 memset( ssl->in_ctr, 0, 8 );
7582 }
7583#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007584
Hanno Beckerf5970a02019-05-08 09:38:41 +01007585 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7588 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007593 mbedtls_ssl_pend_fatal_alert( ssl,
7594 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007596 }
7597 }
7598#endif
7599
Paul Bakker5121ce52009-01-03 21:22:43 +00007600 ssl->state++;
7601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007603
7604 return( 0 );
7605}
7606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7610 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007611 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007612 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7615#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007616 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007619 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007620#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007622}
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007627
7628 /*
7629 * Free our handshake params
7630 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007631 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007633 ssl->handshake = NULL;
7634
7635 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007636 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007637 */
7638 if( ssl->transform )
7639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 mbedtls_ssl_transform_free( ssl->transform );
7641 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007642 }
7643 ssl->transform = ssl->transform_negotiate;
7644 ssl->transform_negotiate = NULL;
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647}
7648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_RENEGOTIATION)
7654 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007657 ssl->renego_records_seen = 0;
7658 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007659#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007660
7661 /*
7662 * Free the previous session and switch in the current one
7663 */
Paul Bakker0a597072012-09-25 21:55:46 +00007664 if( ssl->session )
7665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007667 /* RFC 7366 3.1: keep the EtM state */
7668 ssl->session_negotiate->encrypt_then_mac =
7669 ssl->session->encrypt_then_mac;
7670#endif
7671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 mbedtls_ssl_session_free( ssl->session );
7673 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007674 }
7675 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007676 ssl->session_negotiate = NULL;
7677
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007678#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007679 /*
7680 * Add cache entry
7681 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007682 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007683 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007684 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007685 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007686 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007688 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007689#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007692 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007693 ssl->handshake->flight != NULL )
7694 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007695 /* Cancel handshake timer */
7696 ssl_set_timer( ssl, 0 );
7697
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007698 /* Keep last flight around in case we need to resend it:
7699 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007701 }
7702 else
7703#endif
7704 ssl_handshake_wrapup_free_hs_transform( ssl );
7705
Paul Bakker48916f92012-09-16 19:57:18 +00007706 ssl->state++;
7707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007709}
7710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007712{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007713 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007716
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007717 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007718
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007719 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7720 mbedtls_ssl_suite_get_mac(
7721 mbedtls_ssl_ciphersuite_from_id(
7722 mbedtls_ssl_session_get_ciphersuite(
7723 ssl->session_negotiate ) ) ),
7724 ssl, ssl->out_msg + 4,
7725 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007726
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007727 /*
7728 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7729 * may define some other value. Currently (early 2016), no defined
7730 * ciphersuite does this (and this is unlikely to change as activity has
7731 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7732 */
Hanno Becker2881d802019-05-22 14:44:53 +01007733 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007736 ssl->verify_data_len = hash_len;
7737 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007738#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007739
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7742 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007743
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007744#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007745 /*
7746 * In case of session resuming, invert the client and server
7747 * ChangeCipherSpec messages order.
7748 */
Paul Bakker0a597072012-09-25 21:55:46 +00007749 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007752 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7753 MBEDTLS_SSL_IS_CLIENT )
7754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007756 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007759 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7760 MBEDTLS_SSL_IS_SERVER )
7761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007763 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007765 }
7766 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007767#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007768 ssl->state++;
7769
Paul Bakker48916f92012-09-16 19:57:18 +00007770 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007771 * Switch to our negotiated transform and session parameters for outbound
7772 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007777 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007778 {
7779 unsigned char i;
7780
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007781 /* Remember current epoch settings for resending */
7782 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007783 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007784
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007785 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007786 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007787
7788 /* Increment epoch */
7789 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007790 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007791 break;
7792
7793 /* The loop goes to its end iff the counter is wrapping */
7794 if( i == 0 )
7795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7797 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007798 }
7799 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007800 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007802#if defined(MBEDTLS_SSL_PROTO_TLS)
7803 {
7804 memset( ssl->cur_out_ctr, 0, 8 );
7805 }
7806#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007807
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007808 ssl->transform_out = ssl->transform_negotiate;
7809 ssl->session_out = ssl->session_negotiate;
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7812 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007818 }
7819 }
7820#endif
7821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007823 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007825#endif
7826
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007827 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830 return( ret );
7831 }
7832
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007834 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007835 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7836 {
7837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7838 return( ret );
7839 }
7840#endif
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007843
7844 return( 0 );
7845}
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007848#define SSL_MAX_HASH_LEN 36
7849#else
7850#define SSL_MAX_HASH_LEN 12
7851#endif
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007854{
Paul Bakker23986e52011-04-24 08:57:21 +00007855 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007856 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007857 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007860
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007861 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7862 mbedtls_ssl_suite_get_mac(
7863 mbedtls_ssl_ciphersuite_from_id(
7864 mbedtls_ssl_session_get_ciphersuite(
7865 ssl->session_negotiate ) ) ),
7866 ssl, buf,
7867 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007868
Hanno Becker327c93b2018-08-15 13:56:18 +01007869 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 return( ret );
7873 }
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007878 mbedtls_ssl_pend_fatal_alert( ssl,
7879 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007881 }
7882
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007883 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007885 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007886 hash_len = 36;
7887 else
7888#endif
7889 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7892 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007895 mbedtls_ssl_pend_fatal_alert( ssl,
7896 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 }
7899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007901 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007904 mbedtls_ssl_pend_fatal_alert( ssl,
7905 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007907 }
7908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007910 ssl->verify_data_len = hash_len;
7911 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007912#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007913
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007914#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007915 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007918 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007922 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007925 }
7926 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007927#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007928 ssl->state++;
7929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007931 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007933#endif
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007936
7937 return( 0 );
7938}
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7945 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7946 mbedtls_md5_init( &handshake->fin_md5 );
7947 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007948 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7949 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7952#if defined(MBEDTLS_SHA256_C)
7953 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007954 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SHA512_C)
7957 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007958 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007961
Hanno Becker7e5437a2017-04-28 17:15:26 +01007962#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7963 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7964 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7965#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#if defined(MBEDTLS_DHM_C)
7968 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_ECDH_C)
7971 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007973#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007974 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007975#if defined(MBEDTLS_SSL_CLI_C)
7976 handshake->ecjpake_cache = NULL;
7977 handshake->ecjpake_cache_len = 0;
7978#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007979#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007980
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007981#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007982 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007983#endif
7984
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007985#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7986 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7987#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007988
7989#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7990 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7991 mbedtls_pk_init( &handshake->peer_pubkey );
7992#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007993}
7994
Hanno Becker611a83b2018-01-03 14:27:32 +00007995void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8000 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008001
Hanno Becker92231322018-01-03 15:32:51 +00008002#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_md_init( &transform->md_ctx_enc );
8004 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008005#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008006}
8007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011}
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008014{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008015 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008016 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008020 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008021 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022
8023 /*
8024 * Either the pointers are now NULL or cleared properly and can be freed.
8025 * Now allocate missing structures.
8026 */
8027 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008028 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008029 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008030 }
Paul Bakker48916f92012-09-16 19:57:18 +00008031
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008032 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008033 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008034 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008035 }
Paul Bakker48916f92012-09-16 19:57:18 +00008036
Paul Bakker82788fb2014-10-20 13:59:19 +02008037 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008038 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008039 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008040 }
Paul Bakker48916f92012-09-16 19:57:18 +00008041
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008042 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008043 if( ssl->handshake == NULL ||
8044 ssl->transform_negotiate == NULL ||
8045 ssl->session_negotiate == NULL )
8046 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049 mbedtls_free( ssl->handshake );
8050 mbedtls_free( ssl->transform_negotiate );
8051 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008052
8053 ssl->handshake = NULL;
8054 ssl->transform_negotiate = NULL;
8055 ssl->session_negotiate = NULL;
8056
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008057 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008058 }
8059
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008060 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008062 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008063 ssl_handshake_params_init( ssl->handshake );
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008066 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008067 {
8068 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008069
Hanno Becker2d9623f2019-06-13 12:07:05 +01008070 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008071 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8072 else
8073 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8074 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008075#endif
8076
Paul Bakker48916f92012-09-16 19:57:18 +00008077 return( 0 );
8078}
8079
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008080#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008081/* Dummy cookie callbacks for defaults */
8082static int ssl_cookie_write_dummy( void *ctx,
8083 unsigned char **p, unsigned char *end,
8084 const unsigned char *cli_id, size_t cli_id_len )
8085{
8086 ((void) ctx);
8087 ((void) p);
8088 ((void) end);
8089 ((void) cli_id);
8090 ((void) cli_id_len);
8091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008093}
8094
8095static int ssl_cookie_check_dummy( void *ctx,
8096 const unsigned char *cookie, size_t cookie_len,
8097 const unsigned char *cli_id, size_t cli_id_len )
8098{
8099 ((void) ctx);
8100 ((void) cookie);
8101 ((void) cookie_len);
8102 ((void) cli_id);
8103 ((void) cli_id_len);
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008106}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008107#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008108
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008109/* Once ssl->out_hdr as the address of the beginning of the
8110 * next outgoing record is set, deduce the other pointers.
8111 *
8112 * Note: For TLS, we save the implicit record sequence number
8113 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8114 * and the caller has to make sure there's space for this.
8115 */
8116
8117static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8118 mbedtls_ssl_transform *transform )
8119{
8120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008121 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008122 {
8123 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008124#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008125 ssl->out_cid = ssl->out_ctr + 8;
8126 ssl->out_len = ssl->out_cid;
8127 if( transform != NULL )
8128 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008129#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008130 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008131#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008132 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008134 MBEDTLS_SSL_TRANSPORT_ELSE
8135#endif /* MBEDTLS_SSL_PROTO_DTLS */
8136#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008137 {
8138 ssl->out_ctr = ssl->out_hdr - 8;
8139 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008140#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008141 ssl->out_cid = ssl->out_len;
8142#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008143 ssl->out_iv = ssl->out_hdr + 5;
8144 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008145#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146
8147 /* Adjust out_msg to make space for explicit IV, if used. */
8148 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008149 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008150 {
8151 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8152 }
8153 else
8154 ssl->out_msg = ssl->out_iv;
8155}
8156
8157/* Once ssl->in_hdr as the address of the beginning of the
8158 * next incoming record is set, deduce the other pointers.
8159 *
8160 * Note: For TLS, we save the implicit record sequence number
8161 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8162 * and the caller has to make sure there's space for this.
8163 */
8164
Hanno Beckerf5970a02019-05-08 09:38:41 +01008165static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008167 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008168 * of unprotected TLS/DTLS records, with ssl->in_msg
8169 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008170 *
8171 * When decrypting a protected record, ssl->in_msg
8172 * will be shifted to point to the beginning of the
8173 * record plaintext.
8174 */
8175
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008177 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008178 {
Hanno Becker70e79282019-05-03 14:34:53 +01008179 /* This sets the header pointers to match records
8180 * without CID. When we receive a record containing
8181 * a CID, the fields are shifted accordingly in
8182 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008183 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008184#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008185 ssl->in_cid = ssl->in_ctr + 8;
8186 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008187#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008188 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008189#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008190 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008191 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008192 MBEDTLS_SSL_TRANSPORT_ELSE
8193#endif /* MBEDTLS_SSL_PROTO_DTLS */
8194#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008195 {
8196 ssl->in_ctr = ssl->in_hdr - 8;
8197 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008198#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008199 ssl->in_cid = ssl->in_len;
8200#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008201 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008202 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008203#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008204}
8205
Paul Bakker5121ce52009-01-03 21:22:43 +00008206/*
8207 * Initialize an SSL context
8208 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008209void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8210{
8211 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8212}
8213
8214/*
8215 * Setup an SSL context
8216 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008217
8218static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8219{
8220 /* Set the incoming and outgoing record pointers. */
8221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008222 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008223 {
8224 ssl->out_hdr = ssl->out_buf;
8225 ssl->in_hdr = ssl->in_buf;
8226 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008227 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008229#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008230 {
8231 ssl->out_hdr = ssl->out_buf + 8;
8232 ssl->in_hdr = ssl->in_buf + 8;
8233 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008234#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008235
8236 /* Derive other internal pointers. */
8237 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008238 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008239}
8240
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008241int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008242 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008243{
Paul Bakker48916f92012-09-16 19:57:18 +00008244 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008245
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008246 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008247
Hanno Beckeref982d52019-07-23 15:56:18 +01008248#if defined(MBEDTLS_USE_TINYCRYPT)
8249 uECC_set_rng( &uecc_rng_wrapper );
8250#endif
8251
Paul Bakker62f2dee2012-09-28 07:31:51 +00008252 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008253 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008254 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008255
8256 /* Set to NULL in case of an error condition */
8257 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008258
Angus Grattond8213d02016-05-25 20:56:48 +10008259 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8260 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008261 {
Angus Grattond8213d02016-05-25 20:56:48 +10008262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008263 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008264 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008265 }
8266
8267 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8268 if( ssl->out_buf == NULL )
8269 {
8270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008271 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008272 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008273 }
8274
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008275 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008276
Paul Bakker48916f92012-09-16 19:57:18 +00008277 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008278 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008279
Hanno Beckerc8f52992019-07-25 11:15:08 +01008280 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008281
Paul Bakker5121ce52009-01-03 21:22:43 +00008282 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008283
8284error:
8285 mbedtls_free( ssl->in_buf );
8286 mbedtls_free( ssl->out_buf );
8287
8288 ssl->conf = NULL;
8289
8290 ssl->in_buf = NULL;
8291 ssl->out_buf = NULL;
8292
8293 ssl->in_hdr = NULL;
8294 ssl->in_ctr = NULL;
8295 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008296 ssl->in_msg = NULL;
8297
8298 ssl->out_hdr = NULL;
8299 ssl->out_ctr = NULL;
8300 ssl->out_len = NULL;
8301 ssl->out_iv = NULL;
8302 ssl->out_msg = NULL;
8303
k-stachowiak9f7798e2018-07-31 16:52:32 +02008304 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008305}
8306
8307/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008308 * Reset an initialized and used SSL context for re-use while retaining
8309 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008310 *
8311 * If partial is non-zero, keep data in the input buffer and client ID.
8312 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008313 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008314static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008315{
Paul Bakker48916f92012-09-16 19:57:18 +00008316 int ret;
8317
Hanno Becker7e772132018-08-10 12:38:21 +01008318#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8319 !defined(MBEDTLS_SSL_SRV_C)
8320 ((void) partial);
8321#endif
8322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008324
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008325 /* Cancel any possibly running timer */
8326 ssl_set_timer( ssl, 0 );
8327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328#if defined(MBEDTLS_SSL_RENEGOTIATION)
8329 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008330 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008331
8332 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8334 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008337
Paul Bakker7eb013f2011-10-06 12:37:39 +00008338 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008339 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008340
8341 ssl->in_msgtype = 0;
8342 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008344 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008345 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008348 ssl_dtls_replay_reset( ssl );
8349#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008350
8351 ssl->in_hslen = 0;
8352 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008353
8354 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008355
8356 ssl->out_msgtype = 0;
8357 ssl->out_msglen = 0;
8358 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8360 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008361 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008362#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008363
Hanno Becker19859472018-08-06 09:40:20 +01008364 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8365
Paul Bakker48916f92012-09-16 19:57:18 +00008366 ssl->transform_in = NULL;
8367 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008368
Hanno Becker78640902018-08-13 16:35:15 +01008369 ssl->session_in = NULL;
8370 ssl->session_out = NULL;
8371
Angus Grattond8213d02016-05-25 20:56:48 +10008372 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008373
8374#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008375 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008376#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8377 {
8378 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008379 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008380 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8383 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8386 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8389 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008390 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008391 }
8392#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008393
Paul Bakker48916f92012-09-16 19:57:18 +00008394 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 mbedtls_ssl_transform_free( ssl->transform );
8397 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008398 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008399 }
Paul Bakker48916f92012-09-16 19:57:18 +00008400
Paul Bakkerc0463502013-02-14 11:19:38 +01008401 if( ssl->session )
8402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 mbedtls_ssl_session_free( ssl->session );
8404 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008405 ssl->session = NULL;
8406 }
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008409 ssl->alpn_chosen = NULL;
8410#endif
8411
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008412#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008413#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008414 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008415#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008416 {
8417 mbedtls_free( ssl->cli_id );
8418 ssl->cli_id = NULL;
8419 ssl->cli_id_len = 0;
8420 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008421#endif
8422
Paul Bakker48916f92012-09-16 19:57:18 +00008423 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8424 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008425
8426 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008427}
8428
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008429/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008430 * Reset an initialized and used SSL context for re-use while retaining
8431 * all application-set variables, function pointers and data.
8432 */
8433int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8434{
8435 return( ssl_session_reset_int( ssl, 0 ) );
8436}
8437
8438/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008439 * SSL set accessors
8440 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008441#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008442void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008444 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008445}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008446#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008447
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008448void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008450 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008451}
8452
Hanno Becker7f376f42019-06-12 16:20:48 +01008453#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8454 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008455void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008456{
Hanno Becker7f376f42019-06-12 16:20:48 +01008457 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008458}
Hanno Becker7f376f42019-06-12 16:20:48 +01008459#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008460
Hanno Beckerde671542019-06-12 16:30:46 +01008461#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8462 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8463void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8464 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008466 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008467}
Hanno Beckerde671542019-06-12 16:30:46 +01008468#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008471
Hanno Becker1841b0a2018-08-24 11:13:57 +01008472void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8473 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008474{
8475 ssl->disable_datagram_packing = !allow_packing;
8476}
8477
Hanno Becker1f835fa2019-06-13 10:14:59 +01008478#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8479 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008480void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8481 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008482{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008483 conf->hs_timeout_min = min;
8484 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008485}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008486#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8487 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8488void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8489 uint32_t min, uint32_t max )
8490{
8491 ((void) conf);
8492 ((void) min);
8493 ((void) max);
8494}
8495#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8496 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8497
8498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008499
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008500void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008501{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008502#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8503 conf->authmode = authmode;
8504#else
8505 ((void) conf);
8506 ((void) authmode);
8507#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008508}
8509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008511void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008512 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008513 void *p_vrfy )
8514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 conf->f_vrfy = f_vrfy;
8516 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008519
Hanno Beckerece325c2019-06-13 15:39:27 +01008520#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008522 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008523 void *p_rng )
8524{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008525 conf->f_rng = f_rng;
8526 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008527}
Hanno Beckerece325c2019-06-13 15:39:27 +01008528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008529
Hanno Becker14a4a442019-07-02 17:00:34 +01008530#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008531void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008532 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008533 void *p_dbg )
8534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008535 conf->f_dbg = f_dbg;
8536 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008537}
Hanno Becker14a4a442019-07-02 17:00:34 +01008538#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008539
Hanno Beckera58a8962019-06-13 16:11:15 +01008540#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8541 !defined(MBEDTLS_SSL_CONF_SEND) && \
8542 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008544 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008545 mbedtls_ssl_send_t *f_send,
8546 mbedtls_ssl_recv_t *f_recv,
8547 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008548{
Hanno Beckera58a8962019-06-13 16:11:15 +01008549 ssl->p_bio = p_bio;
8550 ssl->f_send = f_send;
8551 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008552 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008553}
Hanno Beckera58a8962019-06-13 16:11:15 +01008554#else
8555void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8556 void *p_bio )
8557{
8558 ssl->p_bio = p_bio;
8559}
8560#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008561
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008562#if defined(MBEDTLS_SSL_PROTO_DTLS)
8563void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8564{
8565 ssl->mtu = mtu;
8566}
8567#endif
8568
Hanno Becker1f835fa2019-06-13 10:14:59 +01008569#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008570void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008571{
8572 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008573}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008574#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008575
Hanno Becker0ae6b242019-06-13 16:45:36 +01008576#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8577 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008578void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8579 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008580 mbedtls_ssl_set_timer_t *f_set_timer,
8581 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008582{
8583 ssl->p_timer = p_timer;
8584 ssl->f_set_timer = f_set_timer;
8585 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008586 /* Make sure we start with no timer running */
8587 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008588}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008589#else
8590void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8591 void *p_timer )
8592{
8593 ssl->p_timer = p_timer;
8594 /* Make sure we start with no timer running */
8595 ssl_set_timer( ssl, 0 );
8596}
8597#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008598
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008599#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008600void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008601 void *p_cache,
8602 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8603 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008604{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008605 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008606 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008607 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008608}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008609#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008610
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008611#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008614 int ret;
8615
8616 if( ssl == NULL ||
8617 session == NULL ||
8618 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008619 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008622 }
8623
Hanno Becker58fccf22019-02-06 14:30:46 +00008624 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8625 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008626 return( ret );
8627
Paul Bakker0a597072012-09-25 21:55:46 +00008628 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008629
8630 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008631}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008632#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008633
Hanno Becker73f4cb12019-06-27 13:51:07 +01008634#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008635void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008636 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008638 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8639 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8640 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8641 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008642}
8643
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008644void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008645 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008646 int major, int minor )
8647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008649 return;
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008652 return;
8653
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008654 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008655}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008656#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008659void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008660 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008661{
8662 conf->cert_profile = profile;
8663}
8664
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008665/* Append a new keycert entry to a (possibly empty) list */
8666static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8667 mbedtls_x509_crt *cert,
8668 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008669{
niisato8ee24222018-06-25 19:05:48 +09008670 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008671
niisato8ee24222018-06-25 19:05:48 +09008672 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8673 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008674 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008675
niisato8ee24222018-06-25 19:05:48 +09008676 new_cert->cert = cert;
8677 new_cert->key = key;
8678 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008679
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008680 /* Update head is the list was null, else add to the end */
8681 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008682 {
niisato8ee24222018-06-25 19:05:48 +09008683 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008684 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008685 else
8686 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008687 mbedtls_ssl_key_cert *cur = *head;
8688 while( cur->next != NULL )
8689 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008690 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691 }
8692
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008693 return( 0 );
8694}
8695
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008696int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008697 mbedtls_x509_crt *own_cert,
8698 mbedtls_pk_context *pk_key )
8699{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008700 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008701}
8702
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008703void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008704 mbedtls_x509_crt *ca_chain,
8705 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008706{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008707 conf->ca_chain = ca_chain;
8708 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008711
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008712#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8713int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8714 mbedtls_x509_crt *own_cert,
8715 mbedtls_pk_context *pk_key )
8716{
8717 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8718 own_cert, pk_key ) );
8719}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008720
8721void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8722 mbedtls_x509_crt *ca_chain,
8723 mbedtls_x509_crl *ca_crl )
8724{
8725 ssl->handshake->sni_ca_chain = ca_chain;
8726 ssl->handshake->sni_ca_crl = ca_crl;
8727}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008728
8729void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8730 int authmode )
8731{
8732 ssl->handshake->sni_authmode = authmode;
8733}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008734#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8735
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008736#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008737/*
8738 * Set EC J-PAKE password for current handshake
8739 */
8740int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8741 const unsigned char *pw,
8742 size_t pw_len )
8743{
8744 mbedtls_ecjpake_role role;
8745
Janos Follath8eb64132016-06-03 15:40:57 +01008746 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8748
Hanno Becker2d9623f2019-06-13 12:07:05 +01008749 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008750 role = MBEDTLS_ECJPAKE_SERVER;
8751 else
8752 role = MBEDTLS_ECJPAKE_CLIENT;
8753
8754 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8755 role,
8756 MBEDTLS_MD_SHA256,
8757 MBEDTLS_ECP_DP_SECP256R1,
8758 pw, pw_len ) );
8759}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008760#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008763int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008764 const unsigned char *psk, size_t psk_len,
8765 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008766{
Paul Bakker6db455e2013-09-18 17:29:31 +02008767 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008772
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008773 /* Identity len will be encoded on two bytes */
8774 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008775 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008776 {
8777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8778 }
8779
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008780 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008781 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008782 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008783
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008784 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008785 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008786 conf->psk_len = 0;
8787 }
8788 if( conf->psk_identity != NULL )
8789 {
8790 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008791 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008792 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008793 }
8794
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008795 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8796 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008797 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008798 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008799 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008800 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008801 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008802 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008803 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008804
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008805 conf->psk_len = psk_len;
8806 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008807
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008808 memcpy( conf->psk, psk, conf->psk_len );
8809 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008810
8811 return( 0 );
8812}
8813
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008814int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8815 const unsigned char *psk, size_t psk_len )
8816{
8817 if( psk == NULL || ssl->handshake == NULL )
8818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8819
8820 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8822
8823 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008824 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008825 mbedtls_platform_zeroize( ssl->handshake->psk,
8826 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008827 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008828 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008829 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008830
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008831 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008833
8834 ssl->handshake->psk_len = psk_len;
8835 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8836
8837 return( 0 );
8838}
8839
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008840void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008842 size_t),
8843 void *p_psk )
8844{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008845 conf->f_psk = f_psk;
8846 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008849
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008850#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008851
8852#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008853int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008854{
8855 int ret;
8856
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008857 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8858 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8859 {
8860 mbedtls_mpi_free( &conf->dhm_P );
8861 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008862 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008863 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008864
8865 return( 0 );
8866}
Hanno Becker470a8c42017-10-04 15:28:46 +01008867#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008868
Hanno Beckera90658f2017-10-04 15:29:08 +01008869int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8870 const unsigned char *dhm_P, size_t P_len,
8871 const unsigned char *dhm_G, size_t G_len )
8872{
8873 int ret;
8874
8875 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8876 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8877 {
8878 mbedtls_mpi_free( &conf->dhm_P );
8879 mbedtls_mpi_free( &conf->dhm_G );
8880 return( ret );
8881 }
8882
8883 return( 0 );
8884}
Paul Bakker5121ce52009-01-03 21:22:43 +00008885
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008886int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008887{
8888 int ret;
8889
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008890 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8891 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8892 {
8893 mbedtls_mpi_free( &conf->dhm_P );
8894 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008895 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008896 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008897
8898 return( 0 );
8899}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008900#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008901
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008902#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8903/*
8904 * Set the minimum length for Diffie-Hellman parameters
8905 */
8906void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8907 unsigned int bitlen )
8908{
8909 conf->dhm_min_bitlen = bitlen;
8910}
8911#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8912
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008913#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008914/*
8915 * Set allowed/preferred hashes for handshake signatures
8916 */
8917void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8918 const int *hashes )
8919{
Hanno Becker56595f42019-06-19 16:31:38 +01008920#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008921 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008922#else
8923 ((void) conf);
8924 ((void) hashes);
8925#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008926}
Hanno Becker947194e2017-04-07 13:25:49 +01008927#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008928
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008929#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008930#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008931/*
8932 * Set the allowed elliptic curves
8933 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008934void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008935 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008937 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008938}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008939#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008940#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008941
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008942#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008944{
Hanno Becker947194e2017-04-07 13:25:49 +01008945 /* Initialize to suppress unnecessary compiler warning */
8946 size_t hostname_len = 0;
8947
8948 /* Check if new hostname is valid before
8949 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008950 if( hostname != NULL )
8951 {
8952 hostname_len = strlen( hostname );
8953
8954 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8956 }
8957
8958 /* Now it's clear that we will overwrite the old hostname,
8959 * so we can free it safely */
8960
8961 if( ssl->hostname != NULL )
8962 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008963 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008964 mbedtls_free( ssl->hostname );
8965 }
8966
8967 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008968
Paul Bakker5121ce52009-01-03 21:22:43 +00008969 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008970 {
8971 ssl->hostname = NULL;
8972 }
8973 else
8974 {
8975 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008976 if( ssl->hostname == NULL )
8977 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008978
Hanno Becker947194e2017-04-07 13:25:49 +01008979 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008980
Hanno Becker947194e2017-04-07 13:25:49 +01008981 ssl->hostname[hostname_len] = '\0';
8982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008983
8984 return( 0 );
8985}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008986#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008988#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008989void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008991 const unsigned char *, size_t),
8992 void *p_sni )
8993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008994 conf->f_sni = f_sni;
8995 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009000int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009001{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009002 size_t cur_len, tot_len;
9003 const char **p;
9004
9005 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009006 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9007 * MUST NOT be truncated."
9008 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009009 */
9010 tot_len = 0;
9011 for( p = protos; *p != NULL; p++ )
9012 {
9013 cur_len = strlen( *p );
9014 tot_len += cur_len;
9015
9016 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009018 }
9019
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009020 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009021
9022 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009023}
9024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009026{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009027 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009030
Hanno Becker33b9b252019-07-05 11:23:25 +01009031#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9032 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9033void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9034 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009035{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009036 conf->max_major_ver = major;
9037 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009038}
Hanno Becker33b9b252019-07-05 11:23:25 +01009039#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9040 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009041
Hanno Becker33b9b252019-07-05 11:23:25 +01009042#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9043 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9044void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9045 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009047 conf->min_major_ver = major;
9048 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009049}
Hanno Becker33b9b252019-07-05 11:23:25 +01009050#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9051 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009054void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009055{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009056 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009057}
9058#endif
9059
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009060#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009061void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9062 char cert_req_ca_list )
9063{
9064 conf->cert_req_ca_list = cert_req_ca_list;
9065}
9066#endif
9067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009069void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009071 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009072}
9073#endif
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009076#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009080}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009081#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9082#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009083void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009084 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009085{
9086 conf->enforce_extended_master_secret = ems_enf;
9087}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009088#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009089#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009090
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009091#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009094 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009095}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009096#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009099int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009102 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009105 }
9106
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009107 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009108
9109 return( 0 );
9110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009114void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009116 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009122{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009123 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009124}
9125#endif
9126
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009127#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009130 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009131}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009132#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009137 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009138}
9139
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009140void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009142 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009143}
9144
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009145void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009146 const unsigned char period[8] )
9147{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009148 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009152#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009153#if defined(MBEDTLS_SSL_CLI_C)
9154void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009155{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009156 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009157}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009158#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009159
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009160#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009161void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9162 mbedtls_ssl_ticket_write_t *f_ticket_write,
9163 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9164 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009165{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009166 conf->f_ticket_write = f_ticket_write;
9167 conf->f_ticket_parse = f_ticket_parse;
9168 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009169}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009172
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009173#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9174void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9175 mbedtls_ssl_export_keys_t *f_export_keys,
9176 void *p_export_keys )
9177{
9178 conf->f_export_keys = f_export_keys;
9179 conf->p_export_keys = p_export_keys;
9180}
9181#endif
9182
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009183#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009184void mbedtls_ssl_conf_async_private_cb(
9185 mbedtls_ssl_config *conf,
9186 mbedtls_ssl_async_sign_t *f_async_sign,
9187 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9188 mbedtls_ssl_async_resume_t *f_async_resume,
9189 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009191{
9192 conf->f_async_sign_start = f_async_sign;
9193 conf->f_async_decrypt_start = f_async_decrypt;
9194 conf->f_async_resume = f_async_resume;
9195 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009196 conf->p_async_config_data = async_config_data;
9197}
9198
Gilles Peskine8f97af72018-04-26 11:46:10 +02009199void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9200{
9201 return( conf->p_async_config_data );
9202}
9203
Gilles Peskine1febfef2018-04-30 11:54:39 +02009204void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009205{
9206 if( ssl->handshake == NULL )
9207 return( NULL );
9208 else
9209 return( ssl->handshake->user_async_ctx );
9210}
9211
Gilles Peskine1febfef2018-04-30 11:54:39 +02009212void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009213 void *ctx )
9214{
9215 if( ssl->handshake != NULL )
9216 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009217}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009218#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009219
Paul Bakker5121ce52009-01-03 21:22:43 +00009220/*
9221 * SSL get accessors
9222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009224{
9225 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9226}
9227
Hanno Becker8b170a02017-10-10 11:51:19 +01009228int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9229{
9230 /*
9231 * Case A: We're currently holding back
9232 * a message for further processing.
9233 */
9234
9235 if( ssl->keep_current_message == 1 )
9236 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009238 return( 1 );
9239 }
9240
9241 /*
9242 * Case B: Further records are pending in the current datagram.
9243 */
9244
9245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009246 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009247 ssl->in_left > ssl->next_record_offset )
9248 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009250 return( 1 );
9251 }
9252#endif /* MBEDTLS_SSL_PROTO_DTLS */
9253
9254 /*
9255 * Case C: A handshake message is being processed.
9256 */
9257
Hanno Becker8b170a02017-10-10 11:51:19 +01009258 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9259 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 return( 1 );
9262 }
9263
9264 /*
9265 * Case D: An application data message is being processed
9266 */
9267 if( ssl->in_offt != NULL )
9268 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009270 return( 1 );
9271 }
9272
9273 /*
9274 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009275 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009276 * we implement support for multiple alerts in single records.
9277 */
9278
9279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9280 return( 0 );
9281}
9282
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009283uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009284{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009285 if( ssl->session != NULL )
9286 return( ssl->session->verify_result );
9287
9288 if( ssl->session_negotiate != NULL )
9289 return( ssl->session_negotiate->verify_result );
9290
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009291 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009292}
9293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009295{
Hanno Beckere02758c2019-06-26 15:31:31 +01009296 int suite;
9297
Paul Bakker926c8e42013-03-06 10:23:34 +01009298 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009299 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009300
Hanno Beckere02758c2019-06-26 15:31:31 +01009301 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9302 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009303}
9304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009308 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009309 {
Hanno Becker2881d802019-05-22 14:44:53 +01009310 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009313 return( "DTLSv1.0" );
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009316 return( "DTLSv1.2" );
9317
9318 default:
9319 return( "unknown (DTLS)" );
9320 }
9321 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009322 MBEDTLS_SSL_TRANSPORT_ELSE
9323#endif /* MBEDTLS_SSL_PROTO_DTLS */
9324#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009325 {
Hanno Becker2881d802019-05-22 14:44:53 +01009326 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009327 {
9328 case MBEDTLS_SSL_MINOR_VERSION_0:
9329 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009331 case MBEDTLS_SSL_MINOR_VERSION_1:
9332 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009333
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009334 case MBEDTLS_SSL_MINOR_VERSION_2:
9335 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009337 case MBEDTLS_SSL_MINOR_VERSION_3:
9338 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009339
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009340 default:
9341 return( "unknown" );
9342 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009343 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009344#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009345}
9346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009348{
Hanno Becker3136ede2018-08-17 15:28:19 +01009349 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009351
Hanno Becker43395762019-05-03 14:46:38 +01009352 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9353
Hanno Becker78640902018-08-13 16:35:15 +01009354 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009355 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_ZLIB_SUPPORT)
9358 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9359 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009360#endif
9361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009363 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009364#if defined(MBEDTLS_GCM_C) || \
9365 defined(MBEDTLS_CCM_C) || \
9366 defined(MBEDTLS_CHACHAPOLY_C)
9367#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009369#endif
9370#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009372#endif
9373#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009374 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009375#endif
9376 transform_expansion =
9377 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009378 break;
9379
Hanno Beckera9d5c452019-07-25 16:47:12 +01009380#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9381 MBEDTLS_CHACHAPOLY_C */
9382
9383#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9384 case MBEDTLS_MODE_STREAM:
9385 transform_expansion = transform->maclen;
9386 break;
9387#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9388
9389#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009391 {
9392 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009393
9394 block_size = mbedtls_cipher_get_block_size(
9395 &transform->cipher_ctx_enc );
9396
Hanno Becker3136ede2018-08-17 15:28:19 +01009397 /* Expansion due to the addition of the MAC. */
9398 transform_expansion += transform->maclen;
9399
9400 /* Expansion due to the addition of CBC padding;
9401 * Theoretically up to 256 bytes, but we never use
9402 * more than the block size of the underlying cipher. */
9403 transform_expansion += block_size;
9404
9405 /* For TLS 1.1 or higher, an explicit IV is added
9406 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009407#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009408 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009409 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009410#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009411
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009412 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009413 }
9414#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009415
9416 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009419 }
9420
Hanno Beckera5a2b082019-05-15 14:03:01 +01009421#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009422 if( transform->out_cid_len != 0 )
9423 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009424#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009425
Hanno Becker43395762019-05-03 14:46:38 +01009426 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009427}
9428
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009429#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9430size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9431{
9432 size_t max_len;
9433
9434 /*
9435 * Assume mfl_code is correct since it was checked when set
9436 */
Angus Grattond8213d02016-05-25 20:56:48 +10009437 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009438
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009439 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009440 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009441 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009442 {
Angus Grattond8213d02016-05-25 20:56:48 +10009443 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009444 }
9445
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009446 /* During a handshake, use the value being negotiated */
9447 if( ssl->session_negotiate != NULL &&
9448 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9449 {
9450 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9451 }
9452
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009453 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009454}
9455#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9456
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009457#if defined(MBEDTLS_SSL_PROTO_DTLS)
9458static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9459{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009460 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009461 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009462 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9463 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9464 return ( 0 );
9465
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009466 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9467 return( ssl->mtu );
9468
9469 if( ssl->mtu == 0 )
9470 return( ssl->handshake->mtu );
9471
9472 return( ssl->mtu < ssl->handshake->mtu ?
9473 ssl->mtu : ssl->handshake->mtu );
9474}
9475#endif /* MBEDTLS_SSL_PROTO_DTLS */
9476
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009477int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9478{
9479 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9480
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009481#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9482 !defined(MBEDTLS_SSL_PROTO_DTLS)
9483 (void) ssl;
9484#endif
9485
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9487 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9488
9489 if( max_len > mfl )
9490 max_len = mfl;
9491#endif
9492
9493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009494 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009495 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009496 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009497 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9498 const size_t overhead = (size_t) ret;
9499
9500 if( ret < 0 )
9501 return( ret );
9502
9503 if( mtu <= overhead )
9504 {
9505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9506 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9507 }
9508
9509 if( max_len > mtu - overhead )
9510 max_len = mtu - overhead;
9511 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009512#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009513
Hanno Becker0defedb2018-08-10 12:35:02 +01009514#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9515 !defined(MBEDTLS_SSL_PROTO_DTLS)
9516 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009517#endif
9518
9519 return( (int) max_len );
9520}
9521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#if defined(MBEDTLS_X509_CRT_PARSE_C)
9523const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009524{
9525 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009526 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009527
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009528#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009529 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009530#else
9531 return( NULL );
9532#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009537int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9538 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009539{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009540 if( ssl == NULL ||
9541 dst == NULL ||
9542 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009543 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009546 }
9547
Hanno Becker58fccf22019-02-06 14:30:46 +00009548 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009551
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009552const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9553{
9554 if( ssl == NULL )
9555 return( NULL );
9556
9557 return( ssl->session );
9558}
9559
Paul Bakker5121ce52009-01-03 21:22:43 +00009560/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009561 * Define ticket header determining Mbed TLS version
9562 * and structure of the ticket.
9563 */
9564
Hanno Becker41527622019-05-16 12:50:45 +01009565/*
Hanno Becker26829e92019-05-28 14:30:45 +01009566 * Define bitflag determining compile-time settings influencing
9567 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009568 */
9569
Hanno Becker26829e92019-05-28 14:30:45 +01009570#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009571#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009572#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009574#endif /* MBEDTLS_HAVE_TIME */
9575
9576#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009577#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009578#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009580#endif /* MBEDTLS_X509_CRT_PARSE_C */
9581
9582#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009583#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009584#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009585#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009586#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9587
9588#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009589#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009590#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009591#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009592#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9593
9594#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009595#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009596#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009597#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009598#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9599
9600#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009601#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009602#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009603#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009604#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9605
Hanno Becker41527622019-05-16 12:50:45 +01009606#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9607#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9608#else
9609#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9610#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9611
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009612#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9613#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9614#else
9615#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9616#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9617
Hanno Becker88440552019-07-03 14:16:13 +01009618#if defined(MBEDTLS_ZLIB_SUPPORT)
9619#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9620#else
9621#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9622#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9623
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009624#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9625#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9626#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9627#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9628#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9629#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9630#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009631#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009632#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009633
Hanno Becker26829e92019-05-28 14:30:45 +01009634#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009635 ( (uint16_t) ( \
9636 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9637 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9638 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9639 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9640 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9641 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009642 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009643 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009644 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009645
Hanno Becker557fe9f2019-05-16 12:41:07 +01009646static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009647 MBEDTLS_VERSION_MAJOR,
9648 MBEDTLS_VERSION_MINOR,
9649 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009650 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9651 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009652};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009653
9654/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009655 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009656 * (in the presentation language of TLS, RFC 8446 section 3)
9657 *
Hanno Becker26829e92019-05-28 14:30:45 +01009658 * opaque mbedtls_version[3]; // major, minor, patch
9659 * opaque session_format[2]; // version-specific 16-bit field determining
9660 * // the format of the remaining
9661 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009662 *
9663 * Note: When updating the format, remember to keep
9664 * these version+format bytes.
9665 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009666 * // In this version, `session_format` determines
9667 * // the setting of those compile-time
9668 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009669 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009670 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009671 * uint8 ciphersuite[2]; // defined by the standard
9672 * uint8 compression; // 0 or 1
9673 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009674 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009675 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009676 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009677 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9678 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9679 * case disabled: uint8_t peer_cert_digest_type;
9680 * opaque peer_cert_digest<0..2^8-1>;
9681 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009682 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009683 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009684 * uint8 mfl_code; // up to 255 according to standard
9685 * uint8 trunc_hmac; // 0 or 1
9686 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009687 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009688 * The order is the same as in the definition of the structure, except
9689 * verify_result is put before peer_cert so that all mandatory fields come
9690 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009691 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009692static int ssl_session_save( const mbedtls_ssl_session *session,
9693 unsigned char omit_header,
9694 unsigned char *buf,
9695 size_t buf_len,
9696 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009697{
9698 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009699 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009700#if defined(MBEDTLS_HAVE_TIME)
9701 uint64_t start;
9702#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009703#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009704#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009705 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009706#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009707#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009708
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009709 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009710 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009711 /*
9712 * Add version identifier
9713 */
9714
9715 used += sizeof( ssl_serialized_session_header );
9716
9717 if( used <= buf_len )
9718 {
9719 memcpy( p, ssl_serialized_session_header,
9720 sizeof( ssl_serialized_session_header ) );
9721 p += sizeof( ssl_serialized_session_header );
9722 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009723 }
9724
9725 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009726 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009727 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009728#if defined(MBEDTLS_HAVE_TIME)
9729 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009730
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009731 if( used <= buf_len )
9732 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009733 start = (uint64_t) session->start;
9734
9735 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9736 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9737 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9738 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9739 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9740 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9741 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9742 *p++ = (unsigned char)( ( start ) & 0xFF );
9743 }
9744#endif /* MBEDTLS_HAVE_TIME */
9745
9746 /*
9747 * Basic mandatory fields
9748 */
Hanno Becker88440552019-07-03 14:16:13 +01009749 {
9750 size_t const ciphersuite_len = 2;
9751#if defined(MBEDTLS_ZLIB_SUPPORT)
9752 size_t const compression_len = 1;
9753#else
9754 size_t const compression_len = 0;
9755#endif
9756 size_t const id_len_len = 1;
9757 size_t const id_len = 32;
9758 size_t const master_len = 48;
9759 size_t const verif_result_len = 4;
9760
9761 size_t const basic_len =
9762 ciphersuite_len +
9763 compression_len +
9764 id_len_len +
9765 id_len +
9766 master_len +
9767 verif_result_len;
9768
9769 used += basic_len;
9770 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009771
9772 if( used <= buf_len )
9773 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009774 const int ciphersuite =
9775 mbedtls_ssl_session_get_ciphersuite( session );
9776 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9777 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009778
Hanno Becker88440552019-07-03 14:16:13 +01009779#if defined(MBEDTLS_ZLIB_SUPPORT)
9780 *p++ = (unsigned char)(
9781 mbedtls_ssl_session_get_compression( session ) );
9782#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009783
9784 *p++ = (unsigned char)( session->id_len & 0xFF );
9785 memcpy( p, session->id, 32 );
9786 p += 32;
9787
9788 memcpy( p, session->master, 48 );
9789 p += 48;
9790
9791 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9792 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9793 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9794 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009795 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009796
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009797 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009798 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009799 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009800#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009801#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009802 if( session->peer_cert == NULL )
9803 cert_len = 0;
9804 else
9805 cert_len = session->peer_cert->raw.len;
9806
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009807 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009808
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009809 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009810 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009811 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9812 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9813 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9814
9815 if( session->peer_cert != NULL )
9816 {
9817 memcpy( p, session->peer_cert->raw.p, cert_len );
9818 p += cert_len;
9819 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009820 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009821
Hanno Becker5882dd02019-06-06 16:25:57 +01009822#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009823 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009824 if( session->peer_cert_digest != NULL )
9825 {
9826 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9827 if( used <= buf_len )
9828 {
9829 *p++ = (unsigned char) session->peer_cert_digest_type;
9830 *p++ = (unsigned char) session->peer_cert_digest_len;
9831 memcpy( p, session->peer_cert_digest,
9832 session->peer_cert_digest_len );
9833 p += session->peer_cert_digest_len;
9834 }
9835 }
9836 else
9837 {
9838 used += 2;
9839 if( used <= buf_len )
9840 {
9841 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9842 *p++ = 0;
9843 }
9844 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009845#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009846#endif /* MBEDTLS_X509_CRT_PARSE_C */
9847
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009848 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009849 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009850 */
9851#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009852 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009853
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009854 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009855 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009856 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9857 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9858 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9859
9860 if( session->ticket != NULL )
9861 {
9862 memcpy( p, session->ticket, session->ticket_len );
9863 p += session->ticket_len;
9864 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009865
9866 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9867 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9868 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9869 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009870 }
9871#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9872
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009873 /*
9874 * Misc extension-related info
9875 */
9876#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9877 used += 1;
9878
9879 if( used <= buf_len )
9880 *p++ = session->mfl_code;
9881#endif
9882
9883#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9884 used += 1;
9885
9886 if( used <= buf_len )
9887 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9888#endif
9889
9890#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9891 used += 1;
9892
9893 if( used <= buf_len )
9894 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9895#endif
9896
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009897 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009898 *olen = used;
9899
9900 if( used > buf_len )
9901 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009902
9903 return( 0 );
9904}
9905
9906/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009907 * Public wrapper for ssl_session_save()
9908 */
9909int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9910 unsigned char *buf,
9911 size_t buf_len,
9912 size_t *olen )
9913{
9914 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9915}
9916
9917/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009918 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009919 *
9920 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009921 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009922 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009923static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009924 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009925 const unsigned char *buf,
9926 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009927{
9928 const unsigned char *p = buf;
9929 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009930 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009931#if defined(MBEDTLS_HAVE_TIME)
9932 uint64_t start;
9933#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009934#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009935#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009936 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009937#endif
9938#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009939
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009940 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009941 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009942 /*
9943 * Check version identifier
9944 */
9945
9946 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9948
9949 if( memcmp( p, ssl_serialized_session_header,
9950 sizeof( ssl_serialized_session_header ) ) != 0 )
9951 {
9952 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9953 }
9954 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009955 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009956
9957 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009958 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009959 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009960#if defined(MBEDTLS_HAVE_TIME)
9961 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9963
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009964 start = ( (uint64_t) p[0] << 56 ) |
9965 ( (uint64_t) p[1] << 48 ) |
9966 ( (uint64_t) p[2] << 40 ) |
9967 ( (uint64_t) p[3] << 32 ) |
9968 ( (uint64_t) p[4] << 24 ) |
9969 ( (uint64_t) p[5] << 16 ) |
9970 ( (uint64_t) p[6] << 8 ) |
9971 ( (uint64_t) p[7] );
9972 p += 8;
9973
9974 session->start = (time_t) start;
9975#endif /* MBEDTLS_HAVE_TIME */
9976
9977 /*
9978 * Basic mandatory fields
9979 */
Hanno Becker88440552019-07-03 14:16:13 +01009980 {
9981 size_t const ciphersuite_len = 2;
9982#if defined(MBEDTLS_ZLIB_SUPPORT)
9983 size_t const compression_len = 1;
9984#else
9985 size_t const compression_len = 0;
9986#endif
9987 size_t const id_len_len = 1;
9988 size_t const id_len = 32;
9989 size_t const master_len = 48;
9990 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009991
Hanno Becker88440552019-07-03 14:16:13 +01009992 size_t const basic_len =
9993 ciphersuite_len +
9994 compression_len +
9995 id_len_len +
9996 id_len +
9997 master_len +
9998 verif_result_len;
9999
10000 if( basic_len > (size_t)( end - p ) )
10001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10002 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010003
Hanno Beckere02758c2019-06-26 15:31:31 +010010004 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010005 p += 2;
10006
Hanno Becker73f4cb12019-06-27 13:51:07 +010010007#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010008 session->ciphersuite = ciphersuite;
10009#else
10010 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010011 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010012 {
10013 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10014 }
10015#endif
10016
Hanno Becker88440552019-07-03 14:16:13 +010010017#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010018 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010019#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010020
10021 session->id_len = *p++;
10022 memcpy( session->id, p, 32 );
10023 p += 32;
10024
10025 memcpy( session->master, p, 48 );
10026 p += 48;
10027
10028 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10029 ( (uint32_t) p[1] << 16 ) |
10030 ( (uint32_t) p[2] << 8 ) |
10031 ( (uint32_t) p[3] );
10032 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010033
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010034 /* Immediately clear invalid pointer values that have been read, in case
10035 * we exit early before we replaced them with valid ones. */
10036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010037#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010038 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010039#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010040 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010041#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010042#endif
10043#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10044 session->ticket = NULL;
10045#endif
10046
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010047 /*
10048 * Peer certificate
10049 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010050#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010051#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010052 if( 3 > (size_t)( end - p ) )
10053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10054
10055 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10056 p += 3;
10057
10058 if( cert_len == 0 )
10059 {
10060 session->peer_cert = NULL;
10061 }
10062 else
10063 {
10064 int ret;
10065
10066 if( cert_len > (size_t)( end - p ) )
10067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10068
10069 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10070
10071 if( session->peer_cert == NULL )
10072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10073
10074 mbedtls_x509_crt_init( session->peer_cert );
10075
10076 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10077 p, cert_len ) ) != 0 )
10078 {
10079 mbedtls_x509_crt_free( session->peer_cert );
10080 mbedtls_free( session->peer_cert );
10081 session->peer_cert = NULL;
10082 return( ret );
10083 }
10084
10085 p += cert_len;
10086 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010087#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010088 /* Deserialize CRT digest from the end of the ticket. */
10089 if( 2 > (size_t)( end - p ) )
10090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10091
10092 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10093 session->peer_cert_digest_len = (size_t) *p++;
10094
10095 if( session->peer_cert_digest_len != 0 )
10096 {
Hanno Becker2326d202019-06-06 14:54:55 +010010097 const mbedtls_md_info_t *md_info =
10098 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10099 if( md_info == NULL )
10100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10101 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10103
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010104 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10106
10107 session->peer_cert_digest =
10108 mbedtls_calloc( 1, session->peer_cert_digest_len );
10109 if( session->peer_cert_digest == NULL )
10110 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10111
10112 memcpy( session->peer_cert_digest, p,
10113 session->peer_cert_digest_len );
10114 p += session->peer_cert_digest_len;
10115 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010116#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010117#endif /* MBEDTLS_X509_CRT_PARSE_C */
10118
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010119 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010120 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010121 */
10122#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10123 if( 3 > (size_t)( end - p ) )
10124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10125
10126 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10127 p += 3;
10128
10129 if( session->ticket_len != 0 )
10130 {
10131 if( session->ticket_len > (size_t)( end - p ) )
10132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10133
10134 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10135 if( session->ticket == NULL )
10136 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10137
10138 memcpy( session->ticket, p, session->ticket_len );
10139 p += session->ticket_len;
10140 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010141
10142 if( 4 > (size_t)( end - p ) )
10143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10144
10145 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10146 ( (uint32_t) p[1] << 16 ) |
10147 ( (uint32_t) p[2] << 8 ) |
10148 ( (uint32_t) p[3] );
10149 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010150#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10151
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010152 /*
10153 * Misc extension-related info
10154 */
10155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10156 if( 1 > (size_t)( end - p ) )
10157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10158
10159 session->mfl_code = *p++;
10160#endif
10161
10162#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10163 if( 1 > (size_t)( end - p ) )
10164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10165
10166 session->trunc_hmac = *p++;
10167#endif
10168
10169#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10170 if( 1 > (size_t)( end - p ) )
10171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10172
10173 session->encrypt_then_mac = *p++;
10174#endif
10175
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010176 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010177 if( p != end )
10178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10179
10180 return( 0 );
10181}
10182
10183/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010184 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010185 */
10186int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10187 const unsigned char *buf,
10188 size_t len )
10189{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010190 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010191
10192 if( ret != 0 )
10193 mbedtls_ssl_session_free( session );
10194
10195 return( ret );
10196}
10197
10198/*
Paul Bakker1961b702013-01-25 14:49:24 +010010199 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010204
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010205 if( ssl == NULL || ssl->conf == NULL )
10206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010209 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010213 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010215#endif
10216
Hanno Beckerb82350b2019-07-26 07:24:05 +010010217 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010218 return( ret );
10219}
10220
10221/*
10222 * Perform the SSL handshake
10223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010225{
10226 int ret = 0;
10227
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010228 if( ssl == NULL || ssl->conf == NULL )
10229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010236
10237 if( ret != 0 )
10238 break;
10239 }
10240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010242
10243 return( ret );
10244}
10245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246#if defined(MBEDTLS_SSL_RENEGOTIATION)
10247#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010248/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010249 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010252{
10253 int ret;
10254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010256
10257 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10259 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010260
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010261 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010262 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010264 return( ret );
10265 }
10266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010268
10269 return( 0 );
10270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010272
10273/*
10274 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 * - any side: calling mbedtls_ssl_renegotiate(),
10276 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10277 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010278 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010279 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010283{
10284 int ret;
10285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010287
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010288 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10289 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010290
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010291 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10292 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010294 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010296 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010297 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10298 MBEDTLS_SSL_IS_SERVER )
10299 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010300 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010301 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010302 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010303 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010304 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010305 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010306 }
10307#endif
10308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10310 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010315 return( ret );
10316 }
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010319
10320 return( 0 );
10321}
10322
10323/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010324 * Renegotiate current connection on client,
10325 * or request renegotiation on server
10326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010330
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010331 if( ssl == NULL || ssl->conf == NULL )
10332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010335 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010336 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010342
10343 /* Did we already try/start sending HelloRequest? */
10344 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010346
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010347 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010352 /*
10353 * On client, either start the renegotiation process or,
10354 * if already in progress, continue the handshake
10355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360
10361 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010364 return( ret );
10365 }
10366 }
10367 else
10368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010372 return( ret );
10373 }
10374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010376
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010377 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010378}
10379
10380/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010381 * Check record counters and renegotiate if they're above the limit.
10382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010384{
Andres AG2196c7f2016-12-15 17:01:16 +000010385 size_t ep_len = ssl_ep_len( ssl );
10386 int in_ctr_cmp;
10387 int out_ctr_cmp;
10388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10390 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010391 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010392 {
10393 return( 0 );
10394 }
10395
Andres AG2196c7f2016-12-15 17:01:16 +000010396 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10397 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010398 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010399 ssl->conf->renego_period + ep_len, 8 - ep_len );
10400
10401 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010402 {
10403 return( 0 );
10404 }
10405
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010410
10411/*
10412 * Receive application data decrypted from the SSL layer
10413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010415{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010416 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010417 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010418
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010419 if( ssl == NULL || ssl->conf == NULL )
10420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010425 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010428 return( ret );
10429
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010430 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010432 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010433 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010434 return( ret );
10435 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010436 }
10437#endif
10438
Hanno Becker4a810fb2017-05-24 16:27:30 +010010439 /*
10440 * Check if renegotiation is necessary and/or handshake is
10441 * in process. If yes, perform/continue, and fall through
10442 * if an unexpected packet is received while the client
10443 * is waiting for the ServerHello.
10444 *
10445 * (There is no equivalent to the last condition on
10446 * the server-side as it is not treated as within
10447 * a handshake while waiting for the ClientHello
10448 * after a renegotiation request.)
10449 */
10450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010452 ret = ssl_check_ctr_renegotiate( ssl );
10453 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10454 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010457 return( ret );
10458 }
10459#endif
10460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010464 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10465 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010468 return( ret );
10469 }
10470 }
10471
Hanno Beckere41158b2017-10-23 13:30:32 +010010472 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010473 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010475 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010476 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10477 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010478 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010479 ssl_set_timer( ssl,
10480 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010481 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010482
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 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010485 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10486 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010487
Hanno Becker4a810fb2017-05-24 16:27:30 +010010488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10489 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010490 }
10491
10492 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 {
10495 /*
10496 * OpenSSL sends empty messages to randomize the IV
10497 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010498 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010501 return( 0 );
10502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010504 return( ret );
10505 }
10506 }
10507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010511
Hanno Becker4a810fb2017-05-24 16:27:30 +010010512 /*
10513 * - For client-side, expect SERVER_HELLO_REQUEST.
10514 * - For server-side, expect CLIENT_HELLO.
10515 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10516 */
10517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010519 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10520 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010522 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010525
10526 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010528 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010529 {
10530 continue;
10531 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010532 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010533#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010534#if defined(MBEDTLS_SSL_PROTO_TLS)
10535 {
10536 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10537 }
10538#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010539 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010540#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010541
Hanno Becker4a810fb2017-05-24 16:27:30 +010010542#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010543 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10544 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010545 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010548
10549 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010551 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010552 {
10553 continue;
10554 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010555 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010556#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010557#if defined(MBEDTLS_SSL_PROTO_TLS)
10558 {
10559 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10560 }
10561#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010562 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010563#endif /* MBEDTLS_SSL_SRV_C */
10564
Hanno Becker21df7f92017-10-17 11:03:26 +010010565#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010566 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010567 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10568 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010569 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010570 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10571 {
10572 /*
10573 * Accept renegotiation request
10574 */
Paul Bakker48916f92012-09-16 19:57:18 +000010575
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010576 /* DTLS clients need to know renego is server-initiated */
10577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010578 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010579 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10580 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010581 {
10582 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10583 }
10584#endif
10585 ret = ssl_start_renegotiation( ssl );
10586 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10587 ret != 0 )
10588 {
10589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10590 return( ret );
10591 }
10592 }
10593 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010594#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010595 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010596 /*
10597 * Refuse renegotiation
10598 */
10599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010603 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010604 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010605 /* SSLv3 does not have a "no_renegotiation" warning, so
10606 we send a fatal alert and abort the connection. */
10607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10608 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10609 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010610 }
10611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10613#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10614 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010615 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010616 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010617 ret = mbedtls_ssl_send_alert_message( ssl,
10618 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10619 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10620 if( ret != 0 )
10621 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10625 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010629 }
Paul Bakker48916f92012-09-16 19:57:18 +000010630 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010631
Hanno Becker90333da2017-10-10 11:27:13 +010010632 /* At this point, we don't know whether the renegotiation has been
10633 * completed or not. The cases to consider are the following:
10634 * 1) The renegotiation is complete. In this case, no new record
10635 * has been read yet.
10636 * 2) The renegotiation is incomplete because the client received
10637 * an application data record while awaiting the ServerHello.
10638 * 3) The renegotiation is incomplete because the client received
10639 * a non-handshake, non-application data message while awaiting
10640 * the ServerHello.
10641 * In each of these case, looping will be the proper action:
10642 * - For 1), the next iteration will read a new record and check
10643 * if it's application data.
10644 * - For 2), the loop condition isn't satisfied as application data
10645 * is present, hence continue is the same as break
10646 * - For 3), the loop condition is satisfied and read_record
10647 * will re-deliver the message that was held back by the client
10648 * when expecting the ServerHello.
10649 */
10650 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010651 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010652#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010654 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010655 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010656 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010657 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010660 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010662 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010663 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10668 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010671 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010672 }
10673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10677 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010678 }
10679
10680 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010681
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010682 /* We're going to return something now, cancel timer,
10683 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010685 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010686
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010688 /* If we requested renego but received AppData, resend HelloRequest.
10689 * Do it now, after setting in_offt, to avoid taking this branch
10690 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010692 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10693 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010695 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010696 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010699 return( ret );
10700 }
10701 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010703#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010704 }
10705
10706 n = ( len < ssl->in_msglen )
10707 ? len : ssl->in_msglen;
10708
10709 memcpy( buf, ssl->in_offt, n );
10710 ssl->in_msglen -= n;
10711
10712 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010713 {
10714 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010715 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010716 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010717 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010718 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010719 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010720 /* more data available */
10721 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010722 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010725
Paul Bakker23986e52011-04-24 08:57:21 +000010726 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010727}
10728
10729/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010730 * Send application data to be encrypted by the SSL layer, taking care of max
10731 * fragment length and buffer size.
10732 *
10733 * According to RFC 5246 Section 6.2.1:
10734 *
10735 * Zero-length fragments of Application data MAY be sent as they are
10736 * potentially useful as a traffic analysis countermeasure.
10737 *
10738 * Therefore, it is possible that the input message length is 0 and the
10739 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010740 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010741static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010742 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010743{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010744 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10745 const size_t max_len = (size_t) ret;
10746
10747 if( ret < 0 )
10748 {
10749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10750 return( ret );
10751 }
10752
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010753 if( len > max_len )
10754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010759 "maximum fragment length: %d > %d",
10760 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010762 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010763 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010764#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010765#if defined(MBEDTLS_SSL_PROTO_TLS)
10766 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010767 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010768 }
10769#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010770 }
Paul Bakker887bd502011-06-08 13:10:54 +000010771
Paul Bakker5121ce52009-01-03 21:22:43 +000010772 if( ssl->out_left != 0 )
10773 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010774 /*
10775 * The user has previously tried to send the data and
10776 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10777 * written. In this case, we expect the high-level write function
10778 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010783 return( ret );
10784 }
10785 }
Paul Bakker887bd502011-06-08 13:10:54 +000010786 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010787 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010788 /*
10789 * The user is trying to send a message the first time, so we need to
10790 * copy the data into the internal buffers and setup the data structure
10791 * to keep track of partial writes
10792 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010793 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010795 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010796
Hanno Becker67bc7c32018-08-06 11:33:50 +010010797 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010800 return( ret );
10801 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010802 }
10803
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010804 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010805}
10806
10807/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010808 * Write application data, doing 1/n-1 splitting if necessary.
10809 *
10810 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010811 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010812 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010815static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010816 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010817{
10818 int ret;
10819
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010820 if( ssl->conf->cbc_record_splitting ==
10821 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010822 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010823 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10825 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010826 {
10827 return( ssl_write_real( ssl, buf, len ) );
10828 }
10829
10830 if( ssl->split_done == 0 )
10831 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010832 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010833 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010834 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010835 }
10836
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010837 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10838 return( ret );
10839 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010840
10841 return( ret + 1 );
10842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010843#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010844
10845/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010846 * Write application data (public-facing wrapper)
10847 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010848int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010849{
10850 int ret;
10851
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010853
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010854 if( ssl == NULL || ssl->conf == NULL )
10855 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10856
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010857#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010858 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10859 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010860 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010861 return( ret );
10862 }
10863#endif
10864
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010865 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010866 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010867 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010868 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010870 return( ret );
10871 }
10872 }
10873
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010874#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010875 ret = ssl_write_split( ssl, buf, len );
10876#else
10877 ret = ssl_write_real( ssl, buf, len );
10878#endif
10879
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010881
10882 return( ret );
10883}
10884
10885/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010886 * Notify the peer that the connection is being closed
10887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010889{
10890 int ret;
10891
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010892 if( ssl == NULL || ssl->conf == NULL )
10893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010896
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010897 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10903 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10904 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010907 return( ret );
10908 }
10909 }
10910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010912
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010913 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010914}
10915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010917{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010918 if( transform == NULL )
10919 return;
10920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010922 deflateEnd( &transform->ctx_deflate );
10923 inflateEnd( &transform->ctx_inflate );
10924#endif
10925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10927 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010928
Hanno Becker92231322018-01-03 15:32:51 +000010929#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930 mbedtls_md_free( &transform->md_ctx_enc );
10931 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010932#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010933
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010934 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010935}
10936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010937#if defined(MBEDTLS_X509_CRT_PARSE_C)
10938static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010941
10942 while( cur != NULL )
10943 {
10944 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010946 cur = next;
10947 }
10948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010950
Hanno Becker0271f962018-08-16 13:23:47 +010010951#if defined(MBEDTLS_SSL_PROTO_DTLS)
10952
10953static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10954{
10955 unsigned offset;
10956 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10957
10958 if( hs == NULL )
10959 return;
10960
Hanno Becker283f5ef2018-08-24 09:34:47 +010010961 ssl_free_buffered_record( ssl );
10962
Hanno Becker0271f962018-08-16 13:23:47 +010010963 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010964 ssl_buffering_free_slot( ssl, offset );
10965}
10966
10967static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10968 uint8_t slot )
10969{
10970 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10971 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010972
10973 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10974 return;
10975
Hanno Beckere605b192018-08-21 15:59:07 +010010976 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010977 {
Hanno Beckere605b192018-08-21 15:59:07 +010010978 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010979 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010980 mbedtls_free( hs_buf->data );
10981 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010982 }
10983}
10984
10985#endif /* MBEDTLS_SSL_PROTO_DTLS */
10986
Gilles Peskine9b562d52018-04-25 20:32:43 +020010987void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010988{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010989 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10990
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010991 if( handshake == NULL )
10992 return;
10993
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010994#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10995 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10996 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010997 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010998 handshake->async_in_progress = 0;
10999 }
11000#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11001
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011002#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11003 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11004 mbedtls_md5_free( &handshake->fin_md5 );
11005 mbedtls_sha1_free( &handshake->fin_sha1 );
11006#endif
11007#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11008#if defined(MBEDTLS_SHA256_C)
11009 mbedtls_sha256_free( &handshake->fin_sha256 );
11010#endif
11011#if defined(MBEDTLS_SHA512_C)
11012 mbedtls_sha512_free( &handshake->fin_sha512 );
11013#endif
11014#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_DHM_C)
11017 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019#if defined(MBEDTLS_ECDH_C)
11020 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011021#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011022#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011023 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011024#if defined(MBEDTLS_SSL_CLI_C)
11025 mbedtls_free( handshake->ecjpake_cache );
11026 handshake->ecjpake_cache = NULL;
11027 handshake->ecjpake_cache_len = 0;
11028#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011029#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011030
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011031#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11032 if( handshake->psk != NULL )
11033 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011034 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011035 mbedtls_free( handshake->psk );
11036 }
11037#endif
11038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11040 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011041 /*
11042 * Free only the linked list wrapper, not the keys themselves
11043 * since the belong to the SNI callback
11044 */
11045 if( handshake->sni_key_cert != NULL )
11046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011048
11049 while( cur != NULL )
11050 {
11051 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011053 cur = next;
11054 }
11055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011057
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011058#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011059 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011060 if( handshake->ecrs_peer_cert != NULL )
11061 {
11062 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11063 mbedtls_free( handshake->ecrs_peer_cert );
11064 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011065#endif
11066
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011067#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11068 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11069 mbedtls_pk_free( &handshake->peer_pubkey );
11070#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#if defined(MBEDTLS_SSL_PROTO_DTLS)
11073 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011074 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011075 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011076#endif
11077
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011078 mbedtls_platform_zeroize( handshake,
11079 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011080}
11081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011082void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011083{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011084 if( session == NULL )
11085 return;
11086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011088 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011089#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011090
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011091#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011092 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011093#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011094
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011095 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011096}
11097
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011098#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011099
11100#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11101#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11102#else
11103#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11104#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11105
11106#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11107#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11108#else
11109#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11110#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11111
11112#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11113#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11114#else
11115#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11116#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11117
11118#if defined(MBEDTLS_SSL_ALPN)
11119#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11120#else
11121#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11122#endif /* MBEDTLS_SSL_ALPN */
11123
11124#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11125#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11126#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11127#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11128
11129#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11130 ( (uint32_t) ( \
11131 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11132 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11133 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11134 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11135 0u ) )
11136
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011137static unsigned char ssl_serialized_context_header[] = {
11138 MBEDTLS_VERSION_MAJOR,
11139 MBEDTLS_VERSION_MINOR,
11140 MBEDTLS_VERSION_PATCH,
11141 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11142 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011143 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11144 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11145 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011146};
11147
Paul Bakker5121ce52009-01-03 21:22:43 +000011148/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011149 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011150 *
11151 * The format of the serialized data is:
11152 * (in the presentation language of TLS, RFC 8446 section 3)
11153 *
11154 * // header
11155 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011156 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011157 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011158 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011159 * Note: When updating the format, remember to keep these
11160 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011161 *
11162 * // session sub-structure
11163 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11164 * // transform sub-structure
11165 * uint8 random[64]; // ServerHello.random+ClientHello.random
11166 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11167 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11168 * // fields from ssl_context
11169 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11170 * uint64 in_window_top; // DTLS: last validated record seq_num
11171 * uint64 in_window; // DTLS: bitmask for replay protection
11172 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11173 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11174 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11175 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11176 *
11177 * Note that many fields of the ssl_context or sub-structures are not
11178 * serialized, as they fall in one of the following categories:
11179 *
11180 * 1. forced value (eg in_left must be 0)
11181 * 2. pointer to dynamically-allocated memory (eg session, transform)
11182 * 3. value can be re-derived from other data (eg session keys from MS)
11183 * 4. value was temporary (eg content of input buffer)
11184 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011185 */
11186int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11187 unsigned char *buf,
11188 size_t buf_len,
11189 size_t *olen )
11190{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011191 unsigned char *p = buf;
11192 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011193 size_t session_len;
11194 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011195
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011196 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011197 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11198 * this function's documentation.
11199 *
11200 * These are due to assumptions/limitations in the implementation. Some of
11201 * them are likely to stay (no handshake in progress) some might go away
11202 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011203 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011204 /* The initial handshake must be over */
11205 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011207 if( ssl->handshake != NULL )
11208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11209 /* Double-check that sub-structures are indeed ready */
11210 if( ssl->transform == NULL || ssl->session == NULL )
11211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11212 /* There must be no pending incoming or outgoing data */
11213 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11215 if( ssl->out_left != 0 )
11216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11217 /* Protocol must be DLTS, not TLS */
11218 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11220 /* Version must be 1.2 */
11221 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11223 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11225 /* We must be using an AEAD ciphersuite */
11226 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11228 /* Renegotiation must not be enabled */
11229 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011231
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011232 /*
11233 * Version and format identifier
11234 */
11235 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011236
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011237 if( used <= buf_len )
11238 {
11239 memcpy( p, ssl_serialized_context_header,
11240 sizeof( ssl_serialized_context_header ) );
11241 p += sizeof( ssl_serialized_context_header );
11242 }
11243
11244 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011245 * Session (length + data)
11246 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011247 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011248 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11249 return( ret );
11250
11251 used += 4 + session_len;
11252 if( used <= buf_len )
11253 {
11254 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11255 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11256 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11257 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11258
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011259 ret = ssl_session_save( ssl->session, 1,
11260 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011261 if( ret != 0 )
11262 return( ret );
11263
11264 p += session_len;
11265 }
11266
11267 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011268 * Transform
11269 */
11270 used += sizeof( ssl->transform->randbytes );
11271 if( used <= buf_len )
11272 {
11273 memcpy( p, ssl->transform->randbytes,
11274 sizeof( ssl->transform->randbytes ) );
11275 p += sizeof( ssl->transform->randbytes );
11276 }
11277
11278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11279 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11280 if( used <= buf_len )
11281 {
11282 *p++ = ssl->transform->in_cid_len;
11283 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11284 p += ssl->transform->in_cid_len;
11285
11286 *p++ = ssl->transform->out_cid_len;
11287 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11288 p += ssl->transform->out_cid_len;
11289 }
11290#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11291
11292 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011293 * Saved fields from top-level ssl_context structure
11294 */
11295#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11296 used += 4;
11297 if( used <= buf_len )
11298 {
11299 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11300 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11301 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11302 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11303 }
11304#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11305
11306#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11307 used += 16;
11308 if( used <= buf_len )
11309 {
11310 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11311 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11312 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11313 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11314 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11315 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11316 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11317 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11318
11319 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11320 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11321 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11322 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11323 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11324 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11325 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11326 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11327 }
11328#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11329
11330#if defined(MBEDTLS_SSL_PROTO_DTLS)
11331 used += 1;
11332 if( used <= buf_len )
11333 {
11334 *p++ = ssl->disable_datagram_packing;
11335 }
11336#endif /* MBEDTLS_SSL_PROTO_DTLS */
11337
11338 used += 8;
11339 if( used <= buf_len )
11340 {
11341 memcpy( p, ssl->cur_out_ctr, 8 );
11342 p += 8;
11343 }
11344
11345#if defined(MBEDTLS_SSL_PROTO_DTLS)
11346 used += 2;
11347 if( used <= buf_len )
11348 {
11349 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11350 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11351 }
11352#endif /* MBEDTLS_SSL_PROTO_DTLS */
11353
11354#if defined(MBEDTLS_SSL_ALPN)
11355 {
11356 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011357 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011358 : 0;
11359
11360 used += 1 + alpn_len;
11361 if( used <= buf_len )
11362 {
11363 *p++ = alpn_len;
11364
11365 if( ssl->alpn_chosen != NULL )
11366 {
11367 memcpy( p, ssl->alpn_chosen, alpn_len );
11368 p += alpn_len;
11369 }
11370 }
11371 }
11372#endif /* MBEDTLS_SSL_ALPN */
11373
11374 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011375 * Done
11376 */
11377 *olen = used;
11378
11379 if( used > buf_len )
11380 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011381
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011382 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11383
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011384 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011385}
11386
11387/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011388 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011389 *
11390 * This internal version is wrapped by a public function that cleans up in
11391 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011392 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011393static int ssl_context_load( mbedtls_ssl_context *ssl,
11394 const unsigned char *buf,
11395 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011396{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011397 const unsigned char *p = buf;
11398 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011399 size_t session_len;
11400 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011401
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011402 /*
11403 * The context should have been freshly setup or reset.
11404 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011405 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011406 * renegotiating, or if the user mistakenly loaded a session first.)
11407 */
11408 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11409 ssl->session != NULL )
11410 {
11411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11412 }
11413
11414 /*
11415 * We can't check that the config matches the initial one, but we can at
11416 * least check it matches the requirements for serializing.
11417 */
11418 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011419 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11420 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11421 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11422 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11423 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11424 MBEDTLS_SSL_MINOR_VERSION_3 ||
11425 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11426 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011427 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011428 {
11429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11430 }
11431
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011432 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11433
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011434 /*
11435 * Check version identifier
11436 */
11437 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11439
11440 if( memcmp( p, ssl_serialized_context_header,
11441 sizeof( ssl_serialized_context_header ) ) != 0 )
11442 {
11443 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11444 }
11445 p += sizeof( ssl_serialized_context_header );
11446
11447 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011448 * Session
11449 */
11450 if( (size_t)( end - p ) < 4 )
11451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11452
11453 session_len = ( (size_t) p[0] << 24 ) |
11454 ( (size_t) p[1] << 16 ) |
11455 ( (size_t) p[2] << 8 ) |
11456 ( (size_t) p[3] );
11457 p += 4;
11458
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011459 /* This has been allocated by ssl_handshake_init(), called by
11460 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11461 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011462 ssl->session_in = ssl->session;
11463 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011464 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011465
11466 if( (size_t)( end - p ) < session_len )
11467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11468
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011469 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011470 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011471 {
11472 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011473 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011474 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011475
11476 p += session_len;
11477
11478 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011479 * Transform
11480 */
11481
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011482 /* This has been allocated by ssl_handshake_init(), called by
11483 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11484 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011485 ssl->transform_in = ssl->transform;
11486 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011487 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011488
11489 /* Read random bytes and populate structure */
11490 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11492
11493 ret = ssl_populate_transform( ssl->transform,
11494 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11495 ssl->session->master,
11496#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11497#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11498 ssl->session->encrypt_then_mac,
11499#endif
11500#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11501 ssl->session->trunc_hmac,
11502#endif
11503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11504#if defined(MBEDTLS_ZLIB_SUPPORT)
11505 ssl->session->compression,
11506#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011507 p, /* currently pointing to randbytes */
11508 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11509 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11510 ssl );
11511 if( ret != 0 )
11512 return( ret );
11513
11514 p += sizeof( ssl->transform->randbytes );
11515
11516#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11517 /* Read connection IDs and store them */
11518 if( (size_t)( end - p ) < 1 )
11519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11520
11521 ssl->transform->in_cid_len = *p++;
11522
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011523 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11525
11526 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11527 p += ssl->transform->in_cid_len;
11528
11529 ssl->transform->out_cid_len = *p++;
11530
11531 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11533
11534 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11535 p += ssl->transform->out_cid_len;
11536#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11537
11538 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011539 * Saved fields from top-level ssl_context structure
11540 */
11541#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11542 if( (size_t)( end - p ) < 4 )
11543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11544
11545 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11546 ( (uint32_t) p[1] << 16 ) |
11547 ( (uint32_t) p[2] << 8 ) |
11548 ( (uint32_t) p[3] );
11549 p += 4;
11550#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11551
11552#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11553 if( (size_t)( end - p ) < 16 )
11554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11555
11556 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11557 ( (uint64_t) p[1] << 48 ) |
11558 ( (uint64_t) p[2] << 40 ) |
11559 ( (uint64_t) p[3] << 32 ) |
11560 ( (uint64_t) p[4] << 24 ) |
11561 ( (uint64_t) p[5] << 16 ) |
11562 ( (uint64_t) p[6] << 8 ) |
11563 ( (uint64_t) p[7] );
11564 p += 8;
11565
11566 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11567 ( (uint64_t) p[1] << 48 ) |
11568 ( (uint64_t) p[2] << 40 ) |
11569 ( (uint64_t) p[3] << 32 ) |
11570 ( (uint64_t) p[4] << 24 ) |
11571 ( (uint64_t) p[5] << 16 ) |
11572 ( (uint64_t) p[6] << 8 ) |
11573 ( (uint64_t) p[7] );
11574 p += 8;
11575#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11576
11577#if defined(MBEDTLS_SSL_PROTO_DTLS)
11578 if( (size_t)( end - p ) < 1 )
11579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11580
11581 ssl->disable_datagram_packing = *p++;
11582#endif /* MBEDTLS_SSL_PROTO_DTLS */
11583
11584 if( (size_t)( end - p ) < 8 )
11585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11586
11587 memcpy( ssl->cur_out_ctr, p, 8 );
11588 p += 8;
11589
11590#if defined(MBEDTLS_SSL_PROTO_DTLS)
11591 if( (size_t)( end - p ) < 2 )
11592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11593
11594 ssl->mtu = ( p[0] << 8 ) | p[1];
11595 p += 2;
11596#endif /* MBEDTLS_SSL_PROTO_DTLS */
11597
11598#if defined(MBEDTLS_SSL_ALPN)
11599 {
11600 uint8_t alpn_len;
11601 const char **cur;
11602
11603 if( (size_t)( end - p ) < 1 )
11604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11605
11606 alpn_len = *p++;
11607
11608 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11609 {
11610 /* alpn_chosen should point to an item in the configured list */
11611 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11612 {
11613 if( strlen( *cur ) == alpn_len &&
11614 memcmp( p, cur, alpn_len ) == 0 )
11615 {
11616 ssl->alpn_chosen = *cur;
11617 break;
11618 }
11619 }
11620 }
11621
11622 /* can only happen on conf mismatch */
11623 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11625
11626 p += alpn_len;
11627 }
11628#endif /* MBEDTLS_SSL_ALPN */
11629
11630 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011631 * Forced fields from top-level ssl_context structure
11632 *
11633 * Most of them already set to the correct value by mbedtls_ssl_init() and
11634 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11635 */
11636 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11637
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011638#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011639 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011640#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11641#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011642 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011643#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011644
Hanno Becker83985822019-08-30 10:42:49 +010011645 /* Adjust pointers for header fields of outgoing records to
11646 * the given transform, accounting for explicit IV and CID. */
11647 ssl_update_out_pointers( ssl, ssl->transform );
11648
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011649#if defined(MBEDTLS_SSL_PROTO_DTLS)
11650 ssl->in_epoch = 1;
11651#endif
11652
11653 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11654 * which we don't want - otherwise we'd end up freeing the wrong transform
11655 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11656 if( ssl->handshake != NULL )
11657 {
11658 mbedtls_ssl_handshake_free( ssl );
11659 mbedtls_free( ssl->handshake );
11660 ssl->handshake = NULL;
11661 }
11662
11663 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011664 * Done - should have consumed entire buffer
11665 */
11666 if( p != end )
11667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011668
11669 return( 0 );
11670}
11671
11672/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011673 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011674 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011675int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011676 const unsigned char *buf,
11677 size_t len )
11678{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011679 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011680
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011681 if( ret != 0 )
11682 mbedtls_ssl_free( context );
11683
11684 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011685}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011686#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011687
11688/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011689 * Free an SSL context
11690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011691void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011692{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011693 if( ssl == NULL )
11694 return;
11695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011697
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011698 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011699 {
Angus Grattond8213d02016-05-25 20:56:48 +100011700 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011701 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011702 }
11703
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011704 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011705 {
Angus Grattond8213d02016-05-25 20:56:48 +100011706 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011707 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011708 }
11709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011711 if( ssl->compress_buf != NULL )
11712 {
Angus Grattond8213d02016-05-25 20:56:48 +100011713 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011714 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011715 }
11716#endif
11717
Paul Bakker48916f92012-09-16 19:57:18 +000011718 if( ssl->transform )
11719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011720 mbedtls_ssl_transform_free( ssl->transform );
11721 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011722 }
11723
11724 if( ssl->handshake )
11725 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011726 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011727 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11728 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730 mbedtls_free( ssl->handshake );
11731 mbedtls_free( ssl->transform_negotiate );
11732 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011733 }
11734
Paul Bakkerc0463502013-02-14 11:19:38 +010011735 if( ssl->session )
11736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011737 mbedtls_ssl_session_free( ssl->session );
11738 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011739 }
11740
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011741#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011742 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011743 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011744 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011745 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011746 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011747#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011749#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11750 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11753 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011754 }
11755#endif
11756
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011757#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011758 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011759#endif
11760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011762
Paul Bakker86f04f42013-02-14 11:20:09 +010011763 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011764 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011765}
11766
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011767/*
11768 * Initialze mbedtls_ssl_config
11769 */
11770void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11771{
11772 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011773
11774#if !defined(MBEDTLS_SSL_PROTO_TLS)
11775 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11776#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011777}
11778
Simon Butcherc97b6972015-12-27 23:48:17 +000011779#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011780#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011781static int ssl_preset_default_hashes[] = {
11782#if defined(MBEDTLS_SHA512_C)
11783 MBEDTLS_MD_SHA512,
11784 MBEDTLS_MD_SHA384,
11785#endif
11786#if defined(MBEDTLS_SHA256_C)
11787 MBEDTLS_MD_SHA256,
11788 MBEDTLS_MD_SHA224,
11789#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011790#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011791 MBEDTLS_MD_SHA1,
11792#endif
11793 MBEDTLS_MD_NONE
11794};
Simon Butcherc97b6972015-12-27 23:48:17 +000011795#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011796#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011797
Hanno Becker73f4cb12019-06-27 13:51:07 +010011798#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011799static int ssl_preset_suiteb_ciphersuites[] = {
11800 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11801 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11802 0
11803};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011804#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011805
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011806#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011807#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011808static int ssl_preset_suiteb_hashes[] = {
11809 MBEDTLS_MD_SHA256,
11810 MBEDTLS_MD_SHA384,
11811 MBEDTLS_MD_NONE
11812};
11813#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011814#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011815
Hanno Beckerc1096e72019-06-19 12:30:41 +010011816#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011817static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011818#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011819 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011820#endif
11821#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011822 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011823#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011824 MBEDTLS_ECP_DP_NONE
11825};
11826#endif
11827
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011828/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011829 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011830 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011831int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011832 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011833{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011834#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011835 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011836#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011837
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011838 /* Use the functions here so that they are covered in tests,
11839 * but otherwise access member directly for efficiency */
11840 mbedtls_ssl_conf_endpoint( conf, endpoint );
11841 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011842
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011843 /*
11844 * Things that are common to all presets
11845 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011846#if defined(MBEDTLS_SSL_CLI_C)
11847 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11848 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011849#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011850 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011851#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011852#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11853 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11854#endif
11855 }
11856#endif
11857
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011858#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011859 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011860#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011861
11862#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11863 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11864#endif
11865
11866#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011867#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011868 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011869#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11870#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011871 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011872 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011873#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011874#endif
11875
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011876#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11877 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11878#endif
11879
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011880#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011881 conf->f_cookie_write = ssl_cookie_write_dummy;
11882 conf->f_cookie_check = ssl_cookie_check_dummy;
11883#endif
11884
Hanno Becker7f376f42019-06-12 16:20:48 +010011885#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11886 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011887 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11888#endif
11889
Janos Follath088ce432017-04-10 12:42:31 +010011890#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011891#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011892 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011893#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11894#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011895
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011897#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011898 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011899#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11900#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011901 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011902#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011904
11905#if defined(MBEDTLS_SSL_RENEGOTIATION)
11906 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011907 memset( conf->renego_period, 0x00, 2 );
11908 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011909#endif
11910
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011911#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11912 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11913 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011914 const unsigned char dhm_p[] =
11915 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11916 const unsigned char dhm_g[] =
11917 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11918
Hanno Beckera90658f2017-10-04 15:29:08 +010011919 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11920 dhm_p, sizeof( dhm_p ),
11921 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011922 {
11923 return( ret );
11924 }
11925 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011926#endif
11927
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011928 /*
11929 * Preset-specific defaults
11930 */
11931 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011932 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011933 /*
11934 * NSA Suite B
11935 */
11936 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011937#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011938 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011939#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11940#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011941 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011942#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11943#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011944 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011945#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11946#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011948#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011949
Hanno Becker73f4cb12019-06-27 13:51:07 +010011950#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011951 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11953 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11954 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11955 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011956#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011957
11958#if defined(MBEDTLS_X509_CRT_PARSE_C)
11959 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011960#endif
11961
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011962#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011963#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011964 conf->sig_hashes = ssl_preset_suiteb_hashes;
11965#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011966#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011967
11968#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011969#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011970 conf->curve_list = ssl_preset_suiteb_curves;
11971#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011972#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011973 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011974
11975 /*
11976 * Default
11977 */
11978 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011979#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011980 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11981 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11982 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11983 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011984#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11985#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011986 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11987 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11988 MBEDTLS_SSL_MIN_MINOR_VERSION :
11989 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011991 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011992 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11993#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011994#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11995#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11996 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11997#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11998#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11999 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12000#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012001
Hanno Becker73f4cb12019-06-27 13:51:07 +010012002#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012003 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
12004 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
12005 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
12006 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
12007 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012008#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012009
12010#if defined(MBEDTLS_X509_CRT_PARSE_C)
12011 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12012#endif
12013
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012014#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012015#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012016 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012017#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012018#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012019
12020#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012021#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012022 conf->curve_list = mbedtls_ecp_grp_id_list();
12023#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012024#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012025
12026#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12027 conf->dhm_min_bitlen = 1024;
12028#endif
12029 }
12030
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012031 return( 0 );
12032}
12033
12034/*
12035 * Free mbedtls_ssl_config
12036 */
12037void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12038{
12039#if defined(MBEDTLS_DHM_C)
12040 mbedtls_mpi_free( &conf->dhm_P );
12041 mbedtls_mpi_free( &conf->dhm_G );
12042#endif
12043
12044#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12045 if( conf->psk != NULL )
12046 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012047 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012048 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012049 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012050 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012051 }
12052
12053 if( conf->psk_identity != NULL )
12054 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012055 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012056 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012057 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012058 conf->psk_identity_len = 0;
12059 }
12060#endif
12061
12062#if defined(MBEDTLS_X509_CRT_PARSE_C)
12063 ssl_key_cert_free( conf->key_cert );
12064#endif
12065
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012066 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012067}
12068
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012069#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012070 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12071 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012072/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012073 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012075unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012077#if defined(MBEDTLS_RSA_C)
12078 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12079 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012080#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012081#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012082 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12083 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012085 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012086}
12087
Hanno Becker7e5437a2017-04-28 17:15:26 +010012088unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12089{
12090 switch( type ) {
12091 case MBEDTLS_PK_RSA:
12092 return( MBEDTLS_SSL_SIG_RSA );
12093 case MBEDTLS_PK_ECDSA:
12094 case MBEDTLS_PK_ECKEY:
12095 return( MBEDTLS_SSL_SIG_ECDSA );
12096 default:
12097 return( MBEDTLS_SSL_SIG_ANON );
12098 }
12099}
12100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012101mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012102{
12103 switch( sig )
12104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012105#if defined(MBEDTLS_RSA_C)
12106 case MBEDTLS_SSL_SIG_RSA:
12107 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012108#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012109#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012110 case MBEDTLS_SSL_SIG_ECDSA:
12111 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012112#endif
12113 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012114 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012115 }
12116}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012117#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012118
Hanno Becker7e5437a2017-04-28 17:15:26 +010012119#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12120 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12121
12122/* Find an entry in a signature-hash set matching a given hash algorithm. */
12123mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12124 mbedtls_pk_type_t sig_alg )
12125{
12126 switch( sig_alg )
12127 {
12128 case MBEDTLS_PK_RSA:
12129 return( set->rsa );
12130 case MBEDTLS_PK_ECDSA:
12131 return( set->ecdsa );
12132 default:
12133 return( MBEDTLS_MD_NONE );
12134 }
12135}
12136
12137/* Add a signature-hash-pair to a signature-hash set */
12138void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12139 mbedtls_pk_type_t sig_alg,
12140 mbedtls_md_type_t md_alg )
12141{
12142 switch( sig_alg )
12143 {
12144 case MBEDTLS_PK_RSA:
12145 if( set->rsa == MBEDTLS_MD_NONE )
12146 set->rsa = md_alg;
12147 break;
12148
12149 case MBEDTLS_PK_ECDSA:
12150 if( set->ecdsa == MBEDTLS_MD_NONE )
12151 set->ecdsa = md_alg;
12152 break;
12153
12154 default:
12155 break;
12156 }
12157}
12158
12159/* Allow exactly one hash algorithm for each signature. */
12160void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12161 mbedtls_md_type_t md_alg )
12162{
12163 set->rsa = md_alg;
12164 set->ecdsa = md_alg;
12165}
12166
12167#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12168 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12169
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012170/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012171 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012173mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012174{
12175 switch( hash )
12176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177#if defined(MBEDTLS_MD5_C)
12178 case MBEDTLS_SSL_HASH_MD5:
12179 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012181#if defined(MBEDTLS_SHA1_C)
12182 case MBEDTLS_SSL_HASH_SHA1:
12183 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012185#if defined(MBEDTLS_SHA256_C)
12186 case MBEDTLS_SSL_HASH_SHA224:
12187 return( MBEDTLS_MD_SHA224 );
12188 case MBEDTLS_SSL_HASH_SHA256:
12189 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012191#if defined(MBEDTLS_SHA512_C)
12192 case MBEDTLS_SSL_HASH_SHA384:
12193 return( MBEDTLS_MD_SHA384 );
12194 case MBEDTLS_SSL_HASH_SHA512:
12195 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012196#endif
12197 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012198 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012199 }
12200}
12201
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012202/*
12203 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12204 */
12205unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12206{
12207 switch( md )
12208 {
12209#if defined(MBEDTLS_MD5_C)
12210 case MBEDTLS_MD_MD5:
12211 return( MBEDTLS_SSL_HASH_MD5 );
12212#endif
12213#if defined(MBEDTLS_SHA1_C)
12214 case MBEDTLS_MD_SHA1:
12215 return( MBEDTLS_SSL_HASH_SHA1 );
12216#endif
12217#if defined(MBEDTLS_SHA256_C)
12218 case MBEDTLS_MD_SHA224:
12219 return( MBEDTLS_SSL_HASH_SHA224 );
12220 case MBEDTLS_MD_SHA256:
12221 return( MBEDTLS_SSL_HASH_SHA256 );
12222#endif
12223#if defined(MBEDTLS_SHA512_C)
12224 case MBEDTLS_MD_SHA384:
12225 return( MBEDTLS_SSL_HASH_SHA384 );
12226 case MBEDTLS_MD_SHA512:
12227 return( MBEDTLS_SSL_HASH_SHA512 );
12228#endif
12229 default:
12230 return( MBEDTLS_SSL_HASH_NONE );
12231 }
12232}
12233
Hanno Beckeree902df2019-08-23 13:47:47 +010012234#if defined(MBEDTLS_USE_TINYCRYPT)
12235/*
12236 * Check if a curve proposed by the peer is in our list.
12237 * Return 0 if we're willing to use it, -1 otherwise.
12238 */
12239int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12240 mbedtls_uecc_group_id grp_id )
12241{
12242 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12243 if( own_ec_id == grp_id )
12244 return( 0 );
12245 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12246
12247 return( -1 );
12248}
12249#endif /* MBEDTLS_USE_TINYCRYPT */
12250
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012251#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012252/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012253 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012254 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012255 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012256int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12257 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012258{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012259 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12260 if( own_ec_id == grp_id )
12261 return( 0 );
12262 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012263
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012264 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012265}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012266#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012267
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012268#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012269/*
12270 * Check if a hash proposed by the peer is in our list.
12271 * Return 0 if we're willing to use it, -1 otherwise.
12272 */
12273int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12274 mbedtls_md_type_t md )
12275{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012276 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12277 if( md_alg == md )
12278 return( 0 );
12279 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012280
12281 return( -1 );
12282}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012283#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012285#if defined(MBEDTLS_X509_CRT_PARSE_C)
12286int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012287 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012288 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012289 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012290{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012291 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012292#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012293 int usage = 0;
12294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012295#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012296 const char *ext_oid;
12297 size_t ext_len;
12298#endif
12299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012300#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12301 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012302 ((void) cert);
12303 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012304 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012305#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012307#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12308 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012309 {
12310 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012311 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012313 case MBEDTLS_KEY_EXCHANGE_RSA:
12314 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012315 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012316 break;
12317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012318 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12319 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12320 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12321 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012322 break;
12323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012324 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12325 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012326 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012327 break;
12328
12329 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012330 case MBEDTLS_KEY_EXCHANGE_NONE:
12331 case MBEDTLS_KEY_EXCHANGE_PSK:
12332 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12333 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012334 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012335 usage = 0;
12336 }
12337 }
12338 else
12339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012340 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12341 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012342 }
12343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012344 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012345 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012346 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012347 ret = -1;
12348 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012349#else
12350 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012351#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012353#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12354 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012356 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12357 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012358 }
12359 else
12360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012361 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12362 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012363 }
12364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012365 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012366 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012367 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012368 ret = -1;
12369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012370#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012371
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012372 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012374#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012375
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012376#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12377 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12378int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12379 unsigned char *output,
12380 unsigned char *data, size_t data_len )
12381{
12382 int ret = 0;
12383 mbedtls_md5_context mbedtls_md5;
12384 mbedtls_sha1_context mbedtls_sha1;
12385
12386 mbedtls_md5_init( &mbedtls_md5 );
12387 mbedtls_sha1_init( &mbedtls_sha1 );
12388
12389 /*
12390 * digitally-signed struct {
12391 * opaque md5_hash[16];
12392 * opaque sha_hash[20];
12393 * };
12394 *
12395 * md5_hash
12396 * MD5(ClientHello.random + ServerHello.random
12397 * + ServerParams);
12398 * sha_hash
12399 * SHA(ClientHello.random + ServerHello.random
12400 * + ServerParams);
12401 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012402 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012403 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012405 goto exit;
12406 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012407 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012408 ssl->handshake->randbytes, 64 ) ) != 0 )
12409 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012411 goto exit;
12412 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012413 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012414 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012416 goto exit;
12417 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012418 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012419 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012421 goto exit;
12422 }
12423
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012424 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012425 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012427 goto exit;
12428 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012429 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012430 ssl->handshake->randbytes, 64 ) ) != 0 )
12431 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012433 goto exit;
12434 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012435 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012436 data_len ) ) != 0 )
12437 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012439 goto exit;
12440 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012441 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012442 output + 16 ) ) != 0 )
12443 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012445 goto exit;
12446 }
12447
12448exit:
12449 mbedtls_md5_free( &mbedtls_md5 );
12450 mbedtls_sha1_free( &mbedtls_sha1 );
12451
12452 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012453 mbedtls_ssl_pend_fatal_alert( ssl,
12454 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012455
12456 return( ret );
12457
12458}
12459#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12460 MBEDTLS_SSL_PROTO_TLS1_1 */
12461
12462#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12463 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12464int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012465 unsigned char *hash, size_t *hashlen,
12466 unsigned char *data, size_t data_len,
12467 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012468{
12469 int ret = 0;
12470 mbedtls_md_context_t ctx;
12471 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012472 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012473
12474 mbedtls_md_init( &ctx );
12475
12476 /*
12477 * digitally-signed struct {
12478 * opaque client_random[32];
12479 * opaque server_random[32];
12480 * ServerDHParams params;
12481 * };
12482 */
12483 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12484 {
12485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12486 goto exit;
12487 }
12488 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12489 {
12490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12491 goto exit;
12492 }
12493 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12494 {
12495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12496 goto exit;
12497 }
12498 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12499 {
12500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12501 goto exit;
12502 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012503 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012504 {
12505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12506 goto exit;
12507 }
12508
12509exit:
12510 mbedtls_md_free( &ctx );
12511
12512 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012513 mbedtls_ssl_pend_fatal_alert( ssl,
12514 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012515
12516 return( ret );
12517}
12518#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12519 MBEDTLS_SSL_PROTO_TLS1_2 */
12520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012521#endif /* MBEDTLS_SSL_TLS_C */