blob: b17e33d61e4782a6452cbffddab865216d4ddfa9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
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)
873 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
874 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)
1172 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1173 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)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001496 if( 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)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001771 if( 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
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002114 *(p++) = (unsigned char)( psk_len >> 8 );
2115 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002116
2117 if( end < p || (size_t)( end - p ) < psk_len )
2118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2119
2120 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002121 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002122 }
2123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2125#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2126 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002127 {
2128 /*
2129 * other_secret already set by the ClientKeyExchange message,
2130 * and is 48 bytes long
2131 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002132 if( end - p < 2 )
2133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2134
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002135 *p++ = 0;
2136 *p++ = 48;
2137 p += 48;
2138 }
2139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2141#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2142 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002143 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002144 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002145 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002146
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002147 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002149 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002150 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002151 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002154 return( ret );
2155 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002156 *(p++) = (unsigned char)( len >> 8 );
2157 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002158 p += len;
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002161 }
2162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2164#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2165 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002167 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002168 size_t zlen;
2169
Hanno Becker982da7e2019-09-02 09:47:39 +01002170#if defined(MBEDTLS_USE_TINYCRYPT)
2171 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2172 ((void) ret);
2173
2174 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2175 ssl->handshake->ecdh_privkey,
2176 p + 2,
2177 uecc_curve ) )
2178 {
2179 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2180 }
2181
2182 zlen = NUM_ECC_BYTES;
2183#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002185 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002186 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002187 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002190 return( ret );
2191 }
2192
Hanno Becker982da7e2019-09-02 09:47:39 +01002193 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2194 MBEDTLS_DEBUG_ECDH_Z );
2195#endif /* MBEDTLS_USE_TINYCRYPT */
2196
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002197 *(p++) = (unsigned char)( zlen >> 8 );
2198 *(p++) = (unsigned char)( zlen );
2199 p += zlen;
2200
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002201 }
2202 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002207 }
2208
2209 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002210 if( end - p < 2 )
2211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002212
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002213 *(p++) = (unsigned char)( psk_len >> 8 );
2214 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002215
2216 if( end < p || (size_t)( end - p ) < psk_len )
2217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2218
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002219 memcpy( p, psk, psk_len );
2220 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002221
2222 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2223
2224 return( 0 );
2225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002229/*
2230 * SSLv3.0 MAC functions
2231 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002232#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002233static void ssl_mac( mbedtls_md_context_t *md_ctx,
2234 const unsigned char *secret,
2235 const unsigned char *buf, size_t len,
2236 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002237 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238{
2239 unsigned char header[11];
2240 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002241 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2243 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002244
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002245 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002247 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002248 else
Paul Bakker68884e32013-01-07 18:20:04 +01002249 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
2251 memcpy( header, ctr, 8 );
2252 header[ 8] = (unsigned char) type;
2253 header[ 9] = (unsigned char)( len >> 8 );
2254 header[10] = (unsigned char)( len );
2255
Paul Bakker68884e32013-01-07 18:20:04 +01002256 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_md_starts( md_ctx );
2258 mbedtls_md_update( md_ctx, secret, md_size );
2259 mbedtls_md_update( md_ctx, padding, padlen );
2260 mbedtls_md_update( md_ctx, header, 11 );
2261 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002262 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Paul Bakker68884e32013-01-07 18:20:04 +01002264 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_md_starts( md_ctx );
2266 mbedtls_md_update( md_ctx, secret, md_size );
2267 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002268 mbedtls_md_update( md_ctx, out, md_size );
2269 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002272
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002273/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002274 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002275#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002276 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2277 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2278 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2279/* This function makes sure every byte in the memory region is accessed
2280 * (in ascending addresses order) */
2281static void ssl_read_memory( unsigned char *p, size_t len )
2282{
2283 unsigned char acc = 0;
2284 volatile unsigned char force;
2285
2286 for( ; len != 0; p++, len-- )
2287 acc ^= *p;
2288
2289 force = acc;
2290 (void) force;
2291}
2292#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2293
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002294/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002296 */
Hanno Becker3307b532017-12-27 21:37:21 +00002297
Hanno Beckera5a2b082019-05-15 14:03:01 +01002298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002299/* This functions transforms a DTLS plaintext fragment and a record content
2300 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002301 *
2302 * struct {
2303 * opaque content[DTLSPlaintext.length];
2304 * ContentType real_type;
2305 * uint8 zeros[length_of_padding];
2306 * } DTLSInnerPlaintext;
2307 *
2308 * Input:
2309 * - `content`: The beginning of the buffer holding the
2310 * plaintext to be wrapped.
2311 * - `*content_size`: The length of the plaintext in Bytes.
2312 * - `max_len`: The number of Bytes available starting from
2313 * `content`. This must be `>= *content_size`.
2314 * - `rec_type`: The desired record content type.
2315 *
2316 * Output:
2317 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2318 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2319 *
2320 * Returns:
2321 * - `0` on success.
2322 * - A negative error code if `max_len` didn't offer enough space
2323 * for the expansion.
2324 */
2325static int ssl_cid_build_inner_plaintext( unsigned char *content,
2326 size_t *content_size,
2327 size_t remaining,
2328 uint8_t rec_type )
2329{
2330 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002331 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2332 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2333 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002334
2335 /* Write real content type */
2336 if( remaining == 0 )
2337 return( -1 );
2338 content[ len ] = rec_type;
2339 len++;
2340 remaining--;
2341
2342 if( remaining < pad )
2343 return( -1 );
2344 memset( content + len, 0, pad );
2345 len += pad;
2346 remaining -= pad;
2347
2348 *content_size = len;
2349 return( 0 );
2350}
2351
Hanno Becker7dc25772019-05-20 15:08:01 +01002352/* This function parses a DTLSInnerPlaintext structure.
2353 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002354static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2355 size_t *content_size,
2356 uint8_t *rec_type )
2357{
2358 size_t remaining = *content_size;
2359
2360 /* Determine length of padding by skipping zeroes from the back. */
2361 do
2362 {
2363 if( remaining == 0 )
2364 return( -1 );
2365 remaining--;
2366 } while( content[ remaining ] == 0 );
2367
2368 *content_size = remaining;
2369 *rec_type = content[ remaining ];
2370
2371 return( 0 );
2372}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002374
Hanno Becker99abf512019-05-20 14:50:53 +01002375/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002376 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002377static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002378 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002379 mbedtls_record *rec )
2380{
Hanno Becker99abf512019-05-20 14:50:53 +01002381 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002382 *
2383 * additional_data = seq_num + TLSCompressed.type +
2384 * TLSCompressed.version + TLSCompressed.length;
2385 *
Hanno Becker99abf512019-05-20 14:50:53 +01002386 * For the CID extension, this is extended as follows
2387 * (quoting draft-ietf-tls-dtls-connection-id-05,
2388 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002389 *
2390 * additional_data = seq_num + DTLSPlaintext.type +
2391 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002392 * cid +
2393 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002394 * length_of_DTLSInnerPlaintext;
2395 */
2396
Hanno Becker3307b532017-12-27 21:37:21 +00002397 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2398 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002399 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002400
Hanno Beckera5a2b082019-05-15 14:03:01 +01002401#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002402 if( rec->cid_len != 0 )
2403 {
2404 memcpy( add_data + 11, rec->cid, rec->cid_len );
2405 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2406 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2407 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2408 *add_data_len = 13 + 1 + rec->cid_len;
2409 }
2410 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002411#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002412 {
2413 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2414 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2415 *add_data_len = 13;
2416 }
Hanno Becker3307b532017-12-27 21:37:21 +00002417}
2418
Hanno Becker611a83b2018-01-03 14:27:32 +00002419int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2420 mbedtls_ssl_transform *transform,
2421 mbedtls_record *rec,
2422 int (*f_rng)(void *, unsigned char *, size_t),
2423 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002426 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002427 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002428 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002429 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002430 size_t post_avail;
2431
2432 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002433#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002434 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002435 ((void) ssl);
2436#endif
2437
2438 /* The PRNG is used for dynamic IV generation that's used
2439 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2440#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2441 ( defined(MBEDTLS_AES_C) || \
2442 defined(MBEDTLS_ARIA_C) || \
2443 defined(MBEDTLS_CAMELLIA_C) ) && \
2444 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2445 ((void) f_rng);
2446 ((void) p_rng);
2447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Hanno Becker3307b532017-12-27 21:37:21 +00002451 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002452 {
Hanno Becker3307b532017-12-27 21:37:21 +00002453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2455 }
Hanno Becker505089d2019-05-01 09:45:57 +01002456 if( rec == NULL
2457 || rec->buf == NULL
2458 || rec->buf_len < rec->data_offset
2459 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002460#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002461 || rec->cid_len != 0
2462#endif
2463 )
Hanno Becker3307b532017-12-27 21:37:21 +00002464 {
2465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002467 }
2468
Hanno Becker3307b532017-12-27 21:37:21 +00002469 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002470 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002472 data, rec->data_len );
2473
2474 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2475
2476 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2477 {
2478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2479 (unsigned) rec->data_len,
2480 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2482 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002483
Hanno Beckera5a2b082019-05-15 14:03:01 +01002484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002485 /*
2486 * Add CID information
2487 */
2488 rec->cid_len = transform->out_cid_len;
2489 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2490 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002491
2492 if( rec->cid_len != 0 )
2493 {
2494 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002495 * Wrap plaintext into DTLSInnerPlaintext structure.
2496 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002497 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002498 * Note that this changes `rec->data_len`, and hence
2499 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002500 */
2501 if( ssl_cid_build_inner_plaintext( data,
2502 &rec->data_len,
2503 post_avail,
2504 rec->type ) != 0 )
2505 {
2506 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2507 }
2508
2509 rec->type = MBEDTLS_SSL_MSG_CID;
2510 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002511#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002512
Hanno Becker92c930f2019-04-29 17:31:37 +01002513 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2514
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002516 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002518#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 if( mode == MBEDTLS_MODE_STREAM ||
2520 ( mode == MBEDTLS_MODE_CBC
2521#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002522 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002523#endif
2524 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
Hanno Becker3307b532017-12-27 21:37:21 +00002526 if( post_avail < transform->maclen )
2527 {
2528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2529 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2530 }
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002533 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2534 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002535 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002536 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002537 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2538 data, rec->data_len, rec->ctr, rec->type, mac );
2539 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002540 }
2541 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2544 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002545 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2546 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002547 {
Hanno Becker992b6872017-11-09 18:57:39 +00002548 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2549
Hanno Beckere83efe62019-04-29 13:52:53 +01002550 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002551
Hanno Becker3307b532017-12-27 21:37:21 +00002552 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002553 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002554 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2555 data, rec->data_len );
2556 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2557 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2558
2559 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002560 }
2561 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002562#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002566 }
2567
Hanno Becker3307b532017-12-27 21:37:21 +00002568 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2569 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002570
Hanno Becker3307b532017-12-27 21:37:21 +00002571 rec->data_len += transform->maclen;
2572 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002573 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002574 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002575#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002577 /*
2578 * Encrypt
2579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2581 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002583 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002584 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002586 "including %d bytes of padding",
2587 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2590 transform->iv_enc, transform->ivlen,
2591 data, rec->data_len,
2592 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002595 return( ret );
2596 }
2597
Hanno Becker3307b532017-12-27 21:37:21 +00002598 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
Paul Bakker68884e32013-01-07 18:20:04 +01002604 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002606
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002607#if defined(MBEDTLS_GCM_C) || \
2608 defined(MBEDTLS_CCM_C) || \
2609 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611 mode == MBEDTLS_MODE_CCM ||
2612 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002613 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002614 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002616 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002617
Hanno Becker3307b532017-12-27 21:37:21 +00002618 /* Check that there's space for both the authentication tag
2619 * and the explicit IV before and after the record content. */
2620 if( post_avail < transform->taglen ||
2621 rec->data_offset < explicit_iv_len )
2622 {
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2624 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2625 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002626
Paul Bakker68884e32013-01-07 18:20:04 +01002627 /*
2628 * Generate IV
2629 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002630 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002634 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2635 explicit_iv_len );
2636 /* Prefix record content with explicit IV. */
2637 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 }
2639 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2640 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002641 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 unsigned char i;
2643
2644 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2645
2646 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002647 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002648 }
2649 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002650 {
2651 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002654 }
2655
Hanno Beckere83efe62019-04-29 13:52:53 +01002656 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002657
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002658 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2659 iv, transform->ivlen );
2660 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002661 data - explicit_iv_len, explicit_iv_len );
2662 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002663 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002665 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002666 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002667
Paul Bakker68884e32013-01-07 18:20:04 +01002668 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002669 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002670 */
Hanno Becker3307b532017-12-27 21:37:21 +00002671
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002672 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002673 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002674 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002675 data, rec->data_len, /* source */
2676 data, &rec->data_len, /* destination */
2677 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002680 return( ret );
2681 }
2682
Hanno Becker3307b532017-12-27 21:37:21 +00002683 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2684 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002685
Hanno Becker3307b532017-12-27 21:37:21 +00002686 rec->data_len += transform->taglen + explicit_iv_len;
2687 rec->data_offset -= explicit_iv_len;
2688 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002689 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2693#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002694 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002697 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002698 size_t padlen, i;
2699 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002700
Hanno Becker3307b532017-12-27 21:37:21 +00002701 /* Currently we're always using minimal padding
2702 * (up to 255 bytes would be allowed). */
2703 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2704 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 padlen = 0;
2706
Hanno Becker3307b532017-12-27 21:37:21 +00002707 /* Check there's enough space in the buffer for the padding. */
2708 if( post_avail < padlen + 1 )
2709 {
2710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2711 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2712 }
2713
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002715 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Hanno Becker3307b532017-12-27 21:37:21 +00002717 rec->data_len += padlen + 1;
2718 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002721 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002722 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2723 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002724 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002725 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2726 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002727 {
Hanno Becker3307b532017-12-27 21:37:21 +00002728 if( f_rng == NULL )
2729 {
2730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2732 }
2733
2734 if( rec->data_offset < transform->ivlen )
2735 {
2736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2737 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2738 }
2739
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002740 /*
2741 * Generate IV
2742 */
Hanno Becker3307b532017-12-27 21:37:21 +00002743 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002744 if( ret != 0 )
2745 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002746
Hanno Becker3307b532017-12-27 21:37:21 +00002747 memcpy( data - transform->ivlen, transform->iv_enc,
2748 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002749
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002754 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002755 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002756 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Hanno Becker3307b532017-12-27 21:37:21 +00002758 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2759 transform->iv_enc,
2760 transform->ivlen,
2761 data, rec->data_len,
2762 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002765 return( ret );
2766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002767
Hanno Becker3307b532017-12-27 21:37:21 +00002768 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002772 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002775 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2776 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002777 {
2778 /*
2779 * Save IV in SSL3 and TLS1
2780 */
Hanno Becker3307b532017-12-27 21:37:21 +00002781 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2782 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 }
Hanno Becker3307b532017-12-27 21:37:21 +00002784 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002785#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002786 {
2787 data -= transform->ivlen;
2788 rec->data_offset -= transform->ivlen;
2789 rec->data_len += transform->ivlen;
2790 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002793 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002794 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002795 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2796
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002797 /*
2798 * MAC(MAC_write_key, seq_num +
2799 * TLSCipherText.type +
2800 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002801 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002802 * IV + // except for TLS 1.0
2803 * ENC(content + padding + padding_length));
2804 */
Hanno Becker3307b532017-12-27 21:37:21 +00002805
2806 if( post_avail < transform->maclen)
2807 {
2808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2809 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2810 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002811
Hanno Beckere83efe62019-04-29 13:52:53 +01002812 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002815 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002816 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002817
Hanno Becker3307b532017-12-27 21:37:21 +00002818 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002819 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002820 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2821 data, rec->data_len );
2822 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2823 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002824
Hanno Becker3307b532017-12-27 21:37:21 +00002825 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002826
Hanno Becker3307b532017-12-27 21:37:21 +00002827 rec->data_len += transform->maclen;
2828 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002829 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002835 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002841 /* Make extra sure authentication was performed, exactly once */
2842 if( auth_done != 1 )
2843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002846 }
2847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
2850 return( 0 );
2851}
2852
Hanno Becker40478be2019-07-12 08:23:59 +01002853int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002854 mbedtls_ssl_transform *transform,
2855 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002859 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002860#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002861 size_t padlen = 0, correct = 1;
2862#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002863 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002864 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002865 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002866
Hanno Becker611a83b2018-01-03 14:27:32 +00002867#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002868 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002869 ((void) ssl);
2870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002873 if( rec == NULL ||
2874 rec->buf == NULL ||
2875 rec->buf_len < rec->data_offset ||
2876 rec->buf_len - rec->data_offset < rec->data_len )
2877 {
2878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002880 }
2881
Hanno Becker4c6876b2017-12-27 21:28:58 +00002882 data = rec->buf + rec->data_offset;
2883 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Hanno Beckera5a2b082019-05-15 14:03:01 +01002885#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002886 /*
2887 * Match record's CID with incoming CID.
2888 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002889 if( rec->cid_len != transform->in_cid_len ||
2890 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2891 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002892 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002893 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002894#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2897 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002898 {
2899 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002900 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2901 transform->iv_dec,
2902 transform->ivlen,
2903 data, rec->data_len,
2904 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002907 return( ret );
2908 }
2909
Hanno Becker4c6876b2017-12-27 21:28:58 +00002910 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
Paul Bakker68884e32013-01-07 18:20:04 +01002916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002918#if defined(MBEDTLS_GCM_C) || \
2919 defined(MBEDTLS_CCM_C) || \
2920 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002922 mode == MBEDTLS_MODE_CCM ||
2923 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002924 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002925 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002926 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002927
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002928 /*
2929 * Prepare IV from explicit and implicit data.
2930 */
2931
2932 /* Check that there's enough space for the explicit IV
2933 * (at the beginning of the record) and the MAC (at the
2934 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002938 "+ taglen (%d)", rec->data_len,
2939 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002941 }
Paul Bakker68884e32013-01-07 18:20:04 +01002942
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002943#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002944 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2945 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002946 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002947
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002948 /* Fixed */
2949 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2950 /* Explicit */
2951 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002952 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002953 else
2954#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2955#if defined(MBEDTLS_CHACHAPOLY_C)
2956 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002957 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002958 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002959 unsigned char i;
2960
2961 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2962
2963 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002964 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002965 }
2966 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002967#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002968 {
2969 /* Reminder if we ever add an AEAD mode with a different size */
2970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2972 }
2973
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002974 /* Group changes to data, data_len, and add_data, because
2975 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002976 data += explicit_iv_len;
2977 rec->data_offset += explicit_iv_len;
2978 rec->data_len -= explicit_iv_len + transform->taglen;
2979
Hanno Beckere83efe62019-04-29 13:52:53 +01002980 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002981 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002982 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002983
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002984 /* Because of the check above, we know that there are
2985 * explicit_iv_len Bytes preceeding data, and taglen
2986 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002987 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002988 * mbedtls_cipher_auth_decrypt() below. */
2989
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002990 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002991 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002992 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002993
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002994 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002995 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002996 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002997 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2998 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002999 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003000 data, rec->data_len,
3001 data, &olen,
3002 data + rec->data_len,
3003 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3008 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003009
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003010 return( ret );
3011 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003012 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003014 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003015 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003019 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3023#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003024 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003027 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003028
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 /*
Paul Bakker45829992013-01-03 14:52:21 +01003030 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003033 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3034 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003035 {
3036 /* The ciphertext is prefixed with the CBC IV. */
3037 minlen += transform->ivlen;
3038 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039#endif
Paul Bakker45829992013-01-03 14:52:21 +01003040
Hanno Becker4c6876b2017-12-27 21:28:58 +00003041 /* Size considerations:
3042 *
3043 * - The CBC cipher text must not be empty and hence
3044 * at least of size transform->ivlen.
3045 *
3046 * Together with the potential IV-prefix, this explains
3047 * the first of the two checks below.
3048 *
3049 * - The record must contain a MAC, either in plain or
3050 * encrypted, depending on whether Encrypt-then-MAC
3051 * is used or not.
3052 * - If it is, the message contains the IV-prefix,
3053 * the CBC ciphertext, and the MAC.
3054 * - If it is not, the padded plaintext, and hence
3055 * the CBC ciphertext, has at least length maclen + 1
3056 * because there is at least the padding length byte.
3057 *
3058 * As the CBC ciphertext is not empty, both cases give the
3059 * lower bound minlen + maclen + 1 on the record size, which
3060 * we test for in the second check below.
3061 */
3062 if( rec->data_len < minlen + transform->ivlen ||
3063 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003066 "+ 1 ) ( + expl IV )", rec->data_len,
3067 transform->ivlen,
3068 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003070 }
3071
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003072 /*
3073 * Authenticate before decrypt if enabled
3074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003077 {
Hanno Becker992b6872017-11-09 18:57:39 +00003078 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003081
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003082 /* Update data_len in tandem with add_data.
3083 *
3084 * The subtraction is safe because of the previous check
3085 * data_len >= minlen + maclen + 1.
3086 *
3087 * Afterwards, we know that data + data_len is followed by at
3088 * least maclen Bytes, which justifies the call to
3089 * mbedtls_ssl_safer_memcmp() below.
3090 *
3091 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003092 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003093 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003094
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003095 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003096 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3097 add_data_len );
3098 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3099 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003100 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3101 data, rec->data_len );
3102 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3103 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003104
Hanno Becker4c6876b2017-12-27 21:28:58 +00003105 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3106 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003107 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003109
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003110 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003111 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3112 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003116 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003120
3121 /*
3122 * Check length sanity
3123 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003124
3125 /* We know from above that data_len > minlen >= 0,
3126 * so the following check in particular implies that
3127 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003131 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003133 }
3134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003136 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003138 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003139 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3140 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003141 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003142 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003144
Hanno Becker4c6876b2017-12-27 21:28:58 +00003145 data += transform->ivlen;
3146 rec->data_offset += transform->ivlen;
3147 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003150
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003151 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3152
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3154 transform->iv_dec, transform->ivlen,
3155 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003158 return( ret );
3159 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003160
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003161 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003166 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003169 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3170 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003171 {
3172 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003173 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3174 * records is equivalent to CBC decryption of the concatenation
3175 * of the records; in other words, IVs are maintained across
3176 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003177 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003178 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3179 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003181#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Hanno Becker4c6876b2017-12-27 21:28:58 +00003183 /* Safe since data_len >= minlen + maclen + 1, so after having
3184 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003185 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3186 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003187 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003188
Hanno Becker4c6876b2017-12-27 21:28:58 +00003189 if( auth_done == 1 )
3190 {
3191 correct *= ( rec->data_len >= padlen + 1 );
3192 padlen *= ( rec->data_len >= padlen + 1 );
3193 }
3194 else
Paul Bakker45829992013-01-03 14:52:21 +01003195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003197 if( rec->data_len < transform->maclen + padlen + 1 )
3198 {
3199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3200 rec->data_len,
3201 transform->maclen,
3202 padlen + 1 ) );
3203 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003204#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003205
3206 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3207 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003208 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Hanno Becker4c6876b2017-12-27 21:28:58 +00003210 padlen++;
3211
3212 /* Regardless of the validity of the padding,
3213 * we have data_len >= padlen here. */
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003216 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3217 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003219 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#if defined(MBEDTLS_SSL_DEBUG_ALL)
3222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003223 "should be no more than %d",
3224 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003225#endif
Paul Bakker45829992013-01-03 14:52:21 +01003226 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 }
3228 }
3229 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3231#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3232 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003233 if( 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 Becker0a92b812019-06-24 15:46:40 +01003332 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3333 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003334 {
3335 /*
3336 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003337 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003338 *
3339 * Known timing attacks:
3340 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3341 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003342 * To compensate for different timings for the MAC calculation
3343 * depending on how much padding was removed (which is determined
3344 * by padlen), process extra_run more blocks through the hash
3345 * function.
3346 *
3347 * The formula in the paper is
3348 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3349 * where L1 is the size of the header plus the decrypted message
3350 * plus CBC padding and L2 is the size of the header plus the
3351 * decrypted message. This is for an underlying hash function
3352 * with 64-byte blocks.
3353 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3354 * correctly. We round down instead of up, so -56 is the correct
3355 * value for our calculations instead of -55.
3356 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003357 * Repeat the formula rather than defining a block_size variable.
3358 * This avoids requiring division by a variable at runtime
3359 * (which would be marginally less efficient and would require
3360 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003361 */
3362 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003363 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003364
3365 /*
3366 * The next two sizes are the minimum and maximum values of
3367 * in_msglen over all padlen values.
3368 *
3369 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003370 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003371 *
3372 * Note that max_len + maclen is never more than the buffer
3373 * length, as we previously did in_msglen -= maclen too.
3374 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003375 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003376 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3377
Hanno Becker4c6876b2017-12-27 21:28:58 +00003378 memset( tmp, 0, sizeof( tmp ) );
3379
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003380 switch( mbedtls_md_get_type(
3381 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003382 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003383#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3384 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003385 case MBEDTLS_MD_MD5:
3386 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003387 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003388 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003389 extra_run =
3390 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3391 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003392 break;
3393#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003394#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003395 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003396 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003397 extra_run =
3398 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3399 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003400 break;
3401#endif
3402 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3405 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003406
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003407 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003408
Hanno Beckere83efe62019-04-29 13:52:53 +01003409 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3410 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003411 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3412 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003413 /* Make sure we access everything even when padlen > 0. This
3414 * makes the synchronisation requirements for just-in-time
3415 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003416 ssl_read_memory( data + rec->data_len, padlen );
3417 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003418
3419 /* Call mbedtls_md_process at least once due to cache attacks
3420 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003421 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003422 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003423
Hanno Becker4c6876b2017-12-27 21:28:58 +00003424 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003425
3426 /* Make sure we access all the memory that could contain the MAC,
3427 * before we check it in the next code block. This makes the
3428 * synchronisation requirements for just-in-time Prime+Probe
3429 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003430 ssl_read_memory( data + min_len,
3431 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003432 }
3433 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3435 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003441#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003442 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3443 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003444#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003445
Hanno Becker4c6876b2017-12-27 21:28:58 +00003446 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3447 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449#if defined(MBEDTLS_SSL_DEBUG_ALL)
3450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003451#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003452 correct = 0;
3453 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003454 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003455 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003456
3457 /*
3458 * Finally check the correct flag
3459 */
3460 if( correct == 0 )
3461 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003462#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003463
3464 /* Make extra sure authentication was performed, exactly once */
3465 if( auth_done != 1 )
3466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003470
Hanno Beckera5a2b082019-05-15 14:03:01 +01003471#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003472 if( rec->cid_len != 0 )
3473 {
3474 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3475 &rec->type );
3476 if( ret != 0 )
3477 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3478 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003479#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003482
3483 return( 0 );
3484}
3485
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003486#undef MAC_NONE
3487#undef MAC_PLAINTEXT
3488#undef MAC_CIPHERTEXT
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003491/*
3492 * Compression/decompression functions
3493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495{
3496 int ret;
3497 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003498 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003500 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003504 if( len_pre == 0 )
3505 return( 0 );
3506
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507 memcpy( msg_pre, ssl->out_msg, len_pre );
3508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 ssl->out_msglen ) );
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003513 ssl->out_msg, ssl->out_msglen );
3514
Paul Bakker48916f92012-09-16 19:57:18 +00003515 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3516 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3517 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003518 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003519
Paul Bakker48916f92012-09-16 19:57:18 +00003520 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521 if( ret != Z_OK )
3522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3524 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525 }
3526
Angus Grattond8213d02016-05-25 20:56:48 +10003527 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003528 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531 ssl->out_msglen ) );
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003534 ssl->out_msg, ssl->out_msglen );
3535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537
3538 return( 0 );
3539}
3540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003542{
3543 int ret;
3544 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003545 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003546 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003547 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003551 if( len_pre == 0 )
3552 return( 0 );
3553
Paul Bakker2770fbd2012-07-03 13:30:23 +00003554 memcpy( msg_pre, ssl->in_msg, len_pre );
3555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003557 ssl->in_msglen ) );
3558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560 ssl->in_msg, ssl->in_msglen );
3561
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3563 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3564 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003565 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003566 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
Paul Bakker48916f92012-09-16 19:57:18 +00003568 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003569 if( ret != Z_OK )
3570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3572 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003573 }
3574
Angus Grattond8213d02016-05-25 20:56:48 +10003575 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003576 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579 ssl->in_msglen ) );
3580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582 ssl->in_msg, ssl->in_msglen );
3583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003585
3586 return( 0 );
3587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3591static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593#if defined(MBEDTLS_SSL_PROTO_DTLS)
3594static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003595{
3596 /* If renegotiation is not enforced, retransmit until we would reach max
3597 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003598 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003599 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003600 uint32_t ratio =
3601 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3602 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003603 unsigned char doublings = 1;
3604
3605 while( ratio != 0 )
3606 {
3607 ++doublings;
3608 ratio >>= 1;
3609 }
3610
3611 if( ++ssl->renego_records_seen > doublings )
3612 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003614 return( 0 );
3615 }
3616 }
3617
3618 return( ssl_write_hello_request( ssl ) );
3619}
3620#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003622
Paul Bakker5121ce52009-01-03 21:22:43 +00003623/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003624 * Fill the input message buffer by appending data to it.
3625 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003626 *
3627 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3628 * available (from this read and/or a previous one). Otherwise, an error code
3629 * is returned (possibly EOF or WANT_READ).
3630 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003631 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3632 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3633 * since we always read a whole datagram at once.
3634 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003635 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003636 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639{
Paul Bakker23986e52011-04-24 08:57:21 +00003640 int ret;
3641 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
Hanno Beckera58a8962019-06-13 16:11:15 +01003645 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3646 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003649 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003651 }
3652
Angus Grattond8213d02016-05-25 20:56:48 +10003653 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003657 }
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003660 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003662 uint32_t timeout;
3663
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003664 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003665 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3666 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003667 {
3668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3669 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3671 }
3672
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003673 /*
3674 * The point is, we need to always read a full datagram at once, so we
3675 * sometimes read more then requested, and handle the additional data.
3676 * It could be the rest of the current record (while fetching the
3677 * header) and/or some other records in the same datagram.
3678 */
3679
3680 /*
3681 * Move to the next record in the already read datagram if applicable
3682 */
3683 if( ssl->next_record_offset != 0 )
3684 {
3685 if( ssl->in_left < ssl->next_record_offset )
3686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003689 }
3690
3691 ssl->in_left -= ssl->next_record_offset;
3692
3693 if( ssl->in_left != 0 )
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003696 ssl->next_record_offset ) );
3697 memmove( ssl->in_hdr,
3698 ssl->in_hdr + ssl->next_record_offset,
3699 ssl->in_left );
3700 }
3701
3702 ssl->next_record_offset = 0;
3703 }
3704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003707
3708 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003709 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003710 */
3711 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003714 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003715 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003716
3717 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003718 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003719 * are not at the beginning of a new record, the caller did something
3720 * wrong.
3721 */
3722 if( ssl->in_left != 0 )
3723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003726 }
3727
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003728 /*
3729 * Don't even try to read if time's out already.
3730 * This avoids by-passing the timer when repeatedly receiving messages
3731 * that will end up being dropped.
3732 */
3733 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003734 {
3735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003736 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003737 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003738 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003739 {
Angus Grattond8213d02016-05-25 20:56:48 +10003740 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003743 timeout = ssl->handshake->retransmit_timeout;
3744 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003745 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003748
Hanno Beckera58a8962019-06-13 16:11:15 +01003749 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3750 {
3751 ret = mbedtls_ssl_get_recv_timeout( ssl )
3752 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3753 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003754 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003755 {
3756 ret = mbedtls_ssl_get_recv( ssl )
3757 ( ssl->p_bio, ssl->in_hdr, len );
3758 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003761
3762 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003764 }
3765
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003766 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003769 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003772 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003773 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003776 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003777 }
3778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003779 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003782 return( ret );
3783 }
3784
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003785 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003788 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3789 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003791 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003792 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003795 return( ret );
3796 }
3797
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003798 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003799 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003801 }
3802
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 if( ret < 0 )
3804 return( ret );
3805
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003806 ssl->in_left = ret;
3807 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003808 MBEDTLS_SSL_TRANSPORT_ELSE
3809#endif /* MBEDTLS_SSL_PROTO_DTLS */
3810#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003813 ssl->in_left, nb_want ) );
3814
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003815 while( ssl->in_left < nb_want )
3816 {
3817 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003818
3819 if( ssl_check_timer( ssl ) != 0 )
3820 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3821 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003822 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003823 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003824 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003825 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3826 ssl->in_hdr + ssl->in_left, len,
3827 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003828 }
3829 else
3830 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003831 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003832 ssl->in_hdr + ssl->in_left, len );
3833 }
3834 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003837 ssl->in_left, nb_want ) );
3838 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003839
3840 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003842
3843 if( ret < 0 )
3844 return( ret );
3845
mohammad160352aecb92018-03-28 23:41:40 -07003846 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003847 {
Darryl Green11999bb2018-03-13 15:22:58 +00003848 MBEDTLS_SSL_DEBUG_MSG( 1,
3849 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003850 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3852 }
3853
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003854 ssl->in_left += ret;
3855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003857#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
3861 return( 0 );
3862}
3863
3864/*
3865 * Flush any data not yet written
3866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003869 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003870 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003873
Hanno Beckera58a8962019-06-13 16:11:15 +01003874 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003877 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003879 }
3880
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003881 /* Avoid incrementing counter if data is flushed */
3882 if( ssl->out_left == 0 )
3883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003885 return( 0 );
3886 }
3887
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 while( ssl->out_left > 0 )
3889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003891 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Hanno Becker2b1e3542018-08-06 11:19:13 +01003893 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003894 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
3898 if( ret <= 0 )
3899 return( ret );
3900
mohammad160352aecb92018-03-28 23:41:40 -07003901 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003902 {
Darryl Green11999bb2018-03-13 15:22:58 +00003903 MBEDTLS_SSL_DEBUG_MSG( 1,
3904 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003905 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3907 }
3908
Paul Bakker5121ce52009-01-03 21:22:43 +00003909 ssl->out_left -= ret;
3910 }
3911
Hanno Becker2b1e3542018-08-06 11:19:13 +01003912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003913 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003914 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003915 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003916 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003917 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003918#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003919#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003920 {
3921 ssl->out_hdr = ssl->out_buf + 8;
3922 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003923#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003924 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
3928 return( 0 );
3929}
3930
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003931/*
3932 * Functions to handle the DTLS retransmission state machine
3933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003935/*
3936 * Append current handshake message to current outgoing flight
3937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3942 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3943 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003944
3945 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003946 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003947 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003950 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951 }
3952
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003953 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003957 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003958 }
3959
3960 /* Copy current handshake message with headers */
3961 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3962 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003963 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003964 msg->next = NULL;
3965
3966 /* Append to the current flight */
3967 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003968 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003969 else
3970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003972 while( cur->next != NULL )
3973 cur = cur->next;
3974 cur->next = msg;
3975 }
3976
Hanno Becker3b235902018-08-06 09:54:53 +01003977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978 return( 0 );
3979}
3980
3981/*
3982 * Free the current flight of handshake messages
3983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 mbedtls_ssl_flight_item *cur = flight;
3987 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003988
3989 while( cur != NULL )
3990 {
3991 next = cur->next;
3992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 mbedtls_free( cur->p );
3994 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003995
3996 cur = next;
3997 }
3998}
3999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4001static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004002#endif
4003
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004004/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004005 * Swap transform_out and out_ctr with the alternative ones
4006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004010 unsigned char tmp_out_ctr[8];
4011
4012 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004015 return;
4016 }
4017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004019
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004020 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004021 tmp_transform = ssl->transform_out;
4022 ssl->transform_out = ssl->handshake->alt_transform_out;
4023 ssl->handshake->alt_transform_out = tmp_transform;
4024
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004025 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004026 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4027 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004028 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004029
4030 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004031 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4034 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4039 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004040 }
4041 }
4042#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004043}
4044
4045/*
4046 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004047 */
4048int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4049{
4050 int ret = 0;
4051
4052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4053
4054 ret = mbedtls_ssl_flight_transmit( ssl );
4055
4056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4057
4058 return( ret );
4059}
4060
4061/*
4062 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063 *
4064 * Need to remember the current message in case flush_output returns
4065 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004066 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004068int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004069{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004070 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004074 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004076
4077 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004078 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079 ssl_swap_epochs( ssl );
4080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004082 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004083
4084 while( ssl->handshake->cur_msg != NULL )
4085 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004086 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004087 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004088
Hanno Beckere1dcb032018-08-17 16:47:58 +01004089 int const is_finished =
4090 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4091 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4092
Hanno Becker04da1892018-08-14 13:22:10 +01004093 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4094 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4095
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004096 /* Swap epochs before sending Finished: we can't do it after
4097 * sending ChangeCipherSpec, in case write returns WANT_READ.
4098 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004099 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004100 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004102 ssl_swap_epochs( ssl );
4103 }
4104
Hanno Becker67bc7c32018-08-06 11:33:50 +01004105 ret = ssl_get_remaining_payload_in_datagram( ssl );
4106 if( ret < 0 )
4107 return( ret );
4108 max_frag_len = (size_t) ret;
4109
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004110 /* CCS is copied as is, while HS messages may need fragmentation */
4111 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4112 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004113 if( max_frag_len == 0 )
4114 {
4115 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4116 return( ret );
4117
4118 continue;
4119 }
4120
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004121 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004122 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004123 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004124
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004125 /* Update position inside current message */
4126 ssl->handshake->cur_msg_p += cur->len;
4127 }
4128 else
4129 {
4130 const unsigned char * const p = ssl->handshake->cur_msg_p;
4131 const size_t hs_len = cur->len - 12;
4132 const size_t frag_off = p - ( cur->p + 12 );
4133 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004134 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004135
Hanno Beckere1dcb032018-08-17 16:47:58 +01004136 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004137 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004138 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004139 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004140
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4142 return( ret );
4143
4144 continue;
4145 }
4146 max_hs_frag_len = max_frag_len - 12;
4147
4148 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4149 max_hs_frag_len : rem_len;
4150
4151 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004152 {
4153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004154 (unsigned) cur_hs_frag_len,
4155 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004156 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004157
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004158 /* Messages are stored with handshake headers as if not fragmented,
4159 * copy beginning of headers then fill fragmentation fields.
4160 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4161 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004162
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004163 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4164 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4165 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4166
Hanno Becker67bc7c32018-08-06 11:33:50 +01004167 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4168 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4169 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004170
4171 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4172
Hanno Becker3f7b9732018-08-28 09:53:25 +01004173 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004174 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4175 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004176 ssl->out_msgtype = cur->type;
4177
4178 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004179 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004180 }
4181
4182 /* If done with the current message move to the next one if any */
4183 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4184 {
4185 if( cur->next != NULL )
4186 {
4187 ssl->handshake->cur_msg = cur->next;
4188 ssl->handshake->cur_msg_p = cur->next->p + 12;
4189 }
4190 else
4191 {
4192 ssl->handshake->cur_msg = NULL;
4193 ssl->handshake->cur_msg_p = NULL;
4194 }
4195 }
4196
4197 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004198 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004201 return( ret );
4202 }
4203 }
4204
Hanno Becker67bc7c32018-08-06 11:33:50 +01004205 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4206 return( ret );
4207
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004208 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4210 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004211 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004214 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4215 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004216
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004218
4219 return( 0 );
4220}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004221
4222/*
4223 * To be called when the last message of an incoming flight is received.
4224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004226{
4227 /* We won't need to resend that one any more */
4228 ssl_flight_free( ssl->handshake->flight );
4229 ssl->handshake->flight = NULL;
4230 ssl->handshake->cur_msg = NULL;
4231
4232 /* The next incoming flight will start with this msg_seq */
4233 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4234
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004235 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004236 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004237
Hanno Becker0271f962018-08-16 13:23:47 +01004238 /* Clear future message buffering structure. */
4239 ssl_buffering_free( ssl );
4240
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004241 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242 ssl_set_timer( ssl, 0 );
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4245 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004248 }
4249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004251}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004252
4253/*
4254 * To be called when the last message of an outgoing flight is send.
4255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004257{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004258 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004259 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4262 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004265 }
4266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004270
Paul Bakker5121ce52009-01-03 21:22:43 +00004271/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004272 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004274
4275/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004276 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004277 *
4278 * - fill in handshake headers
4279 * - update handshake checksum
4280 * - DTLS: save message for resending
4281 * - then pass to the record layer
4282 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004283 * DTLS: except for HelloRequest, messages are only queued, and will only be
4284 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004285 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004286 * Inputs:
4287 * - ssl->out_msglen: 4 + actual handshake message len
4288 * (4 is the size of handshake headers for TLS)
4289 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4290 * - ssl->out_msg + 4: the handshake message body
4291 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004292 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004293 * - ssl->out_msglen: the length of the record contents
4294 * (including handshake headers but excluding record headers)
4295 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004296 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004297int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004298{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004299 int ret;
4300 const size_t hs_len = ssl->out_msglen - 4;
4301 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004302
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4304
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004305 /*
4306 * Sanity checks
4307 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004308 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004309 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4310 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004311 /* In SSLv3, the client might send a NoCertificate alert. */
4312#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004313 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004314 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004315 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4316 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004317#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4318 {
4319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4321 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004324 /* Whenever we send anything different from a
4325 * HelloRequest we should be in a handshake - double check. */
4326 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4327 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004328 ssl->handshake == NULL )
4329 {
4330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004335 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004336 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004338 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004341 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004342#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004343
Hanno Beckerb50a2532018-08-06 11:52:54 +01004344 /* Double-check that we did not exceed the bounds
4345 * of the outgoing record buffer.
4346 * This should never fail as the various message
4347 * writing functions must obey the bounds of the
4348 * outgoing record buffer, but better be safe.
4349 *
4350 * Note: We deliberately do not check for the MTU or MFL here.
4351 */
4352 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4353 {
4354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4355 "size %u, maximum %u",
4356 (unsigned) ssl->out_msglen,
4357 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4359 }
4360
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004361 /*
4362 * Fill handshake headers
4363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004366 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4367 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4368 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004370 /*
4371 * DTLS has additional fields in the Handshake layer,
4372 * between the length field and the actual payload:
4373 * uint16 message_seq;
4374 * uint24 fragment_offset;
4375 * uint24 fragment_length;
4376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004378 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004379 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004380 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004381 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004382 {
4383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4384 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004385 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004386 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4388 }
4389
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004390 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004391 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004392
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004393 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004394 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004395 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004396 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4397 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4398 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004399 }
4400 else
4401 {
4402 ssl->out_msg[4] = 0;
4403 ssl->out_msg[5] = 0;
4404 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004405
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004406 /* Handshake hashes are computed without fragmentation,
4407 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004408 memset( ssl->out_msg + 6, 0x00, 3 );
4409 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004412
Hanno Becker0207e532018-08-28 10:28:28 +01004413 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004414 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004415 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 }
4417
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004418 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004420 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004421 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4422 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004423 {
4424 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004427 return( ret );
4428 }
4429 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004430 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004431#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004432 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004433 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004434 {
4435 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4436 return( ret );
4437 }
4438 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004439
4440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4441
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004442 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004443}
4444
4445/*
4446 * Record layer functions
4447 */
4448
4449/*
4450 * Write current record.
4451 *
4452 * Uses:
4453 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4454 * - ssl->out_msglen: length of the record content (excl headers)
4455 * - ssl->out_msg: record content
4456 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004457int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004458{
4459 int ret, done = 0;
4460 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004461 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004462
4463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004466 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004468 {
4469 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004472 return( ret );
4473 }
4474
4475 len = ssl->out_msglen;
4476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4480 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 ret = mbedtls_ssl_hw_record_write( ssl );
4485 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4488 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004489 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004490
4491 if( ret == 0 )
4492 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004495 if( !done )
4496 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004497 unsigned i;
4498 size_t protected_record_size;
4499
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004500 /* Skip writing the record content type to after the encryption,
4501 * as it may change when using the CID extension. */
4502
Hanno Becker2881d802019-05-22 14:44:53 +01004503 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4504 mbedtls_ssl_get_minor_ver( ssl ),
4505 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004506
Hanno Becker19859472018-08-06 09:40:20 +01004507 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004508 ssl->out_len[0] = (unsigned char)( len >> 8 );
4509 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004510
Paul Bakker48916f92012-09-16 19:57:18 +00004511 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004512 {
Hanno Becker3307b532017-12-27 21:37:21 +00004513 mbedtls_record rec;
4514
4515 rec.buf = ssl->out_iv;
4516 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4517 ( ssl->out_iv - ssl->out_buf );
4518 rec.data_len = ssl->out_msglen;
4519 rec.data_offset = ssl->out_msg - rec.buf;
4520
4521 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004522 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4523 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004524 ssl->conf->transport, rec.ver );
4525 rec.type = ssl->out_msgtype;
4526
Hanno Beckera5a2b082019-05-15 14:03:01 +01004527#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004528 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004529 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004530#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004531
Hanno Becker611a83b2018-01-03 14:27:32 +00004532 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004533 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004534 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004537 return( ret );
4538 }
4539
Hanno Becker3307b532017-12-27 21:37:21 +00004540 if( rec.data_offset != 0 )
4541 {
4542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4544 }
4545
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004546 /* Update the record content type and CID. */
4547 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004549 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004551 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004552 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4553 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004554 }
4555
Hanno Becker43395762019-05-03 14:46:38 +01004556 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004557
4558#if defined(MBEDTLS_SSL_PROTO_DTLS)
4559 /* In case of DTLS, double-check that we don't exceed
4560 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004561 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004562 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004563 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004564 if( ret < 0 )
4565 return( ret );
4566
4567 if( protected_record_size > (size_t) ret )
4568 {
4569 /* Should never happen */
4570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4571 }
4572 }
4573#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004574
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004575 /* Now write the potentially updated record content type. */
4576 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004579 "version = [%d:%d], msglen = %d",
4580 ssl->out_hdr[0], ssl->out_hdr[1],
4581 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004584 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004585
4586 ssl->out_left += protected_record_size;
4587 ssl->out_hdr += protected_record_size;
4588 ssl_update_out_pointers( ssl, ssl->transform_out );
4589
Hanno Becker04484622018-08-06 09:49:38 +01004590 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4591 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4592 break;
4593
4594 /* The loop goes to its end iff the counter is wrapping */
4595 if( i == ssl_ep_len( ssl ) )
4596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4598 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 }
4601
Hanno Becker67bc7c32018-08-06 11:33:50 +01004602#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004603 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004604 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004605 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004606 size_t remaining;
4607 ret = ssl_get_remaining_payload_in_datagram( ssl );
4608 if( ret < 0 )
4609 {
4610 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4611 ret );
4612 return( ret );
4613 }
4614
4615 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004616 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004617 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004618 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004619 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004620 else
4621 {
Hanno Becker513815a2018-08-20 11:56:09 +01004622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004623 }
4624 }
4625#endif /* MBEDTLS_SSL_PROTO_DTLS */
4626
4627 if( ( flush == SSL_FORCE_FLUSH ) &&
4628 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 return( ret );
4632 }
4633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635
4636 return( 0 );
4637}
4638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004640
4641static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4642{
4643 if( ssl->in_msglen < ssl->in_hslen ||
4644 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4645 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4646 {
4647 return( 1 );
4648 }
4649 return( 0 );
4650}
Hanno Becker44650b72018-08-16 12:51:11 +01004651
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004652static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004653{
4654 return( ( ssl->in_msg[9] << 16 ) |
4655 ( ssl->in_msg[10] << 8 ) |
4656 ssl->in_msg[11] );
4657}
4658
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004659static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004660{
4661 return( ( ssl->in_msg[6] << 16 ) |
4662 ( ssl->in_msg[7] << 8 ) |
4663 ssl->in_msg[8] );
4664}
4665
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004666static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004667{
4668 uint32_t msg_len, frag_off, frag_len;
4669
4670 msg_len = ssl_get_hs_total_len( ssl );
4671 frag_off = ssl_get_hs_frag_off( ssl );
4672 frag_len = ssl_get_hs_frag_len( ssl );
4673
4674 if( frag_off > msg_len )
4675 return( -1 );
4676
4677 if( frag_len > msg_len - frag_off )
4678 return( -1 );
4679
4680 if( frag_len + 12 > ssl->in_msglen )
4681 return( -1 );
4682
4683 return( 0 );
4684}
4685
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004686/*
4687 * Mark bits in bitmask (used for DTLS HS reassembly)
4688 */
4689static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4690{
4691 unsigned int start_bits, end_bits;
4692
4693 start_bits = 8 - ( offset % 8 );
4694 if( start_bits != 8 )
4695 {
4696 size_t first_byte_idx = offset / 8;
4697
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004698 /* Special case */
4699 if( len <= start_bits )
4700 {
4701 for( ; len != 0; len-- )
4702 mask[first_byte_idx] |= 1 << ( start_bits - len );
4703
4704 /* Avoid potential issues with offset or len becoming invalid */
4705 return;
4706 }
4707
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004708 offset += start_bits; /* Now offset % 8 == 0 */
4709 len -= start_bits;
4710
4711 for( ; start_bits != 0; start_bits-- )
4712 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4713 }
4714
4715 end_bits = len % 8;
4716 if( end_bits != 0 )
4717 {
4718 size_t last_byte_idx = ( offset + len ) / 8;
4719
4720 len -= end_bits; /* Now len % 8 == 0 */
4721
4722 for( ; end_bits != 0; end_bits-- )
4723 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4724 }
4725
4726 memset( mask + offset / 8, 0xFF, len / 8 );
4727}
4728
4729/*
4730 * Check that bitmask is full
4731 */
4732static int ssl_bitmask_check( unsigned char *mask, size_t len )
4733{
4734 size_t i;
4735
4736 for( i = 0; i < len / 8; i++ )
4737 if( mask[i] != 0xFF )
4738 return( -1 );
4739
4740 for( i = 0; i < len % 8; i++ )
4741 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4742 return( -1 );
4743
4744 return( 0 );
4745}
4746
Hanno Becker56e205e2018-08-16 09:06:12 +01004747/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004748static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004749 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004750{
Hanno Becker56e205e2018-08-16 09:06:12 +01004751 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004752
Hanno Becker56e205e2018-08-16 09:06:12 +01004753 alloc_len = 12; /* Handshake header */
4754 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004755
Hanno Beckerd07df862018-08-16 09:14:58 +01004756 if( add_bitmap )
4757 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004758
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004759 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004760}
Hanno Becker56e205e2018-08-16 09:06:12 +01004761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004763
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004764static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004765{
4766 return( ( ssl->in_msg[1] << 16 ) |
4767 ( ssl->in_msg[2] << 8 ) |
4768 ssl->in_msg[3] );
4769}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004770
Simon Butcher99000142016-10-13 17:21:01 +01004771int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004776 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004778 }
4779
Hanno Becker12555c62018-08-16 12:47:53 +01004780 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004783 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004784 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004787 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004788 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004789 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004790 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004791
Hanno Becker44650b72018-08-16 12:51:11 +01004792 if( ssl_check_hs_header( ssl ) != 0 )
4793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4795 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4796 }
4797
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004798 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004799 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4800 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4801 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4802 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004803 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004804 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4805 {
4806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4807 recv_msg_seq,
4808 ssl->handshake->in_msg_seq ) );
4809 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4810 }
4811
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004812 /* Retransmit only on last message from previous flight, to avoid
4813 * too many retransmissions.
4814 * Besides, No sane server ever retransmits HelloVerifyRequest */
4815 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004819 "message_seq = %d, start_of_flight = %d",
4820 recv_msg_seq,
4821 ssl->handshake->in_flight_start_seq ) );
4822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004826 return( ret );
4827 }
4828 }
4829 else
4830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004832 "message_seq = %d, expected = %d",
4833 recv_msg_seq,
4834 ssl->handshake->in_msg_seq ) );
4835 }
4836
Hanno Becker90333da2017-10-10 11:27:13 +01004837 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004838 }
4839 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004840
Hanno Becker6d97ef52018-08-16 13:09:04 +01004841 /* Message reassembly is handled alongside buffering of future
4842 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004843 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004844 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004845 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004848 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004849 }
4850 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004851 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004853#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004854 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004855 /* With TLS we don't handle fragmentation (for now) */
4856 if( ssl->in_msglen < ssl->in_hslen )
4857 {
4858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4859 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4860 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004861 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004862#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004863
Simon Butcher99000142016-10-13 17:21:01 +01004864 return( 0 );
4865}
4866
4867void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4868{
Hanno Becker0271f962018-08-16 13:23:47 +01004869 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004870
Hanno Becker0271f962018-08-16 13:23:47 +01004871 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004872 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004873
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004874 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004876 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004877 ssl->handshake != NULL )
4878 {
Hanno Becker0271f962018-08-16 13:23:47 +01004879 unsigned offset;
4880 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004881
Hanno Becker0271f962018-08-16 13:23:47 +01004882 /* Increment handshake sequence number */
4883 hs->in_msg_seq++;
4884
4885 /*
4886 * Clear up handshake buffering and reassembly structure.
4887 */
4888
4889 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004890 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004891
4892 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004893 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4894 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004895 offset++, hs_buf++ )
4896 {
4897 *hs_buf = *(hs_buf + 1);
4898 }
4899
4900 /* Create a fresh last entry */
4901 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004902 }
4903#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004904}
4905
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004906/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004907 * DTLS anti-replay: RFC 6347 4.1.2.6
4908 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004909 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4910 * Bit n is set iff record number in_window_top - n has been seen.
4911 *
4912 * Usually, in_window_top is the last record number seen and the lsb of
4913 * in_window is set. The only exception is the initial state (record number 0
4914 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4917static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004918{
4919 ssl->in_window_top = 0;
4920 ssl->in_window = 0;
4921}
4922
4923static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4924{
4925 return( ( (uint64_t) buf[0] << 40 ) |
4926 ( (uint64_t) buf[1] << 32 ) |
4927 ( (uint64_t) buf[2] << 24 ) |
4928 ( (uint64_t) buf[3] << 16 ) |
4929 ( (uint64_t) buf[4] << 8 ) |
4930 ( (uint64_t) buf[5] ) );
4931}
4932
4933/*
4934 * Return 0 if sequence number is acceptable, -1 otherwise
4935 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004936int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004937{
4938 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4939 uint64_t bit;
4940
Hanno Becker7f376f42019-06-12 16:20:48 +01004941 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4942 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4943 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004944 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004945 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004946
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004947 if( rec_seqnum > ssl->in_window_top )
4948 return( 0 );
4949
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004950 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004951
4952 if( bit >= 64 )
4953 return( -1 );
4954
4955 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4956 return( -1 );
4957
4958 return( 0 );
4959}
4960
4961/*
4962 * Update replay window on new validated record
4963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004965{
4966 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4967
Hanno Becker7f376f42019-06-12 16:20:48 +01004968 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4969 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4970 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004971 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004972 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004973
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974 if( rec_seqnum > ssl->in_window_top )
4975 {
4976 /* Update window_top and the contents of the window */
4977 uint64_t shift = rec_seqnum - ssl->in_window_top;
4978
4979 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004980 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004981 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004982 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004984 ssl->in_window |= 1;
4985 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004986
4987 ssl->in_window_top = rec_seqnum;
4988 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004989 else
4990 {
4991 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004992 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004993
4994 if( bit < 64 ) /* Always true, but be extra sure */
4995 ssl->in_window |= (uint64_t) 1 << bit;
4996 }
4997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004999
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005000#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005001/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005002static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5003
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005004/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005005 * Without any SSL context, check if a datagram looks like a ClientHello with
5006 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005007 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005008 *
5009 * - if cookie is valid, return 0
5010 * - if ClientHello looks superficially valid but cookie is not,
5011 * fill obuf and set olen, then
5012 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5013 * - otherwise return a specific error code
5014 */
5015static int ssl_check_dtls_clihlo_cookie(
5016 mbedtls_ssl_cookie_write_t *f_cookie_write,
5017 mbedtls_ssl_cookie_check_t *f_cookie_check,
5018 void *p_cookie,
5019 const unsigned char *cli_id, size_t cli_id_len,
5020 const unsigned char *in, size_t in_len,
5021 unsigned char *obuf, size_t buf_len, size_t *olen )
5022{
5023 size_t sid_len, cookie_len;
5024 unsigned char *p;
5025
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005026 /*
5027 * Structure of ClientHello with record and handshake headers,
5028 * and expected values. We don't need to check a lot, more checks will be
5029 * done when actually parsing the ClientHello - skipping those checks
5030 * avoids code duplication and does not make cookie forging any easier.
5031 *
5032 * 0-0 ContentType type; copied, must be handshake
5033 * 1-2 ProtocolVersion version; copied
5034 * 3-4 uint16 epoch; copied, must be 0
5035 * 5-10 uint48 sequence_number; copied
5036 * 11-12 uint16 length; (ignored)
5037 *
5038 * 13-13 HandshakeType msg_type; (ignored)
5039 * 14-16 uint24 length; (ignored)
5040 * 17-18 uint16 message_seq; copied
5041 * 19-21 uint24 fragment_offset; copied, must be 0
5042 * 22-24 uint24 fragment_length; (ignored)
5043 *
5044 * 25-26 ProtocolVersion client_version; (ignored)
5045 * 27-58 Random random; (ignored)
5046 * 59-xx SessionID session_id; 1 byte len + sid_len content
5047 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5048 * ...
5049 *
5050 * Minimum length is 61 bytes.
5051 */
5052 if( in_len < 61 ||
5053 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5054 in[3] != 0 || in[4] != 0 ||
5055 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5056 {
5057 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5058 }
5059
5060 sid_len = in[59];
5061 if( sid_len > in_len - 61 )
5062 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5063
5064 cookie_len = in[60 + sid_len];
5065 if( cookie_len > in_len - 60 )
5066 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5067
5068 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5069 cli_id, cli_id_len ) == 0 )
5070 {
5071 /* Valid cookie */
5072 return( 0 );
5073 }
5074
5075 /*
5076 * If we get here, we've got an invalid cookie, let's prepare HVR.
5077 *
5078 * 0-0 ContentType type; copied
5079 * 1-2 ProtocolVersion version; copied
5080 * 3-4 uint16 epoch; copied
5081 * 5-10 uint48 sequence_number; copied
5082 * 11-12 uint16 length; olen - 13
5083 *
5084 * 13-13 HandshakeType msg_type; hello_verify_request
5085 * 14-16 uint24 length; olen - 25
5086 * 17-18 uint16 message_seq; copied
5087 * 19-21 uint24 fragment_offset; copied
5088 * 22-24 uint24 fragment_length; olen - 25
5089 *
5090 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5091 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5092 *
5093 * Minimum length is 28.
5094 */
5095 if( buf_len < 28 )
5096 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5097
5098 /* Copy most fields and adapt others */
5099 memcpy( obuf, in, 25 );
5100 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5101 obuf[25] = 0xfe;
5102 obuf[26] = 0xff;
5103
5104 /* Generate and write actual cookie */
5105 p = obuf + 28;
5106 if( f_cookie_write( p_cookie,
5107 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5108 {
5109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5110 }
5111
5112 *olen = p - obuf;
5113
5114 /* Go back and fill length fields */
5115 obuf[27] = (unsigned char)( *olen - 28 );
5116
5117 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5118 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5119 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5120
5121 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5122 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5123
5124 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5125}
5126
5127/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005128 * Handle possible client reconnect with the same UDP quadruplet
5129 * (RFC 6347 Section 4.2.8).
5130 *
5131 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5132 * that looks like a ClientHello.
5133 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005134 * - if the input looks like a ClientHello without cookies,
5135 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005136 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005137 * - if the input looks like a ClientHello with a valid cookie,
5138 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005139 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005140 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005141 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005142 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005143 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5144 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005145 */
5146static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5147{
5148 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005149 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005150
Hanno Becker87b56262019-07-10 14:37:41 +01005151 if( ssl->conf->f_cookie_write == NULL ||
5152 ssl->conf->f_cookie_check == NULL )
5153 {
5154 /* If we can't use cookies to verify reachability of the peer,
5155 * drop the record. */
5156 return( 0 );
5157 }
5158
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005159 ret = ssl_check_dtls_clihlo_cookie(
5160 ssl->conf->f_cookie_write,
5161 ssl->conf->f_cookie_check,
5162 ssl->conf->p_cookie,
5163 ssl->cli_id, ssl->cli_id_len,
5164 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005165 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005166
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005167 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5168
5169 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005170 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005171 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005172 * If the error is permanent we'll catch it later,
5173 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005174 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005175 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005176 }
5177
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005178 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005179 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005180 /* Got a valid cookie, partially reset context */
5181 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5182 {
5183 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5184 return( ret );
5185 }
5186
5187 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005188 }
5189
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005190 return( ret );
5191}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005192#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005193
Hanno Becker46483f12019-05-03 13:25:54 +01005194static int ssl_check_record_type( uint8_t record_type )
5195{
5196 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5197 record_type != MBEDTLS_SSL_MSG_ALERT &&
5198 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5199 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5200 {
5201 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5202 }
5203
5204 return( 0 );
5205}
5206
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005207/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005208 * ContentType type;
5209 * ProtocolVersion version;
5210 * uint16 epoch; // DTLS only
5211 * uint48 sequence_number; // DTLS only
5212 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005213 *
5214 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005215 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005216 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5217 *
5218 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005219 * 1. proceed with the record if this function returns 0
5220 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5221 * 3. return CLIENT_RECONNECT if this function return that value
5222 * 4. drop the whole datagram if this function returns anything else.
5223 * Point 2 is needed when the peer is resending, and we have already received
5224 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005225 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005226static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005227 unsigned char *buf,
5228 size_t len,
5229 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005230{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005231 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005232
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005233 size_t const rec_hdr_type_offset = 0;
5234 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005235
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005236 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5237 rec_hdr_type_len;
5238 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005240 size_t const rec_hdr_ctr_len = 8;
5241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005242 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005243 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5244 rec_hdr_version_len;
5245
Hanno Beckera5a2b082019-05-15 14:03:01 +01005246#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005247 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5248 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005249 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005250#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5251#endif /* MBEDTLS_SSL_PROTO_DTLS */
5252
5253 size_t rec_hdr_len_offset; /* To be determined */
5254 size_t const rec_hdr_len_len = 2;
5255
5256 /*
5257 * Check minimum lengths for record header.
5258 */
5259
5260#if defined(MBEDTLS_SSL_PROTO_DTLS)
5261 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5262 {
5263 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5264 }
5265 MBEDTLS_SSL_TRANSPORT_ELSE
5266#endif /* MBEDTLS_SSL_PROTO_DTLS */
5267#if defined(MBEDTLS_SSL_PROTO_TLS)
5268 {
5269 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5270 }
5271#endif /* MBEDTLS_SSL_PROTO_DTLS */
5272
5273 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5274 {
5275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5276 (unsigned) len,
5277 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5278 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5279 }
5280
5281 /*
5282 * Parse and validate record content type
5283 */
5284
5285 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005286
5287 /* Check record content type */
5288#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5289 rec->cid_len = 0;
5290
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005291 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005292 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5293 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005294 {
5295 /* Shift pointers to account for record header including CID
5296 * struct {
5297 * ContentType special_type = tls12_cid;
5298 * ProtocolVersion version;
5299 * uint16 epoch;
5300 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005301 * opaque cid[cid_length]; // Additional field compared to
5302 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005303 * uint16 length;
5304 * opaque enc_content[DTLSCiphertext.length];
5305 * } DTLSCiphertext;
5306 */
5307
5308 /* So far, we only support static CID lengths
5309 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005310 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5311 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005312
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005314 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5316 (unsigned) len,
5317 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005318 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005319 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005320
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005321 /* configured CID len is guaranteed at most 255, see
5322 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5323 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005324 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005325 }
5326 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005327#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005328 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 if( ssl_check_record_type( rec->type ) )
5330 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5332 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5334 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005335 }
5336
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005337 /*
5338 * Parse and validate record version
5339 */
5340
Hanno Becker8061c6e2019-07-26 08:07:03 +01005341 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5342 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005343 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5344 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005345 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005346
Hanno Becker2881d802019-05-22 14:44:53 +01005347 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5350 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005351 }
5352
Hanno Beckere965bd32019-06-12 14:04:34 +01005353 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5356 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005357 }
5358
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359 /*
5360 * Parse/Copy record sequence number.
5361 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005362
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005363#if defined(MBEDTLS_SSL_PROTO_DTLS)
5364 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5365 {
5366 /* Copy explicit record sequence number from input buffer. */
5367 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5368 rec_hdr_ctr_len );
5369 }
5370 MBEDTLS_SSL_TRANSPORT_ELSE
5371#endif /* MBEDTLS_SSL_PROTO_DTLS */
5372#if defined(MBEDTLS_SSL_PROTO_TLS)
5373 {
5374 /* Copy implicit record sequence number from SSL context structure. */
5375 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5376 }
5377#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005378
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005379 /*
5380 * Parse record length.
5381 */
5382
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005383 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005384 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5385 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005386 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5387
Hanno Becker8b09b732019-05-08 12:03:28 +01005388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005389 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005390 rec->type,
5391 major_ver, minor_ver, rec->data_len ) );
5392
5393 rec->buf = buf;
5394 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005395
Hanno Beckerec014082019-07-26 08:20:27 +01005396 if( rec->data_len == 0 )
5397 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5398
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005399 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005400 * DTLS-related tests.
5401 * Check epoch before checking length constraint because
5402 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5403 * message gets duplicated before the corresponding Finished message,
5404 * the second ChangeCipherSpec should be discarded because it belongs
5405 * to an old epoch, but not because its length is shorter than
5406 * the minimum record length for packets using the new record transform.
5407 * Note that these two kinds of failures are handled differently,
5408 * as an unexpected record is silently skipped but an invalid
5409 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005410 */
5411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005412 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005413 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005414 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005415
Hanno Beckere0452772019-07-10 17:12:07 +01005416 /* Check that the datagram is large enough to contain a record
5417 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005418 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005419 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5421 (unsigned) len,
5422 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005423 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5424 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005425
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005426 /* Records from other, non-matching epochs are silently discarded.
5427 * (The case of same-port Client reconnects must be considered in
5428 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005429 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005430 {
5431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5432 "expected %d, received %d",
5433 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005434
5435 /* Records from the next epoch are considered for buffering
5436 * (concretely: early Finished messages). */
5437 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5440 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5441 }
5442
Hanno Becker87b56262019-07-10 14:37:41 +01005443 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005444 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005445#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005446 /* For records from the correct epoch, check whether their
5447 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005448 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5451 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5452 }
5453#endif
5454 }
5455#endif /* MBEDTLS_SSL_PROTO_DTLS */
5456
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005457 return( 0 );
5458}
Paul Bakker5121ce52009-01-03 21:22:43 +00005459
Hanno Becker87b56262019-07-10 14:37:41 +01005460
5461#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5462static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5463{
5464 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5465
5466 /*
5467 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5468 * access the first byte of record content (handshake type), as we
5469 * have an active transform (possibly iv_len != 0), so use the
5470 * fact that the record header len is 13 instead.
5471 */
5472 if( rec_epoch == 0 &&
5473 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5474 MBEDTLS_SSL_IS_SERVER &&
5475 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5476 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5477 ssl->in_left > 13 &&
5478 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5479 {
5480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5481 "from the same port" ) );
5482 return( ssl_handle_possible_reconnect( ssl ) );
5483 }
5484
5485 return( 0 );
5486}
5487#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5488
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005489/*
5490 * If applicable, decrypt (and decompress) record content
5491 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005492static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5493 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005494{
5495 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005498 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5501 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 ret = mbedtls_ssl_hw_record_read( ssl );
5506 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5509 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005510 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005511
5512 if( ret == 0 )
5513 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005516 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005518 unsigned char const old_msg_type = rec->type;
5519
Hanno Becker611a83b2018-01-03 14:27:32 +00005520 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005521 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005524
Hanno Beckera5a2b082019-05-15 14:03:01 +01005525#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005526 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005527 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5528 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005529 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005531 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005532 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005533#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005534
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 return( ret );
5536 }
5537
Hanno Becker106f3da2019-07-12 09:35:58 +01005538 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005539 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005540 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005541 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005545 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005546
Hanno Beckera5a2b082019-05-15 14:03:01 +01005547#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005548 /* We have already checked the record content type
5549 * in ssl_parse_record_header(), failing or silently
5550 * dropping the record in the case of an unknown type.
5551 *
5552 * Since with the use of CIDs, the record content type
5553 * might change during decryption, re-check the record
5554 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005555 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5559 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005560#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005561
Hanno Becker106f3da2019-07-12 09:35:58 +01005562 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005563 {
5564#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005565 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005566 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005567 {
5568 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5570 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5571 }
5572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5573
5574 ssl->nb_zero++;
5575
5576 /*
5577 * Three or more empty messages may be a DoS attack
5578 * (excessive CPU consumption).
5579 */
5580 if( ssl->nb_zero > 3 )
5581 {
5582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005583 "messages, possible DoS attack" ) );
5584 /* Treat the records as if they were not properly authenticated,
5585 * thereby failing the connection if we see more than allowed
5586 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005587 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5588 }
5589 }
5590 else
5591 ssl->nb_zero = 0;
5592
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005593 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5594#if defined(MBEDTLS_SSL_PROTO_TLS)
5595 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005596 {
5597 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005598 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005599 if( ++ssl->in_ctr[i - 1] != 0 )
5600 break;
5601
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005602 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005603 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005604 {
5605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5606 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5607 }
5608 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005609#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005610
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 }
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005614 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005616 {
5617 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005620 return( ret );
5621 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005626 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005629 }
5630#endif
5631
Hanno Beckerf0242852019-07-09 17:30:02 +01005632 /* Check actual (decrypted) record content length against
5633 * configured maximum. */
5634 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5635 {
5636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5637 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5638 }
5639
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005640 return( 0 );
5641}
5642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005644
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005645/*
5646 * Read a record.
5647 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005648 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5649 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5650 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005651 */
Hanno Becker1097b342018-08-15 14:09:41 +01005652
5653/* Helper functions for mbedtls_ssl_read_record(). */
5654static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005655static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5656static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005657
Hanno Becker327c93b2018-08-15 13:56:18 +01005658int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005659 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005660{
5661 int ret;
5662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005664
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005665 if( ssl->keep_current_message == 0 )
5666 {
5667 do {
Simon Butcher99000142016-10-13 17:21:01 +01005668
Hanno Becker26994592018-08-15 14:14:59 +01005669 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005670 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005671 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005672
Hanno Beckere74d5562018-08-15 14:26:08 +01005673 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005674 {
Hanno Becker40f50842018-08-15 14:48:01 +01005675#if defined(MBEDTLS_SSL_PROTO_DTLS)
5676 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005677
Hanno Becker40f50842018-08-15 14:48:01 +01005678 /* We only check for buffered messages if the
5679 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005680 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005681 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005682 {
Hanno Becker40f50842018-08-15 14:48:01 +01005683 if( ssl_load_buffered_message( ssl ) == 0 )
5684 have_buffered = 1;
5685 }
5686
5687 if( have_buffered == 0 )
5688#endif /* MBEDTLS_SSL_PROTO_DTLS */
5689 {
5690 ret = ssl_get_next_record( ssl );
5691 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5692 continue;
5693
5694 if( ret != 0 )
5695 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005696 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005697 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005698 return( ret );
5699 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005700 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005701 }
5702
5703 ret = mbedtls_ssl_handle_message_type( ssl );
5704
Hanno Becker40f50842018-08-15 14:48:01 +01005705#if defined(MBEDTLS_SSL_PROTO_DTLS)
5706 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5707 {
5708 /* Buffer future message */
5709 ret = ssl_buffer_message( ssl );
5710 if( ret != 0 )
5711 return( ret );
5712
5713 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5714 }
5715#endif /* MBEDTLS_SSL_PROTO_DTLS */
5716
Hanno Becker90333da2017-10-10 11:27:13 +01005717 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5718 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005719
5720 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005721 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005722 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005723 return( ret );
5724 }
5725
Hanno Becker327c93b2018-08-15 13:56:18 +01005726 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005727 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005728 {
5729 mbedtls_ssl_update_handshake_status( ssl );
5730 }
Simon Butcher99000142016-10-13 17:21:01 +01005731 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005732 else
Simon Butcher99000142016-10-13 17:21:01 +01005733 {
Hanno Becker02f59072018-08-15 14:00:24 +01005734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005735 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005736 }
5737
5738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5739
5740 return( 0 );
5741}
5742
Hanno Becker40f50842018-08-15 14:48:01 +01005743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005744static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005745{
Hanno Becker40f50842018-08-15 14:48:01 +01005746 if( ssl->in_left > ssl->next_record_offset )
5747 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005748
Hanno Becker40f50842018-08-15 14:48:01 +01005749 return( 0 );
5750}
5751
5752static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5753{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005754 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005755 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005756 int ret = 0;
5757
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005758 if( hs == NULL )
5759 return( -1 );
5760
Hanno Beckere00ae372018-08-20 09:39:42 +01005761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5762
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005763 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5764 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5765 {
5766 /* Check if we have seen a ChangeCipherSpec before.
5767 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005768 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005769 {
5770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5771 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005772 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005773 }
5774
Hanno Becker39b8bc92018-08-28 17:17:13 +01005775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005776 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5777 ssl->in_msglen = 1;
5778 ssl->in_msg[0] = 1;
5779
5780 /* As long as they are equal, the exact value doesn't matter. */
5781 ssl->in_left = 0;
5782 ssl->next_record_offset = 0;
5783
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005784 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005785 goto exit;
5786 }
Hanno Becker37f95322018-08-16 13:55:32 +01005787
Hanno Beckerb8f50142018-08-28 10:01:34 +01005788#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005789 /* Debug only */
5790 {
5791 unsigned offset;
5792 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5793 {
5794 hs_buf = &hs->buffering.hs[offset];
5795 if( hs_buf->is_valid == 1 )
5796 {
5797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5798 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005799 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005800 }
5801 }
5802 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005803#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005804
5805 /* Check if we have buffered and/or fully reassembled the
5806 * next handshake message. */
5807 hs_buf = &hs->buffering.hs[0];
5808 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5809 {
5810 /* Synthesize a record containing the buffered HS message. */
5811 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5812 ( hs_buf->data[2] << 8 ) |
5813 hs_buf->data[3];
5814
5815 /* Double-check that we haven't accidentally buffered
5816 * a message that doesn't fit into the input buffer. */
5817 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5821 }
5822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5824 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5825 hs_buf->data, msg_len + 12 );
5826
5827 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5828 ssl->in_hslen = msg_len + 12;
5829 ssl->in_msglen = msg_len + 12;
5830 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5831
5832 ret = 0;
5833 goto exit;
5834 }
5835 else
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5838 hs->in_msg_seq ) );
5839 }
5840
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841 ret = -1;
5842
5843exit:
5844
5845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5846 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005847}
5848
Hanno Beckera02b0b42018-08-21 17:20:27 +01005849static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5850 size_t desired )
5851{
5852 int offset;
5853 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5855 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005856
Hanno Becker01315ea2018-08-21 17:22:17 +01005857 /* Get rid of future records epoch first, if such exist. */
5858 ssl_free_buffered_record( ssl );
5859
5860 /* Check if we have enough space available now. */
5861 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5862 hs->buffering.total_bytes_buffered ) )
5863 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005865 return( 0 );
5866 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005867
Hanno Becker4f432ad2018-08-28 10:02:32 +01005868 /* We don't have enough space to buffer the next expected handshake
5869 * message. Remove buffers used for future messages to gain space,
5870 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005871 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5872 offset >= 0; offset-- )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5875 offset ) );
5876
Hanno Beckerb309b922018-08-23 13:18:05 +01005877 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005878
5879 /* Check if we have enough space available now. */
5880 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5881 hs->buffering.total_bytes_buffered ) )
5882 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005884 return( 0 );
5885 }
5886 }
5887
5888 return( -1 );
5889}
5890
Hanno Becker40f50842018-08-15 14:48:01 +01005891static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5892{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005893 int ret = 0;
5894 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5895
5896 if( hs == NULL )
5897 return( 0 );
5898
5899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5900
5901 switch( ssl->in_msgtype )
5902 {
5903 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005905
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005906 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005907 break;
5908
5909 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005910 {
5911 unsigned recv_msg_seq_offset;
5912 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5913 mbedtls_ssl_hs_buffer *hs_buf;
5914 size_t msg_len = ssl->in_hslen - 12;
5915
5916 /* We should never receive an old handshake
5917 * message - double-check nonetheless. */
5918 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5919 {
5920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5922 }
5923
5924 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5925 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5926 {
5927 /* Silently ignore -- message too far in the future */
5928 MBEDTLS_SSL_DEBUG_MSG( 2,
5929 ( "Ignore future HS message with sequence number %u, "
5930 "buffering window %u - %u",
5931 recv_msg_seq, ssl->handshake->in_msg_seq,
5932 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5933
5934 goto exit;
5935 }
5936
5937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5938 recv_msg_seq, recv_msg_seq_offset ) );
5939
5940 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5941
5942 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005943 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005944 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005945 size_t reassembly_buf_sz;
5946
Hanno Becker37f95322018-08-16 13:55:32 +01005947 hs_buf->is_fragmented =
5948 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5949
5950 /* We copy the message back into the input buffer
5951 * after reassembly, so check that it's not too large.
5952 * This is an implementation-specific limitation
5953 * and not one from the standard, hence it is not
5954 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005955 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005956 {
5957 /* Ignore message */
5958 goto exit;
5959 }
5960
Hanno Beckere0b150f2018-08-21 15:51:03 +01005961 /* Check if we have enough space to buffer the message. */
5962 if( hs->buffering.total_bytes_buffered >
5963 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5964 {
5965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5967 }
5968
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005969 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5970 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005971
5972 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5973 hs->buffering.total_bytes_buffered ) )
5974 {
5975 if( recv_msg_seq_offset > 0 )
5976 {
5977 /* If we can't buffer a future message because
5978 * of space limitations -- ignore. */
5979 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",
5980 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5981 (unsigned) hs->buffering.total_bytes_buffered ) );
5982 goto exit;
5983 }
Hanno Beckere1801392018-08-21 16:51:05 +01005984 else
5985 {
5986 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",
5987 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5988 (unsigned) hs->buffering.total_bytes_buffered ) );
5989 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005990
Hanno Beckera02b0b42018-08-21 17:20:27 +01005991 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005992 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005993 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",
5994 (unsigned) msg_len,
5995 (unsigned) reassembly_buf_sz,
5996 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005997 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005998 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5999 goto exit;
6000 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006001 }
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6004 msg_len ) );
6005
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006006 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6007 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006008 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006009 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006010 goto exit;
6011 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006012 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006013
6014 /* Prepare final header: copy msg_type, length and message_seq,
6015 * then add standardised fragment_offset and fragment_length */
6016 memcpy( hs_buf->data, ssl->in_msg, 6 );
6017 memset( hs_buf->data + 6, 0, 3 );
6018 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
6019
6020 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006021
6022 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006023 }
6024 else
6025 {
6026 /* Make sure msg_type and length are consistent */
6027 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
6028 {
6029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6030 /* Ignore */
6031 goto exit;
6032 }
6033 }
6034
Hanno Becker4422bbb2018-08-20 09:40:19 +01006035 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006036 {
6037 size_t frag_len, frag_off;
6038 unsigned char * const msg = hs_buf->data + 12;
6039
6040 /*
6041 * Check and copy current fragment
6042 */
6043
6044 /* Validation of header fields already done in
6045 * mbedtls_ssl_prepare_handshake_record(). */
6046 frag_off = ssl_get_hs_frag_off( ssl );
6047 frag_len = ssl_get_hs_frag_len( ssl );
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6050 frag_off, frag_len ) );
6051 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6052
6053 if( hs_buf->is_fragmented )
6054 {
6055 unsigned char * const bitmask = msg + msg_len;
6056 ssl_bitmask_set( bitmask, frag_off, frag_len );
6057 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6058 msg_len ) == 0 );
6059 }
6060 else
6061 {
6062 hs_buf->is_complete = 1;
6063 }
6064
6065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6066 hs_buf->is_complete ? "" : "not yet " ) );
6067 }
6068
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006069 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006070 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006071
6072 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006073 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006074 break;
6075 }
6076
6077exit:
6078
6079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6080 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006081}
6082#endif /* MBEDTLS_SSL_PROTO_DTLS */
6083
Hanno Becker1097b342018-08-15 14:09:41 +01006084static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006085{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006086 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006087 * Consume last content-layer message and potentially
6088 * update in_msglen which keeps track of the contents'
6089 * consumption state.
6090 *
6091 * (1) Handshake messages:
6092 * Remove last handshake message, move content
6093 * and adapt in_msglen.
6094 *
6095 * (2) Alert messages:
6096 * Consume whole record content, in_msglen = 0.
6097 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006098 * (3) Change cipher spec:
6099 * Consume whole record content, in_msglen = 0.
6100 *
6101 * (4) Application data:
6102 * Don't do anything - the record layer provides
6103 * the application data as a stream transport
6104 * and consumes through mbedtls_ssl_read only.
6105 *
6106 */
6107
6108 /* Case (1): Handshake messages */
6109 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006110 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006111 /* Hard assertion to be sure that no application data
6112 * is in flight, as corrupting ssl->in_msglen during
6113 * ssl->in_offt != NULL is fatal. */
6114 if( ssl->in_offt != NULL )
6115 {
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6118 }
6119
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006120 /*
6121 * Get next Handshake message in the current record
6122 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006123
Hanno Becker4a810fb2017-05-24 16:27:30 +01006124 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006125 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006126 * current handshake content: If DTLS handshake
6127 * fragmentation is used, that's the fragment
6128 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006129 * size here is faulty and should be changed at
6130 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006131 * (2) While it doesn't seem to cause problems, one
6132 * has to be very careful not to assume that in_hslen
6133 * is always <= in_msglen in a sensible communication.
6134 * Again, it's wrong for DTLS handshake fragmentation.
6135 * The following check is therefore mandatory, and
6136 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006137 * Additionally, ssl->in_hslen might be arbitrarily out of
6138 * bounds after handling a DTLS message with an unexpected
6139 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006140 */
6141 if( ssl->in_hslen < ssl->in_msglen )
6142 {
6143 ssl->in_msglen -= ssl->in_hslen;
6144 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6145 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006146
Hanno Becker4a810fb2017-05-24 16:27:30 +01006147 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6148 ssl->in_msg, ssl->in_msglen );
6149 }
6150 else
6151 {
6152 ssl->in_msglen = 0;
6153 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006154
Hanno Becker4a810fb2017-05-24 16:27:30 +01006155 ssl->in_hslen = 0;
6156 }
6157 /* Case (4): Application data */
6158 else if( ssl->in_offt != NULL )
6159 {
6160 return( 0 );
6161 }
6162 /* Everything else (CCS & Alerts) */
6163 else
6164 {
6165 ssl->in_msglen = 0;
6166 }
6167
Hanno Becker1097b342018-08-15 14:09:41 +01006168 return( 0 );
6169}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006170
Hanno Beckere74d5562018-08-15 14:26:08 +01006171static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6172{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006173 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006174 return( 1 );
6175
6176 return( 0 );
6177}
6178
Hanno Becker5f066e72018-08-16 14:56:31 +01006179#if defined(MBEDTLS_SSL_PROTO_DTLS)
6180
6181static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6182{
6183 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6184 if( hs == NULL )
6185 return;
6186
Hanno Becker01315ea2018-08-21 17:22:17 +01006187 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006188 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006189 hs->buffering.total_bytes_buffered -=
6190 hs->buffering.future_record.len;
6191
6192 mbedtls_free( hs->buffering.future_record.data );
6193 hs->buffering.future_record.data = NULL;
6194 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006195}
6196
6197static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6198{
6199 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6200 unsigned char * rec;
6201 size_t rec_len;
6202 unsigned rec_epoch;
6203
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006204 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006205 return( 0 );
6206
6207 if( hs == NULL )
6208 return( 0 );
6209
Hanno Becker5f066e72018-08-16 14:56:31 +01006210 rec = hs->buffering.future_record.data;
6211 rec_len = hs->buffering.future_record.len;
6212 rec_epoch = hs->buffering.future_record.epoch;
6213
6214 if( rec == NULL )
6215 return( 0 );
6216
Hanno Becker4cb782d2018-08-20 11:19:05 +01006217 /* Only consider loading future records if the
6218 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006219 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006220 return( 0 );
6221
Hanno Becker5f066e72018-08-16 14:56:31 +01006222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6223
6224 if( rec_epoch != ssl->in_epoch )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6227 goto exit;
6228 }
6229
6230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6231
6232 /* Double-check that the record is not too large */
6233 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6234 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6235 {
6236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6238 }
6239
6240 memcpy( ssl->in_hdr, rec, rec_len );
6241 ssl->in_left = rec_len;
6242 ssl->next_record_offset = 0;
6243
6244 ssl_free_buffered_record( ssl );
6245
6246exit:
6247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6248 return( 0 );
6249}
6250
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006251static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6252 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006253{
6254 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006255
6256 /* Don't buffer future records outside handshakes. */
6257 if( hs == NULL )
6258 return( 0 );
6259
6260 /* Only buffer handshake records (we are only interested
6261 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006262 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006263 return( 0 );
6264
6265 /* Don't buffer more than one future epoch record. */
6266 if( hs->buffering.future_record.data != NULL )
6267 return( 0 );
6268
Hanno Becker01315ea2018-08-21 17:22:17 +01006269 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006270 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006271 hs->buffering.total_bytes_buffered ) )
6272 {
6273 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 +01006274 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006275 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006276 return( 0 );
6277 }
6278
Hanno Becker5f066e72018-08-16 14:56:31 +01006279 /* Buffer record */
6280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6281 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006282 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006283
6284 /* ssl_parse_record_header() only considers records
6285 * of the next epoch as candidates for buffering. */
6286 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006287 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006288
6289 hs->buffering.future_record.data =
6290 mbedtls_calloc( 1, hs->buffering.future_record.len );
6291 if( hs->buffering.future_record.data == NULL )
6292 {
6293 /* If we run out of RAM trying to buffer a
6294 * record from the next epoch, just ignore. */
6295 return( 0 );
6296 }
6297
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006298 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006299
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006300 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006301 return( 0 );
6302}
6303
6304#endif /* MBEDTLS_SSL_PROTO_DTLS */
6305
Hanno Beckere74d5562018-08-15 14:26:08 +01006306static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006307{
6308 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006309 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006310
Hanno Becker5f066e72018-08-16 14:56:31 +01006311#if defined(MBEDTLS_SSL_PROTO_DTLS)
6312 /* We might have buffered a future record; if so,
6313 * and if the epoch matches now, load it.
6314 * On success, this call will set ssl->in_left to
6315 * the length of the buffered record, so that
6316 * the calls to ssl_fetch_input() below will
6317 * essentially be no-ops. */
6318 ret = ssl_load_buffered_record( ssl );
6319 if( ret != 0 )
6320 return( ret );
6321#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006322
Hanno Becker8b09b732019-05-08 12:03:28 +01006323 /* Ensure that we have enough space available for the default form
6324 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6325 * with no space for CIDs counted in). */
6326 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6327 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006330 return( ret );
6331 }
6332
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006333 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6334 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006337 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006338 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006339 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6340 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006341 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006342 if( ret != 0 )
6343 return( ret );
6344
6345 /* Fall through to handling of unexpected records */
6346 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6347 }
6348
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006349 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6350 {
Hanno Becker87b56262019-07-10 14:37:41 +01006351#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006352 /* Reset in pointers to default state for TLS/DTLS records,
6353 * assuming no CID and no offset between record content and
6354 * record plaintext. */
6355 ssl_update_in_pointers( ssl );
6356
Hanno Becker69412452019-07-12 08:33:49 +01006357 /* Setup internal message pointers from record structure. */
6358 ssl->in_msgtype = rec.type;
6359#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6360 ssl->in_len = ssl->in_cid + rec.cid_len;
6361#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006362 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006363 ssl->in_msglen = rec.data_len;
6364
Hanno Becker87b56262019-07-10 14:37:41 +01006365 ret = ssl_check_client_reconnect( ssl );
6366 if( ret != 0 )
6367 return( ret );
6368#endif
6369
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006370 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006371 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006372
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6374 "(header)" ) );
6375 }
6376 else
6377 {
6378 /* Skip invalid record and the rest of the datagram */
6379 ssl->next_record_offset = 0;
6380 ssl->in_left = 0;
6381
6382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6383 "(header)" ) );
6384 }
6385
6386 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006387 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006388 }
Hanno Becker87b56262019-07-10 14:37:41 +01006389 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006390#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006391 {
6392 return( ret );
6393 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006394 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006397 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006398 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006399 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006400 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006401 if( ssl->next_record_offset < ssl->in_left )
6402 {
6403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6404 }
6405 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006406 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006407#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006408#if defined(MBEDTLS_SSL_PROTO_TLS)
6409 {
Hanno Beckere0452772019-07-10 17:12:07 +01006410 /*
6411 * Fetch record contents from underlying transport.
6412 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006413 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006414 if( ret != 0 )
6415 {
6416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6417 return( ret );
6418 }
6419
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006420 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006421 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006422#endif /* MBEDTLS_SSL_PROTO_TLS */
6423
6424 /*
6425 * Decrypt record contents.
6426 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006427
Hanno Beckera89610a2019-07-11 13:07:45 +01006428 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006431 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006432 {
6433 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006434 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006435 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006436 /* Except when waiting for Finished as a bad mac here
6437 * probably means something went wrong in the handshake
6438 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6439 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6440 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6441 {
6442#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6443 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6444 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006445 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006446 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6447 }
6448#endif
6449 return( ret );
6450 }
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006453 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6454 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6457 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006458 }
6459#endif
6460
Hanno Becker4a810fb2017-05-24 16:27:30 +01006461 /* As above, invalid records cause
6462 * dismissal of the whole datagram. */
6463
6464 ssl->next_record_offset = 0;
6465 ssl->in_left = 0;
6466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006468 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006469 }
6470
6471 return( ret );
6472 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006473 MBEDTLS_SSL_TRANSPORT_ELSE
6474#endif /* MBEDTLS_SSL_PROTO_DTLS */
6475#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006476 {
6477 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6479 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006480 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006481 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006483 }
6484#endif
6485 return( ret );
6486 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006487#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006488 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006489
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006490
6491 /* Reset in pointers to default state for TLS/DTLS records,
6492 * assuming no CID and no offset between record content and
6493 * record plaintext. */
6494 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006495#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6496 ssl->in_len = ssl->in_cid + rec.cid_len;
6497#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006498 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006499
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006500 /* The record content type may change during decryption,
6501 * so re-read it. */
6502 ssl->in_msgtype = rec.type;
6503 /* Also update the input buffer, because unfortunately
6504 * the server-side ssl_parse_client_hello() reparses the
6505 * record header when receiving a ClientHello initiating
6506 * a renegotiation. */
6507 ssl->in_hdr[0] = rec.type;
6508 ssl->in_msg = rec.buf + rec.data_offset;
6509 ssl->in_msglen = rec.data_len;
6510 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6511 ssl->in_len[1] = (unsigned char)( rec.data_len );
6512
Simon Butcher99000142016-10-13 17:21:01 +01006513 return( 0 );
6514}
6515
6516int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6517{
6518 int ret;
6519
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006520 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006521 * Handle particular types of records
6522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006524 {
Simon Butcher99000142016-10-13 17:21:01 +01006525 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6526 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006527 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 }
6530
Hanno Beckere678eaa2018-08-21 14:57:46 +01006531 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006532 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006533 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006534 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6536 ssl->in_msglen ) );
6537 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006538 }
6539
Hanno Beckere678eaa2018-08-21 14:57:46 +01006540 if( ssl->in_msg[0] != 1 )
6541 {
6542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6543 ssl->in_msg[0] ) );
6544 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6545 }
6546
6547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006548 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006549 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6550 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6551 {
6552 if( ssl->handshake == NULL )
6553 {
6554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6555 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6556 }
6557
6558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6559 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6560 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006561#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006562 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006566 if( ssl->in_msglen != 2 )
6567 {
6568 /* Note: Standard allows for more than one 2 byte alert
6569 to be packed in a single message, but Mbed TLS doesn't
6570 currently support this. */
6571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6572 ssl->in_msglen ) );
6573 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6574 }
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 ssl->in_msg[0], ssl->in_msg[1] ) );
6578
6579 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006580 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006585 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 }
6588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6590 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6593 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006595
6596#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6597 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6598 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6599 {
Hanno Becker90333da2017-10-10 11:27:13 +01006600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006601 /* Will be handled when trying to parse ServerHello */
6602 return( 0 );
6603 }
6604#endif
6605
6606#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006607 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006608 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6609 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006610 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6611 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6612 {
6613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6614 /* Will be handled in mbedtls_ssl_parse_certificate() */
6615 return( 0 );
6616 }
6617#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6618
6619 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006620 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 }
6622
Hanno Beckerc76c6192017-06-06 10:03:17 +01006623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006624 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006625 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006626 /* Drop unexpected ApplicationData records,
6627 * except at the beginning of renegotiations */
6628 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6629 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6630#if defined(MBEDTLS_SSL_RENEGOTIATION)
6631 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6632 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006633#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006634 )
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6637 return( MBEDTLS_ERR_SSL_NON_FATAL );
6638 }
6639
6640 if( ssl->handshake != NULL &&
6641 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6642 {
6643 ssl_handshake_wrapup_free_hs_transform( ssl );
6644 }
6645 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006646#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006647
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 return( 0 );
6649}
6650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006652{
6653 int ret;
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6656 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6657 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006658 {
6659 return( ret );
6660 }
6661
6662 return( 0 );
6663}
6664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006666 unsigned char level,
6667 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006668{
6669 int ret;
6670
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006671 if( ssl == NULL || ssl->conf == NULL )
6672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006678 ssl->out_msglen = 2;
6679 ssl->out_msg[0] = level;
6680 ssl->out_msg[1] = message;
6681
Hanno Becker67bc7c32018-08-06 11:33:50 +01006682 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006685 return( ret );
6686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006688
6689 return( 0 );
6690}
6691
Hanno Becker17572472019-02-08 07:19:04 +00006692#if defined(MBEDTLS_X509_CRT_PARSE_C)
6693static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6694{
6695#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6696 if( session->peer_cert != NULL )
6697 {
6698 mbedtls_x509_crt_free( session->peer_cert );
6699 mbedtls_free( session->peer_cert );
6700 session->peer_cert = NULL;
6701 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006702#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006703 if( session->peer_cert_digest != NULL )
6704 {
6705 /* Zeroization is not necessary. */
6706 mbedtls_free( session->peer_cert_digest );
6707 session->peer_cert_digest = NULL;
6708 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6709 session->peer_cert_digest_len = 0;
6710 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006711#else
6712 ((void) session);
6713#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006714}
6715#endif /* MBEDTLS_X509_CRT_PARSE_C */
6716
Paul Bakker5121ce52009-01-03 21:22:43 +00006717/*
6718 * Handshake functions
6719 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006720#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006721/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006723{
Hanno Beckerdf645962019-06-26 13:02:22 +01006724 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6725 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006728
Hanno Becker5097cba2019-02-05 13:36:46 +00006729 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006732 ssl->state++;
6733 return( 0 );
6734 }
6735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738}
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006741{
Hanno Beckerdf645962019-06-26 13:02:22 +01006742 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6743 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006746
Hanno Becker5097cba2019-02-05 13:36:46 +00006747 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006750 ssl->state++;
6751 return( 0 );
6752 }
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006756}
Gilles Peskinef9828522017-05-03 12:28:43 +02006757
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006758#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006759/* Some certificate support -> implement write and parse */
6760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006764 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006766 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6767 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006770
Hanno Becker5097cba2019-02-05 13:36:46 +00006771 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006774 ssl->state++;
6775 return( 0 );
6776 }
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006779 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6780 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 {
6782 if( ssl->client_auth == 0 )
6783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 ssl->state++;
6786 return( 0 );
6787 }
6788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 /*
6791 * If using SSLv3 and got no cert, send an Alert message
6792 * (otherwise an empty Certificate message will be sent).
6793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006795 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 {
6797 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6799 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6800 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 goto write_msg;
6804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006806 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807#endif /* MBEDTLS_SSL_CLI_C */
6808#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006809 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6814 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 }
6816 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
6821 /*
6822 * 0 . 0 handshake type
6823 * 1 . 3 handshake length
6824 * 4 . 6 length of all certs
6825 * 7 . 9 length of cert. 1
6826 * 10 . n-1 peer certificate
6827 * n . n+2 length of cert. 2
6828 * n+3 . ... upper level cert, etc.
6829 */
6830 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
Paul Bakker29087132010-03-21 21:03:34 +00006833 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 {
6835 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006836 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006839 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 }
6842
6843 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6844 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6845 ssl->out_msg[i + 2] = (unsigned char)( n );
6846
6847 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6848 i += n; crt = crt->next;
6849 }
6850
6851 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6852 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6853 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6854
6855 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6857 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006859#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006860write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
6863 ssl->state++;
6864
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006865 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006866 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006868 return( ret );
6869 }
6870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006872
Paul Bakkered27a042013-04-18 22:46:23 +02006873 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006874}
6875
Hanno Becker285ff0c2019-01-31 07:44:03 +00006876#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006877
6878#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006879static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6880 unsigned char *crt_buf,
6881 size_t crt_buf_len )
6882{
6883 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6884
6885 if( peer_crt == NULL )
6886 return( -1 );
6887
6888 if( peer_crt->raw.len != crt_buf_len )
6889 return( -1 );
6890
Hanno Becker68b856d2019-02-08 14:00:04 +00006891 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006892}
Hanno Becker5882dd02019-06-06 16:25:57 +01006893#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006894static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6895 unsigned char *crt_buf,
6896 size_t crt_buf_len )
6897{
6898 int ret;
6899 unsigned char const * const peer_cert_digest =
6900 ssl->session->peer_cert_digest;
6901 mbedtls_md_type_t const peer_cert_digest_type =
6902 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006903 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00006904 mbedtls_md_info_from_type( peer_cert_digest_type );
6905 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6906 size_t digest_len;
6907
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006908 if( peer_cert_digest == NULL ||
6909 digest_info == MBEDTLS_MD_INVALID_HANDLE )
6910 {
Hanno Beckerdf759382019-02-05 17:02:46 +00006911 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006912 }
Hanno Beckerdf759382019-02-05 17:02:46 +00006913
6914 digest_len = mbedtls_md_get_size( digest_info );
6915 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6916 return( -1 );
6917
6918 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6919 if( ret != 0 )
6920 return( -1 );
6921
6922 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6923}
Hanno Becker5882dd02019-06-06 16:25:57 +01006924#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006925#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006926
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006927/*
6928 * Once the certificate message is read, parse it into a cert chain and
6929 * perform basic checks, but leave actual verification to the caller
6930 */
Hanno Becker35e41772019-02-05 15:37:23 +00006931static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6932 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006933{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006934 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006935#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6936 int crt_cnt=0;
6937#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006938 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006939 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006944 mbedtls_ssl_pend_fatal_alert( ssl,
6945 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 }
6948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6950 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006959
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006963 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006964
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006965 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006969 mbedtls_ssl_pend_fatal_alert( ssl,
6970 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 }
6973
Hanno Becker33c3dc82019-01-30 14:46:46 +00006974 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6975 i += 3;
6976
Hanno Becker33c3dc82019-01-30 14:46:46 +00006977 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006978 while( i < ssl->in_hslen )
6979 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006980 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006981 if ( i + 3 > ssl->in_hslen ) {
6982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006983 mbedtls_ssl_pend_fatal_alert( ssl,
6984 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006985 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6986 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006987 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6988 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 if( ssl->in_msg[i] != 0 )
6990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006992 mbedtls_ssl_pend_fatal_alert( ssl,
6993 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 }
6996
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006998 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6999 | (unsigned int) ssl->in_msg[i + 2];
7000 i += 3;
7001
7002 if( n < 128 || i + n > ssl->in_hslen )
7003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007005 mbedtls_ssl_pend_fatal_alert( ssl,
7006 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007008 }
7009
Hanno Becker33c3dc82019-01-30 14:46:46 +00007010 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007011#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7012 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007013 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7014 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007015 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007016 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007017 /* During client-side renegotiation, check that the server's
7018 * end-CRTs hasn't changed compared to the initial handshake,
7019 * mitigating the triple handshake attack. On success, reuse
7020 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7022 if( ssl_check_peer_crt_unchanged( ssl,
7023 &ssl->in_msg[i],
7024 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007025 {
Hanno Becker35e41772019-02-05 15:37:23 +00007026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007027 mbedtls_ssl_pend_fatal_alert( ssl,
7028 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007029 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007030 }
Hanno Becker35e41772019-02-05 15:37:23 +00007031
7032 /* Now we can safely free the original chain. */
7033 ssl_clear_peer_cert( ssl->session );
7034 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007035#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7036
Hanno Becker33c3dc82019-01-30 14:46:46 +00007037 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007038#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007039 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007040#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007041 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007042 * it in-place from the input buffer instead of making a copy. */
7043 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7044#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007045 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007047 case 0: /*ok*/
7048 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7049 /* Ignore certificate with an unknown algorithm: maybe a
7050 prior certificate was already trusted. */
7051 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007052
Hanno Becker33c3dc82019-01-30 14:46:46 +00007053 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7054 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7055 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007056
Hanno Becker33c3dc82019-01-30 14:46:46 +00007057 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7058 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7059 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007060
Hanno Becker33c3dc82019-01-30 14:46:46 +00007061 default:
7062 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7063 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007064 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007065 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7066 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007067 }
7068
7069 i += n;
7070 }
7071
Hanno Becker35e41772019-02-05 15:37:23 +00007072 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007073 return( 0 );
7074}
7075
Hanno Beckerb8a08572019-02-05 12:49:06 +00007076#if defined(MBEDTLS_SSL_SRV_C)
7077static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7078{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007079 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007080 return( -1 );
7081
7082#if defined(MBEDTLS_SSL_PROTO_SSL3)
7083 /*
7084 * Check if the client sent an empty certificate
7085 */
Hanno Becker2881d802019-05-22 14:44:53 +01007086 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007087 {
7088 if( ssl->in_msglen == 2 &&
7089 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7090 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7091 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7092 {
7093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7094 return( 0 );
7095 }
7096
7097 return( -1 );
7098 }
7099#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7100
7101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7102 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7103 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7104 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7105 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7106 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7107 {
7108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7109 return( 0 );
7110 }
7111
Hanno Beckerb8a08572019-02-05 12:49:06 +00007112#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7113 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007114
7115 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007116}
7117#endif /* MBEDTLS_SSL_SRV_C */
7118
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007119/* Check if a certificate message is expected.
7120 * Return either
7121 * - SSL_CERTIFICATE_EXPECTED, or
7122 * - SSL_CERTIFICATE_SKIP
7123 * indicating whether a Certificate message is expected or not.
7124 */
7125#define SSL_CERTIFICATE_EXPECTED 0
7126#define SSL_CERTIFICATE_SKIP 1
7127static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7128 int authmode )
7129{
Hanno Becker473f98f2019-06-26 10:27:32 +01007130 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007131 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007132
7133 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7134 return( SSL_CERTIFICATE_SKIP );
7135
7136#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007137 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007138 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007139 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7140 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7141 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007142 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007143 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007144
7145 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7146 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007147 ssl->session_negotiate->verify_result =
7148 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7149 return( SSL_CERTIFICATE_SKIP );
7150 }
7151 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007152#else
7153 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007154#endif /* MBEDTLS_SSL_SRV_C */
7155
7156 return( SSL_CERTIFICATE_EXPECTED );
7157}
7158
Hanno Becker3cf50612019-02-05 14:36:34 +00007159static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7160 int authmode,
7161 mbedtls_x509_crt *chain,
7162 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007163{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007164 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007165 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007166 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 mbedtls_x509_crt *ca_chain;
7168 mbedtls_x509_crl *ca_crl;
7169
7170 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7171 return( 0 );
7172
7173#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7174 if( ssl->handshake->sni_ca_chain != NULL )
7175 {
7176 ca_chain = ssl->handshake->sni_ca_chain;
7177 ca_crl = ssl->handshake->sni_ca_crl;
7178 }
7179 else
7180#endif
7181 {
7182 ca_chain = ssl->conf->ca_chain;
7183 ca_crl = ssl->conf->ca_crl;
7184 }
7185
7186 /*
7187 * Main check: verify certificate
7188 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007189 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007190 chain,
7191 ca_chain, ca_crl,
7192 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007193#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007194 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007195#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007196 &ssl->session_negotiate->verify_result,
7197 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7198
Hanno Becker8c13ee62019-02-26 16:48:17 +00007199 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007200 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007201 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007202 }
7203
7204#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007205 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007206 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7207#endif
7208
7209 /*
7210 * Secondary checks: always done, but change 'ret' only if it was 0
7211 */
7212
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007213#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007214 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007215 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007216#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007217 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007218#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007219 mbedtls_pk_context *pk;
7220 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7221 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007222 {
7223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007224 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007225 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007226
7227 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007228 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007229 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007230 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007231 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007232
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007233 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007234#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007235
7236 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007237 {
7238 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7239
7240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007241 if( verify_ret == 0 )
7242 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007243 }
7244 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007245#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007246
7247 if( mbedtls_ssl_check_cert_usage( chain,
7248 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007249 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7250 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007251 &ssl->session_negotiate->verify_result ) != 0 )
7252 {
7253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007254 if( verify_ret == 0 )
7255 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007256 }
7257
7258 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7259 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7260 * with details encoded in the verification flags. All other kinds
7261 * of error codes, including those from the user provided f_vrfy
7262 * functions, are treated as fatal and lead to a failure of
7263 * ssl_parse_certificate even if verification was optional. */
7264 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007265 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7266 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007267 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007268 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007269 }
7270
7271 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007274 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007275 }
7276
Hanno Becker8c13ee62019-02-26 16:48:17 +00007277 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007278 {
7279 uint8_t alert;
7280
7281 /* The certificate may have been rejected for several reasons.
7282 Pick one and send the corresponding alert. Which alert to send
7283 may be a subject of debate in some cases. */
7284 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7285 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7286 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7287 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7288 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7289 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7290 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7291 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7292 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7293 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7294 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7295 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7296 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7297 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7298 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7299 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7300 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7301 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7302 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7303 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7304 else
7305 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007306 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007307 }
7308
7309#if defined(MBEDTLS_DEBUG_C)
7310 if( ssl->session_negotiate->verify_result != 0 )
7311 {
7312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7313 ssl->session_negotiate->verify_result ) );
7314 }
7315 else
7316 {
7317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7318 }
7319#endif /* MBEDTLS_DEBUG_C */
7320
Hanno Becker8c13ee62019-02-26 16:48:17 +00007321 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007322}
7323
Hanno Becker34106f62019-02-08 14:59:05 +00007324#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007325
7326#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007327static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7328 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007329{
7330 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007331 /* Remember digest of the peer's end-CRT. */
7332 ssl->session_negotiate->peer_cert_digest =
7333 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7334 if( ssl->session_negotiate->peer_cert_digest == NULL )
7335 {
7336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7337 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007338 mbedtls_ssl_pend_fatal_alert( ssl,
7339 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007340
7341 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7342 }
7343
7344 ret = mbedtls_md( mbedtls_md_info_from_type(
7345 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7346 start, len,
7347 ssl->session_negotiate->peer_cert_digest );
7348
7349 ssl->session_negotiate->peer_cert_digest_type =
7350 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7351 ssl->session_negotiate->peer_cert_digest_len =
7352 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7353
7354 return( ret );
7355}
Hanno Becker5882dd02019-06-06 16:25:57 +01007356#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007357
7358static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7359 unsigned char *start, size_t len )
7360{
7361 unsigned char *end = start + len;
7362 int ret;
7363
7364 /* Make a copy of the peer's raw public key. */
7365 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7366 ret = mbedtls_pk_parse_subpubkey( &start, end,
7367 &ssl->handshake->peer_pubkey );
7368 if( ret != 0 )
7369 {
7370 /* We should have parsed the public key before. */
7371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7372 }
7373
7374 return( 0 );
7375}
7376#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7377
Hanno Becker3cf50612019-02-05 14:36:34 +00007378int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7379{
7380 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007381 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007382#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7383 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7384 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007385 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007387 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007388#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007389 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007390 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007391
7392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7393
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007394 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7395 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007396 {
7397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007398 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007399 }
7400
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007401#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7402 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007403 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007404 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007405 chain = ssl->handshake->ecrs_peer_cert;
7406 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007407 goto crt_verify;
7408 }
7409#endif
7410
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007411 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007412 {
7413 /* mbedtls_ssl_read_record may have sent an alert already. We
7414 let it decide whether to alert. */
7415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007416 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007417 }
7418
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007419#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007420 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7421 {
7422 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007423
Hanno Beckerb8a08572019-02-05 12:49:06 +00007424 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007425 ret = 0;
7426 else
7427 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007428
Hanno Becker613d4902019-02-05 13:11:17 +00007429 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007430 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007431#endif /* MBEDTLS_SSL_SRV_C */
7432
Hanno Becker35e41772019-02-05 15:37:23 +00007433 /* Clear existing peer CRT structure in case we tried to
7434 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007435 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007436
7437 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7438 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007439 {
7440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7441 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007442 mbedtls_ssl_pend_fatal_alert( ssl,
7443 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007444
Hanno Beckere4aeb762019-02-05 17:19:52 +00007445 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7446 goto exit;
7447 }
7448 mbedtls_x509_crt_init( chain );
7449
7450 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007451 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007452 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007453
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007454#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7455 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007456 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007457
7458crt_verify:
7459 if( ssl->handshake->ecrs_enabled)
7460 rs_ctx = &ssl->handshake->ecrs_ctx;
7461#endif
7462
Hanno Becker3cf50612019-02-05 14:36:34 +00007463 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007464 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007465 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007466 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker3008d282019-02-05 17:02:28 +00007468#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007470 size_t pk_len;
7471 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007472
Hanno Becker34106f62019-02-08 14:59:05 +00007473 /* We parse the CRT chain without copying, so
7474 * these pointers point into the input buffer,
7475 * and are hence still valid after freeing the
7476 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
Hanno Becker5882dd02019-06-06 16:25:57 +01007478#if defined(MBEDTLS_SSL_RENEGOTIATION)
7479 unsigned char *crt_start;
7480 size_t crt_len;
7481
Hanno Becker34106f62019-02-08 14:59:05 +00007482 crt_start = chain->raw.p;
7483 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007484#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007485
Hanno Becker8c13ee62019-02-26 16:48:17 +00007486 pk_start = chain->cache->pk_raw.p;
7487 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007488
7489 /* Free the CRT structures before computing
7490 * digest and copying the peer's public key. */
7491 mbedtls_x509_crt_free( chain );
7492 mbedtls_free( chain );
7493 chain = NULL;
7494
Hanno Becker5882dd02019-06-06 16:25:57 +01007495#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007496 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007498 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007499#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007500
Hanno Becker34106f62019-02-08 14:59:05 +00007501 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007503 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 }
Hanno Becker34106f62019-02-08 14:59:05 +00007505#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7506 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007507 ssl->session_negotiate->peer_cert = chain;
7508 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007509#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007512
Hanno Becker613d4902019-02-05 13:11:17 +00007513exit:
7514
Hanno Beckere4aeb762019-02-05 17:19:52 +00007515 if( ret == 0 )
7516 ssl->state++;
7517
7518#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7519 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7520 {
7521 ssl->handshake->ecrs_peer_cert = chain;
7522 chain = NULL;
7523 }
7524#endif
7525
7526 if( chain != NULL )
7527 {
7528 mbedtls_x509_crt_free( chain );
7529 mbedtls_free( chain );
7530 }
7531
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 return( ret );
7533}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007534#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537{
7538 int ret;
7539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007543 ssl->out_msglen = 1;
7544 ssl->out_msg[0] = 1;
7545
Paul Bakker5121ce52009-01-03 21:22:43 +00007546 ssl->state++;
7547
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007548 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007549 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551 return( ret );
7552 }
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007555
7556 return( 0 );
7557}
7558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007560{
7561 int ret;
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Hanno Becker327c93b2018-08-15 13:56:18 +01007565 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007568 return( ret );
7569 }
7570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007574 mbedtls_ssl_pend_fatal_alert( ssl,
7575 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007577 }
7578
Hanno Beckere678eaa2018-08-21 14:57:46 +01007579 /* CCS records are only accepted if they have length 1 and content '1',
7580 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007582 /*
7583 * Switch to our negotiated transform and session parameters for inbound
7584 * data.
7585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007587 ssl->transform_in = ssl->transform_negotiate;
7588 ssl->session_in = ssl->session_negotiate;
7589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007591 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007594 ssl_dtls_replay_reset( ssl );
7595#endif
7596
7597 /* Increment epoch */
7598 if( ++ssl->in_epoch == 0 )
7599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007601 /* This is highly unlikely to happen for legitimate reasons, so
7602 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007604 }
7605 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007606 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007608#if defined(MBEDTLS_SSL_PROTO_TLS)
7609 {
7610 memset( ssl->in_ctr, 0, 8 );
7611 }
7612#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007613
Hanno Beckerf5970a02019-05-08 09:38:41 +01007614 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7617 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007622 mbedtls_ssl_pend_fatal_alert( ssl,
7623 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007625 }
7626 }
7627#endif
7628
Paul Bakker5121ce52009-01-03 21:22:43 +00007629 ssl->state++;
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007632
7633 return( 0 );
7634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7639 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007640 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007641 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007642#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7644#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007645 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007648 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007651}
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007656
7657 /*
7658 * Free our handshake params
7659 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007660 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007662 ssl->handshake = NULL;
7663
7664 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007665 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007666 */
7667 if( ssl->transform )
7668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669 mbedtls_ssl_transform_free( ssl->transform );
7670 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007671 }
7672 ssl->transform = ssl->transform_negotiate;
7673 ssl->transform_negotiate = NULL;
7674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676}
7677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007679{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_RENEGOTIATION)
7683 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007686 ssl->renego_records_seen = 0;
7687 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007688#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007689
7690 /*
7691 * Free the previous session and switch in the current one
7692 */
Paul Bakker0a597072012-09-25 21:55:46 +00007693 if( ssl->session )
7694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007696 /* RFC 7366 3.1: keep the EtM state */
7697 ssl->session_negotiate->encrypt_then_mac =
7698 ssl->session->encrypt_then_mac;
7699#endif
7700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701 mbedtls_ssl_session_free( ssl->session );
7702 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007703 }
7704 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007705 ssl->session_negotiate = NULL;
7706
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007707#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007708 /*
7709 * Add cache entry
7710 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007711 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007712 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007713 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007714 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007715 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007717 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007718#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007721 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007722 ssl->handshake->flight != NULL )
7723 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007724 /* Cancel handshake timer */
7725 ssl_set_timer( ssl, 0 );
7726
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007727 /* Keep last flight around in case we need to resend it:
7728 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007730 }
7731 else
7732#endif
7733 ssl_handshake_wrapup_free_hs_transform( ssl );
7734
Paul Bakker48916f92012-09-16 19:57:18 +00007735 ssl->state++;
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007738}
7739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007741{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007742 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007745
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007746 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007747
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007748 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7749 mbedtls_ssl_suite_get_mac(
7750 mbedtls_ssl_ciphersuite_from_id(
7751 mbedtls_ssl_session_get_ciphersuite(
7752 ssl->session_negotiate ) ) ),
7753 ssl, ssl->out_msg + 4,
7754 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007755
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007756 /*
7757 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7758 * may define some other value. Currently (early 2016), no defined
7759 * ciphersuite does this (and this is unlikely to change as activity has
7760 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7761 */
Hanno Becker2881d802019-05-22 14:44:53 +01007762 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007765 ssl->verify_data_len = hash_len;
7766 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007767#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007768
Paul Bakker5121ce52009-01-03 21:22:43 +00007769 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7771 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007772
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007773#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007774 /*
7775 * In case of session resuming, invert the client and server
7776 * ChangeCipherSpec messages order.
7777 */
Paul Bakker0a597072012-09-25 21:55:46 +00007778 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007781 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7782 MBEDTLS_SSL_IS_CLIENT )
7783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007785 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007788 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7789 MBEDTLS_SSL_IS_SERVER )
7790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007792 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007793#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007794 }
7795 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007796#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007797 ssl->state++;
7798
Paul Bakker48916f92012-09-16 19:57:18 +00007799 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007800 * Switch to our negotiated transform and session parameters for outbound
7801 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007806 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007807 {
7808 unsigned char i;
7809
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007810 /* Remember current epoch settings for resending */
7811 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007812 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007813
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007814 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007815 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007816
7817 /* Increment epoch */
7818 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007819 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007820 break;
7821
7822 /* The loop goes to its end iff the counter is wrapping */
7823 if( i == 0 )
7824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7826 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007827 }
7828 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007829 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007831#if defined(MBEDTLS_SSL_PROTO_TLS)
7832 {
7833 memset( ssl->cur_out_ctr, 0, 8 );
7834 }
7835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007836
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007837 ssl->transform_out = ssl->transform_negotiate;
7838 ssl->session_out = ssl->session_negotiate;
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7841 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7846 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007847 }
7848 }
7849#endif
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007852 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007854#endif
7855
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007856 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 return( ret );
7860 }
7861
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007863 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007864 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7865 {
7866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7867 return( ret );
7868 }
7869#endif
7870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007872
7873 return( 0 );
7874}
7875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007877#define SSL_MAX_HASH_LEN 36
7878#else
7879#define SSL_MAX_HASH_LEN 12
7880#endif
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007883{
Paul Bakker23986e52011-04-24 08:57:21 +00007884 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007885 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007886 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007889
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007890 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7891 mbedtls_ssl_suite_get_mac(
7892 mbedtls_ssl_ciphersuite_from_id(
7893 mbedtls_ssl_session_get_ciphersuite(
7894 ssl->session_negotiate ) ) ),
7895 ssl, buf,
7896 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007897
Hanno Becker327c93b2018-08-15 13:56:18 +01007898 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 return( ret );
7902 }
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007907 mbedtls_ssl_pend_fatal_alert( ssl,
7908 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007910 }
7911
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007912 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007914 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007915 hash_len = 36;
7916 else
7917#endif
7918 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7921 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007924 mbedtls_ssl_pend_fatal_alert( ssl,
7925 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007927 }
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007930 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007933 mbedtls_ssl_pend_fatal_alert( ssl,
7934 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007936 }
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007939 ssl->verify_data_len = hash_len;
7940 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007941#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007942
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007943#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007944 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007947 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007951 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007953#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007954 }
7955 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007956#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007957 ssl->state++;
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007960 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007962#endif
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007965
7966 return( 0 );
7967}
7968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7974 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7975 mbedtls_md5_init( &handshake->fin_md5 );
7976 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007977 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7978 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7981#if defined(MBEDTLS_SHA256_C)
7982 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007983 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_SHA512_C)
7986 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007987 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007990
Hanno Becker7e5437a2017-04-28 17:15:26 +01007991#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7992 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7993 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7994#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_DHM_C)
7997 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_ECDH_C)
8000 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008001#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008002#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008003 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008004#if defined(MBEDTLS_SSL_CLI_C)
8005 handshake->ecjpake_cache = NULL;
8006 handshake->ecjpake_cache_len = 0;
8007#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008008#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008009
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008010#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008011 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008012#endif
8013
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008014#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8015 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8016#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008017
8018#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8019 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8020 mbedtls_pk_init( &handshake->peer_pubkey );
8021#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022}
8023
Hanno Becker611a83b2018-01-03 14:27:32 +00008024void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8029 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008030
Hanno Becker92231322018-01-03 15:32:51 +00008031#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_md_init( &transform->md_ctx_enc );
8033 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008034#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008035}
8036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008040}
8041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008043{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008044 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008045 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008047 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008049 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008050 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008051
8052 /*
8053 * Either the pointers are now NULL or cleared properly and can be freed.
8054 * Now allocate missing structures.
8055 */
8056 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008057 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008058 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008059 }
Paul Bakker48916f92012-09-16 19:57:18 +00008060
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008061 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008062 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008063 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008064 }
Paul Bakker48916f92012-09-16 19:57:18 +00008065
Paul Bakker82788fb2014-10-20 13:59:19 +02008066 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008067 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008068 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008069 }
Paul Bakker48916f92012-09-16 19:57:18 +00008070
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008071 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008072 if( ssl->handshake == NULL ||
8073 ssl->transform_negotiate == NULL ||
8074 ssl->session_negotiate == NULL )
8075 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078 mbedtls_free( ssl->handshake );
8079 mbedtls_free( ssl->transform_negotiate );
8080 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008081
8082 ssl->handshake = NULL;
8083 ssl->transform_negotiate = NULL;
8084 ssl->session_negotiate = NULL;
8085
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008086 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008087 }
8088
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008089 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008091 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008092 ssl_handshake_params_init( ssl->handshake );
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008095 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008096 {
8097 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008098
Hanno Becker2d9623f2019-06-13 12:07:05 +01008099 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008100 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8101 else
8102 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8103 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008104#endif
8105
Paul Bakker48916f92012-09-16 19:57:18 +00008106 return( 0 );
8107}
8108
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008109#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008110/* Dummy cookie callbacks for defaults */
8111static int ssl_cookie_write_dummy( void *ctx,
8112 unsigned char **p, unsigned char *end,
8113 const unsigned char *cli_id, size_t cli_id_len )
8114{
8115 ((void) ctx);
8116 ((void) p);
8117 ((void) end);
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}
8123
8124static int ssl_cookie_check_dummy( void *ctx,
8125 const unsigned char *cookie, size_t cookie_len,
8126 const unsigned char *cli_id, size_t cli_id_len )
8127{
8128 ((void) ctx);
8129 ((void) cookie);
8130 ((void) cookie_len);
8131 ((void) cli_id);
8132 ((void) cli_id_len);
8133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008135}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008136#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008137
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138/* Once ssl->out_hdr as the address of the beginning of the
8139 * next outgoing record is set, deduce the other pointers.
8140 *
8141 * Note: For TLS, we save the implicit record sequence number
8142 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8143 * and the caller has to make sure there's space for this.
8144 */
8145
8146static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8147 mbedtls_ssl_transform *transform )
8148{
8149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008150 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008151 {
8152 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008154 ssl->out_cid = ssl->out_ctr + 8;
8155 ssl->out_len = ssl->out_cid;
8156 if( transform != NULL )
8157 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008158#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008159 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008160#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008161 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008163 MBEDTLS_SSL_TRANSPORT_ELSE
8164#endif /* MBEDTLS_SSL_PROTO_DTLS */
8165#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166 {
8167 ssl->out_ctr = ssl->out_hdr - 8;
8168 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008169#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008170 ssl->out_cid = ssl->out_len;
8171#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008172 ssl->out_iv = ssl->out_hdr + 5;
8173 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008174#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008175
8176 /* Adjust out_msg to make space for explicit IV, if used. */
8177 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008178 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008179 {
8180 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8181 }
8182 else
8183 ssl->out_msg = ssl->out_iv;
8184}
8185
8186/* Once ssl->in_hdr as the address of the beginning of the
8187 * next incoming record is set, deduce the other pointers.
8188 *
8189 * Note: For TLS, we save the implicit record sequence number
8190 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8191 * and the caller has to make sure there's space for this.
8192 */
8193
Hanno Beckerf5970a02019-05-08 09:38:41 +01008194static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008195{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008196 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008197 * of unprotected TLS/DTLS records, with ssl->in_msg
8198 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008199 *
8200 * When decrypting a protected record, ssl->in_msg
8201 * will be shifted to point to the beginning of the
8202 * record plaintext.
8203 */
8204
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008206 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008207 {
Hanno Becker70e79282019-05-03 14:34:53 +01008208 /* This sets the header pointers to match records
8209 * without CID. When we receive a record containing
8210 * a CID, the fields are shifted accordingly in
8211 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008212 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008213#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008214 ssl->in_cid = ssl->in_ctr + 8;
8215 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008216#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008217 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008219 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008220 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008221 MBEDTLS_SSL_TRANSPORT_ELSE
8222#endif /* MBEDTLS_SSL_PROTO_DTLS */
8223#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008224 {
8225 ssl->in_ctr = ssl->in_hdr - 8;
8226 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008227#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008228 ssl->in_cid = ssl->in_len;
8229#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008230 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008231 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008232#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008233}
8234
Paul Bakker5121ce52009-01-03 21:22:43 +00008235/*
8236 * Initialize an SSL context
8237 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008238void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8239{
8240 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8241}
8242
8243/*
8244 * Setup an SSL context
8245 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008246
8247static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8248{
8249 /* Set the incoming and outgoing record pointers. */
8250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008251 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008252 {
8253 ssl->out_hdr = ssl->out_buf;
8254 ssl->in_hdr = ssl->in_buf;
8255 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008256 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008257#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008258#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008259 {
8260 ssl->out_hdr = ssl->out_buf + 8;
8261 ssl->in_hdr = ssl->in_buf + 8;
8262 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008263#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008264
8265 /* Derive other internal pointers. */
8266 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008267 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008268}
8269
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008270int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008271 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008272{
Paul Bakker48916f92012-09-16 19:57:18 +00008273 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008274
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008275 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008276
Hanno Beckeref982d52019-07-23 15:56:18 +01008277#if defined(MBEDTLS_USE_TINYCRYPT)
8278 uECC_set_rng( &uecc_rng_wrapper );
8279#endif
8280
Paul Bakker62f2dee2012-09-28 07:31:51 +00008281 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008282 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008283 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008284
8285 /* Set to NULL in case of an error condition */
8286 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008287
Angus Grattond8213d02016-05-25 20:56:48 +10008288 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8289 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290 {
Angus Grattond8213d02016-05-25 20:56:48 +10008291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008292 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008293 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008294 }
8295
8296 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8297 if( ssl->out_buf == NULL )
8298 {
8299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008300 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008301 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008302 }
8303
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008304 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008305
Paul Bakker48916f92012-09-16 19:57:18 +00008306 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008307 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008308
Hanno Beckerc8f52992019-07-25 11:15:08 +01008309 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008310
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008312
8313error:
8314 mbedtls_free( ssl->in_buf );
8315 mbedtls_free( ssl->out_buf );
8316
8317 ssl->conf = NULL;
8318
8319 ssl->in_buf = NULL;
8320 ssl->out_buf = NULL;
8321
8322 ssl->in_hdr = NULL;
8323 ssl->in_ctr = NULL;
8324 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008325 ssl->in_msg = NULL;
8326
8327 ssl->out_hdr = NULL;
8328 ssl->out_ctr = NULL;
8329 ssl->out_len = NULL;
8330 ssl->out_iv = NULL;
8331 ssl->out_msg = NULL;
8332
k-stachowiak9f7798e2018-07-31 16:52:32 +02008333 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008334}
8335
8336/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008337 * Reset an initialized and used SSL context for re-use while retaining
8338 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008339 *
8340 * If partial is non-zero, keep data in the input buffer and client ID.
8341 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008342 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008343static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008344{
Paul Bakker48916f92012-09-16 19:57:18 +00008345 int ret;
8346
Hanno Becker7e772132018-08-10 12:38:21 +01008347#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8348 !defined(MBEDTLS_SSL_SRV_C)
8349 ((void) partial);
8350#endif
8351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008353
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008354 /* Cancel any possibly running timer */
8355 ssl_set_timer( ssl, 0 );
8356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_RENEGOTIATION)
8358 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008359 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008360
8361 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8363 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008366
Paul Bakker7eb013f2011-10-06 12:37:39 +00008367 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008368 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008369
8370 ssl->in_msgtype = 0;
8371 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008373 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008374 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008377 ssl_dtls_replay_reset( ssl );
8378#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008379
8380 ssl->in_hslen = 0;
8381 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008382
8383 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008384
8385 ssl->out_msgtype = 0;
8386 ssl->out_msglen = 0;
8387 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8389 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008390 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008391#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008392
Hanno Becker19859472018-08-06 09:40:20 +01008393 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8394
Paul Bakker48916f92012-09-16 19:57:18 +00008395 ssl->transform_in = NULL;
8396 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008397
Hanno Becker78640902018-08-13 16:35:15 +01008398 ssl->session_in = NULL;
8399 ssl->session_out = NULL;
8400
Angus Grattond8213d02016-05-25 20:56:48 +10008401 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008402
8403#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008404 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008405#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8406 {
8407 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008408 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008409 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8412 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8415 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008419 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008420 }
8421#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008422
Paul Bakker48916f92012-09-16 19:57:18 +00008423 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425 mbedtls_ssl_transform_free( ssl->transform );
8426 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008427 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008428 }
Paul Bakker48916f92012-09-16 19:57:18 +00008429
Paul Bakkerc0463502013-02-14 11:19:38 +01008430 if( ssl->session )
8431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432 mbedtls_ssl_session_free( ssl->session );
8433 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008434 ssl->session = NULL;
8435 }
8436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008438 ssl->alpn_chosen = NULL;
8439#endif
8440
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008441#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008443 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008444#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008445 {
8446 mbedtls_free( ssl->cli_id );
8447 ssl->cli_id = NULL;
8448 ssl->cli_id_len = 0;
8449 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008450#endif
8451
Paul Bakker48916f92012-09-16 19:57:18 +00008452 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8453 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008454
8455 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008456}
8457
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008458/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008459 * Reset an initialized and used SSL context for re-use while retaining
8460 * all application-set variables, function pointers and data.
8461 */
8462int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8463{
8464 return( ssl_session_reset_int( ssl, 0 ) );
8465}
8466
8467/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008468 * SSL set accessors
8469 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008470#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008471void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008473 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008474}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008475#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008476
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008477void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008478{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008479 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008480}
8481
Hanno Becker7f376f42019-06-12 16:20:48 +01008482#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8483 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008484void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008485{
Hanno Becker7f376f42019-06-12 16:20:48 +01008486 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008487}
Hanno Becker7f376f42019-06-12 16:20:48 +01008488#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008489
Hanno Beckerde671542019-06-12 16:30:46 +01008490#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8491 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8492void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8493 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008495 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008496}
Hanno Beckerde671542019-06-12 16:30:46 +01008497#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008500
Hanno Becker1841b0a2018-08-24 11:13:57 +01008501void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8502 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008503{
8504 ssl->disable_datagram_packing = !allow_packing;
8505}
8506
Hanno Becker1f835fa2019-06-13 10:14:59 +01008507#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8508 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008509void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8510 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008511{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008512 conf->hs_timeout_min = min;
8513 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008514}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008515#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8516 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8517void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8518 uint32_t min, uint32_t max )
8519{
8520 ((void) conf);
8521 ((void) min);
8522 ((void) max);
8523}
8524#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8525 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8526
8527#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008528
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008529void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008530{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008531#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8532 conf->authmode = authmode;
8533#else
8534 ((void) conf);
8535 ((void) authmode);
8536#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008537}
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008541 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008542 void *p_vrfy )
8543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 conf->f_vrfy = f_vrfy;
8545 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008548
Hanno Beckerece325c2019-06-13 15:39:27 +01008549#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008551 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008552 void *p_rng )
8553{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008554 conf->f_rng = f_rng;
8555 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008556}
Hanno Beckerece325c2019-06-13 15:39:27 +01008557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008558
Hanno Becker14a4a442019-07-02 17:00:34 +01008559#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008560void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008561 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008562 void *p_dbg )
8563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008564 conf->f_dbg = f_dbg;
8565 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008566}
Hanno Becker14a4a442019-07-02 17:00:34 +01008567#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008568
Hanno Beckera58a8962019-06-13 16:11:15 +01008569#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8570 !defined(MBEDTLS_SSL_CONF_SEND) && \
8571 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008573 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008574 mbedtls_ssl_send_t *f_send,
8575 mbedtls_ssl_recv_t *f_recv,
8576 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008577{
Hanno Beckera58a8962019-06-13 16:11:15 +01008578 ssl->p_bio = p_bio;
8579 ssl->f_send = f_send;
8580 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008581 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008582}
Hanno Beckera58a8962019-06-13 16:11:15 +01008583#else
8584void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8585 void *p_bio )
8586{
8587 ssl->p_bio = p_bio;
8588}
8589#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008590
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008591#if defined(MBEDTLS_SSL_PROTO_DTLS)
8592void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8593{
8594 ssl->mtu = mtu;
8595}
8596#endif
8597
Hanno Becker1f835fa2019-06-13 10:14:59 +01008598#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008599void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008600{
8601 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008602}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008603#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008604
Hanno Becker0ae6b242019-06-13 16:45:36 +01008605#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8606 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008607void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8608 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008609 mbedtls_ssl_set_timer_t *f_set_timer,
8610 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008611{
8612 ssl->p_timer = p_timer;
8613 ssl->f_set_timer = f_set_timer;
8614 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008615 /* Make sure we start with no timer running */
8616 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008617}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008618#else
8619void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8620 void *p_timer )
8621{
8622 ssl->p_timer = p_timer;
8623 /* Make sure we start with no timer running */
8624 ssl_set_timer( ssl, 0 );
8625}
8626#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008627
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008628#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008630 void *p_cache,
8631 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8632 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008633{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008634 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008635 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008636 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008637}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008638#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008639
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008640#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008642{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008643 int ret;
8644
8645 if( ssl == NULL ||
8646 session == NULL ||
8647 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008648 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008651 }
8652
Hanno Becker58fccf22019-02-06 14:30:46 +00008653 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8654 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008655 return( ret );
8656
Paul Bakker0a597072012-09-25 21:55:46 +00008657 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008658
8659 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008660}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008661#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008662
Hanno Becker73f4cb12019-06-27 13:51:07 +01008663#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008664void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008665 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008667 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8668 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8669 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8670 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008671}
8672
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008673void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008674 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008675 int major, int minor )
8676{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008678 return;
8679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008681 return;
8682
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008683 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008684}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008685#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008688void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008689 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008690{
8691 conf->cert_profile = profile;
8692}
8693
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008694/* Append a new keycert entry to a (possibly empty) list */
8695static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8696 mbedtls_x509_crt *cert,
8697 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008698{
niisato8ee24222018-06-25 19:05:48 +09008699 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008700
niisato8ee24222018-06-25 19:05:48 +09008701 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8702 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008703 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008704
niisato8ee24222018-06-25 19:05:48 +09008705 new_cert->cert = cert;
8706 new_cert->key = key;
8707 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008708
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008709 /* Update head is the list was null, else add to the end */
8710 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008711 {
niisato8ee24222018-06-25 19:05:48 +09008712 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008713 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008714 else
8715 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008716 mbedtls_ssl_key_cert *cur = *head;
8717 while( cur->next != NULL )
8718 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008719 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008720 }
8721
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008722 return( 0 );
8723}
8724
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008725int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008726 mbedtls_x509_crt *own_cert,
8727 mbedtls_pk_context *pk_key )
8728{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008729 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008730}
8731
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008732void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008733 mbedtls_x509_crt *ca_chain,
8734 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008735{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008736 conf->ca_chain = ca_chain;
8737 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008740
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008741#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8742int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8743 mbedtls_x509_crt *own_cert,
8744 mbedtls_pk_context *pk_key )
8745{
8746 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8747 own_cert, pk_key ) );
8748}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008749
8750void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8751 mbedtls_x509_crt *ca_chain,
8752 mbedtls_x509_crl *ca_crl )
8753{
8754 ssl->handshake->sni_ca_chain = ca_chain;
8755 ssl->handshake->sni_ca_crl = ca_crl;
8756}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008757
8758void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8759 int authmode )
8760{
8761 ssl->handshake->sni_authmode = authmode;
8762}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008763#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8764
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008765#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008766/*
8767 * Set EC J-PAKE password for current handshake
8768 */
8769int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8770 const unsigned char *pw,
8771 size_t pw_len )
8772{
8773 mbedtls_ecjpake_role role;
8774
Janos Follath8eb64132016-06-03 15:40:57 +01008775 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8777
Hanno Becker2d9623f2019-06-13 12:07:05 +01008778 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008779 role = MBEDTLS_ECJPAKE_SERVER;
8780 else
8781 role = MBEDTLS_ECJPAKE_CLIENT;
8782
8783 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8784 role,
8785 MBEDTLS_MD_SHA256,
8786 MBEDTLS_ECP_DP_SECP256R1,
8787 pw, pw_len ) );
8788}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008789#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008792int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008793 const unsigned char *psk, size_t psk_len,
8794 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008795{
Paul Bakker6db455e2013-09-18 17:29:31 +02008796 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008801
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008802 /* Identity len will be encoded on two bytes */
8803 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008804 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008805 {
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8807 }
8808
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008809 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008810 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008811 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008812
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008813 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008814 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008815 conf->psk_len = 0;
8816 }
8817 if( conf->psk_identity != NULL )
8818 {
8819 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008820 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008821 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008822 }
8823
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008824 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8825 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008826 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008827 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008828 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008829 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008830 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008831 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008832 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008833
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008834 conf->psk_len = psk_len;
8835 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008836
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008837 memcpy( conf->psk, psk, conf->psk_len );
8838 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008839
8840 return( 0 );
8841}
8842
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008843int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8844 const unsigned char *psk, size_t psk_len )
8845{
8846 if( psk == NULL || ssl->handshake == NULL )
8847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8848
8849 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8851
8852 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008853 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008854 mbedtls_platform_zeroize( ssl->handshake->psk,
8855 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008856 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008857 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008858 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008859
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008860 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008861 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008862
8863 ssl->handshake->psk_len = psk_len;
8864 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8865
8866 return( 0 );
8867}
8868
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008869void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008871 size_t),
8872 void *p_psk )
8873{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008874 conf->f_psk = f_psk;
8875 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008878
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008879#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008880
8881#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008882int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008883{
8884 int ret;
8885
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8887 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8888 {
8889 mbedtls_mpi_free( &conf->dhm_P );
8890 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008891 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008892 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008893
8894 return( 0 );
8895}
Hanno Becker470a8c42017-10-04 15:28:46 +01008896#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008897
Hanno Beckera90658f2017-10-04 15:29:08 +01008898int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8899 const unsigned char *dhm_P, size_t P_len,
8900 const unsigned char *dhm_G, size_t G_len )
8901{
8902 int ret;
8903
8904 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8905 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8906 {
8907 mbedtls_mpi_free( &conf->dhm_P );
8908 mbedtls_mpi_free( &conf->dhm_G );
8909 return( ret );
8910 }
8911
8912 return( 0 );
8913}
Paul Bakker5121ce52009-01-03 21:22:43 +00008914
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008915int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008916{
8917 int ret;
8918
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008919 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8920 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8921 {
8922 mbedtls_mpi_free( &conf->dhm_P );
8923 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008924 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008925 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008926
8927 return( 0 );
8928}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008929#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008930
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008931#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8932/*
8933 * Set the minimum length for Diffie-Hellman parameters
8934 */
8935void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8936 unsigned int bitlen )
8937{
8938 conf->dhm_min_bitlen = bitlen;
8939}
8940#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8941
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008942#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008943/*
8944 * Set allowed/preferred hashes for handshake signatures
8945 */
8946void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8947 const int *hashes )
8948{
Hanno Becker56595f42019-06-19 16:31:38 +01008949#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008950 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008951#else
8952 ((void) conf);
8953 ((void) hashes);
8954#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008955}
Hanno Becker947194e2017-04-07 13:25:49 +01008956#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008957
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008958#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008959#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008960/*
8961 * Set the allowed elliptic curves
8962 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008963void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008964 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008966 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008967}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008968#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008969#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008970
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008971#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008973{
Hanno Becker947194e2017-04-07 13:25:49 +01008974 /* Initialize to suppress unnecessary compiler warning */
8975 size_t hostname_len = 0;
8976
8977 /* Check if new hostname is valid before
8978 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008979 if( hostname != NULL )
8980 {
8981 hostname_len = strlen( hostname );
8982
8983 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8985 }
8986
8987 /* Now it's clear that we will overwrite the old hostname,
8988 * so we can free it safely */
8989
8990 if( ssl->hostname != NULL )
8991 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008992 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008993 mbedtls_free( ssl->hostname );
8994 }
8995
8996 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008997
Paul Bakker5121ce52009-01-03 21:22:43 +00008998 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008999 {
9000 ssl->hostname = NULL;
9001 }
9002 else
9003 {
9004 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009005 if( ssl->hostname == NULL )
9006 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009007
Hanno Becker947194e2017-04-07 13:25:49 +01009008 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009009
Hanno Becker947194e2017-04-07 13:25:49 +01009010 ssl->hostname[hostname_len] = '\0';
9011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009012
9013 return( 0 );
9014}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009015#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009016
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009017#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009018void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009020 const unsigned char *, size_t),
9021 void *p_sni )
9022{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009023 conf->f_sni = f_sni;
9024 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009029int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009030{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009031 size_t cur_len, tot_len;
9032 const char **p;
9033
9034 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009035 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9036 * MUST NOT be truncated."
9037 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009038 */
9039 tot_len = 0;
9040 for( p = protos; *p != NULL; p++ )
9041 {
9042 cur_len = strlen( *p );
9043 tot_len += cur_len;
9044
9045 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009047 }
9048
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009049 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009050
9051 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009052}
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009055{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009056 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009059
Hanno Becker33b9b252019-07-05 11:23:25 +01009060#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9061 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9062void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9063 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 conf->max_major_ver = major;
9066 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009067}
Hanno Becker33b9b252019-07-05 11:23:25 +01009068#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9069 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009070
Hanno Becker33b9b252019-07-05 11:23:25 +01009071#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9072 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9073void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9074 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009076 conf->min_major_ver = major;
9077 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009078}
Hanno Becker33b9b252019-07-05 11:23:25 +01009079#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9080 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009083void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009084{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009085 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009086}
9087#endif
9088
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009089#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009090void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9091 char cert_req_ca_list )
9092{
9093 conf->cert_req_ca_list = cert_req_ca_list;
9094}
9095#endif
9096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009098void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009099{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009100 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009101}
9102#endif
9103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009105#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009108 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009109}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009110#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9111#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009112void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009113 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009114{
9115 conf->enforce_extended_master_secret = ems_enf;
9116}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009117#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009118#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009119
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009120#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009123 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009124}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009125#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009131 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009134 }
9135
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009136 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009137
9138 return( 0 );
9139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009143void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009145 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009150void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009151{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009152 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009153}
9154#endif
9155
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009156#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009157void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009159 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009160}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009161#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009164void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009166 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009167}
9168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009169void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009171 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009172}
9173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009174void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009175 const unsigned char period[8] )
9176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009177 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009182#if defined(MBEDTLS_SSL_CLI_C)
9183void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009184{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009185 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009186}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009187#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009188
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009189#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009190void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9191 mbedtls_ssl_ticket_write_t *f_ticket_write,
9192 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9193 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009194{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009195 conf->f_ticket_write = f_ticket_write;
9196 conf->f_ticket_parse = f_ticket_parse;
9197 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009198}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009201
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009202#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9203void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9204 mbedtls_ssl_export_keys_t *f_export_keys,
9205 void *p_export_keys )
9206{
9207 conf->f_export_keys = f_export_keys;
9208 conf->p_export_keys = p_export_keys;
9209}
9210#endif
9211
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009212#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009213void mbedtls_ssl_conf_async_private_cb(
9214 mbedtls_ssl_config *conf,
9215 mbedtls_ssl_async_sign_t *f_async_sign,
9216 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9217 mbedtls_ssl_async_resume_t *f_async_resume,
9218 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009219 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009220{
9221 conf->f_async_sign_start = f_async_sign;
9222 conf->f_async_decrypt_start = f_async_decrypt;
9223 conf->f_async_resume = f_async_resume;
9224 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009225 conf->p_async_config_data = async_config_data;
9226}
9227
Gilles Peskine8f97af72018-04-26 11:46:10 +02009228void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9229{
9230 return( conf->p_async_config_data );
9231}
9232
Gilles Peskine1febfef2018-04-30 11:54:39 +02009233void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009234{
9235 if( ssl->handshake == NULL )
9236 return( NULL );
9237 else
9238 return( ssl->handshake->user_async_ctx );
9239}
9240
Gilles Peskine1febfef2018-04-30 11:54:39 +02009241void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009242 void *ctx )
9243{
9244 if( ssl->handshake != NULL )
9245 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009246}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009247#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009248
Paul Bakker5121ce52009-01-03 21:22:43 +00009249/*
9250 * SSL get accessors
9251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009253{
9254 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9255}
9256
Hanno Becker8b170a02017-10-10 11:51:19 +01009257int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9258{
9259 /*
9260 * Case A: We're currently holding back
9261 * a message for further processing.
9262 */
9263
9264 if( ssl->keep_current_message == 1 )
9265 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009267 return( 1 );
9268 }
9269
9270 /*
9271 * Case B: Further records are pending in the current datagram.
9272 */
9273
9274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009275 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009276 ssl->in_left > ssl->next_record_offset )
9277 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009279 return( 1 );
9280 }
9281#endif /* MBEDTLS_SSL_PROTO_DTLS */
9282
9283 /*
9284 * Case C: A handshake message is being processed.
9285 */
9286
Hanno Becker8b170a02017-10-10 11:51:19 +01009287 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9288 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009290 return( 1 );
9291 }
9292
9293 /*
9294 * Case D: An application data message is being processed
9295 */
9296 if( ssl->in_offt != NULL )
9297 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009299 return( 1 );
9300 }
9301
9302 /*
9303 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009304 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009305 * we implement support for multiple alerts in single records.
9306 */
9307
9308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9309 return( 0 );
9310}
9311
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009312uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009313{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009314 if( ssl->session != NULL )
9315 return( ssl->session->verify_result );
9316
9317 if( ssl->session_negotiate != NULL )
9318 return( ssl->session_negotiate->verify_result );
9319
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009320 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009321}
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009324{
Hanno Beckere02758c2019-06-26 15:31:31 +01009325 int suite;
9326
Paul Bakker926c8e42013-03-06 10:23:34 +01009327 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009328 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009329
Hanno Beckere02758c2019-06-26 15:31:31 +01009330 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9331 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009337 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009338 {
Hanno Becker2881d802019-05-22 14:44:53 +01009339 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009342 return( "DTLSv1.0" );
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009345 return( "DTLSv1.2" );
9346
9347 default:
9348 return( "unknown (DTLS)" );
9349 }
9350 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009351 MBEDTLS_SSL_TRANSPORT_ELSE
9352#endif /* MBEDTLS_SSL_PROTO_DTLS */
9353#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009354 {
Hanno Becker2881d802019-05-22 14:44:53 +01009355 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009356 {
9357 case MBEDTLS_SSL_MINOR_VERSION_0:
9358 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009359
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009360 case MBEDTLS_SSL_MINOR_VERSION_1:
9361 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009362
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009363 case MBEDTLS_SSL_MINOR_VERSION_2:
9364 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009365
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009366 case MBEDTLS_SSL_MINOR_VERSION_3:
9367 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009368
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009369 default:
9370 return( "unknown" );
9371 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009372 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009373#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009374}
9375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009377{
Hanno Becker3136ede2018-08-17 15:28:19 +01009378 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009380
Hanno Becker43395762019-05-03 14:46:38 +01009381 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9382
Hanno Becker78640902018-08-13 16:35:15 +01009383 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009384 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386#if defined(MBEDTLS_ZLIB_SUPPORT)
9387 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9388 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009389#endif
9390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009392 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009393#if defined(MBEDTLS_GCM_C) || \
9394 defined(MBEDTLS_CCM_C) || \
9395 defined(MBEDTLS_CHACHAPOLY_C)
9396#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009398#endif
9399#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009401#endif
9402#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009403 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009404#endif
9405 transform_expansion =
9406 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009407 break;
9408
Hanno Beckera9d5c452019-07-25 16:47:12 +01009409#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9410 MBEDTLS_CHACHAPOLY_C */
9411
9412#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9413 case MBEDTLS_MODE_STREAM:
9414 transform_expansion = transform->maclen;
9415 break;
9416#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9417
9418#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009420 {
9421 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009422
9423 block_size = mbedtls_cipher_get_block_size(
9424 &transform->cipher_ctx_enc );
9425
Hanno Becker3136ede2018-08-17 15:28:19 +01009426 /* Expansion due to the addition of the MAC. */
9427 transform_expansion += transform->maclen;
9428
9429 /* Expansion due to the addition of CBC padding;
9430 * Theoretically up to 256 bytes, but we never use
9431 * more than the block size of the underlying cipher. */
9432 transform_expansion += block_size;
9433
9434 /* For TLS 1.1 or higher, an explicit IV is added
9435 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009436#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009437 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009438 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009439#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009440
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009441 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009442 }
9443#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009444
9445 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009448 }
9449
Hanno Beckera5a2b082019-05-15 14:03:01 +01009450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009451 if( transform->out_cid_len != 0 )
9452 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009453#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009454
Hanno Becker43395762019-05-03 14:46:38 +01009455 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009456}
9457
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009458#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9459size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9460{
9461 size_t max_len;
9462
9463 /*
9464 * Assume mfl_code is correct since it was checked when set
9465 */
Angus Grattond8213d02016-05-25 20:56:48 +10009466 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009467
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009468 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009469 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009470 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009471 {
Angus Grattond8213d02016-05-25 20:56:48 +10009472 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009473 }
9474
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009475 /* During a handshake, use the value being negotiated */
9476 if( ssl->session_negotiate != NULL &&
9477 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9478 {
9479 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9480 }
9481
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009483}
9484#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9485
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009486#if defined(MBEDTLS_SSL_PROTO_DTLS)
9487static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9488{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009489 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009490 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009491 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9492 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9493 return ( 0 );
9494
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009495 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9496 return( ssl->mtu );
9497
9498 if( ssl->mtu == 0 )
9499 return( ssl->handshake->mtu );
9500
9501 return( ssl->mtu < ssl->handshake->mtu ?
9502 ssl->mtu : ssl->handshake->mtu );
9503}
9504#endif /* MBEDTLS_SSL_PROTO_DTLS */
9505
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009506int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9507{
9508 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9509
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009510#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9511 !defined(MBEDTLS_SSL_PROTO_DTLS)
9512 (void) ssl;
9513#endif
9514
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009515#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9516 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9517
9518 if( max_len > mfl )
9519 max_len = mfl;
9520#endif
9521
9522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009523 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009524 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009525 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009526 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9527 const size_t overhead = (size_t) ret;
9528
9529 if( ret < 0 )
9530 return( ret );
9531
9532 if( mtu <= overhead )
9533 {
9534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9535 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9536 }
9537
9538 if( max_len > mtu - overhead )
9539 max_len = mtu - overhead;
9540 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009541#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009542
Hanno Becker0defedb2018-08-10 12:35:02 +01009543#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9544 !defined(MBEDTLS_SSL_PROTO_DTLS)
9545 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009546#endif
9547
9548 return( (int) max_len );
9549}
9550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551#if defined(MBEDTLS_X509_CRT_PARSE_C)
9552const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009553{
9554 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009555 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009556
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009557#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009558 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009559#else
9560 return( NULL );
9561#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009566int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9567 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009568{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009569 if( ssl == NULL ||
9570 dst == NULL ||
9571 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009572 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009575 }
9576
Hanno Becker58fccf22019-02-06 14:30:46 +00009577 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009580
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009581const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9582{
9583 if( ssl == NULL )
9584 return( NULL );
9585
9586 return( ssl->session );
9587}
9588
Paul Bakker5121ce52009-01-03 21:22:43 +00009589/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009590 * Define ticket header determining Mbed TLS version
9591 * and structure of the ticket.
9592 */
9593
Hanno Becker41527622019-05-16 12:50:45 +01009594/*
Hanno Becker26829e92019-05-28 14:30:45 +01009595 * Define bitflag determining compile-time settings influencing
9596 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009597 */
9598
Hanno Becker26829e92019-05-28 14:30:45 +01009599#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009600#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009601#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009602#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009603#endif /* MBEDTLS_HAVE_TIME */
9604
9605#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009606#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009607#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009608#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009609#endif /* MBEDTLS_X509_CRT_PARSE_C */
9610
9611#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009612#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009613#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009614#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009615#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9616
9617#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009618#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009619#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009620#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009621#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9622
9623#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009624#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009625#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009626#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009627#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9628
9629#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009630#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009631#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009632#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009633#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9634
Hanno Becker41527622019-05-16 12:50:45 +01009635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9636#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9637#else
9638#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9639#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9640
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009641#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9642#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9643#else
9644#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9645#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9646
Hanno Becker88440552019-07-03 14:16:13 +01009647#if defined(MBEDTLS_ZLIB_SUPPORT)
9648#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9649#else
9650#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9651#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9652
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009653#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9654#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9655#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9656#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9657#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9658#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9659#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009660#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009661#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009662
Hanno Becker26829e92019-05-28 14:30:45 +01009663#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009664 ( (uint16_t) ( \
9665 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9666 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9667 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9668 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9669 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9670 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009671 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009672 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009673 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009674
Hanno Becker557fe9f2019-05-16 12:41:07 +01009675static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009676 MBEDTLS_VERSION_MAJOR,
9677 MBEDTLS_VERSION_MINOR,
9678 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009679 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9680 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009681};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009682
9683/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009684 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009685 * (in the presentation language of TLS, RFC 8446 section 3)
9686 *
Hanno Becker26829e92019-05-28 14:30:45 +01009687 * opaque mbedtls_version[3]; // major, minor, patch
9688 * opaque session_format[2]; // version-specific 16-bit field determining
9689 * // the format of the remaining
9690 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009691 *
9692 * Note: When updating the format, remember to keep
9693 * these version+format bytes.
9694 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009695 * // In this version, `session_format` determines
9696 * // the setting of those compile-time
9697 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009698 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009699 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009700 * uint8 ciphersuite[2]; // defined by the standard
9701 * uint8 compression; // 0 or 1
9702 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009703 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009704 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009705 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009706 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9707 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9708 * case disabled: uint8_t peer_cert_digest_type;
9709 * opaque peer_cert_digest<0..2^8-1>;
9710 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009711 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009712 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009713 * uint8 mfl_code; // up to 255 according to standard
9714 * uint8 trunc_hmac; // 0 or 1
9715 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009716 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009717 * The order is the same as in the definition of the structure, except
9718 * verify_result is put before peer_cert so that all mandatory fields come
9719 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009720 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009721static int ssl_session_save( const mbedtls_ssl_session *session,
9722 unsigned char omit_header,
9723 unsigned char *buf,
9724 size_t buf_len,
9725 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009726{
9727 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009728 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009729#if defined(MBEDTLS_HAVE_TIME)
9730 uint64_t start;
9731#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009733#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009734 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009735#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009736#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009737
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009738 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009739 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009740 /*
9741 * Add version identifier
9742 */
9743
9744 used += sizeof( ssl_serialized_session_header );
9745
9746 if( used <= buf_len )
9747 {
9748 memcpy( p, ssl_serialized_session_header,
9749 sizeof( ssl_serialized_session_header ) );
9750 p += sizeof( ssl_serialized_session_header );
9751 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009752 }
9753
9754 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009755 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009756 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009757#if defined(MBEDTLS_HAVE_TIME)
9758 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009759
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009760 if( used <= buf_len )
9761 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009762 start = (uint64_t) session->start;
9763
9764 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9765 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9766 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9767 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9768 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9769 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9770 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9771 *p++ = (unsigned char)( ( start ) & 0xFF );
9772 }
9773#endif /* MBEDTLS_HAVE_TIME */
9774
9775 /*
9776 * Basic mandatory fields
9777 */
Hanno Becker88440552019-07-03 14:16:13 +01009778 {
9779 size_t const ciphersuite_len = 2;
9780#if defined(MBEDTLS_ZLIB_SUPPORT)
9781 size_t const compression_len = 1;
9782#else
9783 size_t const compression_len = 0;
9784#endif
9785 size_t const id_len_len = 1;
9786 size_t const id_len = 32;
9787 size_t const master_len = 48;
9788 size_t const verif_result_len = 4;
9789
9790 size_t const basic_len =
9791 ciphersuite_len +
9792 compression_len +
9793 id_len_len +
9794 id_len +
9795 master_len +
9796 verif_result_len;
9797
9798 used += basic_len;
9799 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009800
9801 if( used <= buf_len )
9802 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009803 const int ciphersuite =
9804 mbedtls_ssl_session_get_ciphersuite( session );
9805 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9806 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009807
Hanno Becker88440552019-07-03 14:16:13 +01009808#if defined(MBEDTLS_ZLIB_SUPPORT)
9809 *p++ = (unsigned char)(
9810 mbedtls_ssl_session_get_compression( session ) );
9811#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009812
9813 *p++ = (unsigned char)( session->id_len & 0xFF );
9814 memcpy( p, session->id, 32 );
9815 p += 32;
9816
9817 memcpy( p, session->master, 48 );
9818 p += 48;
9819
9820 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9821 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9822 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9823 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009824 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009825
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009826 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009827 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009828 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009829#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009830#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009831 if( session->peer_cert == NULL )
9832 cert_len = 0;
9833 else
9834 cert_len = session->peer_cert->raw.len;
9835
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009836 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009837
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009838 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009839 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009840 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9841 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9842 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9843
9844 if( session->peer_cert != NULL )
9845 {
9846 memcpy( p, session->peer_cert->raw.p, cert_len );
9847 p += cert_len;
9848 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009849 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009850
Hanno Becker5882dd02019-06-06 16:25:57 +01009851#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009852 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009853 if( session->peer_cert_digest != NULL )
9854 {
9855 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9856 if( used <= buf_len )
9857 {
9858 *p++ = (unsigned char) session->peer_cert_digest_type;
9859 *p++ = (unsigned char) session->peer_cert_digest_len;
9860 memcpy( p, session->peer_cert_digest,
9861 session->peer_cert_digest_len );
9862 p += session->peer_cert_digest_len;
9863 }
9864 }
9865 else
9866 {
9867 used += 2;
9868 if( used <= buf_len )
9869 {
9870 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9871 *p++ = 0;
9872 }
9873 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009874#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009875#endif /* MBEDTLS_X509_CRT_PARSE_C */
9876
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009877 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009878 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009879 */
9880#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009881 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009882
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009883 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009884 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009885 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9886 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9887 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9888
9889 if( session->ticket != NULL )
9890 {
9891 memcpy( p, session->ticket, session->ticket_len );
9892 p += session->ticket_len;
9893 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009894
9895 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9896 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9897 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9898 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009899 }
9900#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9901
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009902 /*
9903 * Misc extension-related info
9904 */
9905#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9906 used += 1;
9907
9908 if( used <= buf_len )
9909 *p++ = session->mfl_code;
9910#endif
9911
9912#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9913 used += 1;
9914
9915 if( used <= buf_len )
9916 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9917#endif
9918
9919#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9920 used += 1;
9921
9922 if( used <= buf_len )
9923 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9924#endif
9925
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009926 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009927 *olen = used;
9928
9929 if( used > buf_len )
9930 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009931
9932 return( 0 );
9933}
9934
9935/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009936 * Public wrapper for ssl_session_save()
9937 */
9938int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9939 unsigned char *buf,
9940 size_t buf_len,
9941 size_t *olen )
9942{
9943 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9944}
9945
9946/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009947 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009948 *
9949 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009950 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009951 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009952static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009953 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009954 const unsigned char *buf,
9955 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009956{
9957 const unsigned char *p = buf;
9958 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009959 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009960#if defined(MBEDTLS_HAVE_TIME)
9961 uint64_t start;
9962#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009963#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009964#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009965 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009966#endif
9967#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009968
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009969 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009970 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009971 /*
9972 * Check version identifier
9973 */
9974
9975 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9977
9978 if( memcmp( p, ssl_serialized_session_header,
9979 sizeof( ssl_serialized_session_header ) ) != 0 )
9980 {
9981 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9982 }
9983 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009984 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009985
9986 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009987 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009988 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009989#if defined(MBEDTLS_HAVE_TIME)
9990 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9992
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009993 start = ( (uint64_t) p[0] << 56 ) |
9994 ( (uint64_t) p[1] << 48 ) |
9995 ( (uint64_t) p[2] << 40 ) |
9996 ( (uint64_t) p[3] << 32 ) |
9997 ( (uint64_t) p[4] << 24 ) |
9998 ( (uint64_t) p[5] << 16 ) |
9999 ( (uint64_t) p[6] << 8 ) |
10000 ( (uint64_t) p[7] );
10001 p += 8;
10002
10003 session->start = (time_t) start;
10004#endif /* MBEDTLS_HAVE_TIME */
10005
10006 /*
10007 * Basic mandatory fields
10008 */
Hanno Becker88440552019-07-03 14:16:13 +010010009 {
10010 size_t const ciphersuite_len = 2;
10011#if defined(MBEDTLS_ZLIB_SUPPORT)
10012 size_t const compression_len = 1;
10013#else
10014 size_t const compression_len = 0;
10015#endif
10016 size_t const id_len_len = 1;
10017 size_t const id_len = 32;
10018 size_t const master_len = 48;
10019 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010020
Hanno Becker88440552019-07-03 14:16:13 +010010021 size_t const basic_len =
10022 ciphersuite_len +
10023 compression_len +
10024 id_len_len +
10025 id_len +
10026 master_len +
10027 verif_result_len;
10028
10029 if( basic_len > (size_t)( end - p ) )
10030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10031 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010032
Hanno Beckere02758c2019-06-26 15:31:31 +010010033 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010034 p += 2;
10035
Hanno Becker73f4cb12019-06-27 13:51:07 +010010036#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010037 session->ciphersuite = ciphersuite;
10038#else
10039 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010040 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010041 {
10042 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10043 }
10044#endif
10045
Hanno Becker88440552019-07-03 14:16:13 +010010046#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010047 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010048#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010049
10050 session->id_len = *p++;
10051 memcpy( session->id, p, 32 );
10052 p += 32;
10053
10054 memcpy( session->master, p, 48 );
10055 p += 48;
10056
10057 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10058 ( (uint32_t) p[1] << 16 ) |
10059 ( (uint32_t) p[2] << 8 ) |
10060 ( (uint32_t) p[3] );
10061 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010062
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010063 /* Immediately clear invalid pointer values that have been read, in case
10064 * we exit early before we replaced them with valid ones. */
10065#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010066#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010067 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010068#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010069 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010070#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010071#endif
10072#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10073 session->ticket = NULL;
10074#endif
10075
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010076 /*
10077 * Peer certificate
10078 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010079#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010080#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010081 if( 3 > (size_t)( end - p ) )
10082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10083
10084 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10085 p += 3;
10086
10087 if( cert_len == 0 )
10088 {
10089 session->peer_cert = NULL;
10090 }
10091 else
10092 {
10093 int ret;
10094
10095 if( cert_len > (size_t)( end - p ) )
10096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10097
10098 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10099
10100 if( session->peer_cert == NULL )
10101 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10102
10103 mbedtls_x509_crt_init( session->peer_cert );
10104
10105 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10106 p, cert_len ) ) != 0 )
10107 {
10108 mbedtls_x509_crt_free( session->peer_cert );
10109 mbedtls_free( session->peer_cert );
10110 session->peer_cert = NULL;
10111 return( ret );
10112 }
10113
10114 p += cert_len;
10115 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010116#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010117 /* Deserialize CRT digest from the end of the ticket. */
10118 if( 2 > (size_t)( end - p ) )
10119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10120
10121 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10122 session->peer_cert_digest_len = (size_t) *p++;
10123
10124 if( session->peer_cert_digest_len != 0 )
10125 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010126 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010127 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010128 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10130 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10131 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10132
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010133 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10134 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10135
10136 session->peer_cert_digest =
10137 mbedtls_calloc( 1, session->peer_cert_digest_len );
10138 if( session->peer_cert_digest == NULL )
10139 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10140
10141 memcpy( session->peer_cert_digest, p,
10142 session->peer_cert_digest_len );
10143 p += session->peer_cert_digest_len;
10144 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010145#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010146#endif /* MBEDTLS_X509_CRT_PARSE_C */
10147
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010148 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010149 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010150 */
10151#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10152 if( 3 > (size_t)( end - p ) )
10153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10154
10155 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10156 p += 3;
10157
10158 if( session->ticket_len != 0 )
10159 {
10160 if( session->ticket_len > (size_t)( end - p ) )
10161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10162
10163 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10164 if( session->ticket == NULL )
10165 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10166
10167 memcpy( session->ticket, p, session->ticket_len );
10168 p += session->ticket_len;
10169 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010170
10171 if( 4 > (size_t)( end - p ) )
10172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10173
10174 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10175 ( (uint32_t) p[1] << 16 ) |
10176 ( (uint32_t) p[2] << 8 ) |
10177 ( (uint32_t) p[3] );
10178 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010179#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10180
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010181 /*
10182 * Misc extension-related info
10183 */
10184#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10185 if( 1 > (size_t)( end - p ) )
10186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10187
10188 session->mfl_code = *p++;
10189#endif
10190
10191#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10192 if( 1 > (size_t)( end - p ) )
10193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10194
10195 session->trunc_hmac = *p++;
10196#endif
10197
10198#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10199 if( 1 > (size_t)( end - p ) )
10200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10201
10202 session->encrypt_then_mac = *p++;
10203#endif
10204
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010205 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010206 if( p != end )
10207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10208
10209 return( 0 );
10210}
10211
10212/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010213 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010214 */
10215int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10216 const unsigned char *buf,
10217 size_t len )
10218{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010219 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010220
10221 if( ret != 0 )
10222 mbedtls_ssl_session_free( session );
10223
10224 return( ret );
10225}
10226
10227/*
Paul Bakker1961b702013-01-25 14:49:24 +010010228 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010233
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010234 if( ssl == NULL || ssl->conf == NULL )
10235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010238 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010242 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010244#endif
10245
Hanno Beckerb82350b2019-07-26 07:24:05 +010010246 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010247 return( ret );
10248}
10249
10250/*
10251 * Perform the SSL handshake
10252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010254{
10255 int ret = 0;
10256
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010257 if( ssl == NULL || ssl->conf == NULL )
10258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010265
10266 if( ret != 0 )
10267 break;
10268 }
10269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010271
10272 return( ret );
10273}
10274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275#if defined(MBEDTLS_SSL_RENEGOTIATION)
10276#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010277/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010278 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010281{
10282 int ret;
10283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010285
10286 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10288 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010289
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010290 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010291 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010293 return( ret );
10294 }
10295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010297
10298 return( 0 );
10299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010301
10302/*
10303 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 * - any side: calling mbedtls_ssl_renegotiate(),
10305 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10306 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010307 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010308 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010312{
10313 int ret;
10314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010316
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010317 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10318 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010319
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010320 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10321 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010325 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010326 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10327 MBEDTLS_SSL_IS_SERVER )
10328 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010329 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010330 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010331 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010332 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010333 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010334 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010335 }
10336#endif
10337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10339 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010344 return( ret );
10345 }
10346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010348
10349 return( 0 );
10350}
10351
10352/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010353 * Renegotiate current connection on client,
10354 * or request renegotiation on server
10355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010359
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010360 if( ssl == NULL || ssl->conf == NULL )
10361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010364 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010365 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010371
10372 /* Did we already try/start sending HelloRequest? */
10373 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010375
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010376 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010380#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010381 /*
10382 * On client, either start the renegotiation process or,
10383 * if already in progress, continue the handshake
10384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010389
10390 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010393 return( ret );
10394 }
10395 }
10396 else
10397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010401 return( ret );
10402 }
10403 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010405
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010406 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010407}
10408
10409/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010410 * Check record counters and renegotiate if they're above the limit.
10411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010413{
Andres AG2196c7f2016-12-15 17:01:16 +000010414 size_t ep_len = ssl_ep_len( ssl );
10415 int in_ctr_cmp;
10416 int out_ctr_cmp;
10417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10419 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010420 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010421 {
10422 return( 0 );
10423 }
10424
Andres AG2196c7f2016-12-15 17:01:16 +000010425 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10426 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010427 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010428 ssl->conf->renego_period + ep_len, 8 - ep_len );
10429
10430 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010431 {
10432 return( 0 );
10433 }
10434
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010439
10440/*
10441 * Receive application data decrypted from the SSL layer
10442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010444{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010445 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010446 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010447
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010448 if( ssl == NULL || ssl->conf == NULL )
10449 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010454 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010457 return( ret );
10458
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010459 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010461 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010462 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010463 return( ret );
10464 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010465 }
10466#endif
10467
Hanno Becker4a810fb2017-05-24 16:27:30 +010010468 /*
10469 * Check if renegotiation is necessary and/or handshake is
10470 * in process. If yes, perform/continue, and fall through
10471 * if an unexpected packet is received while the client
10472 * is waiting for the ServerHello.
10473 *
10474 * (There is no equivalent to the last condition on
10475 * the server-side as it is not treated as within
10476 * a handshake while waiting for the ClientHello
10477 * after a renegotiation request.)
10478 */
10479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010481 ret = ssl_check_ctr_renegotiate( ssl );
10482 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10483 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010486 return( ret );
10487 }
10488#endif
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010493 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10494 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010497 return( ret );
10498 }
10499 }
10500
Hanno Beckere41158b2017-10-23 13:30:32 +010010501 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010502 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010503 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010504 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010505 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10506 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010507 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010508 ssl_set_timer( ssl,
10509 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010510 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010511
Hanno Becker327c93b2018-08-15 13:56:18 +010010512 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010513 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010514 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10515 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010516
Hanno Becker4a810fb2017-05-24 16:27:30 +010010517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10518 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010519 }
10520
10521 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010523 {
10524 /*
10525 * OpenSSL sends empty messages to randomize the IV
10526 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010527 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010530 return( 0 );
10531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010533 return( ret );
10534 }
10535 }
10536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010540
Hanno Becker4a810fb2017-05-24 16:27:30 +010010541 /*
10542 * - For client-side, expect SERVER_HELLO_REQUEST.
10543 * - For server-side, expect CLIENT_HELLO.
10544 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10545 */
10546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010548 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10549 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010550 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010551 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010554
10555 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010557 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010558 {
10559 continue;
10560 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010561 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010562#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010563#if defined(MBEDTLS_SSL_PROTO_TLS)
10564 {
10565 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10566 }
10567#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010568 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010569#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010570
Hanno Becker4a810fb2017-05-24 16:27:30 +010010571#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010572 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10573 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010577
10578 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010580 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010581 {
10582 continue;
10583 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010584 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010585#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010586#if defined(MBEDTLS_SSL_PROTO_TLS)
10587 {
10588 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10589 }
10590#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010591 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010592#endif /* MBEDTLS_SSL_SRV_C */
10593
Hanno Becker21df7f92017-10-17 11:03:26 +010010594#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010595 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010596 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10597 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010598 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010599 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10600 {
10601 /*
10602 * Accept renegotiation request
10603 */
Paul Bakker48916f92012-09-16 19:57:18 +000010604
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010605 /* DTLS clients need to know renego is server-initiated */
10606#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010607 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010608 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10609 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010610 {
10611 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10612 }
10613#endif
10614 ret = ssl_start_renegotiation( ssl );
10615 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10616 ret != 0 )
10617 {
10618 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10619 return( ret );
10620 }
10621 }
10622 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010623#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010624 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010625 /*
10626 * Refuse renegotiation
10627 */
10628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010632 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010633 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010634 /* SSLv3 does not have a "no_renegotiation" warning, so
10635 we send a fatal alert and abort the connection. */
10636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10637 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10638 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010639 }
10640 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10642#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10643 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010644 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010645 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010646 ret = mbedtls_ssl_send_alert_message( ssl,
10647 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10648 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10649 if( ret != 0 )
10650 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010652 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10654 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010658 }
Paul Bakker48916f92012-09-16 19:57:18 +000010659 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010660
Hanno Becker90333da2017-10-10 11:27:13 +010010661 /* At this point, we don't know whether the renegotiation has been
10662 * completed or not. The cases to consider are the following:
10663 * 1) The renegotiation is complete. In this case, no new record
10664 * has been read yet.
10665 * 2) The renegotiation is incomplete because the client received
10666 * an application data record while awaiting the ServerHello.
10667 * 3) The renegotiation is incomplete because the client received
10668 * a non-handshake, non-application data message while awaiting
10669 * the ServerHello.
10670 * In each of these case, looping will be the proper action:
10671 * - For 1), the next iteration will read a new record and check
10672 * if it's application data.
10673 * - For 2), the loop condition isn't satisfied as application data
10674 * is present, hence continue is the same as break
10675 * - For 3), the loop condition is satisfied and read_record
10676 * will re-deliver the message that was held back by the client
10677 * when expecting the ServerHello.
10678 */
10679 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010680 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010681#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010683 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010684 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010685 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010686 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010689 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010691 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010692 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10697 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010700 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010701 }
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10706 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010707 }
10708
10709 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010710
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010711 /* We're going to return something now, cancel timer,
10712 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010714 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010715
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010717 /* If we requested renego but received AppData, resend HelloRequest.
10718 * Do it now, after setting in_offt, to avoid taking this branch
10719 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010721 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10722 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010724 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010725 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010728 return( ret );
10729 }
10730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010732#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010733 }
10734
10735 n = ( len < ssl->in_msglen )
10736 ? len : ssl->in_msglen;
10737
10738 memcpy( buf, ssl->in_offt, n );
10739 ssl->in_msglen -= n;
10740
10741 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010742 {
10743 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010744 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010745 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010746 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010747 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010748 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010749 /* more data available */
10750 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010751 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010754
Paul Bakker23986e52011-04-24 08:57:21 +000010755 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010756}
10757
10758/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010759 * Send application data to be encrypted by the SSL layer, taking care of max
10760 * fragment length and buffer size.
10761 *
10762 * According to RFC 5246 Section 6.2.1:
10763 *
10764 * Zero-length fragments of Application data MAY be sent as they are
10765 * potentially useful as a traffic analysis countermeasure.
10766 *
10767 * Therefore, it is possible that the input message length is 0 and the
10768 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010769 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010770static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010771 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010772{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010773 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10774 const size_t max_len = (size_t) ret;
10775
10776 if( ret < 0 )
10777 {
10778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10779 return( ret );
10780 }
10781
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010782 if( len > max_len )
10783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010785 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010788 "maximum fragment length: %d > %d",
10789 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010791 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010792 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010793#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010794#if defined(MBEDTLS_SSL_PROTO_TLS)
10795 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010796 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010797 }
10798#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010799 }
Paul Bakker887bd502011-06-08 13:10:54 +000010800
Paul Bakker5121ce52009-01-03 21:22:43 +000010801 if( ssl->out_left != 0 )
10802 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010803 /*
10804 * The user has previously tried to send the data and
10805 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10806 * written. In this case, we expect the high-level write function
10807 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010812 return( ret );
10813 }
10814 }
Paul Bakker887bd502011-06-08 13:10:54 +000010815 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010816 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010817 /*
10818 * The user is trying to send a message the first time, so we need to
10819 * copy the data into the internal buffers and setup the data structure
10820 * to keep track of partial writes
10821 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010822 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010823 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010824 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010825
Hanno Becker67bc7c32018-08-06 11:33:50 +010010826 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010829 return( ret );
10830 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010831 }
10832
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010833 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010834}
10835
10836/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010837 * Write application data, doing 1/n-1 splitting if necessary.
10838 *
10839 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010840 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010841 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010843#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010844static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010845 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010846{
10847 int ret;
10848
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010849 if( ssl->conf->cbc_record_splitting ==
10850 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010851 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010852 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10854 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010855 {
10856 return( ssl_write_real( ssl, buf, len ) );
10857 }
10858
10859 if( ssl->split_done == 0 )
10860 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010861 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010862 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010863 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010864 }
10865
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010866 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10867 return( ret );
10868 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010869
10870 return( ret + 1 );
10871}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010873
10874/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010875 * Write application data (public-facing wrapper)
10876 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010877int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010878{
10879 int ret;
10880
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010882
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010883 if( ssl == NULL || ssl->conf == NULL )
10884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10885
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010886#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010887 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10888 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010889 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010890 return( ret );
10891 }
10892#endif
10893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010894 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010895 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010896 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010897 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010899 return( ret );
10900 }
10901 }
10902
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010903#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010904 ret = ssl_write_split( ssl, buf, len );
10905#else
10906 ret = ssl_write_real( ssl, buf, len );
10907#endif
10908
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010910
10911 return( ret );
10912}
10913
10914/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010915 * Notify the peer that the connection is being closed
10916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010918{
10919 int ret;
10920
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010921 if( ssl == NULL || ssl->conf == NULL )
10922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010925
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010926 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10932 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10933 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010936 return( ret );
10937 }
10938 }
10939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010941
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010942 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010943}
10944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010945void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010946{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010947 if( transform == NULL )
10948 return;
10949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010951 deflateEnd( &transform->ctx_deflate );
10952 inflateEnd( &transform->ctx_inflate );
10953#endif
10954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10956 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010957
Hanno Becker92231322018-01-03 15:32:51 +000010958#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959 mbedtls_md_free( &transform->md_ctx_enc );
10960 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010961#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010962
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010963 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010964}
10965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_X509_CRT_PARSE_C)
10967static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010970
10971 while( cur != NULL )
10972 {
10973 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010975 cur = next;
10976 }
10977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010979
Hanno Becker0271f962018-08-16 13:23:47 +010010980#if defined(MBEDTLS_SSL_PROTO_DTLS)
10981
10982static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10983{
10984 unsigned offset;
10985 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10986
10987 if( hs == NULL )
10988 return;
10989
Hanno Becker283f5ef2018-08-24 09:34:47 +010010990 ssl_free_buffered_record( ssl );
10991
Hanno Becker0271f962018-08-16 13:23:47 +010010992 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010993 ssl_buffering_free_slot( ssl, offset );
10994}
10995
10996static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10997 uint8_t slot )
10998{
10999 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11000 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011001
11002 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11003 return;
11004
Hanno Beckere605b192018-08-21 15:59:07 +010011005 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011006 {
Hanno Beckere605b192018-08-21 15:59:07 +010011007 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011008 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011009 mbedtls_free( hs_buf->data );
11010 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011011 }
11012}
11013
11014#endif /* MBEDTLS_SSL_PROTO_DTLS */
11015
Gilles Peskine9b562d52018-04-25 20:32:43 +020011016void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011017{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011018 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11019
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011020 if( handshake == NULL )
11021 return;
11022
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011023#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11024 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11025 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011026 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011027 handshake->async_in_progress = 0;
11028 }
11029#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11030
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011031#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11032 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11033 mbedtls_md5_free( &handshake->fin_md5 );
11034 mbedtls_sha1_free( &handshake->fin_sha1 );
11035#endif
11036#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11037#if defined(MBEDTLS_SHA256_C)
11038 mbedtls_sha256_free( &handshake->fin_sha256 );
11039#endif
11040#if defined(MBEDTLS_SHA512_C)
11041 mbedtls_sha512_free( &handshake->fin_sha512 );
11042#endif
11043#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045#if defined(MBEDTLS_DHM_C)
11046 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048#if defined(MBEDTLS_ECDH_C)
11049 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011050#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011051#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011052 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011053#if defined(MBEDTLS_SSL_CLI_C)
11054 mbedtls_free( handshake->ecjpake_cache );
11055 handshake->ecjpake_cache = NULL;
11056 handshake->ecjpake_cache_len = 0;
11057#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011058#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011059
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011060#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11061 if( handshake->psk != NULL )
11062 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011063 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011064 mbedtls_free( handshake->psk );
11065 }
11066#endif
11067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11069 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011070 /*
11071 * Free only the linked list wrapper, not the keys themselves
11072 * since the belong to the SNI callback
11073 */
11074 if( handshake->sni_key_cert != NULL )
11075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011077
11078 while( cur != NULL )
11079 {
11080 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011082 cur = next;
11083 }
11084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011086
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011087#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011088 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011089 if( handshake->ecrs_peer_cert != NULL )
11090 {
11091 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11092 mbedtls_free( handshake->ecrs_peer_cert );
11093 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011094#endif
11095
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011096#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11097 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11098 mbedtls_pk_free( &handshake->peer_pubkey );
11099#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101#if defined(MBEDTLS_SSL_PROTO_DTLS)
11102 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011103 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011104 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011105#endif
11106
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011107 mbedtls_platform_zeroize( handshake,
11108 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011109}
11110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011112{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011113 if( session == NULL )
11114 return;
11115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011117 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011118#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011119
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011120#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011122#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011123
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011124 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011125}
11126
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011127#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011128
11129#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11130#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11131#else
11132#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11133#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11134
11135#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11136#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11137#else
11138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11139#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11140
11141#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11142#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11143#else
11144#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11145#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11146
11147#if defined(MBEDTLS_SSL_ALPN)
11148#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11149#else
11150#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11151#endif /* MBEDTLS_SSL_ALPN */
11152
11153#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11154#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11155#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11156#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11157
11158#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11159 ( (uint32_t) ( \
11160 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11161 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11162 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11163 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11164 0u ) )
11165
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011166static unsigned char ssl_serialized_context_header[] = {
11167 MBEDTLS_VERSION_MAJOR,
11168 MBEDTLS_VERSION_MINOR,
11169 MBEDTLS_VERSION_PATCH,
11170 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11171 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011172 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11173 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11174 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011175};
11176
Paul Bakker5121ce52009-01-03 21:22:43 +000011177/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011178 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011179 *
11180 * The format of the serialized data is:
11181 * (in the presentation language of TLS, RFC 8446 section 3)
11182 *
11183 * // header
11184 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011185 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011186 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011187 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011188 * Note: When updating the format, remember to keep these
11189 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011190 *
11191 * // session sub-structure
11192 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11193 * // transform sub-structure
11194 * uint8 random[64]; // ServerHello.random+ClientHello.random
11195 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11196 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11197 * // fields from ssl_context
11198 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11199 * uint64 in_window_top; // DTLS: last validated record seq_num
11200 * uint64 in_window; // DTLS: bitmask for replay protection
11201 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11202 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11203 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11204 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11205 *
11206 * Note that many fields of the ssl_context or sub-structures are not
11207 * serialized, as they fall in one of the following categories:
11208 *
11209 * 1. forced value (eg in_left must be 0)
11210 * 2. pointer to dynamically-allocated memory (eg session, transform)
11211 * 3. value can be re-derived from other data (eg session keys from MS)
11212 * 4. value was temporary (eg content of input buffer)
11213 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011214 */
11215int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11216 unsigned char *buf,
11217 size_t buf_len,
11218 size_t *olen )
11219{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011220 unsigned char *p = buf;
11221 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011222 size_t session_len;
11223 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011224
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011225 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011226 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11227 * this function's documentation.
11228 *
11229 * These are due to assumptions/limitations in the implementation. Some of
11230 * them are likely to stay (no handshake in progress) some might go away
11231 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011232 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011233 /* The initial handshake must be over */
11234 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011236 if( ssl->handshake != NULL )
11237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11238 /* Double-check that sub-structures are indeed ready */
11239 if( ssl->transform == NULL || ssl->session == NULL )
11240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11241 /* There must be no pending incoming or outgoing data */
11242 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11244 if( ssl->out_left != 0 )
11245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11246 /* Protocol must be DLTS, not TLS */
11247 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11249 /* Version must be 1.2 */
11250 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11252 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11254 /* We must be using an AEAD ciphersuite */
11255 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11257 /* Renegotiation must not be enabled */
11258 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011260
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011261 /*
11262 * Version and format identifier
11263 */
11264 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011265
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011266 if( used <= buf_len )
11267 {
11268 memcpy( p, ssl_serialized_context_header,
11269 sizeof( ssl_serialized_context_header ) );
11270 p += sizeof( ssl_serialized_context_header );
11271 }
11272
11273 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011274 * Session (length + data)
11275 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011276 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011277 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11278 return( ret );
11279
11280 used += 4 + session_len;
11281 if( used <= buf_len )
11282 {
11283 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11284 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11285 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11286 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11287
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011288 ret = ssl_session_save( ssl->session, 1,
11289 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011290 if( ret != 0 )
11291 return( ret );
11292
11293 p += session_len;
11294 }
11295
11296 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011297 * Transform
11298 */
11299 used += sizeof( ssl->transform->randbytes );
11300 if( used <= buf_len )
11301 {
11302 memcpy( p, ssl->transform->randbytes,
11303 sizeof( ssl->transform->randbytes ) );
11304 p += sizeof( ssl->transform->randbytes );
11305 }
11306
11307#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11308 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11309 if( used <= buf_len )
11310 {
11311 *p++ = ssl->transform->in_cid_len;
11312 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11313 p += ssl->transform->in_cid_len;
11314
11315 *p++ = ssl->transform->out_cid_len;
11316 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11317 p += ssl->transform->out_cid_len;
11318 }
11319#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11320
11321 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011322 * Saved fields from top-level ssl_context structure
11323 */
11324#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11325 used += 4;
11326 if( used <= buf_len )
11327 {
11328 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11329 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11330 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11331 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11332 }
11333#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11334
11335#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11336 used += 16;
11337 if( used <= buf_len )
11338 {
11339 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11340 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11341 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11342 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11343 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11344 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11345 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11346 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11347
11348 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11349 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11350 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11351 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11352 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11353 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11354 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11355 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11356 }
11357#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11358
11359#if defined(MBEDTLS_SSL_PROTO_DTLS)
11360 used += 1;
11361 if( used <= buf_len )
11362 {
11363 *p++ = ssl->disable_datagram_packing;
11364 }
11365#endif /* MBEDTLS_SSL_PROTO_DTLS */
11366
11367 used += 8;
11368 if( used <= buf_len )
11369 {
11370 memcpy( p, ssl->cur_out_ctr, 8 );
11371 p += 8;
11372 }
11373
11374#if defined(MBEDTLS_SSL_PROTO_DTLS)
11375 used += 2;
11376 if( used <= buf_len )
11377 {
11378 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11379 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11380 }
11381#endif /* MBEDTLS_SSL_PROTO_DTLS */
11382
11383#if defined(MBEDTLS_SSL_ALPN)
11384 {
11385 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011386 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011387 : 0;
11388
11389 used += 1 + alpn_len;
11390 if( used <= buf_len )
11391 {
11392 *p++ = alpn_len;
11393
11394 if( ssl->alpn_chosen != NULL )
11395 {
11396 memcpy( p, ssl->alpn_chosen, alpn_len );
11397 p += alpn_len;
11398 }
11399 }
11400 }
11401#endif /* MBEDTLS_SSL_ALPN */
11402
11403 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011404 * Done
11405 */
11406 *olen = used;
11407
11408 if( used > buf_len )
11409 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011410
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011411 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11412
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011413 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011414}
11415
11416/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011417 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011418 *
11419 * This internal version is wrapped by a public function that cleans up in
11420 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011421 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011422static int ssl_context_load( mbedtls_ssl_context *ssl,
11423 const unsigned char *buf,
11424 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011425{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011426 const unsigned char *p = buf;
11427 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011428 size_t session_len;
11429 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011430
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011431 /*
11432 * The context should have been freshly setup or reset.
11433 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011434 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011435 * renegotiating, or if the user mistakenly loaded a session first.)
11436 */
11437 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11438 ssl->session != NULL )
11439 {
11440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11441 }
11442
11443 /*
11444 * We can't check that the config matches the initial one, but we can at
11445 * least check it matches the requirements for serializing.
11446 */
11447 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011448 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11449 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11450 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11451 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11452 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11453 MBEDTLS_SSL_MINOR_VERSION_3 ||
11454 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11455 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011456 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011457 {
11458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11459 }
11460
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011461 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11462
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011463 /*
11464 * Check version identifier
11465 */
11466 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11468
11469 if( memcmp( p, ssl_serialized_context_header,
11470 sizeof( ssl_serialized_context_header ) ) != 0 )
11471 {
11472 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11473 }
11474 p += sizeof( ssl_serialized_context_header );
11475
11476 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011477 * Session
11478 */
11479 if( (size_t)( end - p ) < 4 )
11480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11481
11482 session_len = ( (size_t) p[0] << 24 ) |
11483 ( (size_t) p[1] << 16 ) |
11484 ( (size_t) p[2] << 8 ) |
11485 ( (size_t) p[3] );
11486 p += 4;
11487
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011488 /* This has been allocated by ssl_handshake_init(), called by
11489 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11490 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011491 ssl->session_in = ssl->session;
11492 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011493 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011494
11495 if( (size_t)( end - p ) < session_len )
11496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11497
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011498 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011499 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011500 {
11501 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011502 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011503 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011504
11505 p += session_len;
11506
11507 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011508 * Transform
11509 */
11510
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011511 /* This has been allocated by ssl_handshake_init(), called by
11512 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11513 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011514 ssl->transform_in = ssl->transform;
11515 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011516 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011517
11518 /* Read random bytes and populate structure */
11519 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11521
11522 ret = ssl_populate_transform( ssl->transform,
11523 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11524 ssl->session->master,
11525#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11526#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11527 ssl->session->encrypt_then_mac,
11528#endif
11529#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11530 ssl->session->trunc_hmac,
11531#endif
11532#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11533#if defined(MBEDTLS_ZLIB_SUPPORT)
11534 ssl->session->compression,
11535#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011536 p, /* currently pointing to randbytes */
11537 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11538 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11539 ssl );
11540 if( ret != 0 )
11541 return( ret );
11542
11543 p += sizeof( ssl->transform->randbytes );
11544
11545#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11546 /* Read connection IDs and store them */
11547 if( (size_t)( end - p ) < 1 )
11548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11549
11550 ssl->transform->in_cid_len = *p++;
11551
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011552 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11554
11555 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11556 p += ssl->transform->in_cid_len;
11557
11558 ssl->transform->out_cid_len = *p++;
11559
11560 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11562
11563 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11564 p += ssl->transform->out_cid_len;
11565#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11566
11567 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011568 * Saved fields from top-level ssl_context structure
11569 */
11570#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11571 if( (size_t)( end - p ) < 4 )
11572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11573
11574 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11575 ( (uint32_t) p[1] << 16 ) |
11576 ( (uint32_t) p[2] << 8 ) |
11577 ( (uint32_t) p[3] );
11578 p += 4;
11579#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11580
11581#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11582 if( (size_t)( end - p ) < 16 )
11583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11584
11585 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11586 ( (uint64_t) p[1] << 48 ) |
11587 ( (uint64_t) p[2] << 40 ) |
11588 ( (uint64_t) p[3] << 32 ) |
11589 ( (uint64_t) p[4] << 24 ) |
11590 ( (uint64_t) p[5] << 16 ) |
11591 ( (uint64_t) p[6] << 8 ) |
11592 ( (uint64_t) p[7] );
11593 p += 8;
11594
11595 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11596 ( (uint64_t) p[1] << 48 ) |
11597 ( (uint64_t) p[2] << 40 ) |
11598 ( (uint64_t) p[3] << 32 ) |
11599 ( (uint64_t) p[4] << 24 ) |
11600 ( (uint64_t) p[5] << 16 ) |
11601 ( (uint64_t) p[6] << 8 ) |
11602 ( (uint64_t) p[7] );
11603 p += 8;
11604#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11605
11606#if defined(MBEDTLS_SSL_PROTO_DTLS)
11607 if( (size_t)( end - p ) < 1 )
11608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11609
11610 ssl->disable_datagram_packing = *p++;
11611#endif /* MBEDTLS_SSL_PROTO_DTLS */
11612
11613 if( (size_t)( end - p ) < 8 )
11614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11615
11616 memcpy( ssl->cur_out_ctr, p, 8 );
11617 p += 8;
11618
11619#if defined(MBEDTLS_SSL_PROTO_DTLS)
11620 if( (size_t)( end - p ) < 2 )
11621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11622
11623 ssl->mtu = ( p[0] << 8 ) | p[1];
11624 p += 2;
11625#endif /* MBEDTLS_SSL_PROTO_DTLS */
11626
11627#if defined(MBEDTLS_SSL_ALPN)
11628 {
11629 uint8_t alpn_len;
11630 const char **cur;
11631
11632 if( (size_t)( end - p ) < 1 )
11633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11634
11635 alpn_len = *p++;
11636
11637 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11638 {
11639 /* alpn_chosen should point to an item in the configured list */
11640 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11641 {
11642 if( strlen( *cur ) == alpn_len &&
11643 memcmp( p, cur, alpn_len ) == 0 )
11644 {
11645 ssl->alpn_chosen = *cur;
11646 break;
11647 }
11648 }
11649 }
11650
11651 /* can only happen on conf mismatch */
11652 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11654
11655 p += alpn_len;
11656 }
11657#endif /* MBEDTLS_SSL_ALPN */
11658
11659 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011660 * Forced fields from top-level ssl_context structure
11661 *
11662 * Most of them already set to the correct value by mbedtls_ssl_init() and
11663 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11664 */
11665 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11666
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011667#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011668 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011669#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11670#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011671 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011672#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011673
Hanno Becker83985822019-08-30 10:42:49 +010011674 /* Adjust pointers for header fields of outgoing records to
11675 * the given transform, accounting for explicit IV and CID. */
11676 ssl_update_out_pointers( ssl, ssl->transform );
11677
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011678#if defined(MBEDTLS_SSL_PROTO_DTLS)
11679 ssl->in_epoch = 1;
11680#endif
11681
11682 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11683 * which we don't want - otherwise we'd end up freeing the wrong transform
11684 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11685 if( ssl->handshake != NULL )
11686 {
11687 mbedtls_ssl_handshake_free( ssl );
11688 mbedtls_free( ssl->handshake );
11689 ssl->handshake = NULL;
11690 }
11691
11692 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011693 * Done - should have consumed entire buffer
11694 */
11695 if( p != end )
11696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011697
11698 return( 0 );
11699}
11700
11701/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011702 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011703 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011704int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011705 const unsigned char *buf,
11706 size_t len )
11707{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011708 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011709
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011710 if( ret != 0 )
11711 mbedtls_ssl_free( context );
11712
11713 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011714}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011715#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011716
11717/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011718 * Free an SSL context
11719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011720void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011721{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011722 if( ssl == NULL )
11723 return;
11724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011726
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011727 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011728 {
Angus Grattond8213d02016-05-25 20:56:48 +100011729 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011731 }
11732
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011733 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011734 {
Angus Grattond8213d02016-05-25 20:56:48 +100011735 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011736 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011737 }
11738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011739#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011740 if( ssl->compress_buf != NULL )
11741 {
Angus Grattond8213d02016-05-25 20:56:48 +100011742 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011743 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011744 }
11745#endif
11746
Paul Bakker48916f92012-09-16 19:57:18 +000011747 if( ssl->transform )
11748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011749 mbedtls_ssl_transform_free( ssl->transform );
11750 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011751 }
11752
11753 if( ssl->handshake )
11754 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011755 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011756 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11757 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011759 mbedtls_free( ssl->handshake );
11760 mbedtls_free( ssl->transform_negotiate );
11761 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011762 }
11763
Paul Bakkerc0463502013-02-14 11:19:38 +010011764 if( ssl->session )
11765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011766 mbedtls_ssl_session_free( ssl->session );
11767 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011768 }
11769
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011770#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011771 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011772 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011773 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011774 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011775 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011776#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011778#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11779 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11782 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011783 }
11784#endif
11785
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011786#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011787 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011788#endif
11789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011791
Paul Bakker86f04f42013-02-14 11:20:09 +010011792 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011793 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011794}
11795
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011796/*
11797 * Initialze mbedtls_ssl_config
11798 */
11799void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11800{
11801 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011802
11803#if !defined(MBEDTLS_SSL_PROTO_TLS)
11804 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11805#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011806}
11807
Simon Butcherc97b6972015-12-27 23:48:17 +000011808#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011809#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011810static int ssl_preset_default_hashes[] = {
11811#if defined(MBEDTLS_SHA512_C)
11812 MBEDTLS_MD_SHA512,
11813 MBEDTLS_MD_SHA384,
11814#endif
11815#if defined(MBEDTLS_SHA256_C)
11816 MBEDTLS_MD_SHA256,
11817 MBEDTLS_MD_SHA224,
11818#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011819#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011820 MBEDTLS_MD_SHA1,
11821#endif
11822 MBEDTLS_MD_NONE
11823};
Simon Butcherc97b6972015-12-27 23:48:17 +000011824#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011825#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011826
Hanno Becker73f4cb12019-06-27 13:51:07 +010011827#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011828static int ssl_preset_suiteb_ciphersuites[] = {
11829 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11830 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11831 0
11832};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011833#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011834
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011835#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011836#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011837static int ssl_preset_suiteb_hashes[] = {
11838 MBEDTLS_MD_SHA256,
11839 MBEDTLS_MD_SHA384,
11840 MBEDTLS_MD_NONE
11841};
11842#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011843#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011844
Hanno Beckerc1096e72019-06-19 12:30:41 +010011845#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011846static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011847#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011848 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011849#endif
11850#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011851 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011852#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011853 MBEDTLS_ECP_DP_NONE
11854};
11855#endif
11856
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011857/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011858 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011859 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011860int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011861 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011862{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011863#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011864 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011865#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011866
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011867 /* Use the functions here so that they are covered in tests,
11868 * but otherwise access member directly for efficiency */
11869 mbedtls_ssl_conf_endpoint( conf, endpoint );
11870 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011871
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011872 /*
11873 * Things that are common to all presets
11874 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011875#if defined(MBEDTLS_SSL_CLI_C)
11876 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11877 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011878#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011879 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011880#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011881#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11882 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11883#endif
11884 }
11885#endif
11886
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011887#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011888 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011889#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011890
11891#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11892 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11893#endif
11894
11895#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011896#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011897 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011898#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11899#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011900 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011901 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011902#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011903#endif
11904
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011905#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11906 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11907#endif
11908
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011909#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011910 conf->f_cookie_write = ssl_cookie_write_dummy;
11911 conf->f_cookie_check = ssl_cookie_check_dummy;
11912#endif
11913
Hanno Becker7f376f42019-06-12 16:20:48 +010011914#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11915 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011916 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11917#endif
11918
Janos Follath088ce432017-04-10 12:42:31 +010011919#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011920#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011921 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011922#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11923#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011924
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011926#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011927 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011928#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11929#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011930 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011931#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11932#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011933
11934#if defined(MBEDTLS_SSL_RENEGOTIATION)
11935 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011936 memset( conf->renego_period, 0x00, 2 );
11937 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011938#endif
11939
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011940#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11941 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11942 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011943 const unsigned char dhm_p[] =
11944 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11945 const unsigned char dhm_g[] =
11946 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11947
Hanno Beckera90658f2017-10-04 15:29:08 +010011948 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11949 dhm_p, sizeof( dhm_p ),
11950 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011951 {
11952 return( ret );
11953 }
11954 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011955#endif
11956
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011957 /*
11958 * Preset-specific defaults
11959 */
11960 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011961 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011962 /*
11963 * NSA Suite B
11964 */
11965 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011966#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011967 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011968#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11969#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011970 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011971#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11972#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011974#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11975#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011976 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011977#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011978
Hanno Becker73f4cb12019-06-27 13:51:07 +010011979#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011980 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11981 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11982 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11983 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11984 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011985#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011986
11987#if defined(MBEDTLS_X509_CRT_PARSE_C)
11988 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011989#endif
11990
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011991#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011992#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011993 conf->sig_hashes = ssl_preset_suiteb_hashes;
11994#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011995#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011996
11997#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011998#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011999 conf->curve_list = ssl_preset_suiteb_curves;
12000#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012001#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012002 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012003
12004 /*
12005 * Default
12006 */
12007 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012008#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012009 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12010 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12011 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12012 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012013#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12014#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012015 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12016 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12017 MBEDTLS_SSL_MIN_MINOR_VERSION :
12018 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012020 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012021 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12022#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012023#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12024#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12025 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12026#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12027#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12028 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12029#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012030
Hanno Becker73f4cb12019-06-27 13:51:07 +010012031#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012032 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
12033 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
12034 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
12035 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
12036 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012037#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012038
12039#if defined(MBEDTLS_X509_CRT_PARSE_C)
12040 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12041#endif
12042
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012043#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012044#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012045 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012046#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012047#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012048
12049#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012050#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012051 conf->curve_list = mbedtls_ecp_grp_id_list();
12052#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012053#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012054
12055#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12056 conf->dhm_min_bitlen = 1024;
12057#endif
12058 }
12059
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012060 return( 0 );
12061}
12062
12063/*
12064 * Free mbedtls_ssl_config
12065 */
12066void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12067{
12068#if defined(MBEDTLS_DHM_C)
12069 mbedtls_mpi_free( &conf->dhm_P );
12070 mbedtls_mpi_free( &conf->dhm_G );
12071#endif
12072
12073#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12074 if( conf->psk != NULL )
12075 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012076 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012077 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012078 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012079 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012080 }
12081
12082 if( conf->psk_identity != NULL )
12083 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012084 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012085 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012086 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012087 conf->psk_identity_len = 0;
12088 }
12089#endif
12090
12091#if defined(MBEDTLS_X509_CRT_PARSE_C)
12092 ssl_key_cert_free( conf->key_cert );
12093#endif
12094
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012095 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012096}
12097
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012098#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012099 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12100 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012101/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012102 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012104unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012106#if defined(MBEDTLS_RSA_C)
12107 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12108 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012109#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012110#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012111 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12112 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012114 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012115}
12116
Hanno Becker7e5437a2017-04-28 17:15:26 +010012117unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12118{
12119 switch( type ) {
12120 case MBEDTLS_PK_RSA:
12121 return( MBEDTLS_SSL_SIG_RSA );
12122 case MBEDTLS_PK_ECDSA:
12123 case MBEDTLS_PK_ECKEY:
12124 return( MBEDTLS_SSL_SIG_ECDSA );
12125 default:
12126 return( MBEDTLS_SSL_SIG_ANON );
12127 }
12128}
12129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012130mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012131{
12132 switch( sig )
12133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012134#if defined(MBEDTLS_RSA_C)
12135 case MBEDTLS_SSL_SIG_RSA:
12136 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012137#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012138#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012139 case MBEDTLS_SSL_SIG_ECDSA:
12140 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012141#endif
12142 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012143 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012144 }
12145}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012146#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012147
Hanno Becker7e5437a2017-04-28 17:15:26 +010012148#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12149 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12150
12151/* Find an entry in a signature-hash set matching a given hash algorithm. */
12152mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12153 mbedtls_pk_type_t sig_alg )
12154{
12155 switch( sig_alg )
12156 {
12157 case MBEDTLS_PK_RSA:
12158 return( set->rsa );
12159 case MBEDTLS_PK_ECDSA:
12160 return( set->ecdsa );
12161 default:
12162 return( MBEDTLS_MD_NONE );
12163 }
12164}
12165
12166/* Add a signature-hash-pair to a signature-hash set */
12167void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12168 mbedtls_pk_type_t sig_alg,
12169 mbedtls_md_type_t md_alg )
12170{
12171 switch( sig_alg )
12172 {
12173 case MBEDTLS_PK_RSA:
12174 if( set->rsa == MBEDTLS_MD_NONE )
12175 set->rsa = md_alg;
12176 break;
12177
12178 case MBEDTLS_PK_ECDSA:
12179 if( set->ecdsa == MBEDTLS_MD_NONE )
12180 set->ecdsa = md_alg;
12181 break;
12182
12183 default:
12184 break;
12185 }
12186}
12187
12188/* Allow exactly one hash algorithm for each signature. */
12189void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12190 mbedtls_md_type_t md_alg )
12191{
12192 set->rsa = md_alg;
12193 set->ecdsa = md_alg;
12194}
12195
12196#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12197 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12198
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012199/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012200 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012202mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012203{
12204 switch( hash )
12205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012206#if defined(MBEDTLS_MD5_C)
12207 case MBEDTLS_SSL_HASH_MD5:
12208 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012210#if defined(MBEDTLS_SHA1_C)
12211 case MBEDTLS_SSL_HASH_SHA1:
12212 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012214#if defined(MBEDTLS_SHA256_C)
12215 case MBEDTLS_SSL_HASH_SHA224:
12216 return( MBEDTLS_MD_SHA224 );
12217 case MBEDTLS_SSL_HASH_SHA256:
12218 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012220#if defined(MBEDTLS_SHA512_C)
12221 case MBEDTLS_SSL_HASH_SHA384:
12222 return( MBEDTLS_MD_SHA384 );
12223 case MBEDTLS_SSL_HASH_SHA512:
12224 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012225#endif
12226 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012227 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012228 }
12229}
12230
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012231/*
12232 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12233 */
12234unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12235{
12236 switch( md )
12237 {
12238#if defined(MBEDTLS_MD5_C)
12239 case MBEDTLS_MD_MD5:
12240 return( MBEDTLS_SSL_HASH_MD5 );
12241#endif
12242#if defined(MBEDTLS_SHA1_C)
12243 case MBEDTLS_MD_SHA1:
12244 return( MBEDTLS_SSL_HASH_SHA1 );
12245#endif
12246#if defined(MBEDTLS_SHA256_C)
12247 case MBEDTLS_MD_SHA224:
12248 return( MBEDTLS_SSL_HASH_SHA224 );
12249 case MBEDTLS_MD_SHA256:
12250 return( MBEDTLS_SSL_HASH_SHA256 );
12251#endif
12252#if defined(MBEDTLS_SHA512_C)
12253 case MBEDTLS_MD_SHA384:
12254 return( MBEDTLS_SSL_HASH_SHA384 );
12255 case MBEDTLS_MD_SHA512:
12256 return( MBEDTLS_SSL_HASH_SHA512 );
12257#endif
12258 default:
12259 return( MBEDTLS_SSL_HASH_NONE );
12260 }
12261}
12262
Hanno Beckeree902df2019-08-23 13:47:47 +010012263#if defined(MBEDTLS_USE_TINYCRYPT)
12264/*
12265 * Check if a curve proposed by the peer is in our list.
12266 * Return 0 if we're willing to use it, -1 otherwise.
12267 */
12268int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12269 mbedtls_uecc_group_id grp_id )
12270{
12271 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12272 if( own_ec_id == grp_id )
12273 return( 0 );
12274 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12275
12276 return( -1 );
12277}
12278#endif /* MBEDTLS_USE_TINYCRYPT */
12279
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012280#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012281/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012282 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012283 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012284 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012285int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12286 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012287{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012288 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12289 if( own_ec_id == grp_id )
12290 return( 0 );
12291 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012292
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012293 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012294}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012295#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012296
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012297#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012298/*
12299 * Check if a hash proposed by the peer is in our list.
12300 * Return 0 if we're willing to use it, -1 otherwise.
12301 */
12302int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12303 mbedtls_md_type_t md )
12304{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012305 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12306 if( md_alg == md )
12307 return( 0 );
12308 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012309
12310 return( -1 );
12311}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012312#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012314#if defined(MBEDTLS_X509_CRT_PARSE_C)
12315int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012316 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012317 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012318 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012319{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012320 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012321#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012322 int usage = 0;
12323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012324#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012325 const char *ext_oid;
12326 size_t ext_len;
12327#endif
12328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012329#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12330 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012331 ((void) cert);
12332 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012333 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012334#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012336#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12337 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012338 {
12339 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012340 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012342 case MBEDTLS_KEY_EXCHANGE_RSA:
12343 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012344 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012345 break;
12346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012347 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12348 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12349 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12350 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012351 break;
12352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012353 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12354 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012355 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012356 break;
12357
12358 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012359 case MBEDTLS_KEY_EXCHANGE_NONE:
12360 case MBEDTLS_KEY_EXCHANGE_PSK:
12361 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12362 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012363 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012364 usage = 0;
12365 }
12366 }
12367 else
12368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012369 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12370 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012371 }
12372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012373 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012374 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012375 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012376 ret = -1;
12377 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012378#else
12379 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012380#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012382#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12383 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012385 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12386 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012387 }
12388 else
12389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012390 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12391 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012392 }
12393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012394 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012395 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012396 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012397 ret = -1;
12398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012399#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012400
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012401 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012404
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012405#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12406 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12407int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12408 unsigned char *output,
12409 unsigned char *data, size_t data_len )
12410{
12411 int ret = 0;
12412 mbedtls_md5_context mbedtls_md5;
12413 mbedtls_sha1_context mbedtls_sha1;
12414
12415 mbedtls_md5_init( &mbedtls_md5 );
12416 mbedtls_sha1_init( &mbedtls_sha1 );
12417
12418 /*
12419 * digitally-signed struct {
12420 * opaque md5_hash[16];
12421 * opaque sha_hash[20];
12422 * };
12423 *
12424 * md5_hash
12425 * MD5(ClientHello.random + ServerHello.random
12426 * + ServerParams);
12427 * sha_hash
12428 * SHA(ClientHello.random + ServerHello.random
12429 * + ServerParams);
12430 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012431 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012432 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012434 goto exit;
12435 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012436 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012437 ssl->handshake->randbytes, 64 ) ) != 0 )
12438 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012440 goto exit;
12441 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012442 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012443 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012445 goto exit;
12446 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012447 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012448 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012450 goto exit;
12451 }
12452
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012453 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012454 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012456 goto exit;
12457 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012458 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012459 ssl->handshake->randbytes, 64 ) ) != 0 )
12460 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012462 goto exit;
12463 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012464 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012465 data_len ) ) != 0 )
12466 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012468 goto exit;
12469 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012470 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012471 output + 16 ) ) != 0 )
12472 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012474 goto exit;
12475 }
12476
12477exit:
12478 mbedtls_md5_free( &mbedtls_md5 );
12479 mbedtls_sha1_free( &mbedtls_sha1 );
12480
12481 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012482 mbedtls_ssl_pend_fatal_alert( ssl,
12483 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012484
12485 return( ret );
12486
12487}
12488#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12489 MBEDTLS_SSL_PROTO_TLS1_1 */
12490
12491#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12492 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12493int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012494 unsigned char *hash, size_t *hashlen,
12495 unsigned char *data, size_t data_len,
12496 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012497{
12498 int ret = 0;
12499 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012500 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012501 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012502
12503 mbedtls_md_init( &ctx );
12504
12505 /*
12506 * digitally-signed struct {
12507 * opaque client_random[32];
12508 * opaque server_random[32];
12509 * ServerDHParams params;
12510 * };
12511 */
12512 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12513 {
12514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12515 goto exit;
12516 }
12517 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12518 {
12519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12520 goto exit;
12521 }
12522 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12523 {
12524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12525 goto exit;
12526 }
12527 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12528 {
12529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12530 goto exit;
12531 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012532 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012533 {
12534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12535 goto exit;
12536 }
12537
12538exit:
12539 mbedtls_md_free( &ctx );
12540
12541 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012542 mbedtls_ssl_pend_fatal_alert( ssl,
12543 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012544
12545 return( ret );
12546}
12547#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12548 MBEDTLS_SSL_PROTO_TLS1_2 */
12549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012550#endif /* MBEDTLS_SSL_TLS_C */