blob: 3cc6046ef46651629cbc5696d300382db8e051d8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300500 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300510 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100663 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 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 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
685 MBEDTLS_MD_INVALID_HANDLE )
686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100688 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100691 return( ret );
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
694 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
695 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 for( i = 0; i < dlen; i += 16 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_md_hmac_reset ( &md_ctx );
700 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
701 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
705 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
708
709 for( j = 0; j < k; j++ )
710 dstbuf[i + j] = h_i[j];
711 }
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100714
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 /*
716 * XOR out with P_sha1(secret,label+random)[0..dlen]
717 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100718 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
719 MBEDTLS_MD_INVALID_HANDLE )
720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100722 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100725 return( ret );
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
728 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
729 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731 for( i = 0; i < dlen; i += 20 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_md_hmac_reset ( &md_ctx );
734 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
735 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
739 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
742
743 for( j = 0; j < k; j++ )
744 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
745 }
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100748
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
750 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752 return( 0 );
753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100757#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
758MBEDTLS_ALWAYS_INLINE static inline
759#else
760static
761#endif
762int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 const unsigned char *secret, size_t slen,
764 const char *label,
765 const unsigned char *random, size_t rlen,
766 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767{
768 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000770 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100772 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100774 int ret;
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000777
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100778 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
779 MBEDTLS_MD_INVALID_HANDLE )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100782 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
786 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788
789 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790 memcpy( tmp + md_len, label, nb );
791 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792 nb += rlen;
793
794 /*
795 * Compute P_<hash>(secret, label + random)[0..dlen]
796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100798 return( ret );
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
801 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
802 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100803
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100804 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
808 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_hmac_reset ( &md_ctx );
811 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
812 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000813
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100814 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 for( j = 0; j < k; j++ )
817 dstbuf[i + j] = h_i[j];
818 }
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100821
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500822 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
823 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000824
825 return( 0 );
826}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100829MBEDTLS_NO_INLINE static int tls_prf_sha256(
830 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100831 const char *label,
832 const unsigned char *random, size_t rlen,
833 unsigned char *dstbuf, size_t dlen )
834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100836 label, random, rlen, dstbuf, dlen ) );
837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100841MBEDTLS_NO_INLINE static int tls_prf_sha384(
842 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200843 const char *label,
844 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000845 unsigned char *dstbuf, size_t dlen )
846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100848 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_SHA512_C */
851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000852
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100853/*
854 * Call the appropriate PRF function
855 */
Hanno Becker2793f742019-08-16 14:28:43 +0100856MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100857 mbedtls_md_type_t hash,
858 const unsigned char *secret, size_t slen,
859 const char *label,
860 const unsigned char *random, size_t rlen,
861 unsigned char *dstbuf, size_t dlen )
862{
863#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
864 (void) hash;
865#endif
866
867#if defined(MBEDTLS_SSL_PROTO_SSL3)
868 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
869 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
870 else
871#endif
872#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100873 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100874 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
875 else
876#endif
877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
878#if defined(MBEDTLS_SHA512_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
880 hash == MBEDTLS_MD_SHA384 )
881 {
882 return( tls_prf_sha384( secret, slen, label, random, rlen,
883 dstbuf, dlen ) );
884 }
885 else
886#endif
887#if defined(MBEDTLS_SHA256_C)
888 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
889 {
890 return( tls_prf_sha256( secret, slen, label, random, rlen,
891 dstbuf, dlen ) );
892 }
893#endif
894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
895
896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
897}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200898
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100899#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100900MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100901 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
902{
903 const char *sender;
904 mbedtls_md5_context md5;
905 mbedtls_sha1_context sha1;
906
907 unsigned char padbuf[48];
908 unsigned char md5sum[16];
909 unsigned char sha1sum[20];
910
911 mbedtls_ssl_session *session = ssl->session_negotiate;
912 if( !session )
913 session = ssl->session;
914
915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
916
917 mbedtls_md5_init( &md5 );
918 mbedtls_sha1_init( &sha1 );
919
920 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
921 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
922
923 /*
924 * SSLv3:
925 * hash =
926 * MD5( master + pad2 +
927 * MD5( handshake + sender + master + pad1 ) )
928 * + SHA1( master + pad2 +
929 * SHA1( handshake + sender + master + pad1 ) )
930 */
931
932#if !defined(MBEDTLS_MD5_ALT)
933 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
934 md5.state, sizeof( md5.state ) );
935#endif
936
937#if !defined(MBEDTLS_SHA1_ALT)
938 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
939 sha1.state, sizeof( sha1.state ) );
940#endif
941
942 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
943 : "SRVR";
944
945 memset( padbuf, 0x36, 48 );
946
947 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
948 mbedtls_md5_update_ret( &md5, session->master, 48 );
949 mbedtls_md5_update_ret( &md5, padbuf, 48 );
950 mbedtls_md5_finish_ret( &md5, md5sum );
951
952 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
953 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
954 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
955 mbedtls_sha1_finish_ret( &sha1, sha1sum );
956
957 memset( padbuf, 0x5C, 48 );
958
959 mbedtls_md5_starts_ret( &md5 );
960 mbedtls_md5_update_ret( &md5, session->master, 48 );
961 mbedtls_md5_update_ret( &md5, padbuf, 48 );
962 mbedtls_md5_update_ret( &md5, md5sum, 16 );
963 mbedtls_md5_finish_ret( &md5, buf );
964
965 mbedtls_sha1_starts_ret( &sha1 );
966 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
967 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
968 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
969 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
970
971 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
972
973 mbedtls_md5_free( &md5 );
974 mbedtls_sha1_free( &sha1 );
975
976 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
977 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
978 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
979
980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
981}
982#endif /* MBEDTLS_SSL_PROTO_SSL3 */
983
984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100985MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100986 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
987{
988 int len = 12;
989 const char *sender;
990 mbedtls_md5_context md5;
991 mbedtls_sha1_context sha1;
992 unsigned char padbuf[36];
993
994 mbedtls_ssl_session *session = ssl->session_negotiate;
995 if( !session )
996 session = ssl->session;
997
998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
999
1000 mbedtls_md5_init( &md5 );
1001 mbedtls_sha1_init( &sha1 );
1002
1003 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1004 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1005
1006 /*
1007 * TLSv1:
1008 * hash = PRF( master, finished_label,
1009 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1010 */
1011
1012#if !defined(MBEDTLS_MD5_ALT)
1013 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1014 md5.state, sizeof( md5.state ) );
1015#endif
1016
1017#if !defined(MBEDTLS_SHA1_ALT)
1018 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1019 sha1.state, sizeof( sha1.state ) );
1020#endif
1021
1022 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1023 ? "client finished"
1024 : "server finished";
1025
1026 mbedtls_md5_finish_ret( &md5, padbuf );
1027 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1028
1029 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1030 mbedtls_ssl_suite_get_mac(
1031 mbedtls_ssl_ciphersuite_from_id(
1032 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1033 session->master, 48, sender,
1034 padbuf, 36, buf, len );
1035
1036 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1037
1038 mbedtls_md5_free( &md5 );
1039 mbedtls_sha1_free( &sha1 );
1040
1041 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1042
1043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1044}
1045#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1046
1047#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1048#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001049MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001050 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1051{
1052 int len = 12;
1053 const char *sender;
1054 mbedtls_sha256_context sha256;
1055 unsigned char padbuf[32];
1056
1057 mbedtls_ssl_session *session = ssl->session_negotiate;
1058 if( !session )
1059 session = ssl->session;
1060
1061 mbedtls_sha256_init( &sha256 );
1062
1063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1064
1065 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1066
1067 /*
1068 * TLSv1.2:
1069 * hash = PRF( master, finished_label,
1070 * Hash( handshake ) )[0.11]
1071 */
1072
1073#if !defined(MBEDTLS_SHA256_ALT)
1074 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1075 sha256.state, sizeof( sha256.state ) );
1076#endif
1077
1078 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1079 ? "client finished"
1080 : "server finished";
1081
1082 mbedtls_sha256_finish_ret( &sha256, padbuf );
1083
1084 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1085 mbedtls_ssl_suite_get_mac(
1086 mbedtls_ssl_ciphersuite_from_id(
1087 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1088 session->master, 48, sender,
1089 padbuf, 32, buf, len );
1090
1091 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1092
1093 mbedtls_sha256_free( &sha256 );
1094
1095 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1096
1097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1098}
1099#endif /* MBEDTLS_SHA256_C */
1100
1101#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001102MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001103 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1104{
1105 int len = 12;
1106 const char *sender;
1107 mbedtls_sha512_context sha512;
1108 unsigned char padbuf[48];
1109
1110 mbedtls_ssl_session *session = ssl->session_negotiate;
1111 if( !session )
1112 session = ssl->session;
1113
1114 mbedtls_sha512_init( &sha512 );
1115
1116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1117
1118 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1119
1120 /*
1121 * TLSv1.2:
1122 * hash = PRF( master, finished_label,
1123 * Hash( handshake ) )[0.11]
1124 */
1125
1126#if !defined(MBEDTLS_SHA512_ALT)
1127 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1128 sha512.state, sizeof( sha512.state ) );
1129#endif
1130
1131 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1132 ? "client finished"
1133 : "server finished";
1134
1135 mbedtls_sha512_finish_ret( &sha512, padbuf );
1136
1137 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1138 mbedtls_ssl_suite_get_mac(
1139 mbedtls_ssl_ciphersuite_from_id(
1140 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1141 session->master, 48, sender,
1142 padbuf, 48, buf, len );
1143
1144 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1145
1146 mbedtls_sha512_free( &sha512 );
1147
1148 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1149
1150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1151}
1152#endif /* MBEDTLS_SHA512_C */
1153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1154
Hanno Becker2793f742019-08-16 14:28:43 +01001155MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1156 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001157 mbedtls_md_type_t hash,
1158 mbedtls_ssl_context *ssl,
1159 unsigned char *buf,
1160 int from )
1161{
1162#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1163 (void) hash;
1164#endif
1165
1166#if defined(MBEDTLS_SSL_PROTO_SSL3)
1167 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1168 ssl_calc_finished_ssl( ssl, buf, from );
1169 else
1170#endif
1171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001172 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001173 ssl_calc_finished_tls( ssl, buf, from );
1174 else
1175#endif
1176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1177#if defined(MBEDTLS_SHA512_C)
1178 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1179 hash == MBEDTLS_MD_SHA384 )
1180 {
1181 ssl_calc_finished_tls_sha384( ssl, buf, from );
1182 }
1183 else
1184#endif
1185#if defined(MBEDTLS_SHA256_C)
1186 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1187 ssl_calc_finished_tls_sha256( ssl, buf, from );
1188 else
1189#endif
1190#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1192
1193 return( 0 );
1194}
1195
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001196/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001197 * Populate a transform structure with session keys and all the other
1198 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001199 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001200 * Parameters:
1201 * - [in/out]: transform: structure to populate
1202 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001203 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001204 * - [in] ciphersuite
1205 * - [in] master
1206 * - [in] encrypt_then_mac
1207 * - [in] trunc_hmac
1208 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001209 * - [in] tls_prf: pointer to PRF to use for key derivation
1210 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001211 * - [in] minor_ver: SSL/TLS minor version
1212 * - [in] endpoint: client or server
1213 * - [in] ssl: optionally used for:
1214 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1215 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1216 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001217 */
Hanno Becker298a4702019-08-16 10:21:32 +01001218/* Force compilers to inline this function if it's used only
1219 * from one place, because at least ARMC5 doesn't do that
1220 * automatically. */
1221#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1222MBEDTLS_ALWAYS_INLINE static inline
1223#else
1224static
1225#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1226int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001227 int ciphersuite,
1228 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001229#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1231 int encrypt_then_mac,
1232#endif
1233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1234 int trunc_hmac,
1235#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001236#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001237#if defined(MBEDTLS_ZLIB_SUPPORT)
1238 int compression,
1239#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001240 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001241 int minor_ver,
1242 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001243 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001245 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 unsigned char keyblk[256];
1247 unsigned char *key1;
1248 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001249 unsigned char *mac_enc;
1250 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001251 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001252 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001253 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001254 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001256 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001257
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001258#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1259 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1260 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001261 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001262 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001263#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001264
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001265 /*
1266 * Some data just needs copying into the structure
1267 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1269 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001270 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001272
1273#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001274 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001275#else
1276 ((void) minor_ver);
1277#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001279#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1280 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1281#endif
1282
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001283 /*
1284 * Get various info structures
1285 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001286 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001287 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001288 {
1289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001290 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 cipher_info = mbedtls_cipher_info_from_type(
1295 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( cipher_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_cipher( 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 Becker473f98f2019-06-26 10:27:32 +01001303 md_info = mbedtls_md_info_from_type(
1304 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001305 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001308 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001310 }
1311
Hanno Beckera5a2b082019-05-15 14:03:01 +01001312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 /* Copy own and peer's CID if the use of the CID
1314 * extension has been negotiated. */
1315 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1316 {
1317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001318
Hanno Becker4932f9f2019-05-03 15:23:51 +01001319 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001320 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001321 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001323
1324 transform->out_cid_len = ssl->handshake->peer_cid_len;
1325 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1326 ssl->handshake->peer_cid_len );
1327 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1328 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001329 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001330#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001331
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001333 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001334 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001335 ret = ssl_prf( minor_ver,
1336 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1337 master, 48, "key expansion", randbytes, 64,
1338 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001339 if( ret != 0 )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001342 return( ret );
1343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001346 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001347 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001348 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 /*
1352 * Determine the appropriate key, IV and MAC length.
1353 */
Paul Bakker68884e32013-01-07 18:20:04 +01001354
Hanno Beckere7f2df02017-12-27 08:17:40 +00001355 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001356
Hanno Beckerf1229442018-01-03 15:32:31 +00001357#if defined(MBEDTLS_GCM_C) || \
1358 defined(MBEDTLS_CCM_C) || \
1359 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001361 cipher_info->mode == MBEDTLS_MODE_CCM ||
1362 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001364 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001365 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001366 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1367 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001368
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001369 /* All modes haves 96-bit IVs;
1370 * GCM and CCM has 4 implicit and 8 explicit bytes
1371 * ChachaPoly has all 12 bytes implicit
1372 */
Paul Bakker68884e32013-01-07 18:20:04 +01001373 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001374 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1375 transform->fixed_ivlen = 12;
1376 else
1377 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001378 }
1379 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001380#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1381#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1382 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1383 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001384 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001385 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1387 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001390 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001393 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001394 mac_key_len = mbedtls_md_get_size( md_info );
1395 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001398 /*
1399 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1400 * (rfc 6066 page 13 or rfc 2104 section 4),
1401 * so we only need to adjust the length here.
1402 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001403 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001406
1407#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1408 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001409 * HMAC implementation which also truncates the key
1410 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001411 mac_key_len = transform->maclen;
1412#endif
1413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001415
1416 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001417 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001419 else
1420#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1421 {
1422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Hanno Beckera9d5c452019-07-25 16:47:12 +01001426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001427 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001428 (unsigned) transform->ivlen,
1429 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
1431 /*
1432 * Finally setup the cipher contexts, IVs and MAC secrets.
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001435 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001437 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001438 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Paul Bakker68884e32013-01-07 18:20:04 +01001440 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001441 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001443 /*
1444 * This is not used in TLS v1.1.
1445 */
Paul Bakker48916f92012-09-16 19:57:18 +00001446 iv_copy_len = ( transform->fixed_ivlen ) ?
1447 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1449 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001450 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_SSL_CLI_C */
1454#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001455 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001457 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001458 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
Hanno Becker81c7b182017-11-09 18:39:33 +00001460 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001461 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001463 /*
1464 * This is not used in TLS v1.1.
1465 */
Paul Bakker48916f92012-09-16 19:57:18 +00001466 iv_copy_len = ( transform->fixed_ivlen ) ?
1467 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001468 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1469 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001472 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Hanno Becker92231322018-01-03 15:32:51 +00001479#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001481 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001482 {
Hanno Becker92231322018-01-03 15:32:51 +00001483 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001487 }
1488
Hanno Becker81c7b182017-11-09 18:39:33 +00001489 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1490 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001491 }
1492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1495 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001496 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001497 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001498 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1499 For AEAD-based ciphersuites, there is nothing to do here. */
1500 if( mac_key_len != 0 )
1501 {
1502 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1503 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1504 }
Paul Bakker68884e32013-01-07 18:20:04 +01001505 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001506 else
1507#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001511 }
Hanno Becker92231322018-01-03 15:32:51 +00001512#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1515 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001516 {
1517 int ret = 0;
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520
Hanno Beckere7f2df02017-12-27 08:17:40 +00001521 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001522 transform->iv_enc, transform->iv_dec,
1523 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001524 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001525 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001529 }
1530 }
Hanno Becker92231322018-01-03 15:32:51 +00001531#else
1532 ((void) mac_dec);
1533 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001535
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001536#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1537 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001538 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001539 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001540 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001541 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001542 iv_copy_len );
1543 }
1544#endif
1545
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001546 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001547 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 return( ret );
1551 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001552
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001553 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 cipher_info ) ) != 0 )
1555 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 return( ret );
1558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001561 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 return( ret );
1566 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001569 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 return( ret );
1574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_CIPHER_MODE_CBC)
1577 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1580 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001583 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001584 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1587 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001590 return( ret );
1591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001595 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001596
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001597 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001599 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602
Paul Bakker48916f92012-09-16 19:57:18 +00001603 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1604 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001606 if( deflateInit( &transform->ctx_deflate,
1607 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001608 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1611 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612 }
1613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 return( 0 );
1617}
1618
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001619#if defined(MBEDTLS_SSL_PROTO_SSL3)
1620static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1621 unsigned char hash[36],
1622 size_t *hlen )
1623{
1624 mbedtls_md5_context md5;
1625 mbedtls_sha1_context sha1;
1626 unsigned char pad_1[48];
1627 unsigned char pad_2[48];
1628
1629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1630
1631 mbedtls_md5_init( &md5 );
1632 mbedtls_sha1_init( &sha1 );
1633
1634 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1635 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1636
1637 memset( pad_1, 0x36, 48 );
1638 memset( pad_2, 0x5C, 48 );
1639
1640 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1641 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1642 mbedtls_md5_finish_ret( &md5, hash );
1643
1644 mbedtls_md5_starts_ret( &md5 );
1645 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1646 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1647 mbedtls_md5_update_ret( &md5, hash, 16 );
1648 mbedtls_md5_finish_ret( &md5, hash );
1649
1650 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1651 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1652 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1653
1654 mbedtls_sha1_starts_ret( &sha1 );
1655 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1656 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1657 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1658 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1659
1660 *hlen = 36;
1661
1662 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1664
1665 mbedtls_md5_free( &md5 );
1666 mbedtls_sha1_free( &sha1 );
1667
1668 return;
1669}
1670#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1671
1672#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1673static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1674 unsigned char hash[36],
1675 size_t *hlen )
1676{
1677 mbedtls_md5_context md5;
1678 mbedtls_sha1_context sha1;
1679
1680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1681
1682 mbedtls_md5_init( &md5 );
1683 mbedtls_sha1_init( &sha1 );
1684
1685 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1686 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1687
1688 mbedtls_md5_finish_ret( &md5, hash );
1689 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1690
1691 *hlen = 36;
1692
1693 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1695
1696 mbedtls_md5_free( &md5 );
1697 mbedtls_sha1_free( &sha1 );
1698
1699 return;
1700}
1701#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1702
1703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1704#if defined(MBEDTLS_SHA256_C)
1705static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1706 unsigned char hash[32],
1707 size_t *hlen )
1708{
1709 mbedtls_sha256_context sha256;
1710
1711 mbedtls_sha256_init( &sha256 );
1712
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1714
1715 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1716 mbedtls_sha256_finish_ret( &sha256, hash );
1717
1718 *hlen = 32;
1719
1720 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1722
1723 mbedtls_sha256_free( &sha256 );
1724
1725 return;
1726}
1727#endif /* MBEDTLS_SHA256_C */
1728
1729#if defined(MBEDTLS_SHA512_C)
1730static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1731 unsigned char hash[48],
1732 size_t *hlen )
1733{
1734 mbedtls_sha512_context sha512;
1735
1736 mbedtls_sha512_init( &sha512 );
1737
1738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1739
1740 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1741 mbedtls_sha512_finish_ret( &sha512, hash );
1742
1743 *hlen = 48;
1744
1745 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1747
1748 mbedtls_sha512_free( &sha512 );
1749
1750 return;
1751}
1752#endif /* MBEDTLS_SHA512_C */
1753#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1754
Hanno Becker2f41b242019-08-15 17:29:43 +01001755int mbedtls_ssl_calc_verify( int minor_ver,
1756 mbedtls_md_type_t hash,
1757 mbedtls_ssl_context const *ssl,
1758 unsigned char *dst,
1759 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001760{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001761#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1762 (void) hash;
1763#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001765#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001766 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001767 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001768 else
1769#endif
1770#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001771 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001772 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001773 else
1774#endif
1775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1776#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001777 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1778 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001780 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001781 }
1782 else
1783#endif
1784#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001785 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001786 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001787 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001788 }
1789 else
1790#endif
1791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1792 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1794 }
1795
1796 return( 0 );
1797}
1798
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001800 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 *
1802 * Parameters:
1803 * [in/out] handshake
1804 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1805 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001806 * [out] master
1807 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001808 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001809static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001810 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001811 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001812{
1813 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001814
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001815/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1816/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1817/* (void) ssl; */
1818/* #endif */
1819
1820 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1821 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001822
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001823#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001824 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1827 return( 0 );
1828 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001829#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001830
1831 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1832 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001833
1834#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001835 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1836 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001837 {
1838 unsigned char session_hash[48];
1839 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001840
Hanno Becker2f41b242019-08-15 17:29:43 +01001841 mbedtls_ssl_calc_verify(
1842 mbedtls_ssl_get_minor_ver( ssl ),
1843 mbedtls_ssl_suite_get_mac( ciphersuite ),
1844 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001845
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001846 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1847 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001849 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1850 mbedtls_ssl_suite_get_mac( ciphersuite ),
1851 handshake->premaster, handshake->pmslen,
1852 "extended master secret",
1853 session_hash, hash_len,
1854 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001855 }
1856 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001858 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001859 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1860 mbedtls_ssl_suite_get_mac( ciphersuite ),
1861 handshake->premaster, handshake->pmslen,
1862 "master secret",
1863 handshake->randbytes, 64,
1864 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001865 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001866 if( ret != 0 )
1867 {
1868 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1869 return( ret );
1870 }
1871
1872 mbedtls_platform_zeroize( handshake->premaster,
1873 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001874
1875 return( 0 );
1876}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001877
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001878int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1879{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001880 int ret;
1881
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1883
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001884 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001885 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001886 ssl->session_negotiate->master,
1887 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001888 if( ret != 0 )
1889 {
1890 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1891 return( ret );
1892 }
1893
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001894 /* Swap the client and server random values:
1895 * - MS derivation wanted client+server (RFC 5246 8.1)
1896 * - key derivation wants server+client (RFC 5246 6.3) */
1897 {
1898 unsigned char tmp[64];
1899 memcpy( tmp, ssl->handshake->randbytes, 64 );
1900 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1901 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1902 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1903 }
1904
1905 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001906 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001907 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1908 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001909#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001912#endif
1913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001914 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001915#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001916#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001917#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001918 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001919#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001921 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001922 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1923 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001924 if( ret != 0 )
1925 {
1926 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1927 return( ret );
1928 }
1929
1930 /* We no longer need Server/ClientHello.random values */
1931 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1932 sizeof( ssl->handshake->randbytes ) );
1933
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001934 /* Allocate compression buffer */
1935#if defined(MBEDTLS_ZLIB_SUPPORT)
1936 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1937 ssl->compress_buf == NULL )
1938 {
1939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1940 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1941 if( ssl->compress_buf == NULL )
1942 {
1943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001944 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1946 }
1947 }
1948#endif
1949
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1951
1952 return( 0 );
1953}
1954
Hanno Becker09d23642019-07-22 17:18:18 +01001955int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1956{
1957 int ret;
1958
1959 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1960 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1961
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962#if defined(MBEDTLS_USE_TINYCRYPT)
1963 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001964 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001966 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1967 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1968 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1969 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1970 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001971 {
1972 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001973 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001974
1975 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1976 ssl->handshake->ecdh_privkey,
1977 ssl->handshake->premaster,
1978 uecc_curve ) )
1979 {
1980 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1981 }
1982
1983 ssl->handshake->pmslen = NUM_ECC_BYTES;
1984 }
1985 else
1986#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001987#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1988 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1989 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1990 {
1991 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1992 ssl->handshake->premaster,
1993 MBEDTLS_PREMASTER_SIZE,
1994 &ssl->handshake->pmslen,
1995 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001996 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001997 {
1998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1999 return( ret );
2000 }
2001
2002 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2003 }
2004 else
2005#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002006#if defined(MBEDTLS_ECDH_C) && \
2007 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2008 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2009 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2010 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002011 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2012 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2013 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2014 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2015 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2016 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2017 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2018 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2019 {
2020 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2021 &ssl->handshake->pmslen,
2022 ssl->handshake->premaster,
2023 MBEDTLS_MPI_MAX_SIZE,
2024 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002025 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002026 {
2027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2028 return( ret );
2029 }
2030
2031 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2032 }
2033 else
2034#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2035 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2036 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2037 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2038#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2039 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2040 {
2041 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002042 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002043 {
2044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2045 return( ret );
2046 }
2047 }
2048 else
2049#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2050#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2051 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2052 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2053 {
2054 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2055 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2056 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002057 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002058 if( ret != 0 )
2059 {
2060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2061 return( ret );
2062 }
2063 }
2064 else
2065#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2066#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2067 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2068 == MBEDTLS_KEY_EXCHANGE_RSA )
2069 {
2070 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002071 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002072 * ssl_rsa_generate_partial_pms(). Only the
2073 * PMS length needs to be set. */
2074 ssl->handshake->pmslen = 48;
2075 }
2076 else
2077#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2078 {
2079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2081 }
2082
2083 return( 0 );
2084}
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2087int 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 +02002088{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002089 unsigned char *p = ssl->handshake->premaster;
2090 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002091 const unsigned char *psk = ssl->conf->psk;
2092 size_t psk_len = ssl->conf->psk_len;
2093
2094 /* If the psk callback was called, use its result */
2095 if( ssl->handshake->psk != NULL )
2096 {
2097 psk = ssl->handshake->psk;
2098 psk_len = ssl->handshake->psk_len;
2099 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002100
2101 /*
2102 * PMS = struct {
2103 * opaque other_secret<0..2^16-1>;
2104 * opaque psk<0..2^16-1>;
2105 * };
2106 * with "other_secret" depending on the particular key exchange
2107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2109 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002110 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002111 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002114 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002115
2116 if( end < p || (size_t)( end - p ) < psk_len )
2117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2118
2119 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002120 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002121 }
2122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2124#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2125 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002126 {
2127 /*
2128 * other_secret already set by the ClientKeyExchange message,
2129 * and is 48 bytes long
2130 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002131 if( end - p < 2 )
2132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2133
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002134 *p++ = 0;
2135 *p++ = 48;
2136 p += 48;
2137 }
2138 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2140#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2141 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002143 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002144 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002146 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002148 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002149 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002150 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002153 return( ret );
2154 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002155 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002156 p += len;
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 }
2160 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2162#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2163 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002164 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002165 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 size_t zlen;
2167
Hanno Becker982da7e2019-09-02 09:47:39 +01002168#if defined(MBEDTLS_USE_TINYCRYPT)
2169 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2170 ((void) ret);
2171
2172 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2173 ssl->handshake->ecdh_privkey,
2174 p + 2,
2175 uecc_curve ) )
2176 {
2177 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2178 }
2179
2180 zlen = NUM_ECC_BYTES;
2181#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002183 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002184 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002185 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 return( ret );
2189 }
2190
Hanno Becker982da7e2019-09-02 09:47:39 +01002191 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2192 MBEDTLS_DEBUG_ECDH_Z );
2193#endif /* MBEDTLS_USE_TINYCRYPT */
2194
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002195 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002196 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002197 }
2198 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002203 }
2204
2205 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002206 if( end - p < 2 )
2207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002208
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002209 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002210
2211 if( end < p || (size_t)( end - p ) < psk_len )
2212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2213
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002214 memcpy( p, psk, psk_len );
2215 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002216
2217 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2218
2219 return( 0 );
2220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002224/*
2225 * SSLv3.0 MAC functions
2226 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002227#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002228static void ssl_mac( mbedtls_md_context_t *md_ctx,
2229 const unsigned char *secret,
2230 const unsigned char *buf, size_t len,
2231 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002232 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233{
2234 unsigned char header[11];
2235 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002236 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2238 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002239
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002240 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002242 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002243 else
Paul Bakker68884e32013-01-07 18:20:04 +01002244 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
2246 memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002247 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002248 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Paul Bakker68884e32013-01-07 18:20:04 +01002250 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_md_starts( md_ctx );
2252 mbedtls_md_update( md_ctx, secret, md_size );
2253 mbedtls_md_update( md_ctx, padding, padlen );
2254 mbedtls_md_update( md_ctx, header, 11 );
2255 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002256 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Paul Bakker68884e32013-01-07 18:20:04 +01002258 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_md_starts( md_ctx );
2260 mbedtls_md_update( md_ctx, secret, md_size );
2261 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002262 mbedtls_md_update( md_ctx, out, md_size );
2263 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002266
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002267/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002268 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002269#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002270 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2271 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2272 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2273/* This function makes sure every byte in the memory region is accessed
2274 * (in ascending addresses order) */
2275static void ssl_read_memory( unsigned char *p, size_t len )
2276{
2277 unsigned char acc = 0;
2278 volatile unsigned char force;
2279
2280 for( ; len != 0; p++, len-- )
2281 acc ^= *p;
2282
2283 force = acc;
2284 (void) force;
2285}
2286#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2287
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002288/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002290 */
Hanno Becker3307b532017-12-27 21:37:21 +00002291
Hanno Beckera5a2b082019-05-15 14:03:01 +01002292#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002293/* This functions transforms a DTLS plaintext fragment and a record content
2294 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002295 *
2296 * struct {
2297 * opaque content[DTLSPlaintext.length];
2298 * ContentType real_type;
2299 * uint8 zeros[length_of_padding];
2300 * } DTLSInnerPlaintext;
2301 *
2302 * Input:
2303 * - `content`: The beginning of the buffer holding the
2304 * plaintext to be wrapped.
2305 * - `*content_size`: The length of the plaintext in Bytes.
2306 * - `max_len`: The number of Bytes available starting from
2307 * `content`. This must be `>= *content_size`.
2308 * - `rec_type`: The desired record content type.
2309 *
2310 * Output:
2311 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2312 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2313 *
2314 * Returns:
2315 * - `0` on success.
2316 * - A negative error code if `max_len` didn't offer enough space
2317 * for the expansion.
2318 */
2319static int ssl_cid_build_inner_plaintext( unsigned char *content,
2320 size_t *content_size,
2321 size_t remaining,
2322 uint8_t rec_type )
2323{
2324 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002325 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2326 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2327 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002328
2329 /* Write real content type */
2330 if( remaining == 0 )
2331 return( -1 );
2332 content[ len ] = rec_type;
2333 len++;
2334 remaining--;
2335
2336 if( remaining < pad )
2337 return( -1 );
2338 memset( content + len, 0, pad );
2339 len += pad;
2340 remaining -= pad;
2341
2342 *content_size = len;
2343 return( 0 );
2344}
2345
Hanno Becker7dc25772019-05-20 15:08:01 +01002346/* This function parses a DTLSInnerPlaintext structure.
2347 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002348static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2349 size_t *content_size,
2350 uint8_t *rec_type )
2351{
2352 size_t remaining = *content_size;
2353
2354 /* Determine length of padding by skipping zeroes from the back. */
2355 do
2356 {
2357 if( remaining == 0 )
2358 return( -1 );
2359 remaining--;
2360 } while( content[ remaining ] == 0 );
2361
2362 *content_size = remaining;
2363 *rec_type = content[ remaining ];
2364
2365 return( 0 );
2366}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002367#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002368
Hanno Becker99abf512019-05-20 14:50:53 +01002369/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002370 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002371static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002372 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002373 mbedtls_record *rec )
2374{
Hanno Becker99abf512019-05-20 14:50:53 +01002375 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002376 *
2377 * additional_data = seq_num + TLSCompressed.type +
2378 * TLSCompressed.version + TLSCompressed.length;
2379 *
Hanno Becker99abf512019-05-20 14:50:53 +01002380 * For the CID extension, this is extended as follows
2381 * (quoting draft-ietf-tls-dtls-connection-id-05,
2382 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 *
2384 * additional_data = seq_num + DTLSPlaintext.type +
2385 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002386 * cid +
2387 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002388 * length_of_DTLSInnerPlaintext;
2389 */
2390
Hanno Becker3307b532017-12-27 21:37:21 +00002391 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2392 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002393 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002394
Hanno Beckera5a2b082019-05-15 14:03:01 +01002395#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002396 if( rec->cid_len != 0 )
2397 {
2398 memcpy( add_data + 11, rec->cid, rec->cid_len );
2399 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002400 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2401 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002402 *add_data_len = 13 + 1 + rec->cid_len;
2403 }
2404 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002405#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002406 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002407 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002408 *add_data_len = 13;
2409 }
Hanno Becker3307b532017-12-27 21:37:21 +00002410}
2411
Hanno Becker611a83b2018-01-03 14:27:32 +00002412int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2413 mbedtls_ssl_transform *transform,
2414 mbedtls_record *rec,
2415 int (*f_rng)(void *, unsigned char *, size_t),
2416 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002419 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002420 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002421 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002422 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002423 size_t post_avail;
2424
2425 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002426#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002427 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002428 ((void) ssl);
2429#endif
2430
2431 /* The PRNG is used for dynamic IV generation that's used
2432 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2433#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2434 ( defined(MBEDTLS_AES_C) || \
2435 defined(MBEDTLS_ARIA_C) || \
2436 defined(MBEDTLS_CAMELLIA_C) ) && \
2437 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2438 ((void) f_rng);
2439 ((void) p_rng);
2440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Hanno Becker3307b532017-12-27 21:37:21 +00002444 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002445 {
Hanno Becker3307b532017-12-27 21:37:21 +00002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2448 }
Hanno Becker505089d2019-05-01 09:45:57 +01002449 if( rec == NULL
2450 || rec->buf == NULL
2451 || rec->buf_len < rec->data_offset
2452 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002454 || rec->cid_len != 0
2455#endif
2456 )
Hanno Becker3307b532017-12-27 21:37:21 +00002457 {
2458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002460 }
2461
Hanno Becker3307b532017-12-27 21:37:21 +00002462 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002463 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002465 data, rec->data_len );
2466
2467 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2468
2469 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2470 {
2471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2472 (unsigned) rec->data_len,
2473 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2475 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002476
Hanno Beckera5a2b082019-05-15 14:03:01 +01002477#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002478 /*
2479 * Add CID information
2480 */
2481 rec->cid_len = transform->out_cid_len;
2482 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2483 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002484
2485 if( rec->cid_len != 0 )
2486 {
2487 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002488 * Wrap plaintext into DTLSInnerPlaintext structure.
2489 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002490 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002491 * Note that this changes `rec->data_len`, and hence
2492 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002493 */
2494 if( ssl_cid_build_inner_plaintext( data,
2495 &rec->data_len,
2496 post_avail,
2497 rec->type ) != 0 )
2498 {
2499 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2500 }
2501
2502 rec->type = MBEDTLS_SSL_MSG_CID;
2503 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002505
Hanno Becker92c930f2019-04-29 17:31:37 +01002506 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002509 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002511#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 if( mode == MBEDTLS_MODE_STREAM ||
2513 ( mode == MBEDTLS_MODE_CBC
2514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002515 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516#endif
2517 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 {
Hanno Becker3307b532017-12-27 21:37:21 +00002519 if( post_avail < transform->maclen )
2520 {
2521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2522 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2523 }
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002526 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2527 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002528 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002529 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002530 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2531 data, rec->data_len, rec->ctr, rec->type, mac );
2532 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002533 }
2534 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2537 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002538 if( mbedtls_ssl_ver_geq(
2539 mbedtls_ssl_transform_get_minor_ver( transform ),
2540 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002541 {
Hanno Becker992b6872017-11-09 18:57:39 +00002542 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2543
Hanno Beckere83efe62019-04-29 13:52:53 +01002544 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002545
Hanno Becker3307b532017-12-27 21:37:21 +00002546 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002547 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002548 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2549 data, rec->data_len );
2550 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2551 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2552
2553 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554 }
2555 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002560 }
2561
Hanno Becker3307b532017-12-27 21:37:21 +00002562 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2563 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002564
Hanno Becker3307b532017-12-27 21:37:21 +00002565 rec->data_len += transform->maclen;
2566 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002568 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002569#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 /*
2572 * Encrypt
2573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2575 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002577 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002578 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002580 "including %d bytes of padding",
2581 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker3307b532017-12-27 21:37:21 +00002583 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2584 transform->iv_enc, transform->ivlen,
2585 data, rec->data_len,
2586 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002589 return( ret );
2590 }
2591
Hanno Becker3307b532017-12-27 21:37:21 +00002592 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
Paul Bakker68884e32013-01-07 18:20:04 +01002598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601#if defined(MBEDTLS_GCM_C) || \
2602 defined(MBEDTLS_CCM_C) || \
2603 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605 mode == MBEDTLS_MODE_CCM ||
2606 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002607 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002608 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002610 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002611
Hanno Becker3307b532017-12-27 21:37:21 +00002612 /* Check that there's space for both the authentication tag
2613 * and the explicit IV before and after the record content. */
2614 if( post_avail < transform->taglen ||
2615 rec->data_offset < explicit_iv_len )
2616 {
2617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2618 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2619 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002620
Paul Bakker68884e32013-01-07 18:20:04 +01002621 /*
2622 * Generate IV
2623 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2625 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002626 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002628 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2629 explicit_iv_len );
2630 /* Prefix record content with explicit IV. */
2631 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 }
2633 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2634 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002635 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 unsigned char i;
2637
2638 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2639
2640 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002641 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 }
2643 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002644 {
2645 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002648 }
2649
Hanno Beckere83efe62019-04-29 13:52:53 +01002650 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002651
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2653 iv, transform->ivlen );
2654 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002655 data - explicit_iv_len, explicit_iv_len );
2656 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002657 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002660 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002661
Paul Bakker68884e32013-01-07 18:20:04 +01002662 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002663 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002664 */
Hanno Becker3307b532017-12-27 21:37:21 +00002665
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002666 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002667 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002668 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002669 data, rec->data_len, /* source */
2670 data, &rec->data_len, /* destination */
2671 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002674 return( ret );
2675 }
2676
Hanno Becker3307b532017-12-27 21:37:21 +00002677 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2678 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679
Hanno Becker3307b532017-12-27 21:37:21 +00002680 rec->data_len += transform->taglen + explicit_iv_len;
2681 rec->data_offset -= explicit_iv_len;
2682 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002683 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2687#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002688 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002691 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002692 size_t padlen, i;
2693 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002694
Hanno Becker3307b532017-12-27 21:37:21 +00002695 /* Currently we're always using minimal padding
2696 * (up to 255 bytes would be allowed). */
2697 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2698 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 padlen = 0;
2700
Hanno Becker3307b532017-12-27 21:37:21 +00002701 /* Check there's enough space in the buffer for the padding. */
2702 if( post_avail < padlen + 1 )
2703 {
2704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2705 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2706 }
2707
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002709 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Hanno Becker3307b532017-12-27 21:37:21 +00002711 rec->data_len += padlen + 1;
2712 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002716 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2717 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002718 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002719 if( mbedtls_ssl_ver_geq(
2720 mbedtls_ssl_transform_get_minor_ver( transform ),
2721 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002722 {
Hanno Becker3307b532017-12-27 21:37:21 +00002723 if( f_rng == NULL )
2724 {
2725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2727 }
2728
2729 if( rec->data_offset < transform->ivlen )
2730 {
2731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2732 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2733 }
2734
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002735 /*
2736 * Generate IV
2737 */
Hanno Becker3307b532017-12-27 21:37:21 +00002738 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002739 if( ret != 0 )
2740 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002741
Hanno Becker3307b532017-12-27 21:37:21 +00002742 memcpy( data - transform->ivlen, transform->iv_enc,
2743 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002744
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002745 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002749 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002750 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002751 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Hanno Becker3307b532017-12-27 21:37:21 +00002753 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2754 transform->iv_enc,
2755 transform->ivlen,
2756 data, rec->data_len,
2757 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002760 return( ret );
2761 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002762
Hanno Becker3307b532017-12-27 21:37:21 +00002763 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002767 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002770 if( mbedtls_ssl_ver_lt(
2771 mbedtls_ssl_transform_get_minor_ver( transform ),
2772 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002773 {
2774 /*
2775 * Save IV in SSL3 and TLS1
2776 */
Hanno Becker3307b532017-12-27 21:37:21 +00002777 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2778 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
Hanno Becker3307b532017-12-27 21:37:21 +00002780 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002781#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002782 {
2783 data -= transform->ivlen;
2784 rec->data_offset -= transform->ivlen;
2785 rec->data_len += transform->ivlen;
2786 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002789 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002790 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002791 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2792
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 /*
2794 * MAC(MAC_write_key, seq_num +
2795 * TLSCipherText.type +
2796 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002797 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002798 * IV + // except for TLS 1.0
2799 * ENC(content + padding + padding_length));
2800 */
Hanno Becker3307b532017-12-27 21:37:21 +00002801
2802 if( post_avail < transform->maclen)
2803 {
2804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2805 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2806 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002807
Hanno Beckere83efe62019-04-29 13:52:53 +01002808 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002812 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002813
Hanno Becker3307b532017-12-27 21:37:21 +00002814 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002815 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002816 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2817 data, rec->data_len );
2818 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2819 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002820
Hanno Becker3307b532017-12-27 21:37:21 +00002821 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002822
Hanno Becker3307b532017-12-27 21:37:21 +00002823 rec->data_len += transform->maclen;
2824 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002825 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002831 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 /* Make extra sure authentication was performed, exactly once */
2838 if( auth_done != 1 )
2839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002842 }
2843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
2846 return( 0 );
2847}
2848
Hanno Becker40478be2019-07-12 08:23:59 +01002849int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002850 mbedtls_ssl_transform *transform,
2851 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002855 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002856#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002857 size_t padlen = 0, correct = 1;
2858#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002859 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002860 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002861 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002862
Hanno Becker611a83b2018-01-03 14:27:32 +00002863#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002864 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002865 ((void) ssl);
2866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002869 if( rec == NULL ||
2870 rec->buf == NULL ||
2871 rec->buf_len < rec->data_offset ||
2872 rec->buf_len - rec->data_offset < rec->data_len )
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002876 }
2877
Hanno Becker4c6876b2017-12-27 21:28:58 +00002878 data = rec->buf + rec->data_offset;
2879 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Hanno Beckera5a2b082019-05-15 14:03:01 +01002881#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002882 /*
2883 * Match record's CID with incoming CID.
2884 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002885 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03002886 mbedtls_platform_memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
Hanno Beckerabd7c892019-05-08 13:02:22 +01002887 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002888 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002889 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2893 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002894 {
2895 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002896 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2897 transform->iv_dec,
2898 transform->ivlen,
2899 data, rec->data_len,
2900 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002903 return( ret );
2904 }
2905
Hanno Becker4c6876b2017-12-27 21:28:58 +00002906 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
Paul Bakker68884e32013-01-07 18:20:04 +01002912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002914#if defined(MBEDTLS_GCM_C) || \
2915 defined(MBEDTLS_CCM_C) || \
2916 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002918 mode == MBEDTLS_MODE_CCM ||
2919 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002920 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002921 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002922 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002923
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002924 /*
2925 * Prepare IV from explicit and implicit data.
2926 */
2927
2928 /* Check that there's enough space for the explicit IV
2929 * (at the beginning of the record) and the MAC (at the
2930 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002931 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002934 "+ taglen (%d)", rec->data_len,
2935 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002937 }
Paul Bakker68884e32013-01-07 18:20:04 +01002938
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002939#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002940 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2941 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002942 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002943
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002944 /* Fixed */
2945 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2946 /* Explicit */
2947 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002948 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002949 else
2950#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2951#if defined(MBEDTLS_CHACHAPOLY_C)
2952 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002953 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002954 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002955 unsigned char i;
2956
2957 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2958
2959 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002960 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002961 }
2962 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002963#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002964 {
2965 /* Reminder if we ever add an AEAD mode with a different size */
2966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2967 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2968 }
2969
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002970 /* Group changes to data, data_len, and add_data, because
2971 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 data += explicit_iv_len;
2973 rec->data_offset += explicit_iv_len;
2974 rec->data_len -= explicit_iv_len + transform->taglen;
2975
Hanno Beckere83efe62019-04-29 13:52:53 +01002976 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002977 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002978 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002979
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002980 /* Because of the check above, we know that there are
2981 * explicit_iv_len Bytes preceeding data, and taglen
2982 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002983 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002984 * mbedtls_cipher_auth_decrypt() below. */
2985
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002986 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002987 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002988 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002989
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002990 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002991 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002992 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002993 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2994 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002995 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002996 data, rec->data_len,
2997 data, &olen,
2998 data + rec->data_len,
2999 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3004 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003005
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003006 return( ret );
3007 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003008 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003010 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003011 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003015 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3019#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003020 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003023 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003024
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 /*
Paul Bakker45829992013-01-03 14:52:21 +01003026 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003029 if( mbedtls_ssl_ver_geq(
3030 mbedtls_ssl_transform_get_minor_ver( transform ),
3031 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 {
3033 /* The ciphertext is prefixed with the CBC IV. */
3034 minlen += transform->ivlen;
3035 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif
Paul Bakker45829992013-01-03 14:52:21 +01003037
Hanno Becker4c6876b2017-12-27 21:28:58 +00003038 /* Size considerations:
3039 *
3040 * - The CBC cipher text must not be empty and hence
3041 * at least of size transform->ivlen.
3042 *
3043 * Together with the potential IV-prefix, this explains
3044 * the first of the two checks below.
3045 *
3046 * - The record must contain a MAC, either in plain or
3047 * encrypted, depending on whether Encrypt-then-MAC
3048 * is used or not.
3049 * - If it is, the message contains the IV-prefix,
3050 * the CBC ciphertext, and the MAC.
3051 * - If it is not, the padded plaintext, and hence
3052 * the CBC ciphertext, has at least length maclen + 1
3053 * because there is at least the padding length byte.
3054 *
3055 * As the CBC ciphertext is not empty, both cases give the
3056 * lower bound minlen + maclen + 1 on the record size, which
3057 * we test for in the second check below.
3058 */
3059 if( rec->data_len < minlen + transform->ivlen ||
3060 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 "+ 1 ) ( + expl IV )", rec->data_len,
3064 transform->ivlen,
3065 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003067 }
3068
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003069 /*
3070 * Authenticate before decrypt if enabled
3071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003074 {
Hanno Becker992b6872017-11-09 18:57:39 +00003075 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003078
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003079 /* Update data_len in tandem with add_data.
3080 *
3081 * The subtraction is safe because of the previous check
3082 * data_len >= minlen + maclen + 1.
3083 *
3084 * Afterwards, we know that data + data_len is followed by at
3085 * least maclen Bytes, which justifies the call to
3086 * mbedtls_ssl_safer_memcmp() below.
3087 *
3088 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003089 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003090 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003091
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003092 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003093 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3094 add_data_len );
3095 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3096 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003097 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3098 data, rec->data_len );
3099 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3100 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003101
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3103 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003104 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003105 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003106
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003107 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3109 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003113 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003114 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003117
3118 /*
3119 * Check length sanity
3120 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003121
3122 /* We know from above that data_len > minlen >= 0,
3123 * so the following check in particular implies that
3124 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003125 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003130 }
3131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003133 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003135 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003136 if( mbedtls_ssl_ver_geq(
3137 mbedtls_ssl_transform_get_minor_ver( transform ),
3138 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003139 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003140 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003141 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003142
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 data += transform->ivlen;
3144 rec->data_offset += transform->ivlen;
3145 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003148
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003149 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3150
Hanno Becker4c6876b2017-12-27 21:28:58 +00003151 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3152 transform->iv_dec, transform->ivlen,
3153 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003156 return( ret );
3157 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003158
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003159 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003160 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003164 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003167 if( mbedtls_ssl_ver_lt(
3168 mbedtls_ssl_transform_get_minor_ver( transform ),
3169 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003170 {
3171 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003172 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3173 * records is equivalent to CBC decryption of the concatenation
3174 * of the records; in other words, IVs are maintained across
3175 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003176 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003177 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3178 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Hanno Becker4c6876b2017-12-27 21:28:58 +00003182 /* Safe since data_len >= minlen + maclen + 1, so after having
3183 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003184 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3185 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003187
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 if( auth_done == 1 )
3189 {
3190 correct *= ( rec->data_len >= padlen + 1 );
3191 padlen *= ( rec->data_len >= padlen + 1 );
3192 }
3193 else
Paul Bakker45829992013-01-03 14:52:21 +01003194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003196 if( rec->data_len < transform->maclen + padlen + 1 )
3197 {
3198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3199 rec->data_len,
3200 transform->maclen,
3201 padlen + 1 ) );
3202 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003203#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003204
3205 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3206 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Hanno Becker4c6876b2017-12-27 21:28:58 +00003209 padlen++;
3210
3211 /* Regardless of the validity of the padding,
3212 * we have data_len >= padlen here. */
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003215 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3216 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003218 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#if defined(MBEDTLS_SSL_DEBUG_ALL)
3221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003222 "should be no more than %d",
3223 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003224#endif
Paul Bakker45829992013-01-03 14:52:21 +01003225 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 }
3227 }
3228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3230#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3231 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003232 if( mbedtls_ssl_ver_gt(
3233 mbedtls_ssl_transform_get_minor_ver( transform ),
3234 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003236 /* The padding check involves a series of up to 256
3237 * consecutive memory reads at the end of the record
3238 * plaintext buffer. In order to hide the length and
3239 * validity of the padding, always perform exactly
3240 * `min(256,plaintext_len)` reads (but take into account
3241 * only the last `padlen` bytes for the padding check). */
3242 size_t pad_count = 0;
3243 size_t real_count = 0;
3244 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003245
Hanno Becker4c6876b2017-12-27 21:28:58 +00003246 /* Index of first padding byte; it has been ensured above
3247 * that the subtraction is safe. */
3248 size_t const padding_idx = rec->data_len - padlen;
3249 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3250 size_t const start_idx = rec->data_len - num_checks;
3251 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003252
Hanno Becker4c6876b2017-12-27 21:28:58 +00003253 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003254 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003255 real_count |= ( idx >= padding_idx );
3256 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003257 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003261 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003263#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003264 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3268 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003272 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003273
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 /* If the padding was found to be invalid, padlen == 0
3275 * and the subtraction is safe. If the padding was found valid,
3276 * padlen hasn't been changed and the previous assertion
3277 * data_len >= padlen still holds. */
3278 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003282 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003288#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003290 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
3293 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003294 * Authenticate if not done yet.
3295 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003297#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003298 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003299 {
Hanno Becker992b6872017-11-09 18:57:39 +00003300 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003301
Hanno Becker4c6876b2017-12-27 21:28:58 +00003302 /* If the initial value of padlen was such that
3303 * data_len < maclen + padlen + 1, then padlen
3304 * got reset to 1, and the initial check
3305 * data_len >= minlen + maclen + 1
3306 * guarantees that at this point we still
3307 * have at least data_len >= maclen.
3308 *
3309 * If the initial value of padlen was such that
3310 * data_len >= maclen + padlen + 1, then we have
3311 * subtracted either padlen + 1 (if the padding was correct)
3312 * or 0 (if the padding was incorrect) since then,
3313 * hence data_len >= maclen in any case.
3314 */
3315 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003316 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003319 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3320 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003321 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003322 ssl_mac( &transform->md_ctx_dec,
3323 transform->mac_dec,
3324 data, rec->data_len,
3325 rec->ctr, rec->type,
3326 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003327 }
3328 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3330#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3331 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003332 if( mbedtls_ssl_ver_gt(
3333 mbedtls_ssl_transform_get_minor_ver( transform ),
3334 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003335 {
3336 /*
3337 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003338 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003339 *
3340 * Known timing attacks:
3341 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3342 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003343 * To compensate for different timings for the MAC calculation
3344 * depending on how much padding was removed (which is determined
3345 * by padlen), process extra_run more blocks through the hash
3346 * function.
3347 *
3348 * The formula in the paper is
3349 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3350 * where L1 is the size of the header plus the decrypted message
3351 * plus CBC padding and L2 is the size of the header plus the
3352 * decrypted message. This is for an underlying hash function
3353 * with 64-byte blocks.
3354 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3355 * correctly. We round down instead of up, so -56 is the correct
3356 * value for our calculations instead of -55.
3357 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003358 * Repeat the formula rather than defining a block_size variable.
3359 * This avoids requiring division by a variable at runtime
3360 * (which would be marginally less efficient and would require
3361 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003362 */
3363 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003364 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003365
3366 /*
3367 * The next two sizes are the minimum and maximum values of
3368 * in_msglen over all padlen values.
3369 *
3370 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003371 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003372 *
3373 * Note that max_len + maclen is never more than the buffer
3374 * length, as we previously did in_msglen -= maclen too.
3375 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003376 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003377 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3378
Hanno Becker4c6876b2017-12-27 21:28:58 +00003379 memset( tmp, 0, sizeof( tmp ) );
3380
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003381 switch( mbedtls_md_get_type(
3382 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003383 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003384#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3385 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003386 case MBEDTLS_MD_MD5:
3387 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003388 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003389 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003390 extra_run =
3391 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3392 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003393 break;
3394#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003395#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003396 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003397 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003398 extra_run =
3399 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3400 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003401 break;
3402#endif
3403 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3406 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003407
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003408 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003409
Hanno Beckere83efe62019-04-29 13:52:53 +01003410 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3411 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3413 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003414 /* Make sure we access everything even when padlen > 0. This
3415 * makes the synchronisation requirements for just-in-time
3416 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003417 ssl_read_memory( data + rec->data_len, padlen );
3418 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003419
3420 /* Call mbedtls_md_process at least once due to cache attacks
3421 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003422 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003423 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003424
Hanno Becker4c6876b2017-12-27 21:28:58 +00003425 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003426
3427 /* Make sure we access all the memory that could contain the MAC,
3428 * before we check it in the next code block. This makes the
3429 * synchronisation requirements for just-in-time Prime+Probe
3430 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003431 ssl_read_memory( data + min_len,
3432 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003433 }
3434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3436 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003443 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3444 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
Hanno Becker4c6876b2017-12-27 21:28:58 +00003447 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3448 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#if defined(MBEDTLS_SSL_DEBUG_ALL)
3451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003452#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003453 correct = 0;
3454 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003455 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003456 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003457
3458 /*
3459 * Finally check the correct flag
3460 */
3461 if( correct == 0 )
3462 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003463#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003464
3465 /* Make extra sure authentication was performed, exactly once */
3466 if( auth_done != 1 )
3467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Hanno Beckera5a2b082019-05-15 14:03:01 +01003472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003473 if( rec->cid_len != 0 )
3474 {
3475 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3476 &rec->type );
3477 if( ret != 0 )
3478 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3479 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003480#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
3484 return( 0 );
3485}
3486
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003487#undef MAC_NONE
3488#undef MAC_PLAINTEXT
3489#undef MAC_CIPHERTEXT
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003492/*
3493 * Compression/decompression functions
3494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003496{
3497 int ret;
3498 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003499 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003500 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003501 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003505 if( len_pre == 0 )
3506 return( 0 );
3507
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 memcpy( msg_pre, ssl->out_msg, len_pre );
3509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 ssl->out_msglen ) );
3512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 ssl->out_msg, ssl->out_msglen );
3515
Paul Bakker48916f92012-09-16 19:57:18 +00003516 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3517 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3518 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003519 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
Paul Bakker48916f92012-09-16 19:57:18 +00003521 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003522 if( ret != Z_OK )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3525 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003526 }
3527
Angus Grattond8213d02016-05-25 20:56:48 +10003528 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003529 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532 ssl->out_msglen ) );
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535 ssl->out_msg, ssl->out_msglen );
3536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538
3539 return( 0 );
3540}
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543{
3544 int ret;
3545 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003546 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003548 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003551
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003552 if( len_pre == 0 )
3553 return( 0 );
3554
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555 memcpy( msg_pre, ssl->in_msg, len_pre );
3556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003558 ssl->in_msglen ) );
3559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 ssl->in_msg, ssl->in_msglen );
3562
Paul Bakker48916f92012-09-16 19:57:18 +00003563 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3564 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3565 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003566 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003567 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568
Paul Bakker48916f92012-09-16 19:57:18 +00003569 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 if( ret != Z_OK )
3571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3573 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574 }
3575
Angus Grattond8213d02016-05-25 20:56:48 +10003576 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003577 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580 ssl->in_msglen ) );
3581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003583 ssl->in_msg, ssl->in_msglen );
3584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586
3587 return( 0 );
3588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3592static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_PROTO_DTLS)
3595static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003596{
3597 /* If renegotiation is not enforced, retransmit until we would reach max
3598 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003599 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003600 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003601 uint32_t ratio =
3602 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3603 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003604 unsigned char doublings = 1;
3605
3606 while( ratio != 0 )
3607 {
3608 ++doublings;
3609 ratio >>= 1;
3610 }
3611
3612 if( ++ssl->renego_records_seen > doublings )
3613 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003615 return( 0 );
3616 }
3617 }
3618
3619 return( ssl_write_hello_request( ssl ) );
3620}
3621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003625 * Fill the input message buffer by appending data to it.
3626 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003627 *
3628 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3629 * available (from this read and/or a previous one). Otherwise, an error code
3630 * is returned (possibly EOF or WANT_READ).
3631 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003632 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3633 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3634 * since we always read a whole datagram at once.
3635 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003636 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003637 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003640{
Paul Bakker23986e52011-04-24 08:57:21 +00003641 int ret;
3642 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Hanno Beckera58a8962019-06-13 16:11:15 +01003646 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3647 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003650 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003652 }
3653
Angus Grattond8213d02016-05-25 20:56:48 +10003654 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003658 }
3659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003661 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003663 uint32_t timeout;
3664
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003665 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003666 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3667 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003668 {
3669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3670 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3672 }
3673
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003674 /*
3675 * The point is, we need to always read a full datagram at once, so we
3676 * sometimes read more then requested, and handle the additional data.
3677 * It could be the rest of the current record (while fetching the
3678 * header) and/or some other records in the same datagram.
3679 */
3680
3681 /*
3682 * Move to the next record in the already read datagram if applicable
3683 */
3684 if( ssl->next_record_offset != 0 )
3685 {
3686 if( ssl->in_left < ssl->next_record_offset )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003690 }
3691
3692 ssl->in_left -= ssl->next_record_offset;
3693
3694 if( ssl->in_left != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003697 ssl->next_record_offset ) );
3698 memmove( ssl->in_hdr,
3699 ssl->in_hdr + ssl->next_record_offset,
3700 ssl->in_left );
3701 }
3702
3703 ssl->next_record_offset = 0;
3704 }
3705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003708
3709 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003710 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003711 */
3712 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003715 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003716 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003717
3718 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003719 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003720 * are not at the beginning of a new record, the caller did something
3721 * wrong.
3722 */
3723 if( ssl->in_left != 0 )
3724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003727 }
3728
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729 /*
3730 * Don't even try to read if time's out already.
3731 * This avoids by-passing the timer when repeatedly receiving messages
3732 * that will end up being dropped.
3733 */
3734 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003735 {
3736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003737 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003738 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003739 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003740 {
Angus Grattond8213d02016-05-25 20:56:48 +10003741 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744 timeout = ssl->handshake->retransmit_timeout;
3745 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003746 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003749
Hanno Beckera58a8962019-06-13 16:11:15 +01003750 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3751 {
3752 ret = mbedtls_ssl_get_recv_timeout( ssl )
3753 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3754 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003755 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003756 {
3757 ret = mbedtls_ssl_get_recv( ssl )
3758 ( ssl->p_bio, ssl->in_hdr, len );
3759 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003762
3763 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003765 }
3766
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003767 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003770 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003773 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003774 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003777 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003783 return( ret );
3784 }
3785
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003786 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003789 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3790 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003792 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003793 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003796 return( ret );
3797 }
3798
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003799 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003800 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003802 }
3803
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 if( ret < 0 )
3805 return( ret );
3806
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003807 ssl->in_left = ret;
3808 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003809 MBEDTLS_SSL_TRANSPORT_ELSE
3810#endif /* MBEDTLS_SSL_PROTO_DTLS */
3811#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003814 ssl->in_left, nb_want ) );
3815
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003816 while( ssl->in_left < nb_want )
3817 {
3818 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003819
3820 if( ssl_check_timer( ssl ) != 0 )
3821 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3822 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003823 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003824 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003825 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003826 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3827 ssl->in_hdr + ssl->in_left, len,
3828 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003829 }
3830 else
3831 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003832 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003833 ssl->in_hdr + ssl->in_left, len );
3834 }
3835 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003838 ssl->in_left, nb_want ) );
3839 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003840
3841 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003843
3844 if( ret < 0 )
3845 return( ret );
3846
mohammad160352aecb92018-03-28 23:41:40 -07003847 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003848 {
Darryl Green11999bb2018-03-13 15:22:58 +00003849 MBEDTLS_SSL_DEBUG_MSG( 1,
3850 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003851 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3853 }
3854
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003855 ssl->in_left += ret;
3856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003858#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
3862 return( 0 );
3863}
3864
3865/*
3866 * Flush any data not yet written
3867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003869{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003870 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003871 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003874
Hanno Beckera58a8962019-06-13 16:11:15 +01003875 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003878 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003880 }
3881
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003882 /* Avoid incrementing counter if data is flushed */
3883 if( ssl->out_left == 0 )
3884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003886 return( 0 );
3887 }
3888
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 while( ssl->out_left > 0 )
3890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003892 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003893
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003895 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003898
3899 if( ret <= 0 )
3900 return( ret );
3901
mohammad160352aecb92018-03-28 23:41:40 -07003902 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003903 {
Darryl Green11999bb2018-03-13 15:22:58 +00003904 MBEDTLS_SSL_DEBUG_MSG( 1,
3905 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003906 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3908 }
3909
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 ssl->out_left -= ret;
3911 }
3912
Hanno Becker2b1e3542018-08-06 11:19:13 +01003913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003914 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003915 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003916 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003917 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003918 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003919#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003920#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003921 {
3922 ssl->out_hdr = ssl->out_buf + 8;
3923 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003924#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003925 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928
3929 return( 0 );
3930}
3931
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003932/*
3933 * Functions to handle the DTLS retransmission state machine
3934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936/*
3937 * Append current handshake message to current outgoing flight
3938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3943 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3944 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003945
3946 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003947 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003951 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952 }
3953
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003954 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003958 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959 }
3960
3961 /* Copy current handshake message with headers */
3962 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3963 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003964 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965 msg->next = NULL;
3966
3967 /* Append to the current flight */
3968 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003969 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 else
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003973 while( cur->next != NULL )
3974 cur = cur->next;
3975 cur->next = msg;
3976 }
3977
Hanno Becker3b235902018-08-06 09:54:53 +01003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003979 return( 0 );
3980}
3981
3982/*
3983 * Free the current flight of handshake messages
3984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 mbedtls_ssl_flight_item *cur = flight;
3988 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989
3990 while( cur != NULL )
3991 {
3992 next = cur->next;
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 mbedtls_free( cur->p );
3995 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003996
3997 cur = next;
3998 }
3999}
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4002static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004003#endif
4004
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004005/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004006 * Swap transform_out and out_ctr with the alternative ones
4007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 unsigned char tmp_out_ctr[8];
4012
4013 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004016 return;
4017 }
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004020
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004021 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004022 tmp_transform = ssl->transform_out;
4023 ssl->transform_out = ssl->handshake->alt_transform_out;
4024 ssl->handshake->alt_transform_out = tmp_transform;
4025
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004026 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004027 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4028 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004029 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004030
4031 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004032 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4035 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4040 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004041 }
4042 }
4043#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044}
4045
4046/*
4047 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004048 */
4049int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4050{
4051 int ret = 0;
4052
4053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4054
4055 ret = mbedtls_ssl_flight_transmit( ssl );
4056
4057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4058
4059 return( ret );
4060}
4061
4062/*
4063 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064 *
4065 * Need to remember the current message in case flush_output returns
4066 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004067 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004070{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004075 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004077
4078 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004079 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004080 ssl_swap_epochs( ssl );
4081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004083 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084
4085 while( ssl->handshake->cur_msg != NULL )
4086 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004088 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004089
Hanno Beckere1dcb032018-08-17 16:47:58 +01004090 int const is_finished =
4091 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4092 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4093
Hanno Becker04da1892018-08-14 13:22:10 +01004094 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4095 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4096
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004097 /* Swap epochs before sending Finished: we can't do it after
4098 * sending ChangeCipherSpec, in case write returns WANT_READ.
4099 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004100 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004101 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103 ssl_swap_epochs( ssl );
4104 }
4105
Hanno Becker67bc7c32018-08-06 11:33:50 +01004106 ret = ssl_get_remaining_payload_in_datagram( ssl );
4107 if( ret < 0 )
4108 return( ret );
4109 max_frag_len = (size_t) ret;
4110
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004111 /* CCS is copied as is, while HS messages may need fragmentation */
4112 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4113 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004114 if( max_frag_len == 0 )
4115 {
4116 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4117 return( ret );
4118
4119 continue;
4120 }
4121
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004122 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004123 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004124 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004125
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004126 /* Update position inside current message */
4127 ssl->handshake->cur_msg_p += cur->len;
4128 }
4129 else
4130 {
4131 const unsigned char * const p = ssl->handshake->cur_msg_p;
4132 const size_t hs_len = cur->len - 12;
4133 const size_t frag_off = p - ( cur->p + 12 );
4134 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004135 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004136
Hanno Beckere1dcb032018-08-17 16:47:58 +01004137 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004138 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004139 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004140 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004141
Hanno Becker67bc7c32018-08-06 11:33:50 +01004142 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4143 return( ret );
4144
4145 continue;
4146 }
4147 max_hs_frag_len = max_frag_len - 12;
4148
4149 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4150 max_hs_frag_len : rem_len;
4151
4152 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004155 (unsigned) cur_hs_frag_len,
4156 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004157 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004158
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004159 /* Messages are stored with handshake headers as if not fragmented,
4160 * copy beginning of headers then fill fragmentation fields.
4161 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4162 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004164 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4165 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4166 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004167
4168 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4169
Hanno Becker3f7b9732018-08-28 09:53:25 +01004170 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004171 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4172 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004173 ssl->out_msgtype = cur->type;
4174
4175 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004177 }
4178
4179 /* If done with the current message move to the next one if any */
4180 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4181 {
4182 if( cur->next != NULL )
4183 {
4184 ssl->handshake->cur_msg = cur->next;
4185 ssl->handshake->cur_msg_p = cur->next->p + 12;
4186 }
4187 else
4188 {
4189 ssl->handshake->cur_msg = NULL;
4190 ssl->handshake->cur_msg_p = NULL;
4191 }
4192 }
4193
4194 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004195 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004198 return( ret );
4199 }
4200 }
4201
Hanno Becker67bc7c32018-08-06 11:33:50 +01004202 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4203 return( ret );
4204
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004205 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4207 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004208 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004211 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4212 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004213
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004215
4216 return( 0 );
4217}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004218
4219/*
4220 * To be called when the last message of an incoming flight is received.
4221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004223{
4224 /* We won't need to resend that one any more */
4225 ssl_flight_free( ssl->handshake->flight );
4226 ssl->handshake->flight = NULL;
4227 ssl->handshake->cur_msg = NULL;
4228
4229 /* The next incoming flight will start with this msg_seq */
4230 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4231
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004232 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004233 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004234
Hanno Becker0271f962018-08-16 13:23:47 +01004235 /* Clear future message buffering structure. */
4236 ssl_buffering_free( ssl );
4237
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004238 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004239 ssl_set_timer( ssl, 0 );
4240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4242 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004245 }
4246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004248}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004249
4250/*
4251 * To be called when the last message of an outgoing flight is send.
4252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004254{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004255 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004256 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4259 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004262 }
4263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004267
Paul Bakker5121ce52009-01-03 21:22:43 +00004268/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004269 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004271
4272/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004273 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004274 *
4275 * - fill in handshake headers
4276 * - update handshake checksum
4277 * - DTLS: save message for resending
4278 * - then pass to the record layer
4279 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004280 * DTLS: except for HelloRequest, messages are only queued, and will only be
4281 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004282 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004283 * Inputs:
4284 * - ssl->out_msglen: 4 + actual handshake message len
4285 * (4 is the size of handshake headers for TLS)
4286 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4287 * - ssl->out_msg + 4: the handshake message body
4288 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004289 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004290 * - ssl->out_msglen: the length of the record contents
4291 * (including handshake headers but excluding record headers)
4292 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004293 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004294int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004295{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004296 int ret;
4297 const size_t hs_len = ssl->out_msglen - 4;
4298 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4301
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004302 /*
4303 * Sanity checks
4304 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004305 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004306 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4307 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004308 /* In SSLv3, the client might send a NoCertificate alert. */
4309#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004310 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004311 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004312 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4313 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004314#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4315 {
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4318 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004321 /* Whenever we send anything different from a
4322 * HelloRequest we should be in a handshake - double check. */
4323 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4324 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004325 ssl->handshake == NULL )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004332 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004333 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004335 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004338 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004339#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004340
Hanno Beckerb50a2532018-08-06 11:52:54 +01004341 /* Double-check that we did not exceed the bounds
4342 * of the outgoing record buffer.
4343 * This should never fail as the various message
4344 * writing functions must obey the bounds of the
4345 * outgoing record buffer, but better be safe.
4346 *
4347 * Note: We deliberately do not check for the MTU or MFL here.
4348 */
4349 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4350 {
4351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4352 "size %u, maximum %u",
4353 (unsigned) ssl->out_msglen,
4354 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4356 }
4357
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004358 /*
4359 * Fill handshake headers
4360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004363 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004364
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004365 /*
4366 * DTLS has additional fields in the Handshake layer,
4367 * between the length field and the actual payload:
4368 * uint16 message_seq;
4369 * uint24 fragment_offset;
4370 * uint24 fragment_length;
4371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004373 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004374 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004375 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004376 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004377 {
4378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4379 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004381 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4383 }
4384
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004385 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004386 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004387
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004388 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004389 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004390 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004391 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4392 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004393 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004394 }
4395 else
4396 {
4397 ssl->out_msg[4] = 0;
4398 ssl->out_msg[5] = 0;
4399 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004400
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004401 /* Handshake hashes are computed without fragmentation,
4402 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004403 memset( ssl->out_msg + 6, 0x00, 3 );
4404 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004405 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004407
Hanno Becker0207e532018-08-28 10:28:28 +01004408 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004409 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004410 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004413 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004415 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004416 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4417 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004418 {
4419 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004422 return( ret );
4423 }
4424 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004425 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004426#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004427 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004428 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004429 {
4430 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4431 return( ret );
4432 }
4433 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004434
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4436
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004437 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004438}
4439
4440/*
4441 * Record layer functions
4442 */
4443
4444/*
4445 * Write current record.
4446 *
4447 * Uses:
4448 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4449 * - ssl->out_msglen: length of the record content (excl headers)
4450 * - ssl->out_msg: record content
4451 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004452int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004453{
4454 int ret, done = 0;
4455 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004456 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004457
4458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004461 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004463 {
4464 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004467 return( ret );
4468 }
4469
4470 len = ssl->out_msglen;
4471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4475 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 ret = mbedtls_ssl_hw_record_write( ssl );
4480 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004484 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004485
4486 if( ret == 0 )
4487 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004490 if( !done )
4491 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004492 unsigned i;
4493 size_t protected_record_size;
4494
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004495 /* Skip writing the record content type to after the encryption,
4496 * as it may change when using the CID extension. */
4497
Hanno Becker2881d802019-05-22 14:44:53 +01004498 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4499 mbedtls_ssl_get_minor_ver( ssl ),
4500 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004501
Hanno Becker19859472018-08-06 09:40:20 +01004502 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004503 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004504
Paul Bakker48916f92012-09-16 19:57:18 +00004505 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004506 {
Hanno Becker3307b532017-12-27 21:37:21 +00004507 mbedtls_record rec;
4508
4509 rec.buf = ssl->out_iv;
4510 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4511 ( ssl->out_iv - ssl->out_buf );
4512 rec.data_len = ssl->out_msglen;
4513 rec.data_offset = ssl->out_msg - rec.buf;
4514
4515 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004516 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4517 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004518 ssl->conf->transport, rec.ver );
4519 rec.type = ssl->out_msgtype;
4520
Hanno Beckera5a2b082019-05-15 14:03:01 +01004521#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004522 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004523 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004524#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004525
Hanno Becker611a83b2018-01-03 14:27:32 +00004526 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004527 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004528 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004531 return( ret );
4532 }
4533
Hanno Becker3307b532017-12-27 21:37:21 +00004534 if( rec.data_offset != 0 )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4538 }
4539
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004540 /* Update the record content type and CID. */
4541 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004543 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004545 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004546 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004547 }
4548
Hanno Becker43395762019-05-03 14:46:38 +01004549 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004550
4551#if defined(MBEDTLS_SSL_PROTO_DTLS)
4552 /* In case of DTLS, double-check that we don't exceed
4553 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004554 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004555 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004556 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004557 if( ret < 0 )
4558 return( ret );
4559
4560 if( protected_record_size > (size_t) ret )
4561 {
4562 /* Should never happen */
4563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4564 }
4565 }
4566#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004567
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004568 /* Now write the potentially updated record content type. */
4569 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004572 "version = [%d:%d], msglen = %d",
4573 ssl->out_hdr[0], ssl->out_hdr[1],
4574 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004577 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004578
4579 ssl->out_left += protected_record_size;
4580 ssl->out_hdr += protected_record_size;
4581 ssl_update_out_pointers( ssl, ssl->transform_out );
4582
Hanno Becker04484622018-08-06 09:49:38 +01004583 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4584 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4585 break;
4586
4587 /* The loop goes to its end iff the counter is wrapping */
4588 if( i == ssl_ep_len( ssl ) )
4589 {
4590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4591 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 }
4594
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004596 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004597 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004598 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004599 size_t remaining;
4600 ret = ssl_get_remaining_payload_in_datagram( ssl );
4601 if( ret < 0 )
4602 {
4603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4604 ret );
4605 return( ret );
4606 }
4607
4608 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004609 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004610 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004611 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004612 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004613 else
4614 {
Hanno Becker513815a2018-08-20 11:56:09 +01004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004616 }
4617 }
4618#endif /* MBEDTLS_SSL_PROTO_DTLS */
4619
4620 if( ( flush == SSL_FORCE_FLUSH ) &&
4621 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 return( ret );
4625 }
4626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
4629 return( 0 );
4630}
4631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004633
4634static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4635{
4636 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004637 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4638 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004639 {
4640 return( 1 );
4641 }
4642 return( 0 );
4643}
Hanno Becker44650b72018-08-16 12:51:11 +01004644
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004645static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004646{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004647 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004648}
4649
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004650static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004651{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004652 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004653}
4654
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004655static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004656{
4657 uint32_t msg_len, frag_off, frag_len;
4658
4659 msg_len = ssl_get_hs_total_len( ssl );
4660 frag_off = ssl_get_hs_frag_off( ssl );
4661 frag_len = ssl_get_hs_frag_len( ssl );
4662
4663 if( frag_off > msg_len )
4664 return( -1 );
4665
4666 if( frag_len > msg_len - frag_off )
4667 return( -1 );
4668
4669 if( frag_len + 12 > ssl->in_msglen )
4670 return( -1 );
4671
4672 return( 0 );
4673}
4674
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004675/*
4676 * Mark bits in bitmask (used for DTLS HS reassembly)
4677 */
4678static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4679{
4680 unsigned int start_bits, end_bits;
4681
4682 start_bits = 8 - ( offset % 8 );
4683 if( start_bits != 8 )
4684 {
4685 size_t first_byte_idx = offset / 8;
4686
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004687 /* Special case */
4688 if( len <= start_bits )
4689 {
4690 for( ; len != 0; len-- )
4691 mask[first_byte_idx] |= 1 << ( start_bits - len );
4692
4693 /* Avoid potential issues with offset or len becoming invalid */
4694 return;
4695 }
4696
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004697 offset += start_bits; /* Now offset % 8 == 0 */
4698 len -= start_bits;
4699
4700 for( ; start_bits != 0; start_bits-- )
4701 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4702 }
4703
4704 end_bits = len % 8;
4705 if( end_bits != 0 )
4706 {
4707 size_t last_byte_idx = ( offset + len ) / 8;
4708
4709 len -= end_bits; /* Now len % 8 == 0 */
4710
4711 for( ; end_bits != 0; end_bits-- )
4712 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4713 }
4714
4715 memset( mask + offset / 8, 0xFF, len / 8 );
4716}
4717
4718/*
4719 * Check that bitmask is full
4720 */
4721static int ssl_bitmask_check( unsigned char *mask, size_t len )
4722{
4723 size_t i;
4724
4725 for( i = 0; i < len / 8; i++ )
4726 if( mask[i] != 0xFF )
4727 return( -1 );
4728
4729 for( i = 0; i < len % 8; i++ )
4730 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4731 return( -1 );
4732
4733 return( 0 );
4734}
4735
Hanno Becker56e205e2018-08-16 09:06:12 +01004736/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004737static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004738 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004739{
Hanno Becker56e205e2018-08-16 09:06:12 +01004740 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004741
Hanno Becker56e205e2018-08-16 09:06:12 +01004742 alloc_len = 12; /* Handshake header */
4743 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004744
Hanno Beckerd07df862018-08-16 09:14:58 +01004745 if( add_bitmap )
4746 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004747
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004748 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004749}
Hanno Becker56e205e2018-08-16 09:06:12 +01004750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004752
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004753static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004754{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004755 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004756}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004757
Simon Butcher99000142016-10-13 17:21:01 +01004758int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004763 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004765 }
4766
Hanno Becker12555c62018-08-16 12:47:53 +01004767 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004770 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004771 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004774 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004775 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004776 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004777 unsigned int recv_msg_seq = (unsigned int)
4778 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004779
Hanno Becker44650b72018-08-16 12:51:11 +01004780 if( ssl_check_hs_header( ssl ) != 0 )
4781 {
4782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4783 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4784 }
4785
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004786 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004787 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4788 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4789 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4790 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004791 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004792 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4795 recv_msg_seq,
4796 ssl->handshake->in_msg_seq ) );
4797 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4798 }
4799
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004800 /* Retransmit only on last message from previous flight, to avoid
4801 * too many retransmissions.
4802 * Besides, No sane server ever retransmits HelloVerifyRequest */
4803 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004807 "message_seq = %d, start_of_flight = %d",
4808 recv_msg_seq,
4809 ssl->handshake->in_flight_start_seq ) );
4810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004814 return( ret );
4815 }
4816 }
4817 else
4818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004820 "message_seq = %d, expected = %d",
4821 recv_msg_seq,
4822 ssl->handshake->in_msg_seq ) );
4823 }
4824
Hanno Becker90333da2017-10-10 11:27:13 +01004825 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004826 }
4827 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828
Hanno Becker6d97ef52018-08-16 13:09:04 +01004829 /* Message reassembly is handled alongside buffering of future
4830 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004831 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004832 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004833 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004836 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004837 }
4838 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004839 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004841#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004842 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004843 /* With TLS we don't handle fragmentation (for now) */
4844 if( ssl->in_msglen < ssl->in_hslen )
4845 {
4846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4847 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4848 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004850#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851
Simon Butcher99000142016-10-13 17:21:01 +01004852 return( 0 );
4853}
4854
4855void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4856{
Hanno Becker0271f962018-08-16 13:23:47 +01004857 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004858
Hanno Becker0271f962018-08-16 13:23:47 +01004859 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004860 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004861
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004862 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004864 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004865 ssl->handshake != NULL )
4866 {
Hanno Becker0271f962018-08-16 13:23:47 +01004867 unsigned offset;
4868 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004869
Hanno Becker0271f962018-08-16 13:23:47 +01004870 /* Increment handshake sequence number */
4871 hs->in_msg_seq++;
4872
4873 /*
4874 * Clear up handshake buffering and reassembly structure.
4875 */
4876
4877 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004878 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004879
4880 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004881 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4882 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004883 offset++, hs_buf++ )
4884 {
4885 *hs_buf = *(hs_buf + 1);
4886 }
4887
4888 /* Create a fresh last entry */
4889 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004890 }
4891#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004892}
4893
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004894/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004895 * DTLS anti-replay: RFC 6347 4.1.2.6
4896 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004897 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4898 * Bit n is set iff record number in_window_top - n has been seen.
4899 *
4900 * Usually, in_window_top is the last record number seen and the lsb of
4901 * in_window is set. The only exception is the initial state (record number 0
4902 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4905static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004906{
4907 ssl->in_window_top = 0;
4908 ssl->in_window = 0;
4909}
4910
4911static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4912{
4913 return( ( (uint64_t) buf[0] << 40 ) |
4914 ( (uint64_t) buf[1] << 32 ) |
4915 ( (uint64_t) buf[2] << 24 ) |
4916 ( (uint64_t) buf[3] << 16 ) |
4917 ( (uint64_t) buf[4] << 8 ) |
4918 ( (uint64_t) buf[5] ) );
4919}
4920
4921/*
4922 * Return 0 if sequence number is acceptable, -1 otherwise
4923 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004924int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004925{
4926 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4927 uint64_t bit;
4928
Hanno Becker7f376f42019-06-12 16:20:48 +01004929 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4930 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4931 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004932 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004933 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004934
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004935 if( rec_seqnum > ssl->in_window_top )
4936 return( 0 );
4937
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004938 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004939
4940 if( bit >= 64 )
4941 return( -1 );
4942
4943 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4944 return( -1 );
4945
4946 return( 0 );
4947}
4948
4949/*
4950 * Update replay window on new validated record
4951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953{
4954 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4955
Hanno Becker7f376f42019-06-12 16:20:48 +01004956 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4957 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4958 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004959 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004960 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004961
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004962 if( rec_seqnum > ssl->in_window_top )
4963 {
4964 /* Update window_top and the contents of the window */
4965 uint64_t shift = rec_seqnum - ssl->in_window_top;
4966
4967 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004968 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004969 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004970 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004971 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004972 ssl->in_window |= 1;
4973 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974
4975 ssl->in_window_top = rec_seqnum;
4976 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004977 else
4978 {
4979 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004980 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004981
4982 if( bit < 64 ) /* Always true, but be extra sure */
4983 ssl->in_window |= (uint64_t) 1 << bit;
4984 }
4985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004987
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004989/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004990static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4991
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004992/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004993 * Without any SSL context, check if a datagram looks like a ClientHello with
4994 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004995 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 *
4997 * - if cookie is valid, return 0
4998 * - if ClientHello looks superficially valid but cookie is not,
4999 * fill obuf and set olen, then
5000 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5001 * - otherwise return a specific error code
5002 */
5003static int ssl_check_dtls_clihlo_cookie(
5004 mbedtls_ssl_cookie_write_t *f_cookie_write,
5005 mbedtls_ssl_cookie_check_t *f_cookie_check,
5006 void *p_cookie,
5007 const unsigned char *cli_id, size_t cli_id_len,
5008 const unsigned char *in, size_t in_len,
5009 unsigned char *obuf, size_t buf_len, size_t *olen )
5010{
5011 size_t sid_len, cookie_len;
5012 unsigned char *p;
5013
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005014 /*
5015 * Structure of ClientHello with record and handshake headers,
5016 * and expected values. We don't need to check a lot, more checks will be
5017 * done when actually parsing the ClientHello - skipping those checks
5018 * avoids code duplication and does not make cookie forging any easier.
5019 *
5020 * 0-0 ContentType type; copied, must be handshake
5021 * 1-2 ProtocolVersion version; copied
5022 * 3-4 uint16 epoch; copied, must be 0
5023 * 5-10 uint48 sequence_number; copied
5024 * 11-12 uint16 length; (ignored)
5025 *
5026 * 13-13 HandshakeType msg_type; (ignored)
5027 * 14-16 uint24 length; (ignored)
5028 * 17-18 uint16 message_seq; copied
5029 * 19-21 uint24 fragment_offset; copied, must be 0
5030 * 22-24 uint24 fragment_length; (ignored)
5031 *
5032 * 25-26 ProtocolVersion client_version; (ignored)
5033 * 27-58 Random random; (ignored)
5034 * 59-xx SessionID session_id; 1 byte len + sid_len content
5035 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5036 * ...
5037 *
5038 * Minimum length is 61 bytes.
5039 */
5040 if( in_len < 61 ||
5041 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5042 in[3] != 0 || in[4] != 0 ||
5043 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5044 {
5045 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5046 }
5047
5048 sid_len = in[59];
5049 if( sid_len > in_len - 61 )
5050 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5051
5052 cookie_len = in[60 + sid_len];
5053 if( cookie_len > in_len - 60 )
5054 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5055
5056 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5057 cli_id, cli_id_len ) == 0 )
5058 {
5059 /* Valid cookie */
5060 return( 0 );
5061 }
5062
5063 /*
5064 * If we get here, we've got an invalid cookie, let's prepare HVR.
5065 *
5066 * 0-0 ContentType type; copied
5067 * 1-2 ProtocolVersion version; copied
5068 * 3-4 uint16 epoch; copied
5069 * 5-10 uint48 sequence_number; copied
5070 * 11-12 uint16 length; olen - 13
5071 *
5072 * 13-13 HandshakeType msg_type; hello_verify_request
5073 * 14-16 uint24 length; olen - 25
5074 * 17-18 uint16 message_seq; copied
5075 * 19-21 uint24 fragment_offset; copied
5076 * 22-24 uint24 fragment_length; olen - 25
5077 *
5078 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5079 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5080 *
5081 * Minimum length is 28.
5082 */
5083 if( buf_len < 28 )
5084 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5085
5086 /* Copy most fields and adapt others */
5087 memcpy( obuf, in, 25 );
5088 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5089 obuf[25] = 0xfe;
5090 obuf[26] = 0xff;
5091
5092 /* Generate and write actual cookie */
5093 p = obuf + 28;
5094 if( f_cookie_write( p_cookie,
5095 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5096 {
5097 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5098 }
5099
5100 *olen = p - obuf;
5101
5102 /* Go back and fill length fields */
5103 obuf[27] = (unsigned char)( *olen - 28 );
5104
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005105 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005106 obuf[22] = obuf[14];
5107 obuf[23] = obuf[15];
5108 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005109
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005110 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005111
5112 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5113}
5114
5115/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005116 * Handle possible client reconnect with the same UDP quadruplet
5117 * (RFC 6347 Section 4.2.8).
5118 *
5119 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5120 * that looks like a ClientHello.
5121 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005122 * - if the input looks like a ClientHello without cookies,
5123 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005124 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005125 * - if the input looks like a ClientHello with a valid cookie,
5126 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005127 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005128 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005129 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005130 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005131 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5132 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005133 */
5134static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5135{
5136 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005138
Hanno Becker87b56262019-07-10 14:37:41 +01005139 if( ssl->conf->f_cookie_write == NULL ||
5140 ssl->conf->f_cookie_check == NULL )
5141 {
5142 /* If we can't use cookies to verify reachability of the peer,
5143 * drop the record. */
5144 return( 0 );
5145 }
5146
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005147 ret = ssl_check_dtls_clihlo_cookie(
5148 ssl->conf->f_cookie_write,
5149 ssl->conf->f_cookie_check,
5150 ssl->conf->p_cookie,
5151 ssl->cli_id, ssl->cli_id_len,
5152 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005153 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005154
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005155 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5156
5157 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005159 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005160 * If the error is permanent we'll catch it later,
5161 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005162 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005163 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005164 }
5165
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005166 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005167 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005168 /* Got a valid cookie, partially reset context */
5169 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5170 {
5171 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5172 return( ret );
5173 }
5174
5175 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005176 }
5177
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005178 return( ret );
5179}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005180#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005181
Hanno Becker46483f12019-05-03 13:25:54 +01005182static int ssl_check_record_type( uint8_t record_type )
5183{
5184 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5185 record_type != MBEDTLS_SSL_MSG_ALERT &&
5186 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5187 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5188 {
5189 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5190 }
5191
5192 return( 0 );
5193}
5194
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005195/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005196 * ContentType type;
5197 * ProtocolVersion version;
5198 * uint16 epoch; // DTLS only
5199 * uint48 sequence_number; // DTLS only
5200 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005201 *
5202 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005203 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005204 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5205 *
5206 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005207 * 1. proceed with the record if this function returns 0
5208 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5209 * 3. return CLIENT_RECONNECT if this function return that value
5210 * 4. drop the whole datagram if this function returns anything else.
5211 * Point 2 is needed when the peer is resending, and we have already received
5212 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005213 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005214static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005215 unsigned char *buf,
5216 size_t len,
5217 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005218{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005219 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005221 size_t const rec_hdr_type_offset = 0;
5222 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005223
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005224 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5225 rec_hdr_type_len;
5226 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005227
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005228 size_t const rec_hdr_ctr_len = 8;
5229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005230 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005231 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5232 rec_hdr_version_len;
5233
Hanno Beckera5a2b082019-05-15 14:03:01 +01005234#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005235 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5236 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005237 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005238#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5239#endif /* MBEDTLS_SSL_PROTO_DTLS */
5240
5241 size_t rec_hdr_len_offset; /* To be determined */
5242 size_t const rec_hdr_len_len = 2;
5243
5244 /*
5245 * Check minimum lengths for record header.
5246 */
5247
5248#if defined(MBEDTLS_SSL_PROTO_DTLS)
5249 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5250 {
5251 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5252 }
5253 MBEDTLS_SSL_TRANSPORT_ELSE
5254#endif /* MBEDTLS_SSL_PROTO_DTLS */
5255#if defined(MBEDTLS_SSL_PROTO_TLS)
5256 {
5257 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5258 }
5259#endif /* MBEDTLS_SSL_PROTO_DTLS */
5260
5261 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5262 {
5263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5264 (unsigned) len,
5265 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5266 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5267 }
5268
5269 /*
5270 * Parse and validate record content type
5271 */
5272
5273 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005274
5275 /* Check record content type */
5276#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5277 rec->cid_len = 0;
5278
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005279 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5281 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005282 {
5283 /* Shift pointers to account for record header including CID
5284 * struct {
5285 * ContentType special_type = tls12_cid;
5286 * ProtocolVersion version;
5287 * uint16 epoch;
5288 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005289 * opaque cid[cid_length]; // Additional field compared to
5290 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005291 * uint16 length;
5292 * opaque enc_content[DTLSCiphertext.length];
5293 * } DTLSCiphertext;
5294 */
5295
5296 /* So far, we only support static CID lengths
5297 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005298 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5299 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005300
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005301 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005302 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5304 (unsigned) len,
5305 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005306 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005307 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005308
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005309 /* configured CID len is guaranteed at most 255, see
5310 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5311 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005312 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005313 }
5314 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005316 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005317 if( ssl_check_record_type( rec->type ) )
5318 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5320 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005321 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5322 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005323 }
5324
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005325 /*
5326 * Parse and validate record version
5327 */
5328
Hanno Becker8061c6e2019-07-26 08:07:03 +01005329 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5330 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005331 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5332 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005333 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005334
Hanno Becker2881d802019-05-22 14:44:53 +01005335 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5338 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 }
5340
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005341 if( mbedtls_ssl_ver_gt( minor_ver,
5342 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5345 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 }
5347
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005348 /*
5349 * Parse/Copy record sequence number.
5350 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005351
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352#if defined(MBEDTLS_SSL_PROTO_DTLS)
5353 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5354 {
5355 /* Copy explicit record sequence number from input buffer. */
5356 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5357 rec_hdr_ctr_len );
5358 }
5359 MBEDTLS_SSL_TRANSPORT_ELSE
5360#endif /* MBEDTLS_SSL_PROTO_DTLS */
5361#if defined(MBEDTLS_SSL_PROTO_TLS)
5362 {
5363 /* Copy implicit record sequence number from SSL context structure. */
5364 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5365 }
5366#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005367
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005368 /*
5369 * Parse record length.
5370 */
5371
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005372 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005373 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005374 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5375
Hanno Becker8b09b732019-05-08 12:03:28 +01005376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005377 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 rec->type,
5379 major_ver, minor_ver, rec->data_len ) );
5380
5381 rec->buf = buf;
5382 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005383
Hanno Beckerec014082019-07-26 08:20:27 +01005384 if( rec->data_len == 0 )
5385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5386
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005387 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005388 * DTLS-related tests.
5389 * Check epoch before checking length constraint because
5390 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5391 * message gets duplicated before the corresponding Finished message,
5392 * the second ChangeCipherSpec should be discarded because it belongs
5393 * to an old epoch, but not because its length is shorter than
5394 * the minimum record length for packets using the new record transform.
5395 * Note that these two kinds of failures are handled differently,
5396 * as an unexpected record is silently skipped but an invalid
5397 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005398 */
5399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005400 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005401 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005402 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005403
Hanno Beckere0452772019-07-10 17:12:07 +01005404 /* Check that the datagram is large enough to contain a record
5405 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005407 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5409 (unsigned) len,
5410 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5412 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005413
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005414 /* Records from other, non-matching epochs are silently discarded.
5415 * (The case of same-port Client reconnects must be considered in
5416 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005417 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5420 "expected %d, received %d",
5421 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005422
5423 /* Records from the next epoch are considered for buffering
5424 * (concretely: early Finished messages). */
5425 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5426 {
5427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5428 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5429 }
5430
Hanno Becker87b56262019-07-10 14:37:41 +01005431 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005432 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005433#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005434 /* For records from the correct epoch, check whether their
5435 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005436 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005437 {
5438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5439 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5440 }
5441#endif
5442 }
5443#endif /* MBEDTLS_SSL_PROTO_DTLS */
5444
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 return( 0 );
5446}
Paul Bakker5121ce52009-01-03 21:22:43 +00005447
Hanno Becker87b56262019-07-10 14:37:41 +01005448
5449#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5450static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5451{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005452 unsigned int rec_epoch = (unsigned int)
5453 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005454
5455 /*
5456 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5457 * access the first byte of record content (handshake type), as we
5458 * have an active transform (possibly iv_len != 0), so use the
5459 * fact that the record header len is 13 instead.
5460 */
5461 if( rec_epoch == 0 &&
5462 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5463 MBEDTLS_SSL_IS_SERVER &&
5464 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5465 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5466 ssl->in_left > 13 &&
5467 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5468 {
5469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5470 "from the same port" ) );
5471 return( ssl_handle_possible_reconnect( ssl ) );
5472 }
5473
5474 return( 0 );
5475}
5476#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5477
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005478/*
5479 * If applicable, decrypt (and decompress) record content
5480 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005481static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5482 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005483{
5484 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005487 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5490 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 ret = mbedtls_ssl_hw_record_read( ssl );
5495 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5498 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005499 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005500
5501 if( ret == 0 )
5502 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005505 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005507 unsigned char const old_msg_type = rec->type;
5508
Hanno Becker611a83b2018-01-03 14:27:32 +00005509 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005510 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005513
Hanno Beckera5a2b082019-05-15 14:03:01 +01005514#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005515 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005516 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5517 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005518 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005520 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005521 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005522#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005523
Paul Bakker5121ce52009-01-03 21:22:43 +00005524 return( ret );
5525 }
5526
Hanno Becker106f3da2019-07-12 09:35:58 +01005527 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005528 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005529 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005530 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005532
Paul Bakker5121ce52009-01-03 21:22:43 +00005533 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005534 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Hanno Beckera5a2b082019-05-15 14:03:01 +01005536#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005537 /* We have already checked the record content type
5538 * in ssl_parse_record_header(), failing or silently
5539 * dropping the record in the case of an unknown type.
5540 *
5541 * Since with the use of CIDs, the record content type
5542 * might change during decryption, re-check the record
5543 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005544 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5547 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5548 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005549#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005550
Hanno Becker106f3da2019-07-12 09:35:58 +01005551 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005552 {
5553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005554 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005555 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005556 {
5557 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5559 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5560 }
5561#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5562
5563 ssl->nb_zero++;
5564
5565 /*
5566 * Three or more empty messages may be a DoS attack
5567 * (excessive CPU consumption).
5568 */
5569 if( ssl->nb_zero > 3 )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005572 "messages, possible DoS attack" ) );
5573 /* Treat the records as if they were not properly authenticated,
5574 * thereby failing the connection if we see more than allowed
5575 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005576 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5577 }
5578 }
5579 else
5580 ssl->nb_zero = 0;
5581
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005582 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5583#if defined(MBEDTLS_SSL_PROTO_TLS)
5584 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005585 {
5586 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005587 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005588 if( ++ssl->in_ctr[i - 1] != 0 )
5589 break;
5590
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005591 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005592 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005593 {
5594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5595 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5596 }
5597 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005598#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005599
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 }
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005603 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005605 {
5606 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005609 return( ret );
5610 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005615 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005618 }
5619#endif
5620
Hanno Beckerf0242852019-07-09 17:30:02 +01005621 /* Check actual (decrypted) record content length against
5622 * configured maximum. */
5623 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5624 {
5625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5627 }
5628
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005629 return( 0 );
5630}
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005633
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005634/*
5635 * Read a record.
5636 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005637 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5638 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5639 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005640 */
Hanno Becker1097b342018-08-15 14:09:41 +01005641
5642/* Helper functions for mbedtls_ssl_read_record(). */
5643static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005644static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5645static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005646
Hanno Becker327c93b2018-08-15 13:56:18 +01005647int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005648 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005649{
5650 int ret;
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005653
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005654 if( ssl->keep_current_message == 0 )
5655 {
5656 do {
Simon Butcher99000142016-10-13 17:21:01 +01005657
Hanno Becker26994592018-08-15 14:14:59 +01005658 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005659 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005660 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005661
Hanno Beckere74d5562018-08-15 14:26:08 +01005662 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005663 {
Hanno Becker40f50842018-08-15 14:48:01 +01005664#if defined(MBEDTLS_SSL_PROTO_DTLS)
5665 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005666
Hanno Becker40f50842018-08-15 14:48:01 +01005667 /* We only check for buffered messages if the
5668 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005669 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005670 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005671 {
Hanno Becker40f50842018-08-15 14:48:01 +01005672 if( ssl_load_buffered_message( ssl ) == 0 )
5673 have_buffered = 1;
5674 }
5675
5676 if( have_buffered == 0 )
5677#endif /* MBEDTLS_SSL_PROTO_DTLS */
5678 {
5679 ret = ssl_get_next_record( ssl );
5680 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5681 continue;
5682
5683 if( ret != 0 )
5684 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005685 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005686 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005687 return( ret );
5688 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005689 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005690 }
5691
5692 ret = mbedtls_ssl_handle_message_type( ssl );
5693
Hanno Becker40f50842018-08-15 14:48:01 +01005694#if defined(MBEDTLS_SSL_PROTO_DTLS)
5695 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5696 {
5697 /* Buffer future message */
5698 ret = ssl_buffer_message( ssl );
5699 if( ret != 0 )
5700 return( ret );
5701
5702 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5703 }
5704#endif /* MBEDTLS_SSL_PROTO_DTLS */
5705
Hanno Becker90333da2017-10-10 11:27:13 +01005706 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5707 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005708
5709 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005710 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005711 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005712 return( ret );
5713 }
5714
Hanno Becker327c93b2018-08-15 13:56:18 +01005715 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005716 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005717 {
5718 mbedtls_ssl_update_handshake_status( ssl );
5719 }
Simon Butcher99000142016-10-13 17:21:01 +01005720 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005721 else
Simon Butcher99000142016-10-13 17:21:01 +01005722 {
Hanno Becker02f59072018-08-15 14:00:24 +01005723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005724 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005725 }
5726
5727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5728
5729 return( 0 );
5730}
5731
Hanno Becker40f50842018-08-15 14:48:01 +01005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005733static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005734{
Hanno Becker40f50842018-08-15 14:48:01 +01005735 if( ssl->in_left > ssl->next_record_offset )
5736 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005737
Hanno Becker40f50842018-08-15 14:48:01 +01005738 return( 0 );
5739}
5740
5741static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5742{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005744 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005745 int ret = 0;
5746
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005747 if( hs == NULL )
5748 return( -1 );
5749
Hanno Beckere00ae372018-08-20 09:39:42 +01005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5751
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005752 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5753 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5754 {
5755 /* Check if we have seen a ChangeCipherSpec before.
5756 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005757 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005758 {
5759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5760 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005761 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005762 }
5763
Hanno Becker39b8bc92018-08-28 17:17:13 +01005764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005765 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5766 ssl->in_msglen = 1;
5767 ssl->in_msg[0] = 1;
5768
5769 /* As long as they are equal, the exact value doesn't matter. */
5770 ssl->in_left = 0;
5771 ssl->next_record_offset = 0;
5772
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005773 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005774 goto exit;
5775 }
Hanno Becker37f95322018-08-16 13:55:32 +01005776
Hanno Beckerb8f50142018-08-28 10:01:34 +01005777#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005778 /* Debug only */
5779 {
5780 unsigned offset;
5781 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5782 {
5783 hs_buf = &hs->buffering.hs[offset];
5784 if( hs_buf->is_valid == 1 )
5785 {
5786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5787 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005788 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005789 }
5790 }
5791 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005792#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005793
5794 /* Check if we have buffered and/or fully reassembled the
5795 * next handshake message. */
5796 hs_buf = &hs->buffering.hs[0];
5797 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5798 {
5799 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005800 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005801
5802 /* Double-check that we haven't accidentally buffered
5803 * a message that doesn't fit into the input buffer. */
5804 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5805 {
5806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5808 }
5809
5810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5811 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5812 hs_buf->data, msg_len + 12 );
5813
5814 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5815 ssl->in_hslen = msg_len + 12;
5816 ssl->in_msglen = msg_len + 12;
5817 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5818
5819 ret = 0;
5820 goto exit;
5821 }
5822 else
5823 {
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5825 hs->in_msg_seq ) );
5826 }
5827
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 ret = -1;
5829
5830exit:
5831
5832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5833 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005834}
5835
Hanno Beckera02b0b42018-08-21 17:20:27 +01005836static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5837 size_t desired )
5838{
5839 int offset;
5840 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5842 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005843
Hanno Becker01315ea2018-08-21 17:22:17 +01005844 /* Get rid of future records epoch first, if such exist. */
5845 ssl_free_buffered_record( ssl );
5846
5847 /* Check if we have enough space available now. */
5848 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5849 hs->buffering.total_bytes_buffered ) )
5850 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005852 return( 0 );
5853 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005854
Hanno Becker4f432ad2018-08-28 10:02:32 +01005855 /* We don't have enough space to buffer the next expected handshake
5856 * message. Remove buffers used for future messages to gain space,
5857 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005858 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5859 offset >= 0; offset-- )
5860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5862 offset ) );
5863
Hanno Beckerb309b922018-08-23 13:18:05 +01005864 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005865
5866 /* Check if we have enough space available now. */
5867 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5868 hs->buffering.total_bytes_buffered ) )
5869 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005871 return( 0 );
5872 }
5873 }
5874
5875 return( -1 );
5876}
5877
Hanno Becker40f50842018-08-15 14:48:01 +01005878static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5879{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005880 int ret = 0;
5881 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5882
5883 if( hs == NULL )
5884 return( 0 );
5885
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5887
5888 switch( ssl->in_msgtype )
5889 {
5890 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005892
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005893 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005894 break;
5895
5896 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005897 {
5898 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005899 unsigned recv_msg_seq = (unsigned)
5900 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005901
Hanno Becker37f95322018-08-16 13:55:32 +01005902 mbedtls_ssl_hs_buffer *hs_buf;
5903 size_t msg_len = ssl->in_hslen - 12;
5904
5905 /* We should never receive an old handshake
5906 * message - double-check nonetheless. */
5907 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5908 {
5909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5911 }
5912
5913 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5914 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5915 {
5916 /* Silently ignore -- message too far in the future */
5917 MBEDTLS_SSL_DEBUG_MSG( 2,
5918 ( "Ignore future HS message with sequence number %u, "
5919 "buffering window %u - %u",
5920 recv_msg_seq, ssl->handshake->in_msg_seq,
5921 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5922
5923 goto exit;
5924 }
5925
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5927 recv_msg_seq, recv_msg_seq_offset ) );
5928
5929 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5930
5931 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005932 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005933 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005934 size_t reassembly_buf_sz;
5935
Hanno Becker37f95322018-08-16 13:55:32 +01005936 hs_buf->is_fragmented =
5937 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5938
5939 /* We copy the message back into the input buffer
5940 * after reassembly, so check that it's not too large.
5941 * This is an implementation-specific limitation
5942 * and not one from the standard, hence it is not
5943 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005944 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005945 {
5946 /* Ignore message */
5947 goto exit;
5948 }
5949
Hanno Beckere0b150f2018-08-21 15:51:03 +01005950 /* Check if we have enough space to buffer the message. */
5951 if( hs->buffering.total_bytes_buffered >
5952 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5953 {
5954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5956 }
5957
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005958 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5959 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005960
5961 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5962 hs->buffering.total_bytes_buffered ) )
5963 {
5964 if( recv_msg_seq_offset > 0 )
5965 {
5966 /* If we can't buffer a future message because
5967 * of space limitations -- ignore. */
5968 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",
5969 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5970 (unsigned) hs->buffering.total_bytes_buffered ) );
5971 goto exit;
5972 }
Hanno Beckere1801392018-08-21 16:51:05 +01005973 else
5974 {
5975 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",
5976 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5977 (unsigned) hs->buffering.total_bytes_buffered ) );
5978 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979
Hanno Beckera02b0b42018-08-21 17:20:27 +01005980 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005981 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005982 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",
5983 (unsigned) msg_len,
5984 (unsigned) reassembly_buf_sz,
5985 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005986 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005987 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5988 goto exit;
5989 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005990 }
5991
5992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5993 msg_len ) );
5994
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005995 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5996 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005997 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005998 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005999 goto exit;
6000 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006001 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006002
6003 /* Prepare final header: copy msg_type, length and message_seq,
6004 * then add standardised fragment_offset and fragment_length */
6005 memcpy( hs_buf->data, ssl->in_msg, 6 );
6006 memset( hs_buf->data + 6, 0, 3 );
6007 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
6008
6009 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006010
6011 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006012 }
6013 else
6014 {
6015 /* Make sure msg_type and length are consistent */
Teppo Järvelin61f412e2019-10-03 12:25:22 +03006016 if( mbedtls_platform_memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
Hanno Becker37f95322018-08-16 13:55:32 +01006017 {
6018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6019 /* Ignore */
6020 goto exit;
6021 }
6022 }
6023
Hanno Becker4422bbb2018-08-20 09:40:19 +01006024 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006025 {
6026 size_t frag_len, frag_off;
6027 unsigned char * const msg = hs_buf->data + 12;
6028
6029 /*
6030 * Check and copy current fragment
6031 */
6032
6033 /* Validation of header fields already done in
6034 * mbedtls_ssl_prepare_handshake_record(). */
6035 frag_off = ssl_get_hs_frag_off( ssl );
6036 frag_len = ssl_get_hs_frag_len( ssl );
6037
6038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6039 frag_off, frag_len ) );
6040 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6041
6042 if( hs_buf->is_fragmented )
6043 {
6044 unsigned char * const bitmask = msg + msg_len;
6045 ssl_bitmask_set( bitmask, frag_off, frag_len );
6046 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6047 msg_len ) == 0 );
6048 }
6049 else
6050 {
6051 hs_buf->is_complete = 1;
6052 }
6053
6054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6055 hs_buf->is_complete ? "" : "not yet " ) );
6056 }
6057
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006058 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006059 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006060
6061 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006062 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006063 break;
6064 }
6065
6066exit:
6067
6068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6069 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006070}
6071#endif /* MBEDTLS_SSL_PROTO_DTLS */
6072
Hanno Becker1097b342018-08-15 14:09:41 +01006073static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006074{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006075 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006076 * Consume last content-layer message and potentially
6077 * update in_msglen which keeps track of the contents'
6078 * consumption state.
6079 *
6080 * (1) Handshake messages:
6081 * Remove last handshake message, move content
6082 * and adapt in_msglen.
6083 *
6084 * (2) Alert messages:
6085 * Consume whole record content, in_msglen = 0.
6086 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006087 * (3) Change cipher spec:
6088 * Consume whole record content, in_msglen = 0.
6089 *
6090 * (4) Application data:
6091 * Don't do anything - the record layer provides
6092 * the application data as a stream transport
6093 * and consumes through mbedtls_ssl_read only.
6094 *
6095 */
6096
6097 /* Case (1): Handshake messages */
6098 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006099 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006100 /* Hard assertion to be sure that no application data
6101 * is in flight, as corrupting ssl->in_msglen during
6102 * ssl->in_offt != NULL is fatal. */
6103 if( ssl->in_offt != NULL )
6104 {
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6107 }
6108
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006109 /*
6110 * Get next Handshake message in the current record
6111 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006112
Hanno Becker4a810fb2017-05-24 16:27:30 +01006113 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006114 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006115 * current handshake content: If DTLS handshake
6116 * fragmentation is used, that's the fragment
6117 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006118 * size here is faulty and should be changed at
6119 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006120 * (2) While it doesn't seem to cause problems, one
6121 * has to be very careful not to assume that in_hslen
6122 * is always <= in_msglen in a sensible communication.
6123 * Again, it's wrong for DTLS handshake fragmentation.
6124 * The following check is therefore mandatory, and
6125 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006126 * Additionally, ssl->in_hslen might be arbitrarily out of
6127 * bounds after handling a DTLS message with an unexpected
6128 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006129 */
6130 if( ssl->in_hslen < ssl->in_msglen )
6131 {
6132 ssl->in_msglen -= ssl->in_hslen;
6133 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6134 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006135
Hanno Becker4a810fb2017-05-24 16:27:30 +01006136 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6137 ssl->in_msg, ssl->in_msglen );
6138 }
6139 else
6140 {
6141 ssl->in_msglen = 0;
6142 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006143
Hanno Becker4a810fb2017-05-24 16:27:30 +01006144 ssl->in_hslen = 0;
6145 }
6146 /* Case (4): Application data */
6147 else if( ssl->in_offt != NULL )
6148 {
6149 return( 0 );
6150 }
6151 /* Everything else (CCS & Alerts) */
6152 else
6153 {
6154 ssl->in_msglen = 0;
6155 }
6156
Hanno Becker1097b342018-08-15 14:09:41 +01006157 return( 0 );
6158}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006159
Hanno Beckere74d5562018-08-15 14:26:08 +01006160static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6161{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006162 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006163 return( 1 );
6164
6165 return( 0 );
6166}
6167
Hanno Becker5f066e72018-08-16 14:56:31 +01006168#if defined(MBEDTLS_SSL_PROTO_DTLS)
6169
6170static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6171{
6172 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6173 if( hs == NULL )
6174 return;
6175
Hanno Becker01315ea2018-08-21 17:22:17 +01006176 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006178 hs->buffering.total_bytes_buffered -=
6179 hs->buffering.future_record.len;
6180
6181 mbedtls_free( hs->buffering.future_record.data );
6182 hs->buffering.future_record.data = NULL;
6183 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006184}
6185
6186static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6187{
6188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6189 unsigned char * rec;
6190 size_t rec_len;
6191 unsigned rec_epoch;
6192
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006193 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006194 return( 0 );
6195
6196 if( hs == NULL )
6197 return( 0 );
6198
Hanno Becker5f066e72018-08-16 14:56:31 +01006199 rec = hs->buffering.future_record.data;
6200 rec_len = hs->buffering.future_record.len;
6201 rec_epoch = hs->buffering.future_record.epoch;
6202
6203 if( rec == NULL )
6204 return( 0 );
6205
Hanno Becker4cb782d2018-08-20 11:19:05 +01006206 /* Only consider loading future records if the
6207 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006208 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006209 return( 0 );
6210
Hanno Becker5f066e72018-08-16 14:56:31 +01006211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6212
6213 if( rec_epoch != ssl->in_epoch )
6214 {
6215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6216 goto exit;
6217 }
6218
6219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6220
6221 /* Double-check that the record is not too large */
6222 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6223 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6224 {
6225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6227 }
6228
6229 memcpy( ssl->in_hdr, rec, rec_len );
6230 ssl->in_left = rec_len;
6231 ssl->next_record_offset = 0;
6232
6233 ssl_free_buffered_record( ssl );
6234
6235exit:
6236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6237 return( 0 );
6238}
6239
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006240static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6241 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006242{
6243 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006244
6245 /* Don't buffer future records outside handshakes. */
6246 if( hs == NULL )
6247 return( 0 );
6248
6249 /* Only buffer handshake records (we are only interested
6250 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006251 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006252 return( 0 );
6253
6254 /* Don't buffer more than one future epoch record. */
6255 if( hs->buffering.future_record.data != NULL )
6256 return( 0 );
6257
Hanno Becker01315ea2018-08-21 17:22:17 +01006258 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006259 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006260 hs->buffering.total_bytes_buffered ) )
6261 {
6262 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 +01006263 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006264 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006265 return( 0 );
6266 }
6267
Hanno Becker5f066e72018-08-16 14:56:31 +01006268 /* Buffer record */
6269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6270 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006271 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006272
6273 /* ssl_parse_record_header() only considers records
6274 * of the next epoch as candidates for buffering. */
6275 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006276 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006277
6278 hs->buffering.future_record.data =
6279 mbedtls_calloc( 1, hs->buffering.future_record.len );
6280 if( hs->buffering.future_record.data == NULL )
6281 {
6282 /* If we run out of RAM trying to buffer a
6283 * record from the next epoch, just ignore. */
6284 return( 0 );
6285 }
6286
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006287 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006288
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006289 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006290 return( 0 );
6291}
6292
6293#endif /* MBEDTLS_SSL_PROTO_DTLS */
6294
Hanno Beckere74d5562018-08-15 14:26:08 +01006295static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006296{
6297 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006298 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006299
Hanno Becker5f066e72018-08-16 14:56:31 +01006300#if defined(MBEDTLS_SSL_PROTO_DTLS)
6301 /* We might have buffered a future record; if so,
6302 * and if the epoch matches now, load it.
6303 * On success, this call will set ssl->in_left to
6304 * the length of the buffered record, so that
6305 * the calls to ssl_fetch_input() below will
6306 * essentially be no-ops. */
6307 ret = ssl_load_buffered_record( ssl );
6308 if( ret != 0 )
6309 return( ret );
6310#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006311
Hanno Becker8b09b732019-05-08 12:03:28 +01006312 /* Ensure that we have enough space available for the default form
6313 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6314 * with no space for CIDs counted in). */
6315 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6316 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006319 return( ret );
6320 }
6321
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006322 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6323 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006326 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006327 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006328 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6329 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006330 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006331 if( ret != 0 )
6332 return( ret );
6333
6334 /* Fall through to handling of unexpected records */
6335 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6336 }
6337
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006338 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6339 {
Hanno Becker87b56262019-07-10 14:37:41 +01006340#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006341 /* Reset in pointers to default state for TLS/DTLS records,
6342 * assuming no CID and no offset between record content and
6343 * record plaintext. */
6344 ssl_update_in_pointers( ssl );
6345
Hanno Becker69412452019-07-12 08:33:49 +01006346 /* Setup internal message pointers from record structure. */
6347 ssl->in_msgtype = rec.type;
6348#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6349 ssl->in_len = ssl->in_cid + rec.cid_len;
6350#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006351 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006352 ssl->in_msglen = rec.data_len;
6353
Hanno Becker87b56262019-07-10 14:37:41 +01006354 ret = ssl_check_client_reconnect( ssl );
6355 if( ret != 0 )
6356 return( ret );
6357#endif
6358
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006359 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006360 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006361
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6363 "(header)" ) );
6364 }
6365 else
6366 {
6367 /* Skip invalid record and the rest of the datagram */
6368 ssl->next_record_offset = 0;
6369 ssl->in_left = 0;
6370
6371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6372 "(header)" ) );
6373 }
6374
6375 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006376 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006377 }
Hanno Becker87b56262019-07-10 14:37:41 +01006378 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006379#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006380 {
6381 return( ret );
6382 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006383 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006386 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006387 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006388 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006389 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006390 if( ssl->next_record_offset < ssl->in_left )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6393 }
6394 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006395 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006397#if defined(MBEDTLS_SSL_PROTO_TLS)
6398 {
Hanno Beckere0452772019-07-10 17:12:07 +01006399 /*
6400 * Fetch record contents from underlying transport.
6401 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006402 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006403 if( ret != 0 )
6404 {
6405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6406 return( ret );
6407 }
6408
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006409 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006410 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006411#endif /* MBEDTLS_SSL_PROTO_TLS */
6412
6413 /*
6414 * Decrypt record contents.
6415 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006416
Hanno Beckera89610a2019-07-11 13:07:45 +01006417 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006420 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006421 {
6422 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006423 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006424 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006425 /* Except when waiting for Finished as a bad mac here
6426 * probably means something went wrong in the handshake
6427 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6428 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6429 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6430 {
6431#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6432 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6433 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006434 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006435 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6436 }
6437#endif
6438 return( ret );
6439 }
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006442 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6443 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6446 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006447 }
6448#endif
6449
Hanno Becker4a810fb2017-05-24 16:27:30 +01006450 /* As above, invalid records cause
6451 * dismissal of the whole datagram. */
6452
6453 ssl->next_record_offset = 0;
6454 ssl->in_left = 0;
6455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006457 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006458 }
6459
6460 return( ret );
6461 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006462 MBEDTLS_SSL_TRANSPORT_ELSE
6463#endif /* MBEDTLS_SSL_PROTO_DTLS */
6464#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006465 {
6466 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6468 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006469 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006470 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006472 }
6473#endif
6474 return( ret );
6475 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006476#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006477 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006478
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006479
6480 /* Reset in pointers to default state for TLS/DTLS records,
6481 * assuming no CID and no offset between record content and
6482 * record plaintext. */
6483 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6485 ssl->in_len = ssl->in_cid + rec.cid_len;
6486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006487 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006488
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006489 /* The record content type may change during decryption,
6490 * so re-read it. */
6491 ssl->in_msgtype = rec.type;
6492 /* Also update the input buffer, because unfortunately
6493 * the server-side ssl_parse_client_hello() reparses the
6494 * record header when receiving a ClientHello initiating
6495 * a renegotiation. */
6496 ssl->in_hdr[0] = rec.type;
6497 ssl->in_msg = rec.buf + rec.data_offset;
6498 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006499 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006500
Simon Butcher99000142016-10-13 17:21:01 +01006501 return( 0 );
6502}
6503
6504int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6505{
6506 int ret;
6507
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006508 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006509 * Handle particular types of records
6510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 {
Simon Butcher99000142016-10-13 17:21:01 +01006513 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6514 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006515 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 }
6518
Hanno Beckere678eaa2018-08-21 14:57:46 +01006519 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006520 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006521 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006522 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6524 ssl->in_msglen ) );
6525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006526 }
6527
Hanno Beckere678eaa2018-08-21 14:57:46 +01006528 if( ssl->in_msg[0] != 1 )
6529 {
6530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6531 ssl->in_msg[0] ) );
6532 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6533 }
6534
6535#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006536 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006537 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6538 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6539 {
6540 if( ssl->handshake == NULL )
6541 {
6542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6543 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6544 }
6545
6546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6547 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6548 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006549#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006550 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006554 if( ssl->in_msglen != 2 )
6555 {
6556 /* Note: Standard allows for more than one 2 byte alert
6557 to be packed in a single message, but Mbed TLS doesn't
6558 currently support this. */
6559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6560 ssl->in_msglen ) );
6561 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6562 }
6563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 ssl->in_msg[0], ssl->in_msg[1] ) );
6566
6567 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006568 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006573 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 }
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6578 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6581 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006583
6584#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6585 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6586 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6587 {
Hanno Becker90333da2017-10-10 11:27:13 +01006588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006589 /* Will be handled when trying to parse ServerHello */
6590 return( 0 );
6591 }
6592#endif
6593
6594#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006595 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006596 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6597 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006598 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6599 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6602 /* Will be handled in mbedtls_ssl_parse_certificate() */
6603 return( 0 );
6604 }
6605#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6606
6607 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006608 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 }
6610
Hanno Beckerc76c6192017-06-06 10:03:17 +01006611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006612 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006613 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006614 /* Drop unexpected ApplicationData records,
6615 * except at the beginning of renegotiations */
6616 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6617 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6618#if defined(MBEDTLS_SSL_RENEGOTIATION)
6619 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6620 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006621#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006622 )
6623 {
6624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6625 return( MBEDTLS_ERR_SSL_NON_FATAL );
6626 }
6627
6628 if( ssl->handshake != NULL &&
6629 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6630 {
6631 ssl_handshake_wrapup_free_hs_transform( ssl );
6632 }
6633 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006634#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006635
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 return( 0 );
6637}
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006640{
6641 int ret;
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006646 {
6647 return( ret );
6648 }
6649
6650 return( 0 );
6651}
6652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006654 unsigned char level,
6655 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006656{
6657 int ret;
6658
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006659 if( ssl == NULL || ssl->conf == NULL )
6660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006666 ssl->out_msglen = 2;
6667 ssl->out_msg[0] = level;
6668 ssl->out_msg[1] = message;
6669
Hanno Becker67bc7c32018-08-06 11:33:50 +01006670 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006673 return( ret );
6674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006676
6677 return( 0 );
6678}
6679
Hanno Becker17572472019-02-08 07:19:04 +00006680#if defined(MBEDTLS_X509_CRT_PARSE_C)
6681static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6682{
6683#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6684 if( session->peer_cert != NULL )
6685 {
6686 mbedtls_x509_crt_free( session->peer_cert );
6687 mbedtls_free( session->peer_cert );
6688 session->peer_cert = NULL;
6689 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006690#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006691 if( session->peer_cert_digest != NULL )
6692 {
6693 /* Zeroization is not necessary. */
6694 mbedtls_free( session->peer_cert_digest );
6695 session->peer_cert_digest = NULL;
6696 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6697 session->peer_cert_digest_len = 0;
6698 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006699#else
6700 ((void) session);
6701#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006702}
6703#endif /* MBEDTLS_X509_CRT_PARSE_C */
6704
Paul Bakker5121ce52009-01-03 21:22:43 +00006705/*
6706 * Handshake functions
6707 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006708#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006709/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006711{
Hanno Beckerdf645962019-06-26 13:02:22 +01006712 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6713 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Hanno Becker5097cba2019-02-05 13:36:46 +00006717 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006720 ssl->state++;
6721 return( 0 );
6722 }
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726}
6727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006729{
Hanno Beckerdf645962019-06-26 13:02:22 +01006730 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6731 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Hanno Becker5097cba2019-02-05 13:36:46 +00006735 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 ssl->state++;
6739 return( 0 );
6740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744}
Gilles Peskinef9828522017-05-03 12:28:43 +02006745
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006746#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006747/* Some certificate support -> implement write and parse */
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006752 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006754 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6755 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006758
Hanno Becker5097cba2019-02-05 13:36:46 +00006759 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006762 ssl->state++;
6763 return( 0 );
6764 }
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006767 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6768 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 {
6770 if( ssl->client_auth == 0 )
6771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 ssl->state++;
6774 return( 0 );
6775 }
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 /*
6779 * If using SSLv3 and got no cert, send an Alert message
6780 * (otherwise an empty Certificate message will be sent).
6781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006783 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
6785 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6787 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6788 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 goto write_msg;
6792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_CLI_C */
6796#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006797 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6802 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 }
6804 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
6809 /*
6810 * 0 . 0 handshake type
6811 * 1 . 3 handshake length
6812 * 4 . 6 length of all certs
6813 * 7 . 9 length of cert. 1
6814 * 10 . n-1 peer certificate
6815 * n . n+2 length of cert. 2
6816 * n+3 . ... upper level cert, etc.
6817 */
6818 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Paul Bakker29087132010-03-21 21:03:34 +00006821 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
6823 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006824 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006827 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 }
6830
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006831 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6834 i += n; crt = crt->next;
6835 }
6836
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006837 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838
6839 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6841 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006843#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006844write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
6847 ssl->state++;
6848
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006849 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 return( ret );
6853 }
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Paul Bakkered27a042013-04-18 22:46:23 +02006857 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858}
6859
Hanno Becker285ff0c2019-01-31 07:44:03 +00006860#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006861
6862#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006863static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6864 unsigned char *crt_buf,
6865 size_t crt_buf_len )
6866{
6867 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6868
6869 if( peer_crt == NULL )
6870 return( -1 );
6871
6872 if( peer_crt->raw.len != crt_buf_len )
6873 return( -1 );
6874
Teppo Järvelin61f412e2019-10-03 12:25:22 +03006875 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006876}
Hanno Becker5882dd02019-06-06 16:25:57 +01006877#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006878static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6879 unsigned char *crt_buf,
6880 size_t crt_buf_len )
6881{
6882 int ret;
6883 unsigned char const * const peer_cert_digest =
6884 ssl->session->peer_cert_digest;
6885 mbedtls_md_type_t const peer_cert_digest_type =
6886 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006887 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00006888 mbedtls_md_info_from_type( peer_cert_digest_type );
6889 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6890 size_t digest_len;
6891
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006892 if( peer_cert_digest == NULL ||
6893 digest_info == MBEDTLS_MD_INVALID_HANDLE )
6894 {
Hanno Beckerdf759382019-02-05 17:02:46 +00006895 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006896 }
Hanno Beckerdf759382019-02-05 17:02:46 +00006897
6898 digest_len = mbedtls_md_get_size( digest_info );
6899 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6900 return( -1 );
6901
6902 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6903 if( ret != 0 )
6904 return( -1 );
6905
Teppo Järvelin61f412e2019-10-03 12:25:22 +03006906 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00006907}
Hanno Becker5882dd02019-06-06 16:25:57 +01006908#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006909#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006910
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006911/*
6912 * Once the certificate message is read, parse it into a cert chain and
6913 * perform basic checks, but leave actual verification to the caller
6914 */
Hanno Becker35e41772019-02-05 15:37:23 +00006915static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6916 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006917{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006918 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006919#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6920 int crt_cnt=0;
6921#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006922 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006923 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006928 mbedtls_ssl_pend_fatal_alert( ssl,
6929 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 }
6932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6934 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006937 mbedtls_ssl_pend_fatal_alert( ssl,
6938 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 }
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006943
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006947 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006949 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006953 mbedtls_ssl_pend_fatal_alert( ssl,
6954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 }
6957
Hanno Becker33c3dc82019-01-30 14:46:46 +00006958 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6959 i += 3;
6960
Hanno Becker33c3dc82019-01-30 14:46:46 +00006961 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 while( i < ssl->in_hslen )
6963 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006965 if ( i + 3 > ssl->in_hslen ) {
6966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006967 mbedtls_ssl_pend_fatal_alert( ssl,
6968 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006969 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6970 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006971 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6972 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 if( ssl->in_msg[i] != 0 )
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 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006982 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 i += 3;
6984
6985 if( n < 128 || i + n > ssl->in_hslen )
6986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006988 mbedtls_ssl_pend_fatal_alert( ssl,
6989 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 }
6992
Hanno Becker33c3dc82019-01-30 14:46:46 +00006993 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006994#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6995 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006996 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6997 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006998 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006999 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007000 /* During client-side renegotiation, check that the server's
7001 * end-CRTs hasn't changed compared to the initial handshake,
7002 * mitigating the triple handshake attack. On success, reuse
7003 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7005 if( ssl_check_peer_crt_unchanged( ssl,
7006 &ssl->in_msg[i],
7007 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007008 {
Hanno Becker35e41772019-02-05 15:37:23 +00007009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007010 mbedtls_ssl_pend_fatal_alert( ssl,
7011 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007012 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007013 }
Hanno Becker35e41772019-02-05 15:37:23 +00007014
7015 /* Now we can safely free the original chain. */
7016 ssl_clear_peer_cert( ssl->session );
7017 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7019
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007021#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007022 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007023#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007024 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007025 * it in-place from the input buffer instead of making a copy. */
7026 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7027#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007028 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007030 case 0: /*ok*/
7031 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7032 /* Ignore certificate with an unknown algorithm: maybe a
7033 prior certificate was already trusted. */
7034 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007035
Hanno Becker33c3dc82019-01-30 14:46:46 +00007036 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7037 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7038 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007039
Hanno Becker33c3dc82019-01-30 14:46:46 +00007040 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7041 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7042 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007043
Hanno Becker33c3dc82019-01-30 14:46:46 +00007044 default:
7045 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7046 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007047 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007048 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7049 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050 }
7051
7052 i += n;
7053 }
7054
Hanno Becker35e41772019-02-05 15:37:23 +00007055 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007056 return( 0 );
7057}
7058
Hanno Beckerb8a08572019-02-05 12:49:06 +00007059#if defined(MBEDTLS_SSL_SRV_C)
7060static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7061{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007062 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007063 return( -1 );
7064
7065#if defined(MBEDTLS_SSL_PROTO_SSL3)
7066 /*
7067 * Check if the client sent an empty certificate
7068 */
Hanno Becker2881d802019-05-22 14:44:53 +01007069 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007070 {
7071 if( ssl->in_msglen == 2 &&
7072 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7073 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7074 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7075 {
7076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7077 return( 0 );
7078 }
7079
7080 return( -1 );
7081 }
7082#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7083
7084#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7085 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7086 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7087 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7088 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007089 mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007090 {
7091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7092 return( 0 );
7093 }
7094
Hanno Beckerb8a08572019-02-05 12:49:06 +00007095#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7096 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007097
7098 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007099}
7100#endif /* MBEDTLS_SSL_SRV_C */
7101
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007102/* Check if a certificate message is expected.
7103 * Return either
7104 * - SSL_CERTIFICATE_EXPECTED, or
7105 * - SSL_CERTIFICATE_SKIP
7106 * indicating whether a Certificate message is expected or not.
7107 */
7108#define SSL_CERTIFICATE_EXPECTED 0
7109#define SSL_CERTIFICATE_SKIP 1
7110static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7111 int authmode )
7112{
Hanno Becker473f98f2019-06-26 10:27:32 +01007113 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007114 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007115
7116 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7117 return( SSL_CERTIFICATE_SKIP );
7118
7119#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007120 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007121 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007122 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7123 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7124 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007125 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007127
7128 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7129 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007130 ssl->session_negotiate->verify_result =
7131 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7132 return( SSL_CERTIFICATE_SKIP );
7133 }
7134 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007135#else
7136 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007137#endif /* MBEDTLS_SSL_SRV_C */
7138
7139 return( SSL_CERTIFICATE_EXPECTED );
7140}
7141
Hanno Becker3cf50612019-02-05 14:36:34 +00007142static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7143 int authmode,
7144 mbedtls_x509_crt *chain,
7145 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007146{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007147 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007148 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007149 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007150 mbedtls_x509_crt *ca_chain;
7151 mbedtls_x509_crl *ca_crl;
7152
7153 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7154 return( 0 );
7155
7156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7157 if( ssl->handshake->sni_ca_chain != NULL )
7158 {
7159 ca_chain = ssl->handshake->sni_ca_chain;
7160 ca_crl = ssl->handshake->sni_ca_crl;
7161 }
7162 else
7163#endif
7164 {
7165 ca_chain = ssl->conf->ca_chain;
7166 ca_crl = ssl->conf->ca_crl;
7167 }
7168
7169 /*
7170 * Main check: verify certificate
7171 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007172 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007173 chain,
7174 ca_chain, ca_crl,
7175 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007176#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007177 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007178#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007179 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007180#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7181 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7182#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7183 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007184
Hanno Becker8c13ee62019-02-26 16:48:17 +00007185 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007186 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007187 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007188 }
7189
7190#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007192 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7193#endif
7194
7195 /*
7196 * Secondary checks: always done, but change 'ret' only if it was 0
7197 */
7198
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007199#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007200 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007201 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007202#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007203 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007204#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007205 mbedtls_pk_context *pk;
7206 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7207 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007208 {
7209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007210 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007211 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007212
7213 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007214 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007215 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007216 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007217 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007218
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007219 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007220#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221
7222 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007223 {
7224 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7225
7226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007227 if( verify_ret == 0 )
7228 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007229 }
7230 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007231#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007232
7233 if( mbedtls_ssl_check_cert_usage( chain,
7234 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007235 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7236 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007237 &ssl->session_negotiate->verify_result ) != 0 )
7238 {
7239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007240 if( verify_ret == 0 )
7241 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007242 }
7243
7244 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7245 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7246 * with details encoded in the verification flags. All other kinds
7247 * of error codes, including those from the user provided f_vrfy
7248 * functions, are treated as fatal and lead to a failure of
7249 * ssl_parse_certificate even if verification was optional. */
7250 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007251 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7252 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007253 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007254 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007255 }
7256
7257 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7258 {
7259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007260 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007261 }
7262
Hanno Becker8c13ee62019-02-26 16:48:17 +00007263 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007264 {
7265 uint8_t alert;
7266
7267 /* The certificate may have been rejected for several reasons.
7268 Pick one and send the corresponding alert. Which alert to send
7269 may be a subject of debate in some cases. */
7270 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7271 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7272 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7273 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7274 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7275 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7276 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7277 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7278 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7279 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7280 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7281 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7282 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7283 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7284 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7285 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7286 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7287 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7288 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7289 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7290 else
7291 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007292 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007293 }
7294
7295#if defined(MBEDTLS_DEBUG_C)
7296 if( ssl->session_negotiate->verify_result != 0 )
7297 {
7298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7299 ssl->session_negotiate->verify_result ) );
7300 }
7301 else
7302 {
7303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7304 }
7305#endif /* MBEDTLS_DEBUG_C */
7306
Hanno Becker8c13ee62019-02-26 16:48:17 +00007307 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007308}
7309
Hanno Becker34106f62019-02-08 14:59:05 +00007310#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007311
7312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007313static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7314 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007315{
7316 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007317 /* Remember digest of the peer's end-CRT. */
7318 ssl->session_negotiate->peer_cert_digest =
7319 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7320 if( ssl->session_negotiate->peer_cert_digest == NULL )
7321 {
7322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7323 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007324 mbedtls_ssl_pend_fatal_alert( ssl,
7325 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007326
7327 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7328 }
7329
7330 ret = mbedtls_md( mbedtls_md_info_from_type(
7331 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7332 start, len,
7333 ssl->session_negotiate->peer_cert_digest );
7334
7335 ssl->session_negotiate->peer_cert_digest_type =
7336 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7337 ssl->session_negotiate->peer_cert_digest_len =
7338 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7339
7340 return( ret );
7341}
Hanno Becker5882dd02019-06-06 16:25:57 +01007342#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007343
7344static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7345 unsigned char *start, size_t len )
7346{
7347 unsigned char *end = start + len;
7348 int ret;
7349
7350 /* Make a copy of the peer's raw public key. */
7351 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7352 ret = mbedtls_pk_parse_subpubkey( &start, end,
7353 &ssl->handshake->peer_pubkey );
7354 if( ret != 0 )
7355 {
7356 /* We should have parsed the public key before. */
7357 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7358 }
7359
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007360 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007361 return( 0 );
7362}
7363#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7364
Hanno Becker3cf50612019-02-05 14:36:34 +00007365int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7366{
7367 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007368 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007369#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7370 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7371 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007372 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007374 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007375#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007376 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007377 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378
7379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7380
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007381 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7382 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007383 {
7384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007385 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386 }
7387
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007388#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7389 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007390 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007391 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007392 chain = ssl->handshake->ecrs_peer_cert;
7393 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007394 goto crt_verify;
7395 }
7396#endif
7397
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007398 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007399 {
7400 /* mbedtls_ssl_read_record may have sent an alert already. We
7401 let it decide whether to alert. */
7402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007403 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007404 }
7405
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007406#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007407 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7408 {
7409 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007410
Hanno Beckerb8a08572019-02-05 12:49:06 +00007411 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007412 ret = 0;
7413 else
7414 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007415
Hanno Becker613d4902019-02-05 13:11:17 +00007416 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007417 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007418#endif /* MBEDTLS_SSL_SRV_C */
7419
Hanno Becker35e41772019-02-05 15:37:23 +00007420 /* Clear existing peer CRT structure in case we tried to
7421 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007422 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007423
7424 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7425 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007426 {
7427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7428 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007429 mbedtls_ssl_pend_fatal_alert( ssl,
7430 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007431
Hanno Beckere4aeb762019-02-05 17:19:52 +00007432 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7433 goto exit;
7434 }
7435 mbedtls_x509_crt_init( chain );
7436
7437 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007438 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007439 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007440
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007441#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7442 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007443 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007444
7445crt_verify:
7446 if( ssl->handshake->ecrs_enabled)
7447 rs_ctx = &ssl->handshake->ecrs_ctx;
7448#endif
7449
Hanno Becker3cf50612019-02-05 14:36:34 +00007450 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007451 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007452 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007453 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007454
Hanno Becker3008d282019-02-05 17:02:28 +00007455#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007456 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007457 size_t pk_len;
7458 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
Hanno Becker34106f62019-02-08 14:59:05 +00007460 /* We parse the CRT chain without copying, so
7461 * these pointers point into the input buffer,
7462 * and are hence still valid after freeing the
7463 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007464
Hanno Becker5882dd02019-06-06 16:25:57 +01007465#if defined(MBEDTLS_SSL_RENEGOTIATION)
7466 unsigned char *crt_start;
7467 size_t crt_len;
7468
Hanno Becker34106f62019-02-08 14:59:05 +00007469 crt_start = chain->raw.p;
7470 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007471#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007472
Hanno Becker8c13ee62019-02-26 16:48:17 +00007473 pk_start = chain->cache->pk_raw.p;
7474 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007475
7476 /* Free the CRT structures before computing
7477 * digest and copying the peer's public key. */
7478 mbedtls_x509_crt_free( chain );
7479 mbedtls_free( chain );
7480 chain = NULL;
7481
Hanno Becker5882dd02019-06-06 16:25:57 +01007482#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007483 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007485 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007486#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007487
Hanno Becker34106f62019-02-08 14:59:05 +00007488 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007489 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007490 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007491 }
Hanno Becker34106f62019-02-08 14:59:05 +00007492#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7493 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007494 ssl->session_negotiate->peer_cert = chain;
7495 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007496#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Hanno Becker613d4902019-02-05 13:11:17 +00007500exit:
7501
Hanno Beckere4aeb762019-02-05 17:19:52 +00007502 if( ret == 0 )
7503 ssl->state++;
7504
7505#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7506 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7507 {
7508 ssl->handshake->ecrs_peer_cert = chain;
7509 chain = NULL;
7510 }
7511#endif
7512
7513 if( chain != NULL )
7514 {
7515 mbedtls_x509_crt_free( chain );
7516 mbedtls_free( chain );
7517 }
7518
Paul Bakker5121ce52009-01-03 21:22:43 +00007519 return( ret );
7520}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007521#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007524{
7525 int ret;
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007530 ssl->out_msglen = 1;
7531 ssl->out_msg[0] = 1;
7532
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 ssl->state++;
7534
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007535 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007536 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 return( ret );
7539 }
7540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
7543 return( 0 );
7544}
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007547{
7548 int ret;
7549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551
Hanno Becker327c93b2018-08-15 13:56:18 +01007552 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007555 return( ret );
7556 }
7557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007561 mbedtls_ssl_pend_fatal_alert( ssl,
7562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564 }
7565
Hanno Beckere678eaa2018-08-21 14:57:46 +01007566 /* CCS records are only accepted if they have length 1 and content '1',
7567 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007568
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007569 /*
7570 * Switch to our negotiated transform and session parameters for inbound
7571 * data.
7572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574 ssl->transform_in = ssl->transform_negotiate;
7575 ssl->session_in = ssl->session_negotiate;
7576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007578 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 ssl_dtls_replay_reset( ssl );
7582#endif
7583
7584 /* Increment epoch */
7585 if( ++ssl->in_epoch == 0 )
7586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007588 /* This is highly unlikely to happen for legitimate reasons, so
7589 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007591 }
7592 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007593 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007595#if defined(MBEDTLS_SSL_PROTO_TLS)
7596 {
7597 memset( ssl->in_ctr, 0, 8 );
7598 }
7599#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007600
Hanno Beckerf5970a02019-05-08 09:38:41 +01007601 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7604 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007609 mbedtls_ssl_pend_fatal_alert( ssl,
7610 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007612 }
7613 }
7614#endif
7615
Paul Bakker5121ce52009-01-03 21:22:43 +00007616 ssl->state++;
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
7620 return( 0 );
7621}
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7626 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007627 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007628 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7631#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007632 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007635 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007638}
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007643
7644 /*
7645 * Free our handshake params
7646 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007647 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007649 ssl->handshake = NULL;
7650
7651 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007652 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007653 */
7654 if( ssl->transform )
7655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 mbedtls_ssl_transform_free( ssl->transform );
7657 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007658 }
7659 ssl->transform = ssl->transform_negotiate;
7660 ssl->transform_negotiate = NULL;
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007663}
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if defined(MBEDTLS_SSL_RENEGOTIATION)
7670 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007673 ssl->renego_records_seen = 0;
7674 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007675#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676
7677 /*
7678 * Free the previous session and switch in the current one
7679 */
Paul Bakker0a597072012-09-25 21:55:46 +00007680 if( ssl->session )
7681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007683 /* RFC 7366 3.1: keep the EtM state */
7684 ssl->session_negotiate->encrypt_then_mac =
7685 ssl->session->encrypt_then_mac;
7686#endif
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 mbedtls_ssl_session_free( ssl->session );
7689 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007690 }
7691 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007692 ssl->session_negotiate = NULL;
7693
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007694#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007695 /*
7696 * Add cache entry
7697 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007698 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007699 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007700 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007701 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007702 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007704 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007705#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007708 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007709 ssl->handshake->flight != NULL )
7710 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007711 /* Cancel handshake timer */
7712 ssl_set_timer( ssl, 0 );
7713
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007714 /* Keep last flight around in case we need to resend it:
7715 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007717 }
7718 else
7719#endif
7720 ssl_handshake_wrapup_free_hs_transform( ssl );
7721
Paul Bakker48916f92012-09-16 19:57:18 +00007722 ssl->state++;
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007725}
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007729 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007732
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007733 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007734
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007735 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7736 mbedtls_ssl_suite_get_mac(
7737 mbedtls_ssl_ciphersuite_from_id(
7738 mbedtls_ssl_session_get_ciphersuite(
7739 ssl->session_negotiate ) ) ),
7740 ssl, ssl->out_msg + 4,
7741 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007742
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007743 /*
7744 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7745 * may define some other value. Currently (early 2016), no defined
7746 * ciphersuite does this (and this is unlikely to change as activity has
7747 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7748 */
Hanno Becker2881d802019-05-22 14:44:53 +01007749 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007752 ssl->verify_data_len = hash_len;
7753 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007754#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007755
Paul Bakker5121ce52009-01-03 21:22:43 +00007756 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7758 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007759
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007760#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 /*
7762 * In case of session resuming, invert the client and server
7763 * ChangeCipherSpec messages order.
7764 */
Paul Bakker0a597072012-09-25 21:55:46 +00007765 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007768 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7769 MBEDTLS_SSL_IS_CLIENT )
7770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007772 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007775 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7776 MBEDTLS_SSL_IS_SERVER )
7777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007779 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007781 }
7782 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007783#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007784 ssl->state++;
7785
Paul Bakker48916f92012-09-16 19:57:18 +00007786 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007787 * Switch to our negotiated transform and session parameters for outbound
7788 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007793 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007794 {
7795 unsigned char i;
7796
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007797 /* Remember current epoch settings for resending */
7798 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007799 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007800
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007801 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007802 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007803
7804 /* Increment epoch */
7805 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007806 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007807 break;
7808
7809 /* The loop goes to its end iff the counter is wrapping */
7810 if( i == 0 )
7811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7813 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007814 }
7815 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007816 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007818#if defined(MBEDTLS_SSL_PROTO_TLS)
7819 {
7820 memset( ssl->cur_out_ctr, 0, 8 );
7821 }
7822#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007824 ssl->transform_out = ssl->transform_negotiate;
7825 ssl->session_out = ssl->session_negotiate;
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7828 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7833 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007834 }
7835 }
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007839 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007841#endif
7842
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007843 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 return( ret );
7847 }
7848
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007851 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7852 {
7853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7854 return( ret );
7855 }
7856#endif
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859
7860 return( 0 );
7861}
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007864#define SSL_MAX_HASH_LEN 36
7865#else
7866#define SSL_MAX_HASH_LEN 12
7867#endif
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007870{
Paul Bakker23986e52011-04-24 08:57:21 +00007871 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007872 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007873 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007876
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007877 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7878 mbedtls_ssl_suite_get_mac(
7879 mbedtls_ssl_ciphersuite_from_id(
7880 mbedtls_ssl_session_get_ciphersuite(
7881 ssl->session_negotiate ) ) ),
7882 ssl, buf,
7883 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007884
Hanno Becker327c93b2018-08-15 13:56:18 +01007885 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 return( ret );
7889 }
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007894 mbedtls_ssl_pend_fatal_alert( ssl,
7895 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007897 }
7898
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007899 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007901 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007902 hash_len = 36;
7903 else
7904#endif
7905 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7908 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007911 mbedtls_ssl_pend_fatal_alert( ssl,
7912 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007914 }
7915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007917 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007920 mbedtls_ssl_pend_fatal_alert( ssl,
7921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007923 }
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007926 ssl->verify_data_len = hash_len;
7927 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007928#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007929
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007930#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007931 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007934 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007938 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007941 }
7942 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007943#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007944 ssl->state++;
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007947 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007949#endif
7950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007952
7953 return( 0 );
7954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7961 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7962 mbedtls_md5_init( &handshake->fin_md5 );
7963 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007964 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7965 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7968#if defined(MBEDTLS_SHA256_C)
7969 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007970 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#if defined(MBEDTLS_SHA512_C)
7973 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007974 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977
Hanno Becker7e5437a2017-04-28 17:15:26 +01007978#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7979 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7980 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_DHM_C)
7984 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_ECDH_C)
7987 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007988#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007990 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007991#if defined(MBEDTLS_SSL_CLI_C)
7992 handshake->ecjpake_cache = NULL;
7993 handshake->ecjpake_cache_len = 0;
7994#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007995#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007996
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007997#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007998 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007999#endif
8000
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008001#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8002 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8003#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008004
8005#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8006 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8007 mbedtls_pk_init( &handshake->peer_pubkey );
8008#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009}
8010
Hanno Becker611a83b2018-01-03 14:27:32 +00008011void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8016 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008017
Hanno Becker92231322018-01-03 15:32:51 +00008018#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 mbedtls_md_init( &transform->md_ctx_enc );
8020 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008021#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022}
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027}
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008030{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008031 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008032 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008034 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008037 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038
8039 /*
8040 * Either the pointers are now NULL or cleared properly and can be freed.
8041 * Now allocate missing structures.
8042 */
8043 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008044 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008045 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008046 }
Paul Bakker48916f92012-09-16 19:57:18 +00008047
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008048 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008049 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008050 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008051 }
Paul Bakker48916f92012-09-16 19:57:18 +00008052
Paul Bakker82788fb2014-10-20 13:59:19 +02008053 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008054 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008055 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008056 }
Paul Bakker48916f92012-09-16 19:57:18 +00008057
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008058 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008059 if( ssl->handshake == NULL ||
8060 ssl->transform_negotiate == NULL ||
8061 ssl->session_negotiate == NULL )
8062 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 mbedtls_free( ssl->handshake );
8066 mbedtls_free( ssl->transform_negotiate );
8067 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008068
8069 ssl->handshake = NULL;
8070 ssl->transform_negotiate = NULL;
8071 ssl->session_negotiate = NULL;
8072
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008074 }
8075
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008076 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008078 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008079 ssl_handshake_params_init( ssl->handshake );
8080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008082 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008083 {
8084 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008085
Hanno Becker2d9623f2019-06-13 12:07:05 +01008086 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008087 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8088 else
8089 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8090 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008091#endif
8092
Paul Bakker48916f92012-09-16 19:57:18 +00008093 return( 0 );
8094}
8095
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008096#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008097/* Dummy cookie callbacks for defaults */
8098static int ssl_cookie_write_dummy( void *ctx,
8099 unsigned char **p, unsigned char *end,
8100 const unsigned char *cli_id, size_t cli_id_len )
8101{
8102 ((void) ctx);
8103 ((void) p);
8104 ((void) end);
8105 ((void) cli_id);
8106 ((void) cli_id_len);
8107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008109}
8110
8111static int ssl_cookie_check_dummy( void *ctx,
8112 const unsigned char *cookie, size_t cookie_len,
8113 const unsigned char *cli_id, size_t cli_id_len )
8114{
8115 ((void) ctx);
8116 ((void) cookie);
8117 ((void) cookie_len);
8118 ((void) cli_id);
8119 ((void) cli_id_len);
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008122}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008123#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008124
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008125/* Once ssl->out_hdr as the address of the beginning of the
8126 * next outgoing record is set, deduce the other pointers.
8127 *
8128 * Note: For TLS, we save the implicit record sequence number
8129 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8130 * and the caller has to make sure there's space for this.
8131 */
8132
8133static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8134 mbedtls_ssl_transform *transform )
8135{
8136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008137 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138 {
8139 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008140#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008141 ssl->out_cid = ssl->out_ctr + 8;
8142 ssl->out_len = ssl->out_cid;
8143 if( transform != NULL )
8144 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008145#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008146 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008148 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008149 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008150 MBEDTLS_SSL_TRANSPORT_ELSE
8151#endif /* MBEDTLS_SSL_PROTO_DTLS */
8152#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153 {
8154 ssl->out_ctr = ssl->out_hdr - 8;
8155 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008157 ssl->out_cid = ssl->out_len;
8158#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008159 ssl->out_iv = ssl->out_hdr + 5;
8160 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008161#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162
8163 /* Adjust out_msg to make space for explicit IV, if used. */
8164 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008165 mbedtls_ssl_ver_geq(
8166 mbedtls_ssl_get_minor_ver( ssl ),
8167 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 {
8169 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8170 }
8171 else
8172 ssl->out_msg = ssl->out_iv;
8173}
8174
8175/* Once ssl->in_hdr as the address of the beginning of the
8176 * next incoming record is set, deduce the other pointers.
8177 *
8178 * Note: For TLS, we save the implicit record sequence number
8179 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8180 * and the caller has to make sure there's space for this.
8181 */
8182
Hanno Beckerf5970a02019-05-08 09:38:41 +01008183static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008184{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008185 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008186 * of unprotected TLS/DTLS records, with ssl->in_msg
8187 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008188 *
8189 * When decrypting a protected record, ssl->in_msg
8190 * will be shifted to point to the beginning of the
8191 * record plaintext.
8192 */
8193
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008195 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008196 {
Hanno Becker70e79282019-05-03 14:34:53 +01008197 /* This sets the header pointers to match records
8198 * without CID. When we receive a record containing
8199 * a CID, the fields are shifted accordingly in
8200 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008201 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008202#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008203 ssl->in_cid = ssl->in_ctr + 8;
8204 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008205#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008206 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008208 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008209 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008210 MBEDTLS_SSL_TRANSPORT_ELSE
8211#endif /* MBEDTLS_SSL_PROTO_DTLS */
8212#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008213 {
8214 ssl->in_ctr = ssl->in_hdr - 8;
8215 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008217 ssl->in_cid = ssl->in_len;
8218#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008219 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008220 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008221#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008222}
8223
Paul Bakker5121ce52009-01-03 21:22:43 +00008224/*
8225 * Initialize an SSL context
8226 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008227void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8228{
8229 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8230}
8231
8232/*
8233 * Setup an SSL context
8234 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008235
8236static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8237{
8238 /* Set the incoming and outgoing record pointers. */
8239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008240 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008241 {
8242 ssl->out_hdr = ssl->out_buf;
8243 ssl->in_hdr = ssl->in_buf;
8244 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008245 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008247#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008248 {
8249 ssl->out_hdr = ssl->out_buf + 8;
8250 ssl->in_hdr = ssl->in_buf + 8;
8251 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008252#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008253
8254 /* Derive other internal pointers. */
8255 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008256 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008257}
8258
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008259int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008260 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008261{
Paul Bakker48916f92012-09-16 19:57:18 +00008262 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008264 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008265
Hanno Beckeref982d52019-07-23 15:56:18 +01008266#if defined(MBEDTLS_USE_TINYCRYPT)
8267 uECC_set_rng( &uecc_rng_wrapper );
8268#endif
8269
Paul Bakker62f2dee2012-09-28 07:31:51 +00008270 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008271 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008272 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008273
8274 /* Set to NULL in case of an error condition */
8275 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008276
Angus Grattond8213d02016-05-25 20:56:48 +10008277 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8278 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008279 {
Angus Grattond8213d02016-05-25 20:56:48 +10008280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008281 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008282 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008283 }
8284
8285 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8286 if( ssl->out_buf == NULL )
8287 {
8288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008289 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008290 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008291 }
8292
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008293 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008294
Paul Bakker48916f92012-09-16 19:57:18 +00008295 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008296 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008297
Hanno Beckerc8f52992019-07-25 11:15:08 +01008298 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008299
Paul Bakker5121ce52009-01-03 21:22:43 +00008300 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008301
8302error:
8303 mbedtls_free( ssl->in_buf );
8304 mbedtls_free( ssl->out_buf );
8305
8306 ssl->conf = NULL;
8307
8308 ssl->in_buf = NULL;
8309 ssl->out_buf = NULL;
8310
8311 ssl->in_hdr = NULL;
8312 ssl->in_ctr = NULL;
8313 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008314 ssl->in_msg = NULL;
8315
8316 ssl->out_hdr = NULL;
8317 ssl->out_ctr = NULL;
8318 ssl->out_len = NULL;
8319 ssl->out_iv = NULL;
8320 ssl->out_msg = NULL;
8321
k-stachowiak9f7798e2018-07-31 16:52:32 +02008322 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008323}
8324
8325/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008326 * Reset an initialized and used SSL context for re-use while retaining
8327 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008328 *
8329 * If partial is non-zero, keep data in the input buffer and client ID.
8330 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008331 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008332static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008333{
Paul Bakker48916f92012-09-16 19:57:18 +00008334 int ret;
8335
Hanno Becker7e772132018-08-10 12:38:21 +01008336#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8337 !defined(MBEDTLS_SSL_SRV_C)
8338 ((void) partial);
8339#endif
8340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008342
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008343 /* Cancel any possibly running timer */
8344 ssl_set_timer( ssl, 0 );
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_RENEGOTIATION)
8347 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008348 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008349
8350 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8352 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008355
Paul Bakker7eb013f2011-10-06 12:37:39 +00008356 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008357 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008358
8359 ssl->in_msgtype = 0;
8360 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008362 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008363 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008366 ssl_dtls_replay_reset( ssl );
8367#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008368
8369 ssl->in_hslen = 0;
8370 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008371
8372 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008373
8374 ssl->out_msgtype = 0;
8375 ssl->out_msglen = 0;
8376 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8378 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008379 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008380#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008381
Hanno Becker19859472018-08-06 09:40:20 +01008382 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8383
Paul Bakker48916f92012-09-16 19:57:18 +00008384 ssl->transform_in = NULL;
8385 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008386
Hanno Becker78640902018-08-13 16:35:15 +01008387 ssl->session_in = NULL;
8388 ssl->session_out = NULL;
8389
Angus Grattond8213d02016-05-25 20:56:48 +10008390 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008391
8392#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008393 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008394#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8395 {
8396 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008397 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008398 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8401 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8404 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8407 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008408 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008409 }
8410#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008411
Paul Bakker48916f92012-09-16 19:57:18 +00008412 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 mbedtls_ssl_transform_free( ssl->transform );
8415 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008416 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008417 }
Paul Bakker48916f92012-09-16 19:57:18 +00008418
Paul Bakkerc0463502013-02-14 11:19:38 +01008419 if( ssl->session )
8420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 mbedtls_ssl_session_free( ssl->session );
8422 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008423 ssl->session = NULL;
8424 }
8425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008427 ssl->alpn_chosen = NULL;
8428#endif
8429
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008430#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008431#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008432 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008433#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008434 {
8435 mbedtls_free( ssl->cli_id );
8436 ssl->cli_id = NULL;
8437 ssl->cli_id_len = 0;
8438 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008439#endif
8440
Paul Bakker48916f92012-09-16 19:57:18 +00008441 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8442 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008443
8444 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008445}
8446
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008447/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008448 * Reset an initialized and used SSL context for re-use while retaining
8449 * all application-set variables, function pointers and data.
8450 */
8451int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8452{
8453 return( ssl_session_reset_int( ssl, 0 ) );
8454}
8455
8456/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008457 * SSL set accessors
8458 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008459#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008462 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008463}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008464#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008465
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008466void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008467{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008469}
8470
Hanno Becker7f376f42019-06-12 16:20:48 +01008471#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8472 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008473void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008474{
Hanno Becker7f376f42019-06-12 16:20:48 +01008475 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008476}
Hanno Becker7f376f42019-06-12 16:20:48 +01008477#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008478
Hanno Beckerde671542019-06-12 16:30:46 +01008479#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8480 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8481void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8482 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008483{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008484 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008485}
Hanno Beckerde671542019-06-12 16:30:46 +01008486#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008489
Hanno Becker1841b0a2018-08-24 11:13:57 +01008490void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8491 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008492{
8493 ssl->disable_datagram_packing = !allow_packing;
8494}
8495
Hanno Becker1f835fa2019-06-13 10:14:59 +01008496#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8497 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008498void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8499 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008501 conf->hs_timeout_min = min;
8502 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008503}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008504#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8505 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8506void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8507 uint32_t min, uint32_t max )
8508{
8509 ((void) conf);
8510 ((void) min);
8511 ((void) max);
8512}
8513#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8514 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8515
8516#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008517
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008518void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008519{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008520#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8521 conf->authmode = authmode;
8522#else
8523 ((void) conf);
8524 ((void) authmode);
8525#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008526}
8527
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008528#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8529 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008530void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008531 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008532 void *p_vrfy )
8533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008534 conf->f_vrfy = f_vrfy;
8535 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008536}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008537#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008538
Hanno Beckerece325c2019-06-13 15:39:27 +01008539#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008541 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008542 void *p_rng )
8543{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008544 conf->f_rng = f_rng;
8545 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008546}
Hanno Beckerece325c2019-06-13 15:39:27 +01008547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008548
Hanno Becker14a4a442019-07-02 17:00:34 +01008549#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008551 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008552 void *p_dbg )
8553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008554 conf->f_dbg = f_dbg;
8555 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008556}
Hanno Becker14a4a442019-07-02 17:00:34 +01008557#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008558
Hanno Beckera58a8962019-06-13 16:11:15 +01008559#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8560 !defined(MBEDTLS_SSL_CONF_SEND) && \
8561 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008563 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008564 mbedtls_ssl_send_t *f_send,
8565 mbedtls_ssl_recv_t *f_recv,
8566 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008567{
Hanno Beckera58a8962019-06-13 16:11:15 +01008568 ssl->p_bio = p_bio;
8569 ssl->f_send = f_send;
8570 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008571 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008572}
Hanno Beckera58a8962019-06-13 16:11:15 +01008573#else
8574void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8575 void *p_bio )
8576{
8577 ssl->p_bio = p_bio;
8578}
8579#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008580
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008581#if defined(MBEDTLS_SSL_PROTO_DTLS)
8582void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8583{
8584 ssl->mtu = mtu;
8585}
8586#endif
8587
Hanno Becker1f835fa2019-06-13 10:14:59 +01008588#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008589void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008590{
8591 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008592}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008593#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008594
Hanno Becker0ae6b242019-06-13 16:45:36 +01008595#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8596 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008597void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8598 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008599 mbedtls_ssl_set_timer_t *f_set_timer,
8600 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008601{
8602 ssl->p_timer = p_timer;
8603 ssl->f_set_timer = f_set_timer;
8604 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008605 /* Make sure we start with no timer running */
8606 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008607}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008608#else
8609void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8610 void *p_timer )
8611{
8612 ssl->p_timer = p_timer;
8613 /* Make sure we start with no timer running */
8614 ssl_set_timer( ssl, 0 );
8615}
8616#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008617
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008618#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008619void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008620 void *p_cache,
8621 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8622 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008623{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008624 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008625 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008627}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008628#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008629
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008630#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008632{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008633 int ret;
8634
8635 if( ssl == NULL ||
8636 session == NULL ||
8637 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008638 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008641 }
8642
Hanno Becker58fccf22019-02-06 14:30:46 +00008643 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8644 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008645 return( ret );
8646
Paul Bakker0a597072012-09-25 21:55:46 +00008647 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008648
8649 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008650}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008651#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008652
Hanno Becker73f4cb12019-06-27 13:51:07 +01008653#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008654void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008655 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008656{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008657 conf->ciphersuite_list[0] = ciphersuites;
8658 conf->ciphersuite_list[1] = ciphersuites;
8659 conf->ciphersuite_list[2] = ciphersuites;
8660 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008661}
8662
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008663void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008664 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008665 int major, int minor )
8666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008668 return;
8669
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008670 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
8671 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
8672 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008673 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008674 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008675
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008676 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
8677 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008678}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008679#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008682void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008683 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008684{
8685 conf->cert_profile = profile;
8686}
8687
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008688/* Append a new keycert entry to a (possibly empty) list */
8689static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8690 mbedtls_x509_crt *cert,
8691 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008692{
niisato8ee24222018-06-25 19:05:48 +09008693 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008694
niisato8ee24222018-06-25 19:05:48 +09008695 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8696 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008697 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008698
niisato8ee24222018-06-25 19:05:48 +09008699 new_cert->cert = cert;
8700 new_cert->key = key;
8701 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008702
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008703 /* Update head is the list was null, else add to the end */
8704 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008705 {
niisato8ee24222018-06-25 19:05:48 +09008706 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008707 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008708 else
8709 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008710 mbedtls_ssl_key_cert *cur = *head;
8711 while( cur->next != NULL )
8712 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008713 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008714 }
8715
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008716 return( 0 );
8717}
8718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008719int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008720 mbedtls_x509_crt *own_cert,
8721 mbedtls_pk_context *pk_key )
8722{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008723 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008724}
8725
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008726void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008727 mbedtls_x509_crt *ca_chain,
8728 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008729{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008730 conf->ca_chain = ca_chain;
8731 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008734
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008735#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8736int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8737 mbedtls_x509_crt *own_cert,
8738 mbedtls_pk_context *pk_key )
8739{
8740 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8741 own_cert, pk_key ) );
8742}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008743
8744void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8745 mbedtls_x509_crt *ca_chain,
8746 mbedtls_x509_crl *ca_crl )
8747{
8748 ssl->handshake->sni_ca_chain = ca_chain;
8749 ssl->handshake->sni_ca_crl = ca_crl;
8750}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008751
8752void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8753 int authmode )
8754{
8755 ssl->handshake->sni_authmode = authmode;
8756}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008757#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8758
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008759#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008760/*
8761 * Set EC J-PAKE password for current handshake
8762 */
8763int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8764 const unsigned char *pw,
8765 size_t pw_len )
8766{
8767 mbedtls_ecjpake_role role;
8768
Janos Follath8eb64132016-06-03 15:40:57 +01008769 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8771
Hanno Becker2d9623f2019-06-13 12:07:05 +01008772 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008773 role = MBEDTLS_ECJPAKE_SERVER;
8774 else
8775 role = MBEDTLS_ECJPAKE_CLIENT;
8776
8777 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8778 role,
8779 MBEDTLS_MD_SHA256,
8780 MBEDTLS_ECP_DP_SECP256R1,
8781 pw, pw_len ) );
8782}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008783#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008786int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008787 const unsigned char *psk, size_t psk_len,
8788 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008789{
Paul Bakker6db455e2013-09-18 17:29:31 +02008790 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008795
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008796 /* Identity len will be encoded on two bytes */
8797 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008798 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008799 {
8800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8801 }
8802
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008803 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008804 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008805 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008806
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008807 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008808 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008809 conf->psk_len = 0;
8810 }
8811 if( conf->psk_identity != NULL )
8812 {
8813 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008814 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008815 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008816 }
8817
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008818 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8819 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008820 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008821 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008822 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008823 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008824 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008825 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008826 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008827
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008828 conf->psk_len = psk_len;
8829 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008830
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008831 memcpy( conf->psk, psk, conf->psk_len );
8832 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008833
8834 return( 0 );
8835}
8836
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008837int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8838 const unsigned char *psk, size_t psk_len )
8839{
8840 if( psk == NULL || ssl->handshake == NULL )
8841 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8842
8843 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8845
8846 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008847 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008848 mbedtls_platform_zeroize( ssl->handshake->psk,
8849 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008850 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008851 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008852 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008853
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008854 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008855 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008856
8857 ssl->handshake->psk_len = psk_len;
8858 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8859
8860 return( 0 );
8861}
8862
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008863void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008865 size_t),
8866 void *p_psk )
8867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008868 conf->f_psk = f_psk;
8869 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008872
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008873#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008874
8875#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008876int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008877{
8878 int ret;
8879
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8881 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8882 {
8883 mbedtls_mpi_free( &conf->dhm_P );
8884 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008885 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008887
8888 return( 0 );
8889}
Hanno Becker470a8c42017-10-04 15:28:46 +01008890#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008891
Hanno Beckera90658f2017-10-04 15:29:08 +01008892int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8893 const unsigned char *dhm_P, size_t P_len,
8894 const unsigned char *dhm_G, size_t G_len )
8895{
8896 int ret;
8897
8898 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8899 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8900 {
8901 mbedtls_mpi_free( &conf->dhm_P );
8902 mbedtls_mpi_free( &conf->dhm_G );
8903 return( ret );
8904 }
8905
8906 return( 0 );
8907}
Paul Bakker5121ce52009-01-03 21:22:43 +00008908
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008909int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008910{
8911 int ret;
8912
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008913 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8914 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8915 {
8916 mbedtls_mpi_free( &conf->dhm_P );
8917 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008918 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008919 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008920
8921 return( 0 );
8922}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008923#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008924
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008925#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8926/*
8927 * Set the minimum length for Diffie-Hellman parameters
8928 */
8929void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8930 unsigned int bitlen )
8931{
8932 conf->dhm_min_bitlen = bitlen;
8933}
8934#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8935
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008936#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008937/*
8938 * Set allowed/preferred hashes for handshake signatures
8939 */
8940void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8941 const int *hashes )
8942{
Hanno Becker56595f42019-06-19 16:31:38 +01008943#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008944 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008945#else
8946 ((void) conf);
8947 ((void) hashes);
8948#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008949}
Hanno Becker947194e2017-04-07 13:25:49 +01008950#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008951
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008952#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008953#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008954/*
8955 * Set the allowed elliptic curves
8956 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008957void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008958 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008959{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008960 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008961}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008962#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008963#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008964
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008965#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008967{
Hanno Becker947194e2017-04-07 13:25:49 +01008968 /* Initialize to suppress unnecessary compiler warning */
8969 size_t hostname_len = 0;
8970
8971 /* Check if new hostname is valid before
8972 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008973 if( hostname != NULL )
8974 {
8975 hostname_len = strlen( hostname );
8976
8977 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8979 }
8980
8981 /* Now it's clear that we will overwrite the old hostname,
8982 * so we can free it safely */
8983
8984 if( ssl->hostname != NULL )
8985 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008986 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008987 mbedtls_free( ssl->hostname );
8988 }
8989
8990 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008991
Paul Bakker5121ce52009-01-03 21:22:43 +00008992 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008993 {
8994 ssl->hostname = NULL;
8995 }
8996 else
8997 {
8998 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008999 if( ssl->hostname == NULL )
9000 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009001
Hanno Becker947194e2017-04-07 13:25:49 +01009002 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009003
Hanno Becker947194e2017-04-07 13:25:49 +01009004 ssl->hostname[hostname_len] = '\0';
9005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009006
9007 return( 0 );
9008}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009009#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009010
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009011#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009012void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009014 const unsigned char *, size_t),
9015 void *p_sni )
9016{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009017 conf->f_sni = f_sni;
9018 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009023int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009024{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009025 size_t cur_len, tot_len;
9026 const char **p;
9027
9028 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009029 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9030 * MUST NOT be truncated."
9031 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009032 */
9033 tot_len = 0;
9034 for( p = protos; *p != NULL; p++ )
9035 {
9036 cur_len = strlen( *p );
9037 tot_len += cur_len;
9038
9039 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009041 }
9042
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009043 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009044
9045 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009046}
9047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009049{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009050 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009053
Hanno Becker33b9b252019-07-05 11:23:25 +01009054#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9055 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9056void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9057 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009059 conf->max_major_ver = major;
9060 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009061}
Hanno Becker33b9b252019-07-05 11:23:25 +01009062#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9063 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009064
Hanno Becker33b9b252019-07-05 11:23:25 +01009065#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9066 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9067void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9068 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009070 conf->min_major_ver = major;
9071 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009072}
Hanno Becker33b9b252019-07-05 11:23:25 +01009073#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9074 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009078{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009079 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009080}
9081#endif
9082
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009083#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009084void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9085 char cert_req_ca_list )
9086{
9087 conf->cert_req_ca_list = cert_req_ca_list;
9088}
9089#endif
9090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009094 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009095}
9096#endif
9097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009099#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009100void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009101{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009102 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009103}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009104#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9105#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009106void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009107 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009108{
9109 conf->enforce_extended_master_secret = ems_enf;
9110}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009111#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009112#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009113
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009114#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009118}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009119#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009122int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009125 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009128 }
9129
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009130 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009131
9132 return( 0 );
9133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009137void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009139 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009144void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009145{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009146 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009147}
9148#endif
9149
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009150#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009151void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009153 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009154}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009155#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009158void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009160 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009161}
9162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009163void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009165 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009166}
9167
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009168void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009169 const unsigned char period[8] )
9170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009171 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009176#if defined(MBEDTLS_SSL_CLI_C)
9177void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009178{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009179 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009180}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009181#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009182
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009183#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009184void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9185 mbedtls_ssl_ticket_write_t *f_ticket_write,
9186 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9187 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009188{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009189 conf->f_ticket_write = f_ticket_write;
9190 conf->f_ticket_parse = f_ticket_parse;
9191 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009192}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009195
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009196#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9197void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9198 mbedtls_ssl_export_keys_t *f_export_keys,
9199 void *p_export_keys )
9200{
9201 conf->f_export_keys = f_export_keys;
9202 conf->p_export_keys = p_export_keys;
9203}
9204#endif
9205
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009206#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009207void mbedtls_ssl_conf_async_private_cb(
9208 mbedtls_ssl_config *conf,
9209 mbedtls_ssl_async_sign_t *f_async_sign,
9210 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9211 mbedtls_ssl_async_resume_t *f_async_resume,
9212 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009213 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009214{
9215 conf->f_async_sign_start = f_async_sign;
9216 conf->f_async_decrypt_start = f_async_decrypt;
9217 conf->f_async_resume = f_async_resume;
9218 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009219 conf->p_async_config_data = async_config_data;
9220}
9221
Gilles Peskine8f97af72018-04-26 11:46:10 +02009222void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9223{
9224 return( conf->p_async_config_data );
9225}
9226
Gilles Peskine1febfef2018-04-30 11:54:39 +02009227void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009228{
9229 if( ssl->handshake == NULL )
9230 return( NULL );
9231 else
9232 return( ssl->handshake->user_async_ctx );
9233}
9234
Gilles Peskine1febfef2018-04-30 11:54:39 +02009235void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009236 void *ctx )
9237{
9238 if( ssl->handshake != NULL )
9239 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009240}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009241#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009242
Paul Bakker5121ce52009-01-03 21:22:43 +00009243/*
9244 * SSL get accessors
9245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009247{
9248 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9249}
9250
Hanno Becker8b170a02017-10-10 11:51:19 +01009251int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9252{
9253 /*
9254 * Case A: We're currently holding back
9255 * a message for further processing.
9256 */
9257
9258 if( ssl->keep_current_message == 1 )
9259 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 return( 1 );
9262 }
9263
9264 /*
9265 * Case B: Further records are pending in the current datagram.
9266 */
9267
9268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009269 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009270 ssl->in_left > ssl->next_record_offset )
9271 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009273 return( 1 );
9274 }
9275#endif /* MBEDTLS_SSL_PROTO_DTLS */
9276
9277 /*
9278 * Case C: A handshake message is being processed.
9279 */
9280
Hanno Becker8b170a02017-10-10 11:51:19 +01009281 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9282 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009284 return( 1 );
9285 }
9286
9287 /*
9288 * Case D: An application data message is being processed
9289 */
9290 if( ssl->in_offt != NULL )
9291 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009293 return( 1 );
9294 }
9295
9296 /*
9297 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009298 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009299 * we implement support for multiple alerts in single records.
9300 */
9301
9302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9303 return( 0 );
9304}
9305
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009306uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009307{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009308 if( ssl->session != NULL )
9309 return( ssl->session->verify_result );
9310
9311 if( ssl->session_negotiate != NULL )
9312 return( ssl->session_negotiate->verify_result );
9313
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009314 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009315}
9316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009318{
Hanno Beckere02758c2019-06-26 15:31:31 +01009319 int suite;
9320
Paul Bakker926c8e42013-03-06 10:23:34 +01009321 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009322 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009323
Hanno Beckere02758c2019-06-26 15:31:31 +01009324 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9325 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009326}
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009331 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009332 {
Hanno Becker2881d802019-05-22 14:44:53 +01009333 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009336 return( "DTLSv1.0" );
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009339 return( "DTLSv1.2" );
9340
9341 default:
9342 return( "unknown (DTLS)" );
9343 }
9344 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009345 MBEDTLS_SSL_TRANSPORT_ELSE
9346#endif /* MBEDTLS_SSL_PROTO_DTLS */
9347#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009348 {
Hanno Becker2881d802019-05-22 14:44:53 +01009349 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009350 {
9351 case MBEDTLS_SSL_MINOR_VERSION_0:
9352 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009353
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009354 case MBEDTLS_SSL_MINOR_VERSION_1:
9355 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009356
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009357 case MBEDTLS_SSL_MINOR_VERSION_2:
9358 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009359
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009360 case MBEDTLS_SSL_MINOR_VERSION_3:
9361 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009362
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009363 default:
9364 return( "unknown" );
9365 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009366 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009367#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009368}
9369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009371{
Hanno Becker3136ede2018-08-17 15:28:19 +01009372 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009374
Hanno Becker43395762019-05-03 14:46:38 +01009375 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9376
Hanno Becker78640902018-08-13 16:35:15 +01009377 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009378 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_ZLIB_SUPPORT)
9381 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9382 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009383#endif
9384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009386 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009387#if defined(MBEDTLS_GCM_C) || \
9388 defined(MBEDTLS_CCM_C) || \
9389 defined(MBEDTLS_CHACHAPOLY_C)
9390#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009392#endif
9393#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009395#endif
9396#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009397 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009398#endif
9399 transform_expansion =
9400 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009401 break;
9402
Hanno Beckera9d5c452019-07-25 16:47:12 +01009403#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9404 MBEDTLS_CHACHAPOLY_C */
9405
9406#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9407 case MBEDTLS_MODE_STREAM:
9408 transform_expansion = transform->maclen;
9409 break;
9410#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9411
9412#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009414 {
9415 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009416
9417 block_size = mbedtls_cipher_get_block_size(
9418 &transform->cipher_ctx_enc );
9419
Hanno Becker3136ede2018-08-17 15:28:19 +01009420 /* Expansion due to the addition of the MAC. */
9421 transform_expansion += transform->maclen;
9422
9423 /* Expansion due to the addition of CBC padding;
9424 * Theoretically up to 256 bytes, but we never use
9425 * more than the block size of the underlying cipher. */
9426 transform_expansion += block_size;
9427
9428 /* For TLS 1.1 or higher, an explicit IV is added
9429 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009430#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009431 if( mbedtls_ssl_ver_geq(
9432 mbedtls_ssl_get_minor_ver( ssl ),
9433 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9434 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009435 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009436 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009437#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009438
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009439 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009440 }
9441#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009442
9443 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009446 }
9447
Hanno Beckera5a2b082019-05-15 14:03:01 +01009448#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009449 if( transform->out_cid_len != 0 )
9450 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009452
Hanno Becker43395762019-05-03 14:46:38 +01009453 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009454}
9455
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009456#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9457size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9458{
9459 size_t max_len;
9460
9461 /*
9462 * Assume mfl_code is correct since it was checked when set
9463 */
Angus Grattond8213d02016-05-25 20:56:48 +10009464 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009465
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009466 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009467 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009468 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009469 {
Angus Grattond8213d02016-05-25 20:56:48 +10009470 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009471 }
9472
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009473 /* During a handshake, use the value being negotiated */
9474 if( ssl->session_negotiate != NULL &&
9475 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9476 {
9477 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9478 }
9479
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009480 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009481}
9482#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9483
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009484#if defined(MBEDTLS_SSL_PROTO_DTLS)
9485static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9486{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009487 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009488 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009489 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9490 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009491 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009492
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009493 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9494 return( ssl->mtu );
9495
9496 if( ssl->mtu == 0 )
9497 return( ssl->handshake->mtu );
9498
9499 return( ssl->mtu < ssl->handshake->mtu ?
9500 ssl->mtu : ssl->handshake->mtu );
9501}
9502#endif /* MBEDTLS_SSL_PROTO_DTLS */
9503
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009504int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9505{
9506 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9507
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009508#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9509 !defined(MBEDTLS_SSL_PROTO_DTLS)
9510 (void) ssl;
9511#endif
9512
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009513#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9514 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9515
9516 if( max_len > mfl )
9517 max_len = mfl;
9518#endif
9519
9520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009521 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009522 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009523 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009524 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9525 const size_t overhead = (size_t) ret;
9526
9527 if( ret < 0 )
9528 return( ret );
9529
9530 if( mtu <= overhead )
9531 {
9532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9533 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9534 }
9535
9536 if( max_len > mtu - overhead )
9537 max_len = mtu - overhead;
9538 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009539#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009540
Hanno Becker0defedb2018-08-10 12:35:02 +01009541#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9542 !defined(MBEDTLS_SSL_PROTO_DTLS)
9543 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009544#endif
9545
9546 return( (int) max_len );
9547}
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549#if defined(MBEDTLS_X509_CRT_PARSE_C)
9550const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009551{
9552 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009553 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009554
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009555#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009556 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009557#else
9558 return( NULL );
9559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009564int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9565 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009566{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009567 if( ssl == NULL ||
9568 dst == NULL ||
9569 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009570 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009573 }
9574
Hanno Becker58fccf22019-02-06 14:30:46 +00009575 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009578
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009579const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9580{
9581 if( ssl == NULL )
9582 return( NULL );
9583
9584 return( ssl->session );
9585}
9586
Paul Bakker5121ce52009-01-03 21:22:43 +00009587/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009588 * Define ticket header determining Mbed TLS version
9589 * and structure of the ticket.
9590 */
9591
Hanno Becker41527622019-05-16 12:50:45 +01009592/*
Hanno Becker26829e92019-05-28 14:30:45 +01009593 * Define bitflag determining compile-time settings influencing
9594 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009595 */
9596
Hanno Becker26829e92019-05-28 14:30:45 +01009597#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009598#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009599#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009600#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009601#endif /* MBEDTLS_HAVE_TIME */
9602
9603#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009604#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009605#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009606#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009607#endif /* MBEDTLS_X509_CRT_PARSE_C */
9608
9609#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009610#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009611#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009612#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009613#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9614
9615#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009616#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009617#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009618#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009619#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9620
9621#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009622#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009623#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009624#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009625#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9626
9627#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009628#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009629#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009630#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009631#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9632
Hanno Becker41527622019-05-16 12:50:45 +01009633#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9634#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9635#else
9636#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9637#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9638
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009639#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9640#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9641#else
9642#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9643#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9644
Hanno Becker88440552019-07-03 14:16:13 +01009645#if defined(MBEDTLS_ZLIB_SUPPORT)
9646#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9647#else
9648#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9649#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9650
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009651#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9652#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9653#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9654#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9655#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9656#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9657#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009658#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009659#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009660
Hanno Becker26829e92019-05-28 14:30:45 +01009661#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009662 ( (uint16_t) ( \
9663 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9664 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9665 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9666 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9667 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9668 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009669 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009670 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009671 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009672
Hanno Becker557fe9f2019-05-16 12:41:07 +01009673static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009674 MBEDTLS_VERSION_MAJOR,
9675 MBEDTLS_VERSION_MINOR,
9676 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009677 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9678 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009679};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009680
9681/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009682 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009683 * (in the presentation language of TLS, RFC 8446 section 3)
9684 *
Hanno Becker26829e92019-05-28 14:30:45 +01009685 * opaque mbedtls_version[3]; // major, minor, patch
9686 * opaque session_format[2]; // version-specific 16-bit field determining
9687 * // the format of the remaining
9688 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009689 *
9690 * Note: When updating the format, remember to keep
9691 * these version+format bytes.
9692 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009693 * // In this version, `session_format` determines
9694 * // the setting of those compile-time
9695 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009696 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009697 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009698 * uint8 ciphersuite[2]; // defined by the standard
9699 * uint8 compression; // 0 or 1
9700 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009701 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009702 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009703 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009704 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9705 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9706 * case disabled: uint8_t peer_cert_digest_type;
9707 * opaque peer_cert_digest<0..2^8-1>;
9708 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009709 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009710 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009711 * uint8 mfl_code; // up to 255 according to standard
9712 * uint8 trunc_hmac; // 0 or 1
9713 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009714 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009715 * The order is the same as in the definition of the structure, except
9716 * verify_result is put before peer_cert so that all mandatory fields come
9717 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009718 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009719static int ssl_session_save( const mbedtls_ssl_session *session,
9720 unsigned char omit_header,
9721 unsigned char *buf,
9722 size_t buf_len,
9723 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009724{
9725 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009726 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009727#if defined(MBEDTLS_HAVE_TIME)
9728 uint64_t start;
9729#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009730#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009731#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009732 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009733#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009734#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009735
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009736 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009737 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009738 /*
9739 * Add version identifier
9740 */
9741
9742 used += sizeof( ssl_serialized_session_header );
9743
9744 if( used <= buf_len )
9745 {
9746 memcpy( p, ssl_serialized_session_header,
9747 sizeof( ssl_serialized_session_header ) );
9748 p += sizeof( ssl_serialized_session_header );
9749 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009750 }
9751
9752 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009753 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009754 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009755#if defined(MBEDTLS_HAVE_TIME)
9756 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009757
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009758 if( used <= buf_len )
9759 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009760 start = (uint64_t) session->start;
9761
9762 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9763 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9764 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9765 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9766 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9767 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9768 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9769 *p++ = (unsigned char)( ( start ) & 0xFF );
9770 }
9771#endif /* MBEDTLS_HAVE_TIME */
9772
9773 /*
9774 * Basic mandatory fields
9775 */
Hanno Becker88440552019-07-03 14:16:13 +01009776 {
9777 size_t const ciphersuite_len = 2;
9778#if defined(MBEDTLS_ZLIB_SUPPORT)
9779 size_t const compression_len = 1;
9780#else
9781 size_t const compression_len = 0;
9782#endif
9783 size_t const id_len_len = 1;
9784 size_t const id_len = 32;
9785 size_t const master_len = 48;
9786 size_t const verif_result_len = 4;
9787
9788 size_t const basic_len =
9789 ciphersuite_len +
9790 compression_len +
9791 id_len_len +
9792 id_len +
9793 master_len +
9794 verif_result_len;
9795
9796 used += basic_len;
9797 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009798
9799 if( used <= buf_len )
9800 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009801 const int ciphersuite =
9802 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009803 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009804
Hanno Becker88440552019-07-03 14:16:13 +01009805#if defined(MBEDTLS_ZLIB_SUPPORT)
9806 *p++ = (unsigned char)(
9807 mbedtls_ssl_session_get_compression( session ) );
9808#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009809
9810 *p++ = (unsigned char)( session->id_len & 0xFF );
9811 memcpy( p, session->id, 32 );
9812 p += 32;
9813
9814 memcpy( p, session->master, 48 );
9815 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009816 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009817 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009818
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009819 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009820 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009821 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009822#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009823#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009824 if( session->peer_cert == NULL )
9825 cert_len = 0;
9826 else
9827 cert_len = session->peer_cert->raw.len;
9828
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009829 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009830
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009831 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009832 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009833 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009834
9835 if( session->peer_cert != NULL )
9836 {
9837 memcpy( p, session->peer_cert->raw.p, cert_len );
9838 p += cert_len;
9839 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009840 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009841
Hanno Becker5882dd02019-06-06 16:25:57 +01009842#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009843 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009844 if( session->peer_cert_digest != NULL )
9845 {
9846 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9847 if( used <= buf_len )
9848 {
9849 *p++ = (unsigned char) session->peer_cert_digest_type;
9850 *p++ = (unsigned char) session->peer_cert_digest_len;
9851 memcpy( p, session->peer_cert_digest,
9852 session->peer_cert_digest_len );
9853 p += session->peer_cert_digest_len;
9854 }
9855 }
9856 else
9857 {
9858 used += 2;
9859 if( used <= buf_len )
9860 {
9861 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9862 *p++ = 0;
9863 }
9864 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009865#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009866#endif /* MBEDTLS_X509_CRT_PARSE_C */
9867
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009868 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009869 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009870 */
9871#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009872 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009873
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009874 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009875 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009876 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009877
9878 if( session->ticket != NULL )
9879 {
9880 memcpy( p, session->ticket, session->ticket_len );
9881 p += session->ticket_len;
9882 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009883
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009884 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009885 }
9886#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9887
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009888 /*
9889 * Misc extension-related info
9890 */
9891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9892 used += 1;
9893
9894 if( used <= buf_len )
9895 *p++ = session->mfl_code;
9896#endif
9897
9898#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9899 used += 1;
9900
9901 if( used <= buf_len )
9902 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9903#endif
9904
9905#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9906 used += 1;
9907
9908 if( used <= buf_len )
9909 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9910#endif
9911
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009912 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009913 *olen = used;
9914
9915 if( used > buf_len )
9916 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009917
9918 return( 0 );
9919}
9920
9921/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009922 * Public wrapper for ssl_session_save()
9923 */
9924int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9925 unsigned char *buf,
9926 size_t buf_len,
9927 size_t *olen )
9928{
9929 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9930}
9931
9932/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009933 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009934 *
9935 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009936 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009937 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009938static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009939 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009940 const unsigned char *buf,
9941 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009942{
9943 const unsigned char *p = buf;
9944 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009945 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009946#if defined(MBEDTLS_HAVE_TIME)
9947 uint64_t start;
9948#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009949#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009950#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009951 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009952#endif
9953#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009954
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009955 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009956 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009957 /*
9958 * Check version identifier
9959 */
9960
9961 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9963
Teppo Järvelin61f412e2019-10-03 12:25:22 +03009964 if( mbedtls_platform_memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009965 sizeof( ssl_serialized_session_header ) ) != 0 )
9966 {
9967 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9968 }
9969 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009970 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009971
9972 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009973 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009974 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009975#if defined(MBEDTLS_HAVE_TIME)
9976 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9978
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009979 start = ( (uint64_t) p[0] << 56 ) |
9980 ( (uint64_t) p[1] << 48 ) |
9981 ( (uint64_t) p[2] << 40 ) |
9982 ( (uint64_t) p[3] << 32 ) |
9983 ( (uint64_t) p[4] << 24 ) |
9984 ( (uint64_t) p[5] << 16 ) |
9985 ( (uint64_t) p[6] << 8 ) |
9986 ( (uint64_t) p[7] );
9987 p += 8;
9988
9989 session->start = (time_t) start;
9990#endif /* MBEDTLS_HAVE_TIME */
9991
9992 /*
9993 * Basic mandatory fields
9994 */
Hanno Becker88440552019-07-03 14:16:13 +01009995 {
9996 size_t const ciphersuite_len = 2;
9997#if defined(MBEDTLS_ZLIB_SUPPORT)
9998 size_t const compression_len = 1;
9999#else
10000 size_t const compression_len = 0;
10001#endif
10002 size_t const id_len_len = 1;
10003 size_t const id_len = 32;
10004 size_t const master_len = 48;
10005 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010006
Hanno Becker88440552019-07-03 14:16:13 +010010007 size_t const basic_len =
10008 ciphersuite_len +
10009 compression_len +
10010 id_len_len +
10011 id_len +
10012 master_len +
10013 verif_result_len;
10014
10015 if( basic_len > (size_t)( end - p ) )
10016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10017 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010018
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010019 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010020 p += 2;
10021
Hanno Becker73f4cb12019-06-27 13:51:07 +010010022#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010023 session->ciphersuite = ciphersuite;
10024#else
10025 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010026 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010027 {
10028 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10029 }
10030#endif
10031
Hanno Becker88440552019-07-03 14:16:13 +010010032#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010033 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010034#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010035
10036 session->id_len = *p++;
10037 memcpy( session->id, p, 32 );
10038 p += 32;
10039
10040 memcpy( session->master, p, 48 );
10041 p += 48;
10042
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010043 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010044 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010045
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010046 /* Immediately clear invalid pointer values that have been read, in case
10047 * we exit early before we replaced them with valid ones. */
10048#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010049#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010050 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010051#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010052 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010053#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010054#endif
10055#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10056 session->ticket = NULL;
10057#endif
10058
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010059 /*
10060 * Peer certificate
10061 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010062#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010063#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010064 if( 3 > (size_t)( end - p ) )
10065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10066
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010067 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10068
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010069 p += 3;
10070
10071 if( cert_len == 0 )
10072 {
10073 session->peer_cert = NULL;
10074 }
10075 else
10076 {
10077 int ret;
10078
10079 if( cert_len > (size_t)( end - p ) )
10080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10081
10082 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10083
10084 if( session->peer_cert == NULL )
10085 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10086
10087 mbedtls_x509_crt_init( session->peer_cert );
10088
10089 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10090 p, cert_len ) ) != 0 )
10091 {
10092 mbedtls_x509_crt_free( session->peer_cert );
10093 mbedtls_free( session->peer_cert );
10094 session->peer_cert = NULL;
10095 return( ret );
10096 }
10097
10098 p += cert_len;
10099 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010100#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010101 /* Deserialize CRT digest from the end of the ticket. */
10102 if( 2 > (size_t)( end - p ) )
10103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10104
10105 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10106 session->peer_cert_digest_len = (size_t) *p++;
10107
10108 if( session->peer_cert_digest_len != 0 )
10109 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010110 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010111 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010112 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10114 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10116
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010117 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10119
10120 session->peer_cert_digest =
10121 mbedtls_calloc( 1, session->peer_cert_digest_len );
10122 if( session->peer_cert_digest == NULL )
10123 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10124
10125 memcpy( session->peer_cert_digest, p,
10126 session->peer_cert_digest_len );
10127 p += session->peer_cert_digest_len;
10128 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010129#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010130#endif /* MBEDTLS_X509_CRT_PARSE_C */
10131
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010132 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010133 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010134 */
10135#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10136 if( 3 > (size_t)( end - p ) )
10137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10138
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010139 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010140 p += 3;
10141
10142 if( session->ticket_len != 0 )
10143 {
10144 if( session->ticket_len > (size_t)( end - p ) )
10145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10146
10147 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10148 if( session->ticket == NULL )
10149 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10150
10151 memcpy( session->ticket, p, session->ticket_len );
10152 p += session->ticket_len;
10153 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010154
10155 if( 4 > (size_t)( end - p ) )
10156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10157
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010158 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010159 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010160#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10161
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010162 /*
10163 * Misc extension-related info
10164 */
10165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10166 if( 1 > (size_t)( end - p ) )
10167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10168
10169 session->mfl_code = *p++;
10170#endif
10171
10172#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10173 if( 1 > (size_t)( end - p ) )
10174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10175
10176 session->trunc_hmac = *p++;
10177#endif
10178
10179#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10180 if( 1 > (size_t)( end - p ) )
10181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10182
10183 session->encrypt_then_mac = *p++;
10184#endif
10185
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010186 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010187 if( p != end )
10188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10189
10190 return( 0 );
10191}
10192
10193/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010194 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010195 */
10196int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10197 const unsigned char *buf,
10198 size_t len )
10199{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010200 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010201
10202 if( ret != 0 )
10203 mbedtls_ssl_session_free( session );
10204
10205 return( ret );
10206}
10207
10208/*
Paul Bakker1961b702013-01-25 14:49:24 +010010209 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010214
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010215 if( ssl == NULL || ssl->conf == NULL )
10216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010219 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010223 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010225#endif
10226
Hanno Beckerb82350b2019-07-26 07:24:05 +010010227 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010228 return( ret );
10229}
10230
10231/*
10232 * Perform the SSL handshake
10233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010235{
10236 int ret = 0;
10237
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010238 if( ssl == NULL || ssl->conf == NULL )
10239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010246
10247 if( ret != 0 )
10248 break;
10249 }
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010252
10253 return( ret );
10254}
10255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256#if defined(MBEDTLS_SSL_RENEGOTIATION)
10257#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010258/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010259 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010262{
10263 int ret;
10264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010266
10267 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10269 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010270
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010271 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010272 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010274 return( ret );
10275 }
10276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010278
10279 return( 0 );
10280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010282
10283/*
10284 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 * - any side: calling mbedtls_ssl_renegotiate(),
10286 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10287 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010288 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010289 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010292static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010293{
10294 int ret;
10295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010297
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010298 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10299 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010300
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010301 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10302 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010304 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010306 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010307 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10308 MBEDTLS_SSL_IS_SERVER )
10309 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010310 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010311 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010312 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010313 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010314 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010315 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010316 }
10317#endif
10318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10320 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010325 return( ret );
10326 }
10327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010329
10330 return( 0 );
10331}
10332
10333/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010334 * Renegotiate current connection on client,
10335 * or request renegotiation on server
10336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010341 if( ssl == NULL || ssl->conf == NULL )
10342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010345 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010346 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010352
10353 /* Did we already try/start sending HelloRequest? */
10354 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010356
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010357 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010362 /*
10363 * On client, either start the renegotiation process or,
10364 * if already in progress, continue the handshake
10365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010370
10371 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010374 return( ret );
10375 }
10376 }
10377 else
10378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010382 return( ret );
10383 }
10384 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010386
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010387 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010388}
10389
10390/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010391 * Check record counters and renegotiate if they're above the limit.
10392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010394{
Andres AG2196c7f2016-12-15 17:01:16 +000010395 size_t ep_len = ssl_ep_len( ssl );
10396 int in_ctr_cmp;
10397 int out_ctr_cmp;
10398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10400 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010401 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010402 {
10403 return( 0 );
10404 }
10405
Teppo Järvelin61f412e2019-10-03 12:25:22 +030010406 in_ctr_cmp = mbedtls_platform_memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010407 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin61f412e2019-10-03 12:25:22 +030010408 out_ctr_cmp = mbedtls_platform_memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010409 ssl->conf->renego_period + ep_len, 8 - ep_len );
10410
10411 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010412 {
10413 return( 0 );
10414 }
10415
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010420
10421/*
10422 * Receive application data decrypted from the SSL layer
10423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010425{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010426 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010427 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010428
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010429 if( ssl == NULL || ssl->conf == NULL )
10430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010435 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010438 return( ret );
10439
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010440 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010442 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010443 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010444 return( ret );
10445 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010446 }
10447#endif
10448
Hanno Becker4a810fb2017-05-24 16:27:30 +010010449 /*
10450 * Check if renegotiation is necessary and/or handshake is
10451 * in process. If yes, perform/continue, and fall through
10452 * if an unexpected packet is received while the client
10453 * is waiting for the ServerHello.
10454 *
10455 * (There is no equivalent to the last condition on
10456 * the server-side as it is not treated as within
10457 * a handshake while waiting for the ClientHello
10458 * after a renegotiation request.)
10459 */
10460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010462 ret = ssl_check_ctr_renegotiate( ssl );
10463 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10464 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010467 return( ret );
10468 }
10469#endif
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010474 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10475 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010478 return( ret );
10479 }
10480 }
10481
Hanno Beckere41158b2017-10-23 13:30:32 +010010482 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010483 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010484 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010485 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010486 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10487 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010488 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010489 ssl_set_timer( ssl,
10490 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010491 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010492
Hanno Becker327c93b2018-08-15 13:56:18 +010010493 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010495 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10496 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010497
Hanno Becker4a810fb2017-05-24 16:27:30 +010010498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10499 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010500 }
10501
10502 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010504 {
10505 /*
10506 * OpenSSL sends empty messages to randomize the IV
10507 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010508 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010511 return( 0 );
10512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010514 return( ret );
10515 }
10516 }
10517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010521
Hanno Becker4a810fb2017-05-24 16:27:30 +010010522 /*
10523 * - For client-side, expect SERVER_HELLO_REQUEST.
10524 * - For server-side, expect CLIENT_HELLO.
10525 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10526 */
10527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010529 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10530 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010532 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010535
10536 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010538 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010539 {
10540 continue;
10541 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010542 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010543#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010544#if defined(MBEDTLS_SSL_PROTO_TLS)
10545 {
10546 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10547 }
10548#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010549 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010550#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010551
Hanno Becker4a810fb2017-05-24 16:27:30 +010010552#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010553 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10554 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010555 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010558
10559 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010561 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010562 {
10563 continue;
10564 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010565 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010566#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010567#if defined(MBEDTLS_SSL_PROTO_TLS)
10568 {
10569 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10570 }
10571#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010572 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010573#endif /* MBEDTLS_SSL_SRV_C */
10574
Hanno Becker21df7f92017-10-17 11:03:26 +010010575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010576 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010577 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10578 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010579 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010580 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10581 {
10582 /*
10583 * Accept renegotiation request
10584 */
Paul Bakker48916f92012-09-16 19:57:18 +000010585
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010586 /* DTLS clients need to know renego is server-initiated */
10587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010588 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010589 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10590 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010591 {
10592 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10593 }
10594#endif
10595 ret = ssl_start_renegotiation( ssl );
10596 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10597 ret != 0 )
10598 {
10599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10600 return( ret );
10601 }
10602 }
10603 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010604#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010605 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010606 /*
10607 * Refuse renegotiation
10608 */
10609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010613 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010614 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010615 /* SSLv3 does not have a "no_renegotiation" warning, so
10616 we send a fatal alert and abort the connection. */
10617 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10618 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10619 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010620 }
10621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010625 if( mbedtls_ssl_ver_geq(
10626 mbedtls_ssl_get_minor_ver( ssl ),
10627 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010628 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010629 ret = mbedtls_ssl_send_alert_message( ssl,
10630 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10631 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10632 if( ret != 0 )
10633 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010634 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10637 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010641 }
Paul Bakker48916f92012-09-16 19:57:18 +000010642 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010643
Hanno Becker90333da2017-10-10 11:27:13 +010010644 /* At this point, we don't know whether the renegotiation has been
10645 * completed or not. The cases to consider are the following:
10646 * 1) The renegotiation is complete. In this case, no new record
10647 * has been read yet.
10648 * 2) The renegotiation is incomplete because the client received
10649 * an application data record while awaiting the ServerHello.
10650 * 3) The renegotiation is incomplete because the client received
10651 * a non-handshake, non-application data message while awaiting
10652 * the ServerHello.
10653 * In each of these case, looping will be the proper action:
10654 * - For 1), the next iteration will read a new record and check
10655 * if it's application data.
10656 * - For 2), the loop condition isn't satisfied as application data
10657 * is present, hence continue is the same as break
10658 * - For 3), the loop condition is satisfied and read_record
10659 * will re-deliver the message that was held back by the client
10660 * when expecting the ServerHello.
10661 */
10662 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010663 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010664#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010666 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010667 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010668 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010669 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010672 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010674 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010675 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10680 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010683 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010684 }
10685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10689 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010690 }
10691
10692 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010693
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010694 /* We're going to return something now, cancel timer,
10695 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010697 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010698
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010700 /* If we requested renego but received AppData, resend HelloRequest.
10701 * Do it now, after setting in_offt, to avoid taking this branch
10702 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010704 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10705 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010707 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010708 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010711 return( ret );
10712 }
10713 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010715#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010716 }
10717
10718 n = ( len < ssl->in_msglen )
10719 ? len : ssl->in_msglen;
10720
10721 memcpy( buf, ssl->in_offt, n );
10722 ssl->in_msglen -= n;
10723
10724 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010725 {
10726 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010727 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010728 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010729 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010730 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010731 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010732 /* more data available */
10733 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010734 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010737
Paul Bakker23986e52011-04-24 08:57:21 +000010738 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010739}
10740
10741/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010742 * Send application data to be encrypted by the SSL layer, taking care of max
10743 * fragment length and buffer size.
10744 *
10745 * According to RFC 5246 Section 6.2.1:
10746 *
10747 * Zero-length fragments of Application data MAY be sent as they are
10748 * potentially useful as a traffic analysis countermeasure.
10749 *
10750 * Therefore, it is possible that the input message length is 0 and the
10751 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010752 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010753static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010754 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010755{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010756 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10757 const size_t max_len = (size_t) ret;
10758
10759 if( ret < 0 )
10760 {
10761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10762 return( ret );
10763 }
10764
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010765 if( len > max_len )
10766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010768 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010771 "maximum fragment length: %d > %d",
10772 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010774 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010775 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010776#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010777#if defined(MBEDTLS_SSL_PROTO_TLS)
10778 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010779 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010780 }
10781#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010782 }
Paul Bakker887bd502011-06-08 13:10:54 +000010783
Paul Bakker5121ce52009-01-03 21:22:43 +000010784 if( ssl->out_left != 0 )
10785 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010786 /*
10787 * The user has previously tried to send the data and
10788 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10789 * written. In this case, we expect the high-level write function
10790 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010795 return( ret );
10796 }
10797 }
Paul Bakker887bd502011-06-08 13:10:54 +000010798 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010799 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010800 /*
10801 * The user is trying to send a message the first time, so we need to
10802 * copy the data into the internal buffers and setup the data structure
10803 * to keep track of partial writes
10804 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010805 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010807 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010808
Hanno Becker67bc7c32018-08-06 11:33:50 +010010809 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010812 return( ret );
10813 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010814 }
10815
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010816 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010817}
10818
10819/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010820 * Write application data, doing 1/n-1 splitting if necessary.
10821 *
10822 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010823 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010824 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010827static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010828 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010829{
10830 int ret;
10831
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010832 if( ssl->conf->cbc_record_splitting ==
10833 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010834 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010835 mbedtls_ssl_ver_gt(
10836 mbedtls_ssl_get_minor_ver( ssl ),
10837 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10839 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010840 {
10841 return( ssl_write_real( ssl, buf, len ) );
10842 }
10843
10844 if( ssl->split_done == 0 )
10845 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010846 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010847 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010848 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010849 }
10850
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010851 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10852 return( ret );
10853 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010854
10855 return( ret + 1 );
10856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010858
10859/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010860 * Write application data (public-facing wrapper)
10861 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010862int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010863{
10864 int ret;
10865
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010867
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010868 if( ssl == NULL || ssl->conf == NULL )
10869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10870
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010872 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10873 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010875 return( ret );
10876 }
10877#endif
10878
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010879 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010880 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010881 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010882 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010884 return( ret );
10885 }
10886 }
10887
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010888#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010889 ret = ssl_write_split( ssl, buf, len );
10890#else
10891 ret = ssl_write_real( ssl, buf, len );
10892#endif
10893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010895
10896 return( ret );
10897}
10898
10899/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010900 * Notify the peer that the connection is being closed
10901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010903{
10904 int ret;
10905
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010906 if( ssl == NULL || ssl->conf == NULL )
10907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010910
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010911 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10917 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10918 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010921 return( ret );
10922 }
10923 }
10924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010926
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010927 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010928}
10929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010931{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010932 if( transform == NULL )
10933 return;
10934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010936 deflateEnd( &transform->ctx_deflate );
10937 inflateEnd( &transform->ctx_inflate );
10938#endif
10939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10941 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010942
Hanno Becker92231322018-01-03 15:32:51 +000010943#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944 mbedtls_md_free( &transform->md_ctx_enc );
10945 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010946#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010947
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010948 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010949}
10950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951#if defined(MBEDTLS_X509_CRT_PARSE_C)
10952static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010955
10956 while( cur != NULL )
10957 {
10958 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010960 cur = next;
10961 }
10962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010964
Hanno Becker0271f962018-08-16 13:23:47 +010010965#if defined(MBEDTLS_SSL_PROTO_DTLS)
10966
10967static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10968{
10969 unsigned offset;
10970 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10971
10972 if( hs == NULL )
10973 return;
10974
Hanno Becker283f5ef2018-08-24 09:34:47 +010010975 ssl_free_buffered_record( ssl );
10976
Hanno Becker0271f962018-08-16 13:23:47 +010010977 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010978 ssl_buffering_free_slot( ssl, offset );
10979}
10980
10981static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10982 uint8_t slot )
10983{
10984 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10985 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010986
10987 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10988 return;
10989
Hanno Beckere605b192018-08-21 15:59:07 +010010990 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010991 {
Hanno Beckere605b192018-08-21 15:59:07 +010010992 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010993 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010994 mbedtls_free( hs_buf->data );
10995 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010996 }
10997}
10998
10999#endif /* MBEDTLS_SSL_PROTO_DTLS */
11000
Gilles Peskine9b562d52018-04-25 20:32:43 +020011001void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011002{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011003 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11004
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011005 if( handshake == NULL )
11006 return;
11007
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011008#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11009 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11010 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011011 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011012 handshake->async_in_progress = 0;
11013 }
11014#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11015
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011016#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11017 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11018 mbedtls_md5_free( &handshake->fin_md5 );
11019 mbedtls_sha1_free( &handshake->fin_sha1 );
11020#endif
11021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11022#if defined(MBEDTLS_SHA256_C)
11023 mbedtls_sha256_free( &handshake->fin_sha256 );
11024#endif
11025#if defined(MBEDTLS_SHA512_C)
11026 mbedtls_sha512_free( &handshake->fin_sha512 );
11027#endif
11028#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011030#if defined(MBEDTLS_DHM_C)
11031 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033#if defined(MBEDTLS_ECDH_C)
11034 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011035#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011036#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011037 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011038#if defined(MBEDTLS_SSL_CLI_C)
11039 mbedtls_free( handshake->ecjpake_cache );
11040 handshake->ecjpake_cache = NULL;
11041 handshake->ecjpake_cache_len = 0;
11042#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011043#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011044
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011045#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11046 if( handshake->psk != NULL )
11047 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011048 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011049 mbedtls_free( handshake->psk );
11050 }
11051#endif
11052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11054 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011055 /*
11056 * Free only the linked list wrapper, not the keys themselves
11057 * since the belong to the SNI callback
11058 */
11059 if( handshake->sni_key_cert != NULL )
11060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011061 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011062
11063 while( cur != NULL )
11064 {
11065 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011067 cur = next;
11068 }
11069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011071
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011072#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011073 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011074 if( handshake->ecrs_peer_cert != NULL )
11075 {
11076 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11077 mbedtls_free( handshake->ecrs_peer_cert );
11078 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011079#endif
11080
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011081#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11082 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11083 mbedtls_pk_free( &handshake->peer_pubkey );
11084#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086#if defined(MBEDTLS_SSL_PROTO_DTLS)
11087 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011088 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011089 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011090#endif
11091
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011092 mbedtls_platform_zeroize( handshake,
11093 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011094}
11095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011097{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011098 if( session == NULL )
11099 return;
11100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011102 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011103#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011104
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011105#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011107#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011109 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011110}
11111
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011112#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011113
11114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11115#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11116#else
11117#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11118#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11119
11120#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11121#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11122#else
11123#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11124#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11125
11126#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11127#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11128#else
11129#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11130#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11131
11132#if defined(MBEDTLS_SSL_ALPN)
11133#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11134#else
11135#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11136#endif /* MBEDTLS_SSL_ALPN */
11137
11138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11139#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11140#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11141#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11142
11143#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11144 ( (uint32_t) ( \
11145 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11146 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11147 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11148 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11149 0u ) )
11150
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011151static unsigned char ssl_serialized_context_header[] = {
11152 MBEDTLS_VERSION_MAJOR,
11153 MBEDTLS_VERSION_MINOR,
11154 MBEDTLS_VERSION_PATCH,
11155 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11156 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011157 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11158 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11159 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011160};
11161
Paul Bakker5121ce52009-01-03 21:22:43 +000011162/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011163 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011164 *
11165 * The format of the serialized data is:
11166 * (in the presentation language of TLS, RFC 8446 section 3)
11167 *
11168 * // header
11169 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011170 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011171 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011172 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011173 * Note: When updating the format, remember to keep these
11174 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011175 *
11176 * // session sub-structure
11177 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11178 * // transform sub-structure
11179 * uint8 random[64]; // ServerHello.random+ClientHello.random
11180 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11181 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11182 * // fields from ssl_context
11183 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11184 * uint64 in_window_top; // DTLS: last validated record seq_num
11185 * uint64 in_window; // DTLS: bitmask for replay protection
11186 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11187 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11188 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11189 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11190 *
11191 * Note that many fields of the ssl_context or sub-structures are not
11192 * serialized, as they fall in one of the following categories:
11193 *
11194 * 1. forced value (eg in_left must be 0)
11195 * 2. pointer to dynamically-allocated memory (eg session, transform)
11196 * 3. value can be re-derived from other data (eg session keys from MS)
11197 * 4. value was temporary (eg content of input buffer)
11198 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011199 */
11200int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11201 unsigned char *buf,
11202 size_t buf_len,
11203 size_t *olen )
11204{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011205 unsigned char *p = buf;
11206 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011207 size_t session_len;
11208 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011209
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011210 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011211 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11212 * this function's documentation.
11213 *
11214 * These are due to assumptions/limitations in the implementation. Some of
11215 * them are likely to stay (no handshake in progress) some might go away
11216 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011217 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011218 /* The initial handshake must be over */
11219 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011221 if( ssl->handshake != NULL )
11222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11223 /* Double-check that sub-structures are indeed ready */
11224 if( ssl->transform == NULL || ssl->session == NULL )
11225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11226 /* There must be no pending incoming or outgoing data */
11227 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11229 if( ssl->out_left != 0 )
11230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11231 /* Protocol must be DLTS, not TLS */
11232 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11234 /* Version must be 1.2 */
11235 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11237 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11239 /* We must be using an AEAD ciphersuite */
11240 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11242 /* Renegotiation must not be enabled */
11243 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011245
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011246 /*
11247 * Version and format identifier
11248 */
11249 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011250
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011251 if( used <= buf_len )
11252 {
11253 memcpy( p, ssl_serialized_context_header,
11254 sizeof( ssl_serialized_context_header ) );
11255 p += sizeof( ssl_serialized_context_header );
11256 }
11257
11258 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011259 * Session (length + data)
11260 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011261 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011262 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11263 return( ret );
11264
11265 used += 4 + session_len;
11266 if( used <= buf_len )
11267 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011268 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011269
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011270 ret = ssl_session_save( ssl->session, 1,
11271 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011272 if( ret != 0 )
11273 return( ret );
11274
11275 p += session_len;
11276 }
11277
11278 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011279 * Transform
11280 */
11281 used += sizeof( ssl->transform->randbytes );
11282 if( used <= buf_len )
11283 {
11284 memcpy( p, ssl->transform->randbytes,
11285 sizeof( ssl->transform->randbytes ) );
11286 p += sizeof( ssl->transform->randbytes );
11287 }
11288
11289#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11290 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11291 if( used <= buf_len )
11292 {
11293 *p++ = ssl->transform->in_cid_len;
11294 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11295 p += ssl->transform->in_cid_len;
11296
11297 *p++ = ssl->transform->out_cid_len;
11298 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11299 p += ssl->transform->out_cid_len;
11300 }
11301#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11302
11303 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011304 * Saved fields from top-level ssl_context structure
11305 */
11306#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11307 used += 4;
11308 if( used <= buf_len )
11309 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011310 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011311 }
11312#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11313
11314#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11315 used += 16;
11316 if( used <= buf_len )
11317 {
11318 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11319 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11320 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11321 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11322 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11323 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11324 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11325 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11326
11327 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11328 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11329 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11330 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11331 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11332 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11333 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11334 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11335 }
11336#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11337
11338#if defined(MBEDTLS_SSL_PROTO_DTLS)
11339 used += 1;
11340 if( used <= buf_len )
11341 {
11342 *p++ = ssl->disable_datagram_packing;
11343 }
11344#endif /* MBEDTLS_SSL_PROTO_DTLS */
11345
11346 used += 8;
11347 if( used <= buf_len )
11348 {
11349 memcpy( p, ssl->cur_out_ctr, 8 );
11350 p += 8;
11351 }
11352
11353#if defined(MBEDTLS_SSL_PROTO_DTLS)
11354 used += 2;
11355 if( used <= buf_len )
11356 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011357 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011358 }
11359#endif /* MBEDTLS_SSL_PROTO_DTLS */
11360
11361#if defined(MBEDTLS_SSL_ALPN)
11362 {
11363 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011364 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011365 : 0;
11366
11367 used += 1 + alpn_len;
11368 if( used <= buf_len )
11369 {
11370 *p++ = alpn_len;
11371
11372 if( ssl->alpn_chosen != NULL )
11373 {
11374 memcpy( p, ssl->alpn_chosen, alpn_len );
11375 p += alpn_len;
11376 }
11377 }
11378 }
11379#endif /* MBEDTLS_SSL_ALPN */
11380
11381 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011382 * Done
11383 */
11384 *olen = used;
11385
11386 if( used > buf_len )
11387 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011388
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011389 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11390
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011391 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011392}
11393
11394/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011395 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011396 *
11397 * This internal version is wrapped by a public function that cleans up in
11398 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011399 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011400static int ssl_context_load( mbedtls_ssl_context *ssl,
11401 const unsigned char *buf,
11402 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011403{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011404 const unsigned char *p = buf;
11405 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011406 size_t session_len;
11407 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011408
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011409 /*
11410 * The context should have been freshly setup or reset.
11411 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011412 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011413 * renegotiating, or if the user mistakenly loaded a session first.)
11414 */
11415 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11416 ssl->session != NULL )
11417 {
11418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11419 }
11420
11421 /*
11422 * We can't check that the config matches the initial one, but we can at
11423 * least check it matches the requirements for serializing.
11424 */
11425 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011426 mbedtls_ssl_ver_lt(
11427 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11428 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11429 mbedtls_ssl_ver_gt(
11430 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11431 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11432 mbedtls_ssl_ver_lt(
11433 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11434 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11435 mbedtls_ssl_ver_gt(
11436 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11437 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011438 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011439 {
11440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11441 }
11442
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011443 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11444
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011445 /*
11446 * Check version identifier
11447 */
11448 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11450
Teppo Järvelin61f412e2019-10-03 12:25:22 +030011451 if( mbedtls_platform_memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011452 sizeof( ssl_serialized_context_header ) ) != 0 )
11453 {
11454 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11455 }
11456 p += sizeof( ssl_serialized_context_header );
11457
11458 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011459 * Session
11460 */
11461 if( (size_t)( end - p ) < 4 )
11462 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11463
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011464 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011465 p += 4;
11466
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011467 /* This has been allocated by ssl_handshake_init(), called by
11468 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11469 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011470 ssl->session_in = ssl->session;
11471 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011472 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011473
11474 if( (size_t)( end - p ) < session_len )
11475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11476
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011477 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011478 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011479 {
11480 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011481 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011482 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011483
11484 p += session_len;
11485
11486 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011487 * Transform
11488 */
11489
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011490 /* This has been allocated by ssl_handshake_init(), called by
11491 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11492 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011493 ssl->transform_in = ssl->transform;
11494 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011495 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011496
11497 /* Read random bytes and populate structure */
11498 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11500
11501 ret = ssl_populate_transform( ssl->transform,
11502 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11503 ssl->session->master,
11504#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11505#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11506 ssl->session->encrypt_then_mac,
11507#endif
11508#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11509 ssl->session->trunc_hmac,
11510#endif
11511#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11512#if defined(MBEDTLS_ZLIB_SUPPORT)
11513 ssl->session->compression,
11514#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011515 p, /* currently pointing to randbytes */
11516 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11517 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11518 ssl );
11519 if( ret != 0 )
11520 return( ret );
11521
11522 p += sizeof( ssl->transform->randbytes );
11523
11524#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11525 /* Read connection IDs and store them */
11526 if( (size_t)( end - p ) < 1 )
11527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11528
11529 ssl->transform->in_cid_len = *p++;
11530
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011531 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11533
11534 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11535 p += ssl->transform->in_cid_len;
11536
11537 ssl->transform->out_cid_len = *p++;
11538
11539 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11541
11542 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11543 p += ssl->transform->out_cid_len;
11544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11545
11546 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011547 * Saved fields from top-level ssl_context structure
11548 */
11549#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11550 if( (size_t)( end - p ) < 4 )
11551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11552
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011553 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011554 p += 4;
11555#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11556
11557#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11558 if( (size_t)( end - p ) < 16 )
11559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11560
11561 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11562 ( (uint64_t) p[1] << 48 ) |
11563 ( (uint64_t) p[2] << 40 ) |
11564 ( (uint64_t) p[3] << 32 ) |
11565 ( (uint64_t) p[4] << 24 ) |
11566 ( (uint64_t) p[5] << 16 ) |
11567 ( (uint64_t) p[6] << 8 ) |
11568 ( (uint64_t) p[7] );
11569 p += 8;
11570
11571 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11572 ( (uint64_t) p[1] << 48 ) |
11573 ( (uint64_t) p[2] << 40 ) |
11574 ( (uint64_t) p[3] << 32 ) |
11575 ( (uint64_t) p[4] << 24 ) |
11576 ( (uint64_t) p[5] << 16 ) |
11577 ( (uint64_t) p[6] << 8 ) |
11578 ( (uint64_t) p[7] );
11579 p += 8;
11580#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11581
11582#if defined(MBEDTLS_SSL_PROTO_DTLS)
11583 if( (size_t)( end - p ) < 1 )
11584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11585
11586 ssl->disable_datagram_packing = *p++;
11587#endif /* MBEDTLS_SSL_PROTO_DTLS */
11588
11589 if( (size_t)( end - p ) < 8 )
11590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11591
11592 memcpy( ssl->cur_out_ctr, p, 8 );
11593 p += 8;
11594
11595#if defined(MBEDTLS_SSL_PROTO_DTLS)
11596 if( (size_t)( end - p ) < 2 )
11597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011598 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011599 p += 2;
11600#endif /* MBEDTLS_SSL_PROTO_DTLS */
11601
11602#if defined(MBEDTLS_SSL_ALPN)
11603 {
11604 uint8_t alpn_len;
11605 const char **cur;
11606
11607 if( (size_t)( end - p ) < 1 )
11608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11609
11610 alpn_len = *p++;
11611
11612 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11613 {
11614 /* alpn_chosen should point to an item in the configured list */
11615 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11616 {
11617 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030011618 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011619 {
11620 ssl->alpn_chosen = *cur;
11621 break;
11622 }
11623 }
11624 }
11625
11626 /* can only happen on conf mismatch */
11627 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11629
11630 p += alpn_len;
11631 }
11632#endif /* MBEDTLS_SSL_ALPN */
11633
11634 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011635 * Forced fields from top-level ssl_context structure
11636 *
11637 * Most of them already set to the correct value by mbedtls_ssl_init() and
11638 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11639 */
11640 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11641
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011642#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011643 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011644#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11645#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011646 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011647#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011648
Hanno Becker83985822019-08-30 10:42:49 +010011649 /* Adjust pointers for header fields of outgoing records to
11650 * the given transform, accounting for explicit IV and CID. */
11651 ssl_update_out_pointers( ssl, ssl->transform );
11652
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011653#if defined(MBEDTLS_SSL_PROTO_DTLS)
11654 ssl->in_epoch = 1;
11655#endif
11656
11657 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11658 * which we don't want - otherwise we'd end up freeing the wrong transform
11659 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11660 if( ssl->handshake != NULL )
11661 {
11662 mbedtls_ssl_handshake_free( ssl );
11663 mbedtls_free( ssl->handshake );
11664 ssl->handshake = NULL;
11665 }
11666
11667 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011668 * Done - should have consumed entire buffer
11669 */
11670 if( p != end )
11671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011672
11673 return( 0 );
11674}
11675
11676/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011677 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011678 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011679int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011680 const unsigned char *buf,
11681 size_t len )
11682{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011683 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011684
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011685 if( ret != 0 )
11686 mbedtls_ssl_free( context );
11687
11688 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011689}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011690#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011691
11692/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011693 * Free an SSL context
11694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011695void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011696{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011697 if( ssl == NULL )
11698 return;
11699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011701
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011702 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011703 {
Angus Grattond8213d02016-05-25 20:56:48 +100011704 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011705 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011706 }
11707
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011708 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011709 {
Angus Grattond8213d02016-05-25 20:56:48 +100011710 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011711 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011712 }
11713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011714#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011715 if( ssl->compress_buf != NULL )
11716 {
Angus Grattond8213d02016-05-25 20:56:48 +100011717 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011718 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011719 }
11720#endif
11721
Paul Bakker48916f92012-09-16 19:57:18 +000011722 if( ssl->transform )
11723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011724 mbedtls_ssl_transform_free( ssl->transform );
11725 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011726 }
11727
11728 if( ssl->handshake )
11729 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011730 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011731 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11732 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011734 mbedtls_free( ssl->handshake );
11735 mbedtls_free( ssl->transform_negotiate );
11736 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011737 }
11738
Paul Bakkerc0463502013-02-14 11:19:38 +010011739 if( ssl->session )
11740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011741 mbedtls_ssl_session_free( ssl->session );
11742 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011743 }
11744
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011745#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011746 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011747 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011748 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011749 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011750 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011753#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11754 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11757 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011758 }
11759#endif
11760
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011761#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011762 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011763#endif
11764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011766
Paul Bakker86f04f42013-02-14 11:20:09 +010011767 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011768 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011769}
11770
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011771/*
11772 * Initialze mbedtls_ssl_config
11773 */
11774void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11775{
11776 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011777
11778#if !defined(MBEDTLS_SSL_PROTO_TLS)
11779 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11780#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011781}
11782
Simon Butcherc97b6972015-12-27 23:48:17 +000011783#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011784#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011785static int ssl_preset_default_hashes[] = {
11786#if defined(MBEDTLS_SHA512_C)
11787 MBEDTLS_MD_SHA512,
11788 MBEDTLS_MD_SHA384,
11789#endif
11790#if defined(MBEDTLS_SHA256_C)
11791 MBEDTLS_MD_SHA256,
11792 MBEDTLS_MD_SHA224,
11793#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011794#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011795 MBEDTLS_MD_SHA1,
11796#endif
11797 MBEDTLS_MD_NONE
11798};
Simon Butcherc97b6972015-12-27 23:48:17 +000011799#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011800#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011801
Hanno Becker73f4cb12019-06-27 13:51:07 +010011802#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011803static int ssl_preset_suiteb_ciphersuites[] = {
11804 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11805 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11806 0
11807};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011808#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011809
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011810#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011811#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011812static int ssl_preset_suiteb_hashes[] = {
11813 MBEDTLS_MD_SHA256,
11814 MBEDTLS_MD_SHA384,
11815 MBEDTLS_MD_NONE
11816};
11817#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011818#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011819
Hanno Beckerc1096e72019-06-19 12:30:41 +010011820#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011821static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011822#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011823 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011824#endif
11825#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011826 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011827#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011828 MBEDTLS_ECP_DP_NONE
11829};
11830#endif
11831
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011832/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011833 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011834 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011835int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011836 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011837{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011838#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011839 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011840#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011841
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011842 /* Use the functions here so that they are covered in tests,
11843 * but otherwise access member directly for efficiency */
11844 mbedtls_ssl_conf_endpoint( conf, endpoint );
11845 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011846
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011847 /*
11848 * Things that are common to all presets
11849 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011850#if defined(MBEDTLS_SSL_CLI_C)
11851 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11852 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011853#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011854 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011855#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011856#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11857 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11858#endif
11859 }
11860#endif
11861
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011862#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011863 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011864#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011865
11866#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11867 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11868#endif
11869
11870#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011871#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011872 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011873#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11874#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011875 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011876 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011877#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011878#endif
11879
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011880#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11881 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11882#endif
11883
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011884#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011885 conf->f_cookie_write = ssl_cookie_write_dummy;
11886 conf->f_cookie_check = ssl_cookie_check_dummy;
11887#endif
11888
Hanno Becker7f376f42019-06-12 16:20:48 +010011889#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11890 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011891 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11892#endif
11893
Janos Follath088ce432017-04-10 12:42:31 +010011894#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011895#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011896 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011897#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11898#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011899
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011901#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011902 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011903#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11904#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011905 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011906#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011908
11909#if defined(MBEDTLS_SSL_RENEGOTIATION)
11910 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011911 memset( conf->renego_period, 0x00, 2 );
11912 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011913#endif
11914
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011915#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11916 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11917 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011918 const unsigned char dhm_p[] =
11919 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11920 const unsigned char dhm_g[] =
11921 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11922
Hanno Beckera90658f2017-10-04 15:29:08 +010011923 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11924 dhm_p, sizeof( dhm_p ),
11925 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011926 {
11927 return( ret );
11928 }
11929 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011930#endif
11931
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011932 /*
11933 * Preset-specific defaults
11934 */
11935 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011936 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011937 /*
11938 * NSA Suite B
11939 */
11940 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011941#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011942 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011943#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11944#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011945 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011946#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11947#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011948 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011949#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11950#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011951 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011952#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011953
Hanno Becker73f4cb12019-06-27 13:51:07 +010011954#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011955 conf->ciphersuite_list[0] =
11956 conf->ciphersuite_list[1] =
11957 conf->ciphersuite_list[2] =
11958 conf->ciphersuite_list[3] =
11959 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011960#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011961
11962#if defined(MBEDTLS_X509_CRT_PARSE_C)
11963 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011964#endif
11965
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011966#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011967#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011968 conf->sig_hashes = ssl_preset_suiteb_hashes;
11969#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011970#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011971
11972#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011973#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011974 conf->curve_list = ssl_preset_suiteb_curves;
11975#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011976#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011977 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011978
11979 /*
11980 * Default
11981 */
11982 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011983#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011984 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11985 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11986 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11987 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011988#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11989#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011990 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11991 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11992 MBEDTLS_SSL_MIN_MINOR_VERSION :
11993 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011995 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011996 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11997#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011998#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11999#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12000 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12001#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12002#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12003 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12004#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012005
Hanno Becker73f4cb12019-06-27 13:51:07 +010012006#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012007 conf->ciphersuite_list[0] =
12008 conf->ciphersuite_list[1] =
12009 conf->ciphersuite_list[2] =
12010 conf->ciphersuite_list[3] =
12011 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012012#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012013
12014#if defined(MBEDTLS_X509_CRT_PARSE_C)
12015 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12016#endif
12017
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012018#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012019#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012020 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012021#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012022#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012023
12024#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012025#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012026 conf->curve_list = mbedtls_ecp_grp_id_list();
12027#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012028#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012029
12030#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12031 conf->dhm_min_bitlen = 1024;
12032#endif
12033 }
12034
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012035 return( 0 );
12036}
12037
12038/*
12039 * Free mbedtls_ssl_config
12040 */
12041void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12042{
12043#if defined(MBEDTLS_DHM_C)
12044 mbedtls_mpi_free( &conf->dhm_P );
12045 mbedtls_mpi_free( &conf->dhm_G );
12046#endif
12047
12048#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12049 if( conf->psk != NULL )
12050 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012051 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012052 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012053 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012054 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012055 }
12056
12057 if( conf->psk_identity != NULL )
12058 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012059 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012060 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012061 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012062 conf->psk_identity_len = 0;
12063 }
12064#endif
12065
12066#if defined(MBEDTLS_X509_CRT_PARSE_C)
12067 ssl_key_cert_free( conf->key_cert );
12068#endif
12069
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012070 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012071}
12072
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012073#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012074 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12075 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012076/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012077 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012079unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012081#if defined(MBEDTLS_RSA_C)
12082 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12083 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012084#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012085#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012086 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12087 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012089 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012090}
12091
Hanno Becker7e5437a2017-04-28 17:15:26 +010012092unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12093{
12094 switch( type ) {
12095 case MBEDTLS_PK_RSA:
12096 return( MBEDTLS_SSL_SIG_RSA );
12097 case MBEDTLS_PK_ECDSA:
12098 case MBEDTLS_PK_ECKEY:
12099 return( MBEDTLS_SSL_SIG_ECDSA );
12100 default:
12101 return( MBEDTLS_SSL_SIG_ANON );
12102 }
12103}
12104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012105mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012106{
12107 switch( sig )
12108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012109#if defined(MBEDTLS_RSA_C)
12110 case MBEDTLS_SSL_SIG_RSA:
12111 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012112#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012113#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012114 case MBEDTLS_SSL_SIG_ECDSA:
12115 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012116#endif
12117 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012118 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012119 }
12120}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012121#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012122
Hanno Becker7e5437a2017-04-28 17:15:26 +010012123#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12124 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12125
12126/* Find an entry in a signature-hash set matching a given hash algorithm. */
12127mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12128 mbedtls_pk_type_t sig_alg )
12129{
12130 switch( sig_alg )
12131 {
12132 case MBEDTLS_PK_RSA:
12133 return( set->rsa );
12134 case MBEDTLS_PK_ECDSA:
12135 return( set->ecdsa );
12136 default:
12137 return( MBEDTLS_MD_NONE );
12138 }
12139}
12140
12141/* Add a signature-hash-pair to a signature-hash set */
12142void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12143 mbedtls_pk_type_t sig_alg,
12144 mbedtls_md_type_t md_alg )
12145{
12146 switch( sig_alg )
12147 {
12148 case MBEDTLS_PK_RSA:
12149 if( set->rsa == MBEDTLS_MD_NONE )
12150 set->rsa = md_alg;
12151 break;
12152
12153 case MBEDTLS_PK_ECDSA:
12154 if( set->ecdsa == MBEDTLS_MD_NONE )
12155 set->ecdsa = md_alg;
12156 break;
12157
12158 default:
12159 break;
12160 }
12161}
12162
12163/* Allow exactly one hash algorithm for each signature. */
12164void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12165 mbedtls_md_type_t md_alg )
12166{
12167 set->rsa = md_alg;
12168 set->ecdsa = md_alg;
12169}
12170
12171#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12172 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12173
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012174/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012175 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012178{
12179 switch( hash )
12180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012181#if defined(MBEDTLS_MD5_C)
12182 case MBEDTLS_SSL_HASH_MD5:
12183 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012185#if defined(MBEDTLS_SHA1_C)
12186 case MBEDTLS_SSL_HASH_SHA1:
12187 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012189#if defined(MBEDTLS_SHA256_C)
12190 case MBEDTLS_SSL_HASH_SHA224:
12191 return( MBEDTLS_MD_SHA224 );
12192 case MBEDTLS_SSL_HASH_SHA256:
12193 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012195#if defined(MBEDTLS_SHA512_C)
12196 case MBEDTLS_SSL_HASH_SHA384:
12197 return( MBEDTLS_MD_SHA384 );
12198 case MBEDTLS_SSL_HASH_SHA512:
12199 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012200#endif
12201 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012202 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012203 }
12204}
12205
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012206/*
12207 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12208 */
12209unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12210{
12211 switch( md )
12212 {
12213#if defined(MBEDTLS_MD5_C)
12214 case MBEDTLS_MD_MD5:
12215 return( MBEDTLS_SSL_HASH_MD5 );
12216#endif
12217#if defined(MBEDTLS_SHA1_C)
12218 case MBEDTLS_MD_SHA1:
12219 return( MBEDTLS_SSL_HASH_SHA1 );
12220#endif
12221#if defined(MBEDTLS_SHA256_C)
12222 case MBEDTLS_MD_SHA224:
12223 return( MBEDTLS_SSL_HASH_SHA224 );
12224 case MBEDTLS_MD_SHA256:
12225 return( MBEDTLS_SSL_HASH_SHA256 );
12226#endif
12227#if defined(MBEDTLS_SHA512_C)
12228 case MBEDTLS_MD_SHA384:
12229 return( MBEDTLS_SSL_HASH_SHA384 );
12230 case MBEDTLS_MD_SHA512:
12231 return( MBEDTLS_SSL_HASH_SHA512 );
12232#endif
12233 default:
12234 return( MBEDTLS_SSL_HASH_NONE );
12235 }
12236}
12237
Hanno Beckeree902df2019-08-23 13:47:47 +010012238#if defined(MBEDTLS_USE_TINYCRYPT)
12239/*
12240 * Check if a curve proposed by the peer is in our list.
12241 * Return 0 if we're willing to use it, -1 otherwise.
12242 */
12243int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12244 mbedtls_uecc_group_id grp_id )
12245{
12246 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12247 if( own_ec_id == grp_id )
12248 return( 0 );
12249 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12250
12251 return( -1 );
12252}
12253#endif /* MBEDTLS_USE_TINYCRYPT */
12254
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012255#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012256/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012257 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012258 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012259 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012260int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12261 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012262{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012263 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12264 if( own_ec_id == grp_id )
12265 return( 0 );
12266 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012267
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012268 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012269}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012270#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012271
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012272#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012273/*
12274 * Check if a hash proposed by the peer is in our list.
12275 * Return 0 if we're willing to use it, -1 otherwise.
12276 */
12277int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12278 mbedtls_md_type_t md )
12279{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012280 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12281 if( md_alg == md )
12282 return( 0 );
12283 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012284
12285 return( -1 );
12286}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012287#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012289#if defined(MBEDTLS_X509_CRT_PARSE_C)
12290int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012291 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012292 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012293 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012294{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012295 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012296#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012297 int usage = 0;
12298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012299#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012300 const char *ext_oid;
12301 size_t ext_len;
12302#endif
12303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012304#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12305 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012306 ((void) cert);
12307 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012308 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012309#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012311#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12312 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012313 {
12314 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012315 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012317 case MBEDTLS_KEY_EXCHANGE_RSA:
12318 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012319 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012320 break;
12321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012322 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12323 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12324 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12325 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012326 break;
12327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012328 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12329 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012330 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012331 break;
12332
12333 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012334 case MBEDTLS_KEY_EXCHANGE_NONE:
12335 case MBEDTLS_KEY_EXCHANGE_PSK:
12336 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12337 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012338 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012339 usage = 0;
12340 }
12341 }
12342 else
12343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012344 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12345 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012346 }
12347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012348 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012349 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012350 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012351 ret = -1;
12352 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012353#else
12354 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012355#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012357#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12358 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012360 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12361 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012362 }
12363 else
12364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012365 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12366 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012367 }
12368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012369 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012370 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012371 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012372 ret = -1;
12373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012374#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012375
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012376 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012378#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012379
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012380#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12381 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12382int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12383 unsigned char *output,
12384 unsigned char *data, size_t data_len )
12385{
12386 int ret = 0;
12387 mbedtls_md5_context mbedtls_md5;
12388 mbedtls_sha1_context mbedtls_sha1;
12389
12390 mbedtls_md5_init( &mbedtls_md5 );
12391 mbedtls_sha1_init( &mbedtls_sha1 );
12392
12393 /*
12394 * digitally-signed struct {
12395 * opaque md5_hash[16];
12396 * opaque sha_hash[20];
12397 * };
12398 *
12399 * md5_hash
12400 * MD5(ClientHello.random + ServerHello.random
12401 * + ServerParams);
12402 * sha_hash
12403 * SHA(ClientHello.random + ServerHello.random
12404 * + ServerParams);
12405 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012406 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012407 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012409 goto exit;
12410 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012411 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012412 ssl->handshake->randbytes, 64 ) ) != 0 )
12413 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012415 goto exit;
12416 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012417 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012418 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012420 goto exit;
12421 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012422 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012423 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012425 goto exit;
12426 }
12427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012428 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012429 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012431 goto exit;
12432 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012433 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012434 ssl->handshake->randbytes, 64 ) ) != 0 )
12435 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012437 goto exit;
12438 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012439 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012440 data_len ) ) != 0 )
12441 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012443 goto exit;
12444 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012445 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012446 output + 16 ) ) != 0 )
12447 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012449 goto exit;
12450 }
12451
12452exit:
12453 mbedtls_md5_free( &mbedtls_md5 );
12454 mbedtls_sha1_free( &mbedtls_sha1 );
12455
12456 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012457 mbedtls_ssl_pend_fatal_alert( ssl,
12458 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012459
12460 return( ret );
12461
12462}
12463#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12464 MBEDTLS_SSL_PROTO_TLS1_1 */
12465
12466#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12467 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12468int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012469 unsigned char *hash, size_t *hashlen,
12470 unsigned char *data, size_t data_len,
12471 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012472{
12473 int ret = 0;
12474 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012475 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012476 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012477
12478 mbedtls_md_init( &ctx );
12479
12480 /*
12481 * digitally-signed struct {
12482 * opaque client_random[32];
12483 * opaque server_random[32];
12484 * ServerDHParams params;
12485 * };
12486 */
12487 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12488 {
12489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12490 goto exit;
12491 }
12492 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12493 {
12494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12495 goto exit;
12496 }
12497 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12498 {
12499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12500 goto exit;
12501 }
12502 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12503 {
12504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12505 goto exit;
12506 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012507 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012508 {
12509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12510 goto exit;
12511 }
12512
12513exit:
12514 mbedtls_md_free( &ctx );
12515
12516 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012517 mbedtls_ssl_pend_fatal_alert( ssl,
12518 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012519
12520 return( ret );
12521}
12522#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12523 MBEDTLS_SSL_PROTO_TLS1_2 */
12524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012525#endif /* MBEDTLS_SSL_TLS_C */