blob: cf6ef129a8d08039856390c01f4532d17b5315a3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300500 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300510 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001955 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001957 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1958 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1959 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1960 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1961 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962 {
1963 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001964 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965
1966 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1967 ssl->handshake->ecdh_privkey,
1968 ssl->handshake->premaster,
1969 uecc_curve ) )
1970 {
1971 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1972 }
1973
1974 ssl->handshake->pmslen = NUM_ECC_BYTES;
1975 }
1976 else
1977#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001978#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1980 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1981 {
1982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1983 ssl->handshake->premaster,
1984 MBEDTLS_PREMASTER_SIZE,
1985 &ssl->handshake->pmslen,
1986 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001987 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001988 {
1989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1990 return( ret );
1991 }
1992
1993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1994 }
1995 else
1996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001997#if defined(MBEDTLS_ECDH_C) && \
1998 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2001 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2006 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2007 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2008 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2009 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2010 {
2011 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2012 &ssl->handshake->pmslen,
2013 ssl->handshake->premaster,
2014 MBEDTLS_MPI_MAX_SIZE,
2015 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002016 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002017 {
2018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2019 return( ret );
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2023 }
2024 else
2025#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2026 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2027 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2029#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2030 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2031 {
2032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002033 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2036 return( ret );
2037 }
2038 }
2039 else
2040#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2041#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2042 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2043 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2044 {
2045 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2046 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2047 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002048 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( ret != 0 )
2050 {
2051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2052 return( ret );
2053 }
2054 }
2055 else
2056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2057#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_RSA )
2060 {
2061 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002062 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002063 * ssl_rsa_generate_partial_pms(). Only the
2064 * PMS length needs to be set. */
2065 ssl->handshake->pmslen = 48;
2066 }
2067 else
2068#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2072 }
2073
2074 return( 0 );
2075}
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2078int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 unsigned char *p = ssl->handshake->premaster;
2081 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002082 const unsigned char *psk = ssl->conf->psk;
2083 size_t psk_len = ssl->conf->psk_len;
2084
2085 /* If the psk callback was called, use its result */
2086 if( ssl->handshake->psk != NULL )
2087 {
2088 psk = ssl->handshake->psk;
2089 psk_len = ssl->handshake->psk_len;
2090 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091
2092 /*
2093 * PMS = struct {
2094 * opaque other_secret<0..2^16-1>;
2095 * opaque psk<0..2^16-1>;
2096 * };
2097 * with "other_secret" depending on the particular key exchange
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2100 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002101 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002102 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002105 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002106
2107 if( end < p || (size_t)( end - p ) < psk_len )
2108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2109
2110 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002111 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002112 }
2113 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2115#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2116 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002117 {
2118 /*
2119 * other_secret already set by the ClientKeyExchange message,
2120 * and is 48 bytes long
2121 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002122 if( end - p < 2 )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002125 *p++ = 0;
2126 *p++ = 48;
2127 p += 48;
2128 }
2129 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2131#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2132 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002134 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002135 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002136
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002137 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002139 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002140 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002141 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002144 return( ret );
2145 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002146 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002147 p += len;
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002150 }
2151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2153#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2154 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002156 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 size_t zlen;
2158
Hanno Becker982da7e2019-09-02 09:47:39 +01002159#if defined(MBEDTLS_USE_TINYCRYPT)
2160 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2161 ((void) ret);
2162
2163 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2164 ssl->handshake->ecdh_privkey,
2165 p + 2,
2166 uecc_curve ) )
2167 {
2168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2169 }
2170
2171 zlen = NUM_ECC_BYTES;
2172#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002174 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002175 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002176 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179 return( ret );
2180 }
2181
Hanno Becker982da7e2019-09-02 09:47:39 +01002182 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2183 MBEDTLS_DEBUG_ECDH_Z );
2184#endif /* MBEDTLS_USE_TINYCRYPT */
2185
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002186 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002187 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 }
2189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002194 }
2195
2196 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002197 if( end - p < 2 )
2198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002199
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002200 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002201
2202 if( end < p || (size_t)( end - p ) < psk_len )
2203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2204
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002205 memcpy( p, psk, psk_len );
2206 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002207
2208 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2209
2210 return( 0 );
2211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002215/*
2216 * SSLv3.0 MAC functions
2217 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002218#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002219static void ssl_mac( mbedtls_md_context_t *md_ctx,
2220 const unsigned char *secret,
2221 const unsigned char *buf, size_t len,
2222 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002223 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002224{
2225 unsigned char header[11];
2226 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002227 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2229 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002230
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002231 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002233 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002234 else
Paul Bakker68884e32013-01-07 18:20:04 +01002235 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002238 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002239 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Paul Bakker68884e32013-01-07 18:20:04 +01002241 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_md_starts( md_ctx );
2243 mbedtls_md_update( md_ctx, secret, md_size );
2244 mbedtls_md_update( md_ctx, padding, padlen );
2245 mbedtls_md_update( md_ctx, header, 11 );
2246 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002247 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Paul Bakker68884e32013-01-07 18:20:04 +01002249 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 mbedtls_md_starts( md_ctx );
2251 mbedtls_md_update( md_ctx, secret, md_size );
2252 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002253 mbedtls_md_update( md_ctx, out, md_size );
2254 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002257
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002258/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002259 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002260#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002261 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2262 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2263 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2264/* This function makes sure every byte in the memory region is accessed
2265 * (in ascending addresses order) */
2266static void ssl_read_memory( unsigned char *p, size_t len )
2267{
2268 unsigned char acc = 0;
2269 volatile unsigned char force;
2270
2271 for( ; len != 0; p++, len-- )
2272 acc ^= *p;
2273
2274 force = acc;
2275 (void) force;
2276}
2277#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2278
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002279/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002281 */
Hanno Becker3307b532017-12-27 21:37:21 +00002282
Hanno Beckera5a2b082019-05-15 14:03:01 +01002283#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002284/* This functions transforms a DTLS plaintext fragment and a record content
2285 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002286 *
2287 * struct {
2288 * opaque content[DTLSPlaintext.length];
2289 * ContentType real_type;
2290 * uint8 zeros[length_of_padding];
2291 * } DTLSInnerPlaintext;
2292 *
2293 * Input:
2294 * - `content`: The beginning of the buffer holding the
2295 * plaintext to be wrapped.
2296 * - `*content_size`: The length of the plaintext in Bytes.
2297 * - `max_len`: The number of Bytes available starting from
2298 * `content`. This must be `>= *content_size`.
2299 * - `rec_type`: The desired record content type.
2300 *
2301 * Output:
2302 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2303 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2304 *
2305 * Returns:
2306 * - `0` on success.
2307 * - A negative error code if `max_len` didn't offer enough space
2308 * for the expansion.
2309 */
2310static int ssl_cid_build_inner_plaintext( unsigned char *content,
2311 size_t *content_size,
2312 size_t remaining,
2313 uint8_t rec_type )
2314{
2315 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002316 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2317 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2318 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002319
2320 /* Write real content type */
2321 if( remaining == 0 )
2322 return( -1 );
2323 content[ len ] = rec_type;
2324 len++;
2325 remaining--;
2326
2327 if( remaining < pad )
2328 return( -1 );
2329 memset( content + len, 0, pad );
2330 len += pad;
2331 remaining -= pad;
2332
2333 *content_size = len;
2334 return( 0 );
2335}
2336
Hanno Becker7dc25772019-05-20 15:08:01 +01002337/* This function parses a DTLSInnerPlaintext structure.
2338 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002339static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2340 size_t *content_size,
2341 uint8_t *rec_type )
2342{
2343 size_t remaining = *content_size;
2344
2345 /* Determine length of padding by skipping zeroes from the back. */
2346 do
2347 {
2348 if( remaining == 0 )
2349 return( -1 );
2350 remaining--;
2351 } while( content[ remaining ] == 0 );
2352
2353 *content_size = remaining;
2354 *rec_type = content[ remaining ];
2355
2356 return( 0 );
2357}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002358#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002359
Hanno Becker99abf512019-05-20 14:50:53 +01002360/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002361 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002362static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002363 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002364 mbedtls_record *rec )
2365{
Hanno Becker99abf512019-05-20 14:50:53 +01002366 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002367 *
2368 * additional_data = seq_num + TLSCompressed.type +
2369 * TLSCompressed.version + TLSCompressed.length;
2370 *
Hanno Becker99abf512019-05-20 14:50:53 +01002371 * For the CID extension, this is extended as follows
2372 * (quoting draft-ietf-tls-dtls-connection-id-05,
2373 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002374 *
2375 * additional_data = seq_num + DTLSPlaintext.type +
2376 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002377 * cid +
2378 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002379 * length_of_DTLSInnerPlaintext;
2380 */
2381
Hanno Becker3307b532017-12-27 21:37:21 +00002382 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2383 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002384 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002385
Hanno Beckera5a2b082019-05-15 14:03:01 +01002386#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002387 if( rec->cid_len != 0 )
2388 {
2389 memcpy( add_data + 11, rec->cid, rec->cid_len );
2390 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002391 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2392 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002393 *add_data_len = 13 + 1 + rec->cid_len;
2394 }
2395 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002397 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002398 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002399 *add_data_len = 13;
2400 }
Hanno Becker3307b532017-12-27 21:37:21 +00002401}
2402
Hanno Becker611a83b2018-01-03 14:27:32 +00002403int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2404 mbedtls_ssl_transform *transform,
2405 mbedtls_record *rec,
2406 int (*f_rng)(void *, unsigned char *, size_t),
2407 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002410 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002411 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002412 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002413 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002414 size_t post_avail;
2415
2416 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002417#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002418 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002419 ((void) ssl);
2420#endif
2421
2422 /* The PRNG is used for dynamic IV generation that's used
2423 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2424#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2425 ( defined(MBEDTLS_AES_C) || \
2426 defined(MBEDTLS_ARIA_C) || \
2427 defined(MBEDTLS_CAMELLIA_C) ) && \
2428 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2429 ((void) f_rng);
2430 ((void) p_rng);
2431#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Hanno Becker3307b532017-12-27 21:37:21 +00002435 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002436 {
Hanno Becker3307b532017-12-27 21:37:21 +00002437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2439 }
Hanno Becker505089d2019-05-01 09:45:57 +01002440 if( rec == NULL
2441 || rec->buf == NULL
2442 || rec->buf_len < rec->data_offset
2443 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002445 || rec->cid_len != 0
2446#endif
2447 )
Hanno Becker3307b532017-12-27 21:37:21 +00002448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002451 }
2452
Hanno Becker3307b532017-12-27 21:37:21 +00002453 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002454 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002456 data, rec->data_len );
2457
2458 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2459
2460 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2461 {
2462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2463 (unsigned) rec->data_len,
2464 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2466 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002467
Hanno Beckera5a2b082019-05-15 14:03:01 +01002468#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002469 /*
2470 * Add CID information
2471 */
2472 rec->cid_len = transform->out_cid_len;
2473 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2474 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002475
2476 if( rec->cid_len != 0 )
2477 {
2478 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002479 * Wrap plaintext into DTLSInnerPlaintext structure.
2480 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002481 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002482 * Note that this changes `rec->data_len`, and hence
2483 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002484 */
2485 if( ssl_cid_build_inner_plaintext( data,
2486 &rec->data_len,
2487 post_avail,
2488 rec->type ) != 0 )
2489 {
2490 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2491 }
2492
2493 rec->type = MBEDTLS_SSL_MSG_CID;
2494 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002495#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002496
Hanno Becker92c930f2019-04-29 17:31:37 +01002497 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2498
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002500 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002502#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 if( mode == MBEDTLS_MODE_STREAM ||
2504 ( mode == MBEDTLS_MODE_CBC
2505#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002506 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002507#endif
2508 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
Hanno Becker3307b532017-12-27 21:37:21 +00002510 if( post_avail < transform->maclen )
2511 {
2512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2514 }
2515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002517 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2518 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002519 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002520 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002521 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2522 data, rec->data_len, rec->ctr, rec->type, mac );
2523 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002524 }
2525 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002526#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2528 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002529 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2530 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 {
Hanno Becker992b6872017-11-09 18:57:39 +00002532 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2533
Hanno Beckere83efe62019-04-29 13:52:53 +01002534 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002535
Hanno Becker3307b532017-12-27 21:37:21 +00002536 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002537 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002538 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2539 data, rec->data_len );
2540 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2541 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2542
2543 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002544 }
2545 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002546#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 }
2551
Hanno Becker3307b532017-12-27 21:37:21 +00002552 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2553 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554
Hanno Becker3307b532017-12-27 21:37:21 +00002555 rec->data_len += transform->maclen;
2556 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002557 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002558 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002559#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561 /*
2562 * Encrypt
2563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2565 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002567 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002568 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002570 "including %d bytes of padding",
2571 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Becker3307b532017-12-27 21:37:21 +00002573 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2574 transform->iv_enc, transform->ivlen,
2575 data, rec->data_len,
2576 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002579 return( ret );
2580 }
2581
Hanno Becker3307b532017-12-27 21:37:21 +00002582 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
Paul Bakker68884e32013-01-07 18:20:04 +01002588 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002591#if defined(MBEDTLS_GCM_C) || \
2592 defined(MBEDTLS_CCM_C) || \
2593 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002595 mode == MBEDTLS_MODE_CCM ||
2596 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002598 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002600 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002601
Hanno Becker3307b532017-12-27 21:37:21 +00002602 /* Check that there's space for both the authentication tag
2603 * and the explicit IV before and after the record content. */
2604 if( post_avail < transform->taglen ||
2605 rec->data_offset < explicit_iv_len )
2606 {
2607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2608 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2609 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002610
Paul Bakker68884e32013-01-07 18:20:04 +01002611 /*
2612 * Generate IV
2613 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2615 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002616 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002617 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002618 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2619 explicit_iv_len );
2620 /* Prefix record content with explicit IV. */
2621 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 }
2623 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2624 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002625 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 unsigned char i;
2627
2628 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2629
2630 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002631 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 }
2633 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002634 {
2635 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002638 }
2639
Hanno Beckere83efe62019-04-29 13:52:53 +01002640 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002641
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2643 iv, transform->ivlen );
2644 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002645 data - explicit_iv_len, explicit_iv_len );
2646 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002647 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002649 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002650 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002651
Paul Bakker68884e32013-01-07 18:20:04 +01002652 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002653 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002654 */
Hanno Becker3307b532017-12-27 21:37:21 +00002655
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002656 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002657 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002658 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002659 data, rec->data_len, /* source */
2660 data, &rec->data_len, /* destination */
2661 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002664 return( ret );
2665 }
2666
Hanno Becker3307b532017-12-27 21:37:21 +00002667 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2668 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669
Hanno Becker3307b532017-12-27 21:37:21 +00002670 rec->data_len += transform->taglen + explicit_iv_len;
2671 rec->data_offset -= explicit_iv_len;
2672 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002673 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2677#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002678 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002681 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002682 size_t padlen, i;
2683 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684
Hanno Becker3307b532017-12-27 21:37:21 +00002685 /* Currently we're always using minimal padding
2686 * (up to 255 bytes would be allowed). */
2687 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2688 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 padlen = 0;
2690
Hanno Becker3307b532017-12-27 21:37:21 +00002691 /* Check there's enough space in the buffer for the padding. */
2692 if( post_avail < padlen + 1 )
2693 {
2694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2695 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2696 }
2697
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002699 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Hanno Becker3307b532017-12-27 21:37:21 +00002701 rec->data_len += padlen + 1;
2702 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002705 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002706 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2707 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002708 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002709 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2710 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711 {
Hanno Becker3307b532017-12-27 21:37:21 +00002712 if( f_rng == NULL )
2713 {
2714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2716 }
2717
2718 if( rec->data_offset < transform->ivlen )
2719 {
2720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2721 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2722 }
2723
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002724 /*
2725 * Generate IV
2726 */
Hanno Becker3307b532017-12-27 21:37:21 +00002727 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002728 if( ret != 0 )
2729 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002730
Hanno Becker3307b532017-12-27 21:37:21 +00002731 memcpy( data - transform->ivlen, transform->iv_enc,
2732 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002733
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002738 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002739 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002740 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Hanno Becker3307b532017-12-27 21:37:21 +00002742 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2743 transform->iv_enc,
2744 transform->ivlen,
2745 data, rec->data_len,
2746 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002749 return( ret );
2750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002751
Hanno Becker3307b532017-12-27 21:37:21 +00002752 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002756 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002759 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2760 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002761 {
2762 /*
2763 * Save IV in SSL3 and TLS1
2764 */
Hanno Becker3307b532017-12-27 21:37:21 +00002765 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2766 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 }
Hanno Becker3307b532017-12-27 21:37:21 +00002768 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002769#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002770 {
2771 data -= transform->ivlen;
2772 rec->data_offset -= transform->ivlen;
2773 rec->data_len += transform->ivlen;
2774 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002777 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002779 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2780
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781 /*
2782 * MAC(MAC_write_key, seq_num +
2783 * TLSCipherText.type +
2784 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002785 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786 * IV + // except for TLS 1.0
2787 * ENC(content + padding + padding_length));
2788 */
Hanno Becker3307b532017-12-27 21:37:21 +00002789
2790 if( post_avail < transform->maclen)
2791 {
2792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2793 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2794 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795
Hanno Beckere83efe62019-04-29 13:52:53 +01002796 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002799 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002800 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002801
Hanno Becker3307b532017-12-27 21:37:21 +00002802 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002803 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002804 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2805 data, rec->data_len );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2807 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002808
Hanno Becker3307b532017-12-27 21:37:21 +00002809 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002810
Hanno Becker3307b532017-12-27 21:37:21 +00002811 rec->data_len += transform->maclen;
2812 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002813 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002819 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002825 /* Make extra sure authentication was performed, exactly once */
2826 if( auth_done != 1 )
2827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002830 }
2831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
2834 return( 0 );
2835}
2836
Hanno Becker40478be2019-07-12 08:23:59 +01002837int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002838 mbedtls_ssl_transform *transform,
2839 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002841 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002843 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002844#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002845 size_t padlen = 0, correct = 1;
2846#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002847 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002848 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002849 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002850
Hanno Becker611a83b2018-01-03 14:27:32 +00002851#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002852 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 ((void) ssl);
2854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857 if( rec == NULL ||
2858 rec->buf == NULL ||
2859 rec->buf_len < rec->data_offset ||
2860 rec->buf_len - rec->data_offset < rec->data_len )
2861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002864 }
2865
Hanno Becker4c6876b2017-12-27 21:28:58 +00002866 data = rec->buf + rec->data_offset;
2867 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002870 /*
2871 * Match record's CID with incoming CID.
2872 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002873 if( rec->cid_len != transform->in_cid_len ||
2874 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2875 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002876 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002877 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002878#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2881 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002882 {
2883 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002884 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2885 transform->iv_dec,
2886 transform->ivlen,
2887 data, rec->data_len,
2888 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002891 return( ret );
2892 }
2893
Hanno Becker4c6876b2017-12-27 21:28:58 +00002894 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 }
Paul Bakker68884e32013-01-07 18:20:04 +01002900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002902#if defined(MBEDTLS_GCM_C) || \
2903 defined(MBEDTLS_CCM_C) || \
2904 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002906 mode == MBEDTLS_MODE_CCM ||
2907 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002908 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002910 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002911
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002912 /*
2913 * Prepare IV from explicit and implicit data.
2914 */
2915
2916 /* Check that there's enough space for the explicit IV
2917 * (at the beginning of the record) and the MAC (at the
2918 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002919 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002922 "+ taglen (%d)", rec->data_len,
2923 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002925 }
Paul Bakker68884e32013-01-07 18:20:04 +01002926
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002927#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002928 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2929 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002930 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002931
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002932 /* Fixed */
2933 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2934 /* Explicit */
2935 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002936 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002937 else
2938#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2939#if defined(MBEDTLS_CHACHAPOLY_C)
2940 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002941 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002942 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002943 unsigned char i;
2944
2945 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2946
2947 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002949 }
2950 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002951#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002952 {
2953 /* Reminder if we ever add an AEAD mode with a different size */
2954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2956 }
2957
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002958 /* Group changes to data, data_len, and add_data, because
2959 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002960 data += explicit_iv_len;
2961 rec->data_offset += explicit_iv_len;
2962 rec->data_len -= explicit_iv_len + transform->taglen;
2963
Hanno Beckere83efe62019-04-29 13:52:53 +01002964 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002965 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002966 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002967
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002968 /* Because of the check above, we know that there are
2969 * explicit_iv_len Bytes preceeding data, and taglen
2970 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002971 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002972 * mbedtls_cipher_auth_decrypt() below. */
2973
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002974 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002975 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002976 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002977
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002978 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002979 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002980 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002981 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2982 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002983 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002984 data, rec->data_len,
2985 data, &olen,
2986 data + rec->data_len,
2987 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2992 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002993
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002994 return( ret );
2995 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002996 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002998 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002999 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003003 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3007#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003008 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003011 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003012
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 /*
Paul Bakker45829992013-01-03 14:52:21 +01003014 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003017 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3018 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003019 {
3020 /* The ciphertext is prefixed with the CBC IV. */
3021 minlen += transform->ivlen;
3022 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003023#endif
Paul Bakker45829992013-01-03 14:52:21 +01003024
Hanno Becker4c6876b2017-12-27 21:28:58 +00003025 /* Size considerations:
3026 *
3027 * - The CBC cipher text must not be empty and hence
3028 * at least of size transform->ivlen.
3029 *
3030 * Together with the potential IV-prefix, this explains
3031 * the first of the two checks below.
3032 *
3033 * - The record must contain a MAC, either in plain or
3034 * encrypted, depending on whether Encrypt-then-MAC
3035 * is used or not.
3036 * - If it is, the message contains the IV-prefix,
3037 * the CBC ciphertext, and the MAC.
3038 * - If it is not, the padded plaintext, and hence
3039 * the CBC ciphertext, has at least length maclen + 1
3040 * because there is at least the padding length byte.
3041 *
3042 * As the CBC ciphertext is not empty, both cases give the
3043 * lower bound minlen + maclen + 1 on the record size, which
3044 * we test for in the second check below.
3045 */
3046 if( rec->data_len < minlen + transform->ivlen ||
3047 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003050 "+ 1 ) ( + expl IV )", rec->data_len,
3051 transform->ivlen,
3052 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003054 }
3055
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003056 /*
3057 * Authenticate before decrypt if enabled
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003060 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003061 {
Hanno Becker992b6872017-11-09 18:57:39 +00003062 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003065
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003066 /* Update data_len in tandem with add_data.
3067 *
3068 * The subtraction is safe because of the previous check
3069 * data_len >= minlen + maclen + 1.
3070 *
3071 * Afterwards, we know that data + data_len is followed by at
3072 * least maclen Bytes, which justifies the call to
3073 * mbedtls_ssl_safer_memcmp() below.
3074 *
3075 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003077 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003078
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003079 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003080 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3081 add_data_len );
3082 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3083 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003084 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3085 data, rec->data_len );
3086 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3087 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003088
Hanno Becker4c6876b2017-12-27 21:28:58 +00003089 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3090 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003091 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003092 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003093
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003094 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003095 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3096 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003101 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104
3105 /*
3106 * Check length sanity
3107 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003108
3109 /* We know from above that data_len > minlen >= 0,
3110 * so the following check in particular implies that
3111 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003112 if( rec->data_len % transform->ivlen != 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, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003115 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003117 }
3118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003120 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003122 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003123 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3124 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003125 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003126 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003127 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003128
Hanno Becker4c6876b2017-12-27 21:28:58 +00003129 data += transform->ivlen;
3130 rec->data_offset += transform->ivlen;
3131 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003134
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003135 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3136
Hanno Becker4c6876b2017-12-27 21:28:58 +00003137 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3138 transform->iv_dec, transform->ivlen,
3139 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003142 return( ret );
3143 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003144
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003145 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003146 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003150 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003153 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3154 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003155 {
3156 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003157 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3158 * records is equivalent to CBC decryption of the concatenation
3159 * of the records; in other words, IVs are maintained across
3160 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003161 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3163 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 /* Safe since data_len >= minlen + maclen + 1, so after having
3168 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003169 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3170 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003171 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003172
Hanno Becker4c6876b2017-12-27 21:28:58 +00003173 if( auth_done == 1 )
3174 {
3175 correct *= ( rec->data_len >= padlen + 1 );
3176 padlen *= ( rec->data_len >= padlen + 1 );
3177 }
3178 else
Paul Bakker45829992013-01-03 14:52:21 +01003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 if( rec->data_len < transform->maclen + padlen + 1 )
3182 {
3183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3184 rec->data_len,
3185 transform->maclen,
3186 padlen + 1 ) );
3187 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003188#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003189
3190 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3191 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Hanno Becker4c6876b2017-12-27 21:28:58 +00003194 padlen++;
3195
3196 /* Regardless of the validity of the padding,
3197 * we have data_len >= padlen here. */
3198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003200 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3201 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003203 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205#if defined(MBEDTLS_SSL_DEBUG_ALL)
3206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 "should be no more than %d",
3208 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003209#endif
Paul Bakker45829992013-01-03 14:52:21 +01003210 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212 }
3213 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3215#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3216 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003217 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3218 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003220 /* The padding check involves a series of up to 256
3221 * consecutive memory reads at the end of the record
3222 * plaintext buffer. In order to hide the length and
3223 * validity of the padding, always perform exactly
3224 * `min(256,plaintext_len)` reads (but take into account
3225 * only the last `padlen` bytes for the padding check). */
3226 size_t pad_count = 0;
3227 size_t real_count = 0;
3228 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003229
Hanno Becker4c6876b2017-12-27 21:28:58 +00003230 /* Index of first padding byte; it has been ensured above
3231 * that the subtraction is safe. */
3232 size_t const padding_idx = rec->data_len - padlen;
3233 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3234 size_t const start_idx = rec->data_len - num_checks;
3235 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003236
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003238 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 real_count |= ( idx >= padding_idx );
3240 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003241 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003242 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003245 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003247#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003248 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3252 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003256 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003257
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 /* If the padding was found to be invalid, padlen == 0
3259 * and the subtraction is safe. If the padding was found valid,
3260 * padlen hasn't been changed and the previous assertion
3261 * data_len >= padlen still holds. */
3262 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003266 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003278 * Authenticate if not done yet.
3279 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003281#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003282 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003283 {
Hanno Becker992b6872017-11-09 18:57:39 +00003284 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003285
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286 /* If the initial value of padlen was such that
3287 * data_len < maclen + padlen + 1, then padlen
3288 * got reset to 1, and the initial check
3289 * data_len >= minlen + maclen + 1
3290 * guarantees that at this point we still
3291 * have at least data_len >= maclen.
3292 *
3293 * If the initial value of padlen was such that
3294 * data_len >= maclen + padlen + 1, then we have
3295 * subtracted either padlen + 1 (if the padding was correct)
3296 * or 0 (if the padding was incorrect) since then,
3297 * hence data_len >= maclen in any case.
3298 */
3299 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003300 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003303 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3304 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 ssl_mac( &transform->md_ctx_dec,
3307 transform->mac_dec,
3308 data, rec->data_len,
3309 rec->ctr, rec->type,
3310 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003311 }
3312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3314#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3315 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003316 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3317 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003318 {
3319 /*
3320 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003321 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003322 *
3323 * Known timing attacks:
3324 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3325 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003326 * To compensate for different timings for the MAC calculation
3327 * depending on how much padding was removed (which is determined
3328 * by padlen), process extra_run more blocks through the hash
3329 * function.
3330 *
3331 * The formula in the paper is
3332 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3333 * where L1 is the size of the header plus the decrypted message
3334 * plus CBC padding and L2 is the size of the header plus the
3335 * decrypted message. This is for an underlying hash function
3336 * with 64-byte blocks.
3337 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3338 * correctly. We round down instead of up, so -56 is the correct
3339 * value for our calculations instead of -55.
3340 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003341 * Repeat the formula rather than defining a block_size variable.
3342 * This avoids requiring division by a variable at runtime
3343 * (which would be marginally less efficient and would require
3344 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003345 */
3346 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003347 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003348
3349 /*
3350 * The next two sizes are the minimum and maximum values of
3351 * in_msglen over all padlen values.
3352 *
3353 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003354 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003355 *
3356 * Note that max_len + maclen is never more than the buffer
3357 * length, as we previously did in_msglen -= maclen too.
3358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003359 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003360 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3361
Hanno Becker4c6876b2017-12-27 21:28:58 +00003362 memset( tmp, 0, sizeof( tmp ) );
3363
3364 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003365 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003366#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3367 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003368 case MBEDTLS_MD_MD5:
3369 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003371 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003372 extra_run =
3373 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3374 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003375 break;
3376#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003377#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003379 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003380 extra_run =
3381 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3382 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003383 break;
3384#endif
3385 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3388 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003389
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003390 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003391
Hanno Beckere83efe62019-04-29 13:52:53 +01003392 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3393 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003394 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3395 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003396 /* Make sure we access everything even when padlen > 0. This
3397 * makes the synchronisation requirements for just-in-time
3398 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003399 ssl_read_memory( data + rec->data_len, padlen );
3400 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003401
3402 /* Call mbedtls_md_process at least once due to cache attacks
3403 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003404 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003405 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003406
Hanno Becker4c6876b2017-12-27 21:28:58 +00003407 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003408
3409 /* Make sure we access all the memory that could contain the MAC,
3410 * before we check it in the next code block. This makes the
3411 * synchronisation requirements for just-in-time Prime+Probe
3412 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003413 ssl_read_memory( data + min_len,
3414 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003415 }
3416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3418 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003424#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003425 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3426 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003427#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003428
Hanno Becker4c6876b2017-12-27 21:28:58 +00003429 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3430 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_DEBUG_ALL)
3433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003434#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003435 correct = 0;
3436 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003437 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003438 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003439
3440 /*
3441 * Finally check the correct flag
3442 */
3443 if( correct == 0 )
3444 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003445#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003446
3447 /* Make extra sure authentication was performed, exactly once */
3448 if( auth_done != 1 )
3449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Hanno Beckera5a2b082019-05-15 14:03:01 +01003454#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003455 if( rec->cid_len != 0 )
3456 {
3457 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3458 &rec->type );
3459 if( ret != 0 )
3460 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3461 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 return( 0 );
3467}
3468
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003469#undef MAC_NONE
3470#undef MAC_PLAINTEXT
3471#undef MAC_CIPHERTEXT
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003474/*
3475 * Compression/decompression functions
3476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003478{
3479 int ret;
3480 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003481 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003482 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003483 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003486
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003487 if( len_pre == 0 )
3488 return( 0 );
3489
Paul Bakker2770fbd2012-07-03 13:30:23 +00003490 memcpy( msg_pre, ssl->out_msg, len_pre );
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493 ssl->out_msglen ) );
3494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003496 ssl->out_msg, ssl->out_msglen );
3497
Paul Bakker48916f92012-09-16 19:57:18 +00003498 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3499 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3500 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003501 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502
Paul Bakker48916f92012-09-16 19:57:18 +00003503 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504 if( ret != Z_OK )
3505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3507 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 }
3509
Angus Grattond8213d02016-05-25 20:56:48 +10003510 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003511 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 ssl->out_msglen ) );
3515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003517 ssl->out_msg, ssl->out_msglen );
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
3521 return( 0 );
3522}
3523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525{
3526 int ret;
3527 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003528 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003530 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003534 if( len_pre == 0 )
3535 return( 0 );
3536
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537 memcpy( msg_pre, ssl->in_msg, len_pre );
3538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540 ssl->in_msglen ) );
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 ssl->in_msg, ssl->in_msglen );
3544
Paul Bakker48916f92012-09-16 19:57:18 +00003545 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3546 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3547 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003548 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003549 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 if( ret != Z_OK )
3553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3555 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556 }
3557
Angus Grattond8213d02016-05-25 20:56:48 +10003558 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003559 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562 ssl->in_msglen ) );
3563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003565 ssl->in_msg, ssl->in_msglen );
3566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568
3569 return( 0 );
3570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3574static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_PROTO_DTLS)
3577static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003578{
3579 /* If renegotiation is not enforced, retransmit until we would reach max
3580 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003581 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003582 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003583 uint32_t ratio =
3584 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3585 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003586 unsigned char doublings = 1;
3587
3588 while( ratio != 0 )
3589 {
3590 ++doublings;
3591 ratio >>= 1;
3592 }
3593
3594 if( ++ssl->renego_records_seen > doublings )
3595 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003597 return( 0 );
3598 }
3599 }
3600
3601 return( ssl_write_hello_request( ssl ) );
3602}
3603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003605
Paul Bakker5121ce52009-01-03 21:22:43 +00003606/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003607 * Fill the input message buffer by appending data to it.
3608 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003609 *
3610 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3611 * available (from this read and/or a previous one). Otherwise, an error code
3612 * is returned (possibly EOF or WANT_READ).
3613 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003614 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3615 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3616 * since we always read a whole datagram at once.
3617 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003618 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003619 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622{
Paul Bakker23986e52011-04-24 08:57:21 +00003623 int ret;
3624 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Hanno Beckera58a8962019-06-13 16:11:15 +01003628 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3629 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003632 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003634 }
3635
Angus Grattond8213d02016-05-25 20:56:48 +10003636 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003640 }
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003643 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003645 uint32_t timeout;
3646
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003647 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003648 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3649 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003650 {
3651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3652 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3654 }
3655
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003656 /*
3657 * The point is, we need to always read a full datagram at once, so we
3658 * sometimes read more then requested, and handle the additional data.
3659 * It could be the rest of the current record (while fetching the
3660 * header) and/or some other records in the same datagram.
3661 */
3662
3663 /*
3664 * Move to the next record in the already read datagram if applicable
3665 */
3666 if( ssl->next_record_offset != 0 )
3667 {
3668 if( ssl->in_left < ssl->next_record_offset )
3669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003672 }
3673
3674 ssl->in_left -= ssl->next_record_offset;
3675
3676 if( ssl->in_left != 0 )
3677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 ssl->next_record_offset ) );
3680 memmove( ssl->in_hdr,
3681 ssl->in_hdr + ssl->next_record_offset,
3682 ssl->in_left );
3683 }
3684
3685 ssl->next_record_offset = 0;
3686 }
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003690
3691 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003692 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003693 */
3694 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003698 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003699
3700 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003701 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003702 * are not at the beginning of a new record, the caller did something
3703 * wrong.
3704 */
3705 if( ssl->in_left != 0 )
3706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003709 }
3710
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003711 /*
3712 * Don't even try to read if time's out already.
3713 * This avoids by-passing the timer when repeatedly receiving messages
3714 * that will end up being dropped.
3715 */
3716 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003717 {
3718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003719 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003720 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003721 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003722 {
Angus Grattond8213d02016-05-25 20:56:48 +10003723 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003726 timeout = ssl->handshake->retransmit_timeout;
3727 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003728 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
Hanno Beckera58a8962019-06-13 16:11:15 +01003732 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3733 {
3734 ret = mbedtls_ssl_get_recv_timeout( ssl )
3735 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3736 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003737 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003738 {
3739 ret = mbedtls_ssl_get_recv( ssl )
3740 ( ssl->p_bio, ssl->in_hdr, len );
3741 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744
3745 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003747 }
3748
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003749 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003752 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003755 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003756 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003759 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003760 }
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003765 return( ret );
3766 }
3767
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003768 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003771 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3772 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003774 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003775 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003778 return( ret );
3779 }
3780
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003781 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003784 }
3785
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 if( ret < 0 )
3787 return( ret );
3788
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003789 ssl->in_left = ret;
3790 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003791 MBEDTLS_SSL_TRANSPORT_ELSE
3792#endif /* MBEDTLS_SSL_PROTO_DTLS */
3793#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003796 ssl->in_left, nb_want ) );
3797
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003798 while( ssl->in_left < nb_want )
3799 {
3800 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003801
3802 if( ssl_check_timer( ssl ) != 0 )
3803 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3804 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003805 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003806 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003807 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003808 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3809 ssl->in_hdr + ssl->in_left, len,
3810 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003811 }
3812 else
3813 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003814 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003815 ssl->in_hdr + ssl->in_left, len );
3816 }
3817 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003820 ssl->in_left, nb_want ) );
3821 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003822
3823 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003825
3826 if( ret < 0 )
3827 return( ret );
3828
mohammad160352aecb92018-03-28 23:41:40 -07003829 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003830 {
Darryl Green11999bb2018-03-13 15:22:58 +00003831 MBEDTLS_SSL_DEBUG_MSG( 1,
3832 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003833 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3835 }
3836
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003837 ssl->in_left += ret;
3838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003840#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
3844 return( 0 );
3845}
3846
3847/*
3848 * Flush any data not yet written
3849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003851{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003852 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003853 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Hanno Beckera58a8962019-06-13 16:11:15 +01003857 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003860 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003862 }
3863
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003864 /* Avoid incrementing counter if data is flushed */
3865 if( ssl->out_left == 0 )
3866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003868 return( 0 );
3869 }
3870
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 while( ssl->out_left > 0 )
3872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003874 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003875
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003877 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
3881 if( ret <= 0 )
3882 return( ret );
3883
mohammad160352aecb92018-03-28 23:41:40 -07003884 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003885 {
Darryl Green11999bb2018-03-13 15:22:58 +00003886 MBEDTLS_SSL_DEBUG_MSG( 1,
3887 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003888 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3890 }
3891
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 ssl->out_left -= ret;
3893 }
3894
Hanno Becker2b1e3542018-08-06 11:19:13 +01003895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003896 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003897 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003898 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003899 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003900 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003901#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003902#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003903 {
3904 ssl->out_hdr = ssl->out_buf + 8;
3905 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003906#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003907 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 return( 0 );
3912}
3913
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003914/*
3915 * Functions to handle the DTLS retransmission state machine
3916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003918/*
3919 * Append current handshake message to current outgoing flight
3920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3925 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3926 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003927
3928 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003929 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003930 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003933 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 }
3935
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003936 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003940 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941 }
3942
3943 /* Copy current handshake message with headers */
3944 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3945 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003946 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003947 msg->next = NULL;
3948
3949 /* Append to the current flight */
3950 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003951 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952 else
3953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955 while( cur->next != NULL )
3956 cur = cur->next;
3957 cur->next = msg;
3958 }
3959
Hanno Becker3b235902018-08-06 09:54:53 +01003960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003961 return( 0 );
3962}
3963
3964/*
3965 * Free the current flight of handshake messages
3966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 mbedtls_ssl_flight_item *cur = flight;
3970 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003971
3972 while( cur != NULL )
3973 {
3974 next = cur->next;
3975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 mbedtls_free( cur->p );
3977 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978
3979 cur = next;
3980 }
3981}
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3984static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003985#endif
3986
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003988 * Swap transform_out and out_ctr with the alternative ones
3989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003993 unsigned char tmp_out_ctr[8];
3994
3995 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998 return;
3999 }
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004003 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004004 tmp_transform = ssl->transform_out;
4005 ssl->transform_out = ssl->handshake->alt_transform_out;
4006 ssl->handshake->alt_transform_out = tmp_transform;
4007
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004008 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004009 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4010 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004012
4013 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004014 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4017 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4022 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004023 }
4024 }
4025#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004026}
4027
4028/*
4029 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004030 */
4031int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4032{
4033 int ret = 0;
4034
4035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4036
4037 ret = mbedtls_ssl_flight_transmit( ssl );
4038
4039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4040
4041 return( ret );
4042}
4043
4044/*
4045 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004046 *
4047 * Need to remember the current message in case flush_output returns
4048 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004051int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004053 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004057 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004059
4060 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004061 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004062 ssl_swap_epochs( ssl );
4063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004065 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066
4067 while( ssl->handshake->cur_msg != NULL )
4068 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004069 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004070 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071
Hanno Beckere1dcb032018-08-17 16:47:58 +01004072 int const is_finished =
4073 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4074 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4075
Hanno Becker04da1892018-08-14 13:22:10 +01004076 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4077 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4078
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004079 /* Swap epochs before sending Finished: we can't do it after
4080 * sending ChangeCipherSpec, in case write returns WANT_READ.
4081 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004082 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004083 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004085 ssl_swap_epochs( ssl );
4086 }
4087
Hanno Becker67bc7c32018-08-06 11:33:50 +01004088 ret = ssl_get_remaining_payload_in_datagram( ssl );
4089 if( ret < 0 )
4090 return( ret );
4091 max_frag_len = (size_t) ret;
4092
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004093 /* CCS is copied as is, while HS messages may need fragmentation */
4094 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4095 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004096 if( max_frag_len == 0 )
4097 {
4098 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4099 return( ret );
4100
4101 continue;
4102 }
4103
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004104 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004105 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004106 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004107
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004108 /* Update position inside current message */
4109 ssl->handshake->cur_msg_p += cur->len;
4110 }
4111 else
4112 {
4113 const unsigned char * const p = ssl->handshake->cur_msg_p;
4114 const size_t hs_len = cur->len - 12;
4115 const size_t frag_off = p - ( cur->p + 12 );
4116 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004117 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004118
Hanno Beckere1dcb032018-08-17 16:47:58 +01004119 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004120 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004121 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004122 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004123
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4125 return( ret );
4126
4127 continue;
4128 }
4129 max_hs_frag_len = max_frag_len - 12;
4130
4131 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4132 max_hs_frag_len : rem_len;
4133
4134 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004137 (unsigned) cur_hs_frag_len,
4138 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004139 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004140
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004141 /* Messages are stored with handshake headers as if not fragmented,
4142 * copy beginning of headers then fill fragmentation fields.
4143 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4144 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004145
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004146 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4147 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4148 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004149
4150 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4151
Hanno Becker3f7b9732018-08-28 09:53:25 +01004152 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004153 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4154 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004155 ssl->out_msgtype = cur->type;
4156
4157 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004158 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004159 }
4160
4161 /* If done with the current message move to the next one if any */
4162 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4163 {
4164 if( cur->next != NULL )
4165 {
4166 ssl->handshake->cur_msg = cur->next;
4167 ssl->handshake->cur_msg_p = cur->next->p + 12;
4168 }
4169 else
4170 {
4171 ssl->handshake->cur_msg = NULL;
4172 ssl->handshake->cur_msg_p = NULL;
4173 }
4174 }
4175
4176 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004177 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004180 return( ret );
4181 }
4182 }
4183
Hanno Becker67bc7c32018-08-06 11:33:50 +01004184 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4185 return( ret );
4186
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004187 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4189 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004190 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004193 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4194 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004195
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004197
4198 return( 0 );
4199}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004200
4201/*
4202 * To be called when the last message of an incoming flight is received.
4203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004205{
4206 /* We won't need to resend that one any more */
4207 ssl_flight_free( ssl->handshake->flight );
4208 ssl->handshake->flight = NULL;
4209 ssl->handshake->cur_msg = NULL;
4210
4211 /* The next incoming flight will start with this msg_seq */
4212 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4213
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004214 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004215 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004216
Hanno Becker0271f962018-08-16 13:23:47 +01004217 /* Clear future message buffering structure. */
4218 ssl_buffering_free( ssl );
4219
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004220 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004221 ssl_set_timer( ssl, 0 );
4222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4224 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004227 }
4228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004230}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004231
4232/*
4233 * To be called when the last message of an outgoing flight is send.
4234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004236{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004237 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004238 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4241 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004244 }
4245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004249
Paul Bakker5121ce52009-01-03 21:22:43 +00004250/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004251 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004253
4254/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004255 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004256 *
4257 * - fill in handshake headers
4258 * - update handshake checksum
4259 * - DTLS: save message for resending
4260 * - then pass to the record layer
4261 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004262 * DTLS: except for HelloRequest, messages are only queued, and will only be
4263 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004264 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004265 * Inputs:
4266 * - ssl->out_msglen: 4 + actual handshake message len
4267 * (4 is the size of handshake headers for TLS)
4268 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4269 * - ssl->out_msg + 4: the handshake message body
4270 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004271 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004272 * - ssl->out_msglen: the length of the record contents
4273 * (including handshake headers but excluding record headers)
4274 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004275 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004276int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004278 int ret;
4279 const size_t hs_len = ssl->out_msglen - 4;
4280 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4283
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004284 /*
4285 * Sanity checks
4286 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004287 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004288 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4289 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004290 /* In SSLv3, the client might send a NoCertificate alert. */
4291#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004292 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004293 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004294 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4295 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004296#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4297 {
4298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4300 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004302
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004303 /* Whenever we send anything different from a
4304 * HelloRequest we should be in a handshake - double check. */
4305 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4306 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004307 ssl->handshake == NULL )
4308 {
4309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004314 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004315 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004317 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004320 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004321#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004322
Hanno Beckerb50a2532018-08-06 11:52:54 +01004323 /* Double-check that we did not exceed the bounds
4324 * of the outgoing record buffer.
4325 * This should never fail as the various message
4326 * writing functions must obey the bounds of the
4327 * outgoing record buffer, but better be safe.
4328 *
4329 * Note: We deliberately do not check for the MTU or MFL here.
4330 */
4331 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4332 {
4333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4334 "size %u, maximum %u",
4335 (unsigned) ssl->out_msglen,
4336 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4338 }
4339
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004340 /*
4341 * Fill handshake headers
4342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004345 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004347 /*
4348 * DTLS has additional fields in the Handshake layer,
4349 * between the length field and the actual payload:
4350 * uint16 message_seq;
4351 * uint24 fragment_offset;
4352 * uint24 fragment_length;
4353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004355 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004356 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004357 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004358 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004359 {
4360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4361 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004362 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004363 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4365 }
4366
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004367 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004368 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004369
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004370 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004371 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004372 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004373 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4374 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004375 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004376 }
4377 else
4378 {
4379 ssl->out_msg[4] = 0;
4380 ssl->out_msg[5] = 0;
4381 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004382
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004383 /* Handshake hashes are computed without fragmentation,
4384 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004385 memset( ssl->out_msg + 6, 0x00, 3 );
4386 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004389
Hanno Becker0207e532018-08-28 10:28:28 +01004390 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004391 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004392 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004393 }
4394
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004395 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004397 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004398 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4399 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004400 {
4401 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004404 return( ret );
4405 }
4406 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004407 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004408#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004409 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004410 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004411 {
4412 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4413 return( ret );
4414 }
4415 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004416
4417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4418
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004419 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004420}
4421
4422/*
4423 * Record layer functions
4424 */
4425
4426/*
4427 * Write current record.
4428 *
4429 * Uses:
4430 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4431 * - ssl->out_msglen: length of the record content (excl headers)
4432 * - ssl->out_msg: record content
4433 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004434int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004435{
4436 int ret, done = 0;
4437 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004438 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004439
4440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004443 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004445 {
4446 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004449 return( ret );
4450 }
4451
4452 len = ssl->out_msglen;
4453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4457 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 ret = mbedtls_ssl_hw_record_write( ssl );
4462 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4465 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004466 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004467
4468 if( ret == 0 )
4469 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004472 if( !done )
4473 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004474 unsigned i;
4475 size_t protected_record_size;
4476
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004477 /* Skip writing the record content type to after the encryption,
4478 * as it may change when using the CID extension. */
4479
Hanno Becker2881d802019-05-22 14:44:53 +01004480 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4481 mbedtls_ssl_get_minor_ver( ssl ),
4482 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004483
Hanno Becker19859472018-08-06 09:40:20 +01004484 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004485 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004486
Paul Bakker48916f92012-09-16 19:57:18 +00004487 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004488 {
Hanno Becker3307b532017-12-27 21:37:21 +00004489 mbedtls_record rec;
4490
4491 rec.buf = ssl->out_iv;
4492 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4493 ( ssl->out_iv - ssl->out_buf );
4494 rec.data_len = ssl->out_msglen;
4495 rec.data_offset = ssl->out_msg - rec.buf;
4496
4497 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004498 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4499 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004500 ssl->conf->transport, rec.ver );
4501 rec.type = ssl->out_msgtype;
4502
Hanno Beckera5a2b082019-05-15 14:03:01 +01004503#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004504 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004505 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004506#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004507
Hanno Becker611a83b2018-01-03 14:27:32 +00004508 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004509 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004510 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004513 return( ret );
4514 }
4515
Hanno Becker3307b532017-12-27 21:37:21 +00004516 if( rec.data_offset != 0 )
4517 {
4518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4520 }
4521
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004522 /* Update the record content type and CID. */
4523 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004524#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004525 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004526#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004527 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004528 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004529 }
4530
Hanno Becker43395762019-05-03 14:46:38 +01004531 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004532
4533#if defined(MBEDTLS_SSL_PROTO_DTLS)
4534 /* In case of DTLS, double-check that we don't exceed
4535 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004536 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004537 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004538 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004539 if( ret < 0 )
4540 return( ret );
4541
4542 if( protected_record_size > (size_t) ret )
4543 {
4544 /* Should never happen */
4545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4546 }
4547 }
4548#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004549
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004550 /* Now write the potentially updated record content type. */
4551 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004554 "version = [%d:%d], msglen = %d",
4555 ssl->out_hdr[0], ssl->out_hdr[1],
4556 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004559 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004560
4561 ssl->out_left += protected_record_size;
4562 ssl->out_hdr += protected_record_size;
4563 ssl_update_out_pointers( ssl, ssl->transform_out );
4564
Hanno Becker04484622018-08-06 09:49:38 +01004565 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4566 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4567 break;
4568
4569 /* The loop goes to its end iff the counter is wrapping */
4570 if( i == ssl_ep_len( ssl ) )
4571 {
4572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4573 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 }
4576
Hanno Becker67bc7c32018-08-06 11:33:50 +01004577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004578 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004579 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004580 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004581 size_t remaining;
4582 ret = ssl_get_remaining_payload_in_datagram( ssl );
4583 if( ret < 0 )
4584 {
4585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4586 ret );
4587 return( ret );
4588 }
4589
4590 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004591 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004592 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004593 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004594 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595 else
4596 {
Hanno Becker513815a2018-08-20 11:56:09 +01004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004598 }
4599 }
4600#endif /* MBEDTLS_SSL_PROTO_DTLS */
4601
4602 if( ( flush == SSL_FORCE_FLUSH ) &&
4603 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 return( ret );
4607 }
4608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004610
4611 return( 0 );
4612}
4613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004615
4616static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4617{
4618 if( ssl->in_msglen < ssl->in_hslen ||
4619 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4620 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4621 {
4622 return( 1 );
4623 }
4624 return( 0 );
4625}
Hanno Becker44650b72018-08-16 12:51:11 +01004626
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004627static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004628{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004629 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004630}
4631
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004632static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004633{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004634 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004635}
4636
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004637static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004638{
4639 uint32_t msg_len, frag_off, frag_len;
4640
4641 msg_len = ssl_get_hs_total_len( ssl );
4642 frag_off = ssl_get_hs_frag_off( ssl );
4643 frag_len = ssl_get_hs_frag_len( ssl );
4644
4645 if( frag_off > msg_len )
4646 return( -1 );
4647
4648 if( frag_len > msg_len - frag_off )
4649 return( -1 );
4650
4651 if( frag_len + 12 > ssl->in_msglen )
4652 return( -1 );
4653
4654 return( 0 );
4655}
4656
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004657/*
4658 * Mark bits in bitmask (used for DTLS HS reassembly)
4659 */
4660static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4661{
4662 unsigned int start_bits, end_bits;
4663
4664 start_bits = 8 - ( offset % 8 );
4665 if( start_bits != 8 )
4666 {
4667 size_t first_byte_idx = offset / 8;
4668
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004669 /* Special case */
4670 if( len <= start_bits )
4671 {
4672 for( ; len != 0; len-- )
4673 mask[first_byte_idx] |= 1 << ( start_bits - len );
4674
4675 /* Avoid potential issues with offset or len becoming invalid */
4676 return;
4677 }
4678
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004679 offset += start_bits; /* Now offset % 8 == 0 */
4680 len -= start_bits;
4681
4682 for( ; start_bits != 0; start_bits-- )
4683 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4684 }
4685
4686 end_bits = len % 8;
4687 if( end_bits != 0 )
4688 {
4689 size_t last_byte_idx = ( offset + len ) / 8;
4690
4691 len -= end_bits; /* Now len % 8 == 0 */
4692
4693 for( ; end_bits != 0; end_bits-- )
4694 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4695 }
4696
4697 memset( mask + offset / 8, 0xFF, len / 8 );
4698}
4699
4700/*
4701 * Check that bitmask is full
4702 */
4703static int ssl_bitmask_check( unsigned char *mask, size_t len )
4704{
4705 size_t i;
4706
4707 for( i = 0; i < len / 8; i++ )
4708 if( mask[i] != 0xFF )
4709 return( -1 );
4710
4711 for( i = 0; i < len % 8; i++ )
4712 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4713 return( -1 );
4714
4715 return( 0 );
4716}
4717
Hanno Becker56e205e2018-08-16 09:06:12 +01004718/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004719static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004720 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004721{
Hanno Becker56e205e2018-08-16 09:06:12 +01004722 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004723
Hanno Becker56e205e2018-08-16 09:06:12 +01004724 alloc_len = 12; /* Handshake header */
4725 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004726
Hanno Beckerd07df862018-08-16 09:14:58 +01004727 if( add_bitmap )
4728 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004729
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004730 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004731}
Hanno Becker56e205e2018-08-16 09:06:12 +01004732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004734
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004735static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004736{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004737 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004738}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004739
Simon Butcher99000142016-10-13 17:21:01 +01004740int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004745 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004747 }
4748
Hanno Becker12555c62018-08-16 12:47:53 +01004749 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004752 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004753 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004757 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004758 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004759 unsigned int recv_msg_seq = (unsigned int)
4760 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004761
Hanno Becker44650b72018-08-16 12:51:11 +01004762 if( ssl_check_hs_header( ssl ) != 0 )
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004768 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004769 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4770 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4771 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4772 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004773 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004774 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4777 recv_msg_seq,
4778 ssl->handshake->in_msg_seq ) );
4779 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4780 }
4781
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004782 /* Retransmit only on last message from previous flight, to avoid
4783 * too many retransmissions.
4784 * Besides, No sane server ever retransmits HelloVerifyRequest */
4785 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004789 "message_seq = %d, start_of_flight = %d",
4790 recv_msg_seq,
4791 ssl->handshake->in_flight_start_seq ) );
4792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004796 return( ret );
4797 }
4798 }
4799 else
4800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004802 "message_seq = %d, expected = %d",
4803 recv_msg_seq,
4804 ssl->handshake->in_msg_seq ) );
4805 }
4806
Hanno Becker90333da2017-10-10 11:27:13 +01004807 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004808 }
4809 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004810
Hanno Becker6d97ef52018-08-16 13:09:04 +01004811 /* Message reassembly is handled alongside buffering of future
4812 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004813 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004814 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004815 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004818 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004819 }
4820 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004821 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004823#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004824 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004825 /* With TLS we don't handle fragmentation (for now) */
4826 if( ssl->in_msglen < ssl->in_hslen )
4827 {
4828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4829 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4830 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004831 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004832#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004833
Simon Butcher99000142016-10-13 17:21:01 +01004834 return( 0 );
4835}
4836
4837void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4838{
Hanno Becker0271f962018-08-16 13:23:47 +01004839 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004840
Hanno Becker0271f962018-08-16 13:23:47 +01004841 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004842 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004843
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004844 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004846 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004847 ssl->handshake != NULL )
4848 {
Hanno Becker0271f962018-08-16 13:23:47 +01004849 unsigned offset;
4850 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004851
Hanno Becker0271f962018-08-16 13:23:47 +01004852 /* Increment handshake sequence number */
4853 hs->in_msg_seq++;
4854
4855 /*
4856 * Clear up handshake buffering and reassembly structure.
4857 */
4858
4859 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004860 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004861
4862 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004863 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4864 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004865 offset++, hs_buf++ )
4866 {
4867 *hs_buf = *(hs_buf + 1);
4868 }
4869
4870 /* Create a fresh last entry */
4871 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004872 }
4873#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004874}
4875
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004877 * DTLS anti-replay: RFC 6347 4.1.2.6
4878 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004879 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4880 * Bit n is set iff record number in_window_top - n has been seen.
4881 *
4882 * Usually, in_window_top is the last record number seen and the lsb of
4883 * in_window is set. The only exception is the initial state (record number 0
4884 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4887static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004888{
4889 ssl->in_window_top = 0;
4890 ssl->in_window = 0;
4891}
4892
4893static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4894{
4895 return( ( (uint64_t) buf[0] << 40 ) |
4896 ( (uint64_t) buf[1] << 32 ) |
4897 ( (uint64_t) buf[2] << 24 ) |
4898 ( (uint64_t) buf[3] << 16 ) |
4899 ( (uint64_t) buf[4] << 8 ) |
4900 ( (uint64_t) buf[5] ) );
4901}
4902
4903/*
4904 * Return 0 if sequence number is acceptable, -1 otherwise
4905 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004906int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004907{
4908 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4909 uint64_t bit;
4910
Hanno Becker7f376f42019-06-12 16:20:48 +01004911 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4912 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4913 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004914 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004915 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004916
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004917 if( rec_seqnum > ssl->in_window_top )
4918 return( 0 );
4919
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004920 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004921
4922 if( bit >= 64 )
4923 return( -1 );
4924
4925 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4926 return( -1 );
4927
4928 return( 0 );
4929}
4930
4931/*
4932 * Update replay window on new validated record
4933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004935{
4936 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4937
Hanno Becker7f376f42019-06-12 16:20:48 +01004938 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4939 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4940 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004941 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004942 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004943
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004944 if( rec_seqnum > ssl->in_window_top )
4945 {
4946 /* Update window_top and the contents of the window */
4947 uint64_t shift = rec_seqnum - ssl->in_window_top;
4948
4949 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004950 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004951 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004952 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004954 ssl->in_window |= 1;
4955 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004956
4957 ssl->in_window_top = rec_seqnum;
4958 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004959 else
4960 {
4961 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004962 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004963
4964 if( bit < 64 ) /* Always true, but be extra sure */
4965 ssl->in_window |= (uint64_t) 1 << bit;
4966 }
4967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004969
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004970#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004971/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004972static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4973
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004974/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 * Without any SSL context, check if a datagram looks like a ClientHello with
4976 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004977 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004978 *
4979 * - if cookie is valid, return 0
4980 * - if ClientHello looks superficially valid but cookie is not,
4981 * fill obuf and set olen, then
4982 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4983 * - otherwise return a specific error code
4984 */
4985static int ssl_check_dtls_clihlo_cookie(
4986 mbedtls_ssl_cookie_write_t *f_cookie_write,
4987 mbedtls_ssl_cookie_check_t *f_cookie_check,
4988 void *p_cookie,
4989 const unsigned char *cli_id, size_t cli_id_len,
4990 const unsigned char *in, size_t in_len,
4991 unsigned char *obuf, size_t buf_len, size_t *olen )
4992{
4993 size_t sid_len, cookie_len;
4994 unsigned char *p;
4995
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 /*
4997 * Structure of ClientHello with record and handshake headers,
4998 * and expected values. We don't need to check a lot, more checks will be
4999 * done when actually parsing the ClientHello - skipping those checks
5000 * avoids code duplication and does not make cookie forging any easier.
5001 *
5002 * 0-0 ContentType type; copied, must be handshake
5003 * 1-2 ProtocolVersion version; copied
5004 * 3-4 uint16 epoch; copied, must be 0
5005 * 5-10 uint48 sequence_number; copied
5006 * 11-12 uint16 length; (ignored)
5007 *
5008 * 13-13 HandshakeType msg_type; (ignored)
5009 * 14-16 uint24 length; (ignored)
5010 * 17-18 uint16 message_seq; copied
5011 * 19-21 uint24 fragment_offset; copied, must be 0
5012 * 22-24 uint24 fragment_length; (ignored)
5013 *
5014 * 25-26 ProtocolVersion client_version; (ignored)
5015 * 27-58 Random random; (ignored)
5016 * 59-xx SessionID session_id; 1 byte len + sid_len content
5017 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5018 * ...
5019 *
5020 * Minimum length is 61 bytes.
5021 */
5022 if( in_len < 61 ||
5023 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5024 in[3] != 0 || in[4] != 0 ||
5025 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5026 {
5027 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5028 }
5029
5030 sid_len = in[59];
5031 if( sid_len > in_len - 61 )
5032 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5033
5034 cookie_len = in[60 + sid_len];
5035 if( cookie_len > in_len - 60 )
5036 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5037
5038 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5039 cli_id, cli_id_len ) == 0 )
5040 {
5041 /* Valid cookie */
5042 return( 0 );
5043 }
5044
5045 /*
5046 * If we get here, we've got an invalid cookie, let's prepare HVR.
5047 *
5048 * 0-0 ContentType type; copied
5049 * 1-2 ProtocolVersion version; copied
5050 * 3-4 uint16 epoch; copied
5051 * 5-10 uint48 sequence_number; copied
5052 * 11-12 uint16 length; olen - 13
5053 *
5054 * 13-13 HandshakeType msg_type; hello_verify_request
5055 * 14-16 uint24 length; olen - 25
5056 * 17-18 uint16 message_seq; copied
5057 * 19-21 uint24 fragment_offset; copied
5058 * 22-24 uint24 fragment_length; olen - 25
5059 *
5060 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5061 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5062 *
5063 * Minimum length is 28.
5064 */
5065 if( buf_len < 28 )
5066 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5067
5068 /* Copy most fields and adapt others */
5069 memcpy( obuf, in, 25 );
5070 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5071 obuf[25] = 0xfe;
5072 obuf[26] = 0xff;
5073
5074 /* Generate and write actual cookie */
5075 p = obuf + 28;
5076 if( f_cookie_write( p_cookie,
5077 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5078 {
5079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5080 }
5081
5082 *olen = p - obuf;
5083
5084 /* Go back and fill length fields */
5085 obuf[27] = (unsigned char)( *olen - 28 );
5086
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005087 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005088 obuf[22] = obuf[14];
5089 obuf[23] = obuf[15];
5090 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005091
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005092 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005093
5094 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5095}
5096
5097/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005098 * Handle possible client reconnect with the same UDP quadruplet
5099 * (RFC 6347 Section 4.2.8).
5100 *
5101 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5102 * that looks like a ClientHello.
5103 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005104 * - if the input looks like a ClientHello without cookies,
5105 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005106 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005107 * - if the input looks like a ClientHello with a valid cookie,
5108 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005109 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005111 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005113 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5114 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005115 */
5116static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5117{
5118 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005119 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005120
Hanno Becker87b56262019-07-10 14:37:41 +01005121 if( ssl->conf->f_cookie_write == NULL ||
5122 ssl->conf->f_cookie_check == NULL )
5123 {
5124 /* If we can't use cookies to verify reachability of the peer,
5125 * drop the record. */
5126 return( 0 );
5127 }
5128
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005129 ret = ssl_check_dtls_clihlo_cookie(
5130 ssl->conf->f_cookie_write,
5131 ssl->conf->f_cookie_check,
5132 ssl->conf->p_cookie,
5133 ssl->cli_id, ssl->cli_id_len,
5134 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005135 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005136
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5138
5139 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005141 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005142 * If the error is permanent we'll catch it later,
5143 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005144 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005145 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005146 }
5147
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005148 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005149 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005150 /* Got a valid cookie, partially reset context */
5151 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5152 {
5153 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5154 return( ret );
5155 }
5156
5157 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158 }
5159
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160 return( ret );
5161}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005162#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005163
Hanno Becker46483f12019-05-03 13:25:54 +01005164static int ssl_check_record_type( uint8_t record_type )
5165{
5166 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5167 record_type != MBEDTLS_SSL_MSG_ALERT &&
5168 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5169 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5170 {
5171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5172 }
5173
5174 return( 0 );
5175}
5176
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005177/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005178 * ContentType type;
5179 * ProtocolVersion version;
5180 * uint16 epoch; // DTLS only
5181 * uint48 sequence_number; // DTLS only
5182 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005183 *
5184 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005185 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005186 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5187 *
5188 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005189 * 1. proceed with the record if this function returns 0
5190 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5191 * 3. return CLIENT_RECONNECT if this function return that value
5192 * 4. drop the whole datagram if this function returns anything else.
5193 * Point 2 is needed when the peer is resending, and we have already received
5194 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005195 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005196static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005197 unsigned char *buf,
5198 size_t len,
5199 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005200{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005201 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005202
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005203 size_t const rec_hdr_type_offset = 0;
5204 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005205
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005206 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5207 rec_hdr_type_len;
5208 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005210 size_t const rec_hdr_ctr_len = 8;
5211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005212 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005213 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5214 rec_hdr_version_len;
5215
Hanno Beckera5a2b082019-05-15 14:03:01 +01005216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5218 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005219 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5221#endif /* MBEDTLS_SSL_PROTO_DTLS */
5222
5223 size_t rec_hdr_len_offset; /* To be determined */
5224 size_t const rec_hdr_len_len = 2;
5225
5226 /*
5227 * Check minimum lengths for record header.
5228 */
5229
5230#if defined(MBEDTLS_SSL_PROTO_DTLS)
5231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5232 {
5233 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5234 }
5235 MBEDTLS_SSL_TRANSPORT_ELSE
5236#endif /* MBEDTLS_SSL_PROTO_DTLS */
5237#if defined(MBEDTLS_SSL_PROTO_TLS)
5238 {
5239 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5240 }
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
5243 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5244 {
5245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5246 (unsigned) len,
5247 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5248 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5249 }
5250
5251 /*
5252 * Parse and validate record content type
5253 */
5254
5255 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005256
5257 /* Check record content type */
5258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5259 rec->cid_len = 0;
5260
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005261 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005262 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5263 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005264 {
5265 /* Shift pointers to account for record header including CID
5266 * struct {
5267 * ContentType special_type = tls12_cid;
5268 * ProtocolVersion version;
5269 * uint16 epoch;
5270 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005271 * opaque cid[cid_length]; // Additional field compared to
5272 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005273 * uint16 length;
5274 * opaque enc_content[DTLSCiphertext.length];
5275 * } DTLSCiphertext;
5276 */
5277
5278 /* So far, we only support static CID lengths
5279 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5281 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005282
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005283 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005284 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5286 (unsigned) len,
5287 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005288 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005289 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005290
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005291 /* configured CID len is guaranteed at most 255, see
5292 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5293 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005294 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005295 }
5296 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005298 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005299 if( ssl_check_record_type( rec->type ) )
5300 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5302 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5304 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005305 }
5306
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005307 /*
5308 * Parse and validate record version
5309 */
5310
Hanno Becker8061c6e2019-07-26 08:07:03 +01005311 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5312 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5314 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005315 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005316
Hanno Becker2881d802019-05-22 14:44:53 +01005317 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5320 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 }
5322
Hanno Beckere965bd32019-06-12 14:04:34 +01005323 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5326 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
5328
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 /*
5330 * Parse/Copy record sequence number.
5331 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005332
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333#if defined(MBEDTLS_SSL_PROTO_DTLS)
5334 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5335 {
5336 /* Copy explicit record sequence number from input buffer. */
5337 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5338 rec_hdr_ctr_len );
5339 }
5340 MBEDTLS_SSL_TRANSPORT_ELSE
5341#endif /* MBEDTLS_SSL_PROTO_DTLS */
5342#if defined(MBEDTLS_SSL_PROTO_TLS)
5343 {
5344 /* Copy implicit record sequence number from SSL context structure. */
5345 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5346 }
5347#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 /*
5350 * Parse record length.
5351 */
5352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005354 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005355 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5356
Hanno Becker8b09b732019-05-08 12:03:28 +01005357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005358 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359 rec->type,
5360 major_ver, minor_ver, rec->data_len ) );
5361
5362 rec->buf = buf;
5363 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005364
Hanno Beckerec014082019-07-26 08:20:27 +01005365 if( rec->data_len == 0 )
5366 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5367
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005368 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005369 * DTLS-related tests.
5370 * Check epoch before checking length constraint because
5371 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5372 * message gets duplicated before the corresponding Finished message,
5373 * the second ChangeCipherSpec should be discarded because it belongs
5374 * to an old epoch, but not because its length is shorter than
5375 * the minimum record length for packets using the new record transform.
5376 * Note that these two kinds of failures are handled differently,
5377 * as an unexpected record is silently skipped but an invalid
5378 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005379 */
5380#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005381 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005382 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005383 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005384
Hanno Beckere0452772019-07-10 17:12:07 +01005385 /* Check that the datagram is large enough to contain a record
5386 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005387 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005388 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5390 (unsigned) len,
5391 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005392 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5393 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005394
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005395 /* Records from other, non-matching epochs are silently discarded.
5396 * (The case of same-port Client reconnects must be considered in
5397 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005398 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005399 {
5400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5401 "expected %d, received %d",
5402 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005403
5404 /* Records from the next epoch are considered for buffering
5405 * (concretely: early Finished messages). */
5406 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5407 {
5408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5409 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5410 }
5411
Hanno Becker87b56262019-07-10 14:37:41 +01005412 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005413 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005414#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005415 /* For records from the correct epoch, check whether their
5416 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005417 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5420 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5421 }
5422#endif
5423 }
5424#endif /* MBEDTLS_SSL_PROTO_DTLS */
5425
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005426 return( 0 );
5427}
Paul Bakker5121ce52009-01-03 21:22:43 +00005428
Hanno Becker87b56262019-07-10 14:37:41 +01005429
5430#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5431static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5432{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005433 unsigned int rec_epoch = (unsigned int)
5434 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005435
5436 /*
5437 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5438 * access the first byte of record content (handshake type), as we
5439 * have an active transform (possibly iv_len != 0), so use the
5440 * fact that the record header len is 13 instead.
5441 */
5442 if( rec_epoch == 0 &&
5443 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5444 MBEDTLS_SSL_IS_SERVER &&
5445 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5446 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5447 ssl->in_left > 13 &&
5448 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5451 "from the same port" ) );
5452 return( ssl_handle_possible_reconnect( ssl ) );
5453 }
5454
5455 return( 0 );
5456}
5457#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459/*
5460 * If applicable, decrypt (and decompress) record content
5461 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005462static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5463 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464{
5465 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005468 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5471 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 ret = mbedtls_ssl_hw_record_read( ssl );
5476 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5479 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005480 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005481
5482 if( ret == 0 )
5483 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005486 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005488 unsigned char const old_msg_type = rec->type;
5489
Hanno Becker611a83b2018-01-03 14:27:32 +00005490 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005491 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005494
Hanno Beckera5a2b082019-05-15 14:03:01 +01005495#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005496 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005497 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5498 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005499 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005501 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005502 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005503#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005504
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 return( ret );
5506 }
5507
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005509 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005510 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005511 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005515 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Hanno Beckera5a2b082019-05-15 14:03:01 +01005517#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005518 /* We have already checked the record content type
5519 * in ssl_parse_record_header(), failing or silently
5520 * dropping the record in the case of an unknown type.
5521 *
5522 * Since with the use of CIDs, the record content type
5523 * might change during decryption, re-check the record
5524 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005525 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5528 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5529 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005530#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005531
Hanno Becker106f3da2019-07-12 09:35:58 +01005532 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005533 {
5534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005535 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005536 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005537 {
5538 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5541 }
5542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5543
5544 ssl->nb_zero++;
5545
5546 /*
5547 * Three or more empty messages may be a DoS attack
5548 * (excessive CPU consumption).
5549 */
5550 if( ssl->nb_zero > 3 )
5551 {
5552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005553 "messages, possible DoS attack" ) );
5554 /* Treat the records as if they were not properly authenticated,
5555 * thereby failing the connection if we see more than allowed
5556 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5558 }
5559 }
5560 else
5561 ssl->nb_zero = 0;
5562
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005563 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5564#if defined(MBEDTLS_SSL_PROTO_TLS)
5565 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005566 {
5567 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005568 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005569 if( ++ssl->in_ctr[i - 1] != 0 )
5570 break;
5571
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005572 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005573 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005574 {
5575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5576 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5577 }
5578 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005579#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005580
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005584 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005586 {
5587 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005590 return( ret );
5591 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005596 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005599 }
5600#endif
5601
Hanno Beckerf0242852019-07-09 17:30:02 +01005602 /* Check actual (decrypted) record content length against
5603 * configured maximum. */
5604 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5607 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5608 }
5609
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 return( 0 );
5611}
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005614
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615/*
5616 * Read a record.
5617 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005618 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5619 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5620 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005621 */
Hanno Becker1097b342018-08-15 14:09:41 +01005622
5623/* Helper functions for mbedtls_ssl_read_record(). */
5624static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005625static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5626static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005627
Hanno Becker327c93b2018-08-15 13:56:18 +01005628int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005629 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630{
5631 int ret;
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005634
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005635 if( ssl->keep_current_message == 0 )
5636 {
5637 do {
Simon Butcher99000142016-10-13 17:21:01 +01005638
Hanno Becker26994592018-08-15 14:14:59 +01005639 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005640 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005641 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005644 {
Hanno Becker40f50842018-08-15 14:48:01 +01005645#if defined(MBEDTLS_SSL_PROTO_DTLS)
5646 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005647
Hanno Becker40f50842018-08-15 14:48:01 +01005648 /* We only check for buffered messages if the
5649 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005651 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005652 {
Hanno Becker40f50842018-08-15 14:48:01 +01005653 if( ssl_load_buffered_message( ssl ) == 0 )
5654 have_buffered = 1;
5655 }
5656
5657 if( have_buffered == 0 )
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
5659 {
5660 ret = ssl_get_next_record( ssl );
5661 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5662 continue;
5663
5664 if( ret != 0 )
5665 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005666 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005667 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005668 return( ret );
5669 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005670 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005671 }
5672
5673 ret = mbedtls_ssl_handle_message_type( ssl );
5674
Hanno Becker40f50842018-08-15 14:48:01 +01005675#if defined(MBEDTLS_SSL_PROTO_DTLS)
5676 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5677 {
5678 /* Buffer future message */
5679 ret = ssl_buffer_message( ssl );
5680 if( ret != 0 )
5681 return( ret );
5682
5683 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5684 }
5685#endif /* MBEDTLS_SSL_PROTO_DTLS */
5686
Hanno Becker90333da2017-10-10 11:27:13 +01005687 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5688 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005689
5690 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005691 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005692 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005693 return( ret );
5694 }
5695
Hanno Becker327c93b2018-08-15 13:56:18 +01005696 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005697 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005698 {
5699 mbedtls_ssl_update_handshake_status( ssl );
5700 }
Simon Butcher99000142016-10-13 17:21:01 +01005701 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005702 else
Simon Butcher99000142016-10-13 17:21:01 +01005703 {
Hanno Becker02f59072018-08-15 14:00:24 +01005704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005705 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005706 }
5707
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5709
5710 return( 0 );
5711}
5712
Hanno Becker40f50842018-08-15 14:48:01 +01005713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005714static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005715{
Hanno Becker40f50842018-08-15 14:48:01 +01005716 if( ssl->in_left > ssl->next_record_offset )
5717 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005718
Hanno Becker40f50842018-08-15 14:48:01 +01005719 return( 0 );
5720}
5721
5722static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5723{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005724 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005725 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005726 int ret = 0;
5727
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005728 if( hs == NULL )
5729 return( -1 );
5730
Hanno Beckere00ae372018-08-20 09:39:42 +01005731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5732
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005733 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5734 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5735 {
5736 /* Check if we have seen a ChangeCipherSpec before.
5737 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005738 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005739 {
5740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5741 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005742 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 }
5744
Hanno Becker39b8bc92018-08-28 17:17:13 +01005745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005746 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5747 ssl->in_msglen = 1;
5748 ssl->in_msg[0] = 1;
5749
5750 /* As long as they are equal, the exact value doesn't matter. */
5751 ssl->in_left = 0;
5752 ssl->next_record_offset = 0;
5753
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005754 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005755 goto exit;
5756 }
Hanno Becker37f95322018-08-16 13:55:32 +01005757
Hanno Beckerb8f50142018-08-28 10:01:34 +01005758#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005759 /* Debug only */
5760 {
5761 unsigned offset;
5762 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5763 {
5764 hs_buf = &hs->buffering.hs[offset];
5765 if( hs_buf->is_valid == 1 )
5766 {
5767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5768 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005769 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005770 }
5771 }
5772 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005773#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005774
5775 /* Check if we have buffered and/or fully reassembled the
5776 * next handshake message. */
5777 hs_buf = &hs->buffering.hs[0];
5778 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5779 {
5780 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005781 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005782
5783 /* Double-check that we haven't accidentally buffered
5784 * a message that doesn't fit into the input buffer. */
5785 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5786 {
5787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5789 }
5790
5791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5792 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5793 hs_buf->data, msg_len + 12 );
5794
5795 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5796 ssl->in_hslen = msg_len + 12;
5797 ssl->in_msglen = msg_len + 12;
5798 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5799
5800 ret = 0;
5801 goto exit;
5802 }
5803 else
5804 {
5805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5806 hs->in_msg_seq ) );
5807 }
5808
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005809 ret = -1;
5810
5811exit:
5812
5813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5814 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005815}
5816
Hanno Beckera02b0b42018-08-21 17:20:27 +01005817static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5818 size_t desired )
5819{
5820 int offset;
5821 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5823 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005824
Hanno Becker01315ea2018-08-21 17:22:17 +01005825 /* Get rid of future records epoch first, if such exist. */
5826 ssl_free_buffered_record( ssl );
5827
5828 /* Check if we have enough space available now. */
5829 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5830 hs->buffering.total_bytes_buffered ) )
5831 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005833 return( 0 );
5834 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005835
Hanno Becker4f432ad2018-08-28 10:02:32 +01005836 /* We don't have enough space to buffer the next expected handshake
5837 * message. Remove buffers used for future messages to gain space,
5838 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005839 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5840 offset >= 0; offset-- )
5841 {
5842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5843 offset ) );
5844
Hanno Beckerb309b922018-08-23 13:18:05 +01005845 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005846
5847 /* Check if we have enough space available now. */
5848 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5849 hs->buffering.total_bytes_buffered ) )
5850 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005852 return( 0 );
5853 }
5854 }
5855
5856 return( -1 );
5857}
5858
Hanno Becker40f50842018-08-15 14:48:01 +01005859static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5860{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005861 int ret = 0;
5862 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5863
5864 if( hs == NULL )
5865 return( 0 );
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5868
5869 switch( ssl->in_msgtype )
5870 {
5871 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005873
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005874 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005875 break;
5876
5877 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005878 {
5879 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005880 unsigned recv_msg_seq = (unsigned)
5881 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005882
Hanno Becker37f95322018-08-16 13:55:32 +01005883 mbedtls_ssl_hs_buffer *hs_buf;
5884 size_t msg_len = ssl->in_hslen - 12;
5885
5886 /* We should never receive an old handshake
5887 * message - double-check nonetheless. */
5888 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5889 {
5890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5892 }
5893
5894 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5895 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5896 {
5897 /* Silently ignore -- message too far in the future */
5898 MBEDTLS_SSL_DEBUG_MSG( 2,
5899 ( "Ignore future HS message with sequence number %u, "
5900 "buffering window %u - %u",
5901 recv_msg_seq, ssl->handshake->in_msg_seq,
5902 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5903
5904 goto exit;
5905 }
5906
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5908 recv_msg_seq, recv_msg_seq_offset ) );
5909
5910 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5911
5912 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005913 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005914 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005915 size_t reassembly_buf_sz;
5916
Hanno Becker37f95322018-08-16 13:55:32 +01005917 hs_buf->is_fragmented =
5918 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5919
5920 /* We copy the message back into the input buffer
5921 * after reassembly, so check that it's not too large.
5922 * This is an implementation-specific limitation
5923 * and not one from the standard, hence it is not
5924 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005925 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005926 {
5927 /* Ignore message */
5928 goto exit;
5929 }
5930
Hanno Beckere0b150f2018-08-21 15:51:03 +01005931 /* Check if we have enough space to buffer the message. */
5932 if( hs->buffering.total_bytes_buffered >
5933 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5937 }
5938
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005939 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5940 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005941
5942 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5943 hs->buffering.total_bytes_buffered ) )
5944 {
5945 if( recv_msg_seq_offset > 0 )
5946 {
5947 /* If we can't buffer a future message because
5948 * of space limitations -- ignore. */
5949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5950 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5951 (unsigned) hs->buffering.total_bytes_buffered ) );
5952 goto exit;
5953 }
Hanno Beckere1801392018-08-21 16:51:05 +01005954 else
5955 {
5956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5957 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5958 (unsigned) hs->buffering.total_bytes_buffered ) );
5959 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005960
Hanno Beckera02b0b42018-08-21 17:20:27 +01005961 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005962 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5964 (unsigned) msg_len,
5965 (unsigned) reassembly_buf_sz,
5966 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005967 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005968 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5969 goto exit;
5970 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005971 }
5972
5973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5974 msg_len ) );
5975
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005976 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5977 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005978 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005980 goto exit;
5981 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005982 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005983
5984 /* Prepare final header: copy msg_type, length and message_seq,
5985 * then add standardised fragment_offset and fragment_length */
5986 memcpy( hs_buf->data, ssl->in_msg, 6 );
5987 memset( hs_buf->data + 6, 0, 3 );
5988 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5989
5990 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005991
5992 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005993 }
5994 else
5995 {
5996 /* Make sure msg_type and length are consistent */
5997 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6000 /* Ignore */
6001 goto exit;
6002 }
6003 }
6004
Hanno Becker4422bbb2018-08-20 09:40:19 +01006005 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006006 {
6007 size_t frag_len, frag_off;
6008 unsigned char * const msg = hs_buf->data + 12;
6009
6010 /*
6011 * Check and copy current fragment
6012 */
6013
6014 /* Validation of header fields already done in
6015 * mbedtls_ssl_prepare_handshake_record(). */
6016 frag_off = ssl_get_hs_frag_off( ssl );
6017 frag_len = ssl_get_hs_frag_len( ssl );
6018
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6020 frag_off, frag_len ) );
6021 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6022
6023 if( hs_buf->is_fragmented )
6024 {
6025 unsigned char * const bitmask = msg + msg_len;
6026 ssl_bitmask_set( bitmask, frag_off, frag_len );
6027 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6028 msg_len ) == 0 );
6029 }
6030 else
6031 {
6032 hs_buf->is_complete = 1;
6033 }
6034
6035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6036 hs_buf->is_complete ? "" : "not yet " ) );
6037 }
6038
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006039 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006040 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006041
6042 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006043 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006044 break;
6045 }
6046
6047exit:
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6050 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006051}
6052#endif /* MBEDTLS_SSL_PROTO_DTLS */
6053
Hanno Becker1097b342018-08-15 14:09:41 +01006054static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006055{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006056 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006057 * Consume last content-layer message and potentially
6058 * update in_msglen which keeps track of the contents'
6059 * consumption state.
6060 *
6061 * (1) Handshake messages:
6062 * Remove last handshake message, move content
6063 * and adapt in_msglen.
6064 *
6065 * (2) Alert messages:
6066 * Consume whole record content, in_msglen = 0.
6067 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006068 * (3) Change cipher spec:
6069 * Consume whole record content, in_msglen = 0.
6070 *
6071 * (4) Application data:
6072 * Don't do anything - the record layer provides
6073 * the application data as a stream transport
6074 * and consumes through mbedtls_ssl_read only.
6075 *
6076 */
6077
6078 /* Case (1): Handshake messages */
6079 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006080 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006081 /* Hard assertion to be sure that no application data
6082 * is in flight, as corrupting ssl->in_msglen during
6083 * ssl->in_offt != NULL is fatal. */
6084 if( ssl->in_offt != NULL )
6085 {
6086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6088 }
6089
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006090 /*
6091 * Get next Handshake message in the current record
6092 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006093
Hanno Becker4a810fb2017-05-24 16:27:30 +01006094 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006095 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 * current handshake content: If DTLS handshake
6097 * fragmentation is used, that's the fragment
6098 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006099 * size here is faulty and should be changed at
6100 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006101 * (2) While it doesn't seem to cause problems, one
6102 * has to be very careful not to assume that in_hslen
6103 * is always <= in_msglen in a sensible communication.
6104 * Again, it's wrong for DTLS handshake fragmentation.
6105 * The following check is therefore mandatory, and
6106 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006107 * Additionally, ssl->in_hslen might be arbitrarily out of
6108 * bounds after handling a DTLS message with an unexpected
6109 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006110 */
6111 if( ssl->in_hslen < ssl->in_msglen )
6112 {
6113 ssl->in_msglen -= ssl->in_hslen;
6114 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6115 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006116
Hanno Becker4a810fb2017-05-24 16:27:30 +01006117 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6118 ssl->in_msg, ssl->in_msglen );
6119 }
6120 else
6121 {
6122 ssl->in_msglen = 0;
6123 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006124
Hanno Becker4a810fb2017-05-24 16:27:30 +01006125 ssl->in_hslen = 0;
6126 }
6127 /* Case (4): Application data */
6128 else if( ssl->in_offt != NULL )
6129 {
6130 return( 0 );
6131 }
6132 /* Everything else (CCS & Alerts) */
6133 else
6134 {
6135 ssl->in_msglen = 0;
6136 }
6137
Hanno Becker1097b342018-08-15 14:09:41 +01006138 return( 0 );
6139}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006140
Hanno Beckere74d5562018-08-15 14:26:08 +01006141static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6142{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006143 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006144 return( 1 );
6145
6146 return( 0 );
6147}
6148
Hanno Becker5f066e72018-08-16 14:56:31 +01006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
6150
6151static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6152{
6153 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6154 if( hs == NULL )
6155 return;
6156
Hanno Becker01315ea2018-08-21 17:22:17 +01006157 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006158 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006159 hs->buffering.total_bytes_buffered -=
6160 hs->buffering.future_record.len;
6161
6162 mbedtls_free( hs->buffering.future_record.data );
6163 hs->buffering.future_record.data = NULL;
6164 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006165}
6166
6167static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6168{
6169 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6170 unsigned char * rec;
6171 size_t rec_len;
6172 unsigned rec_epoch;
6173
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006174 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006175 return( 0 );
6176
6177 if( hs == NULL )
6178 return( 0 );
6179
Hanno Becker5f066e72018-08-16 14:56:31 +01006180 rec = hs->buffering.future_record.data;
6181 rec_len = hs->buffering.future_record.len;
6182 rec_epoch = hs->buffering.future_record.epoch;
6183
6184 if( rec == NULL )
6185 return( 0 );
6186
Hanno Becker4cb782d2018-08-20 11:19:05 +01006187 /* Only consider loading future records if the
6188 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006189 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006190 return( 0 );
6191
Hanno Becker5f066e72018-08-16 14:56:31 +01006192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6193
6194 if( rec_epoch != ssl->in_epoch )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6197 goto exit;
6198 }
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6201
6202 /* Double-check that the record is not too large */
6203 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6204 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6205 {
6206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6208 }
6209
6210 memcpy( ssl->in_hdr, rec, rec_len );
6211 ssl->in_left = rec_len;
6212 ssl->next_record_offset = 0;
6213
6214 ssl_free_buffered_record( ssl );
6215
6216exit:
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6218 return( 0 );
6219}
6220
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006221static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6222 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006223{
6224 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006225
6226 /* Don't buffer future records outside handshakes. */
6227 if( hs == NULL )
6228 return( 0 );
6229
6230 /* Only buffer handshake records (we are only interested
6231 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006232 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006233 return( 0 );
6234
6235 /* Don't buffer more than one future epoch record. */
6236 if( hs->buffering.future_record.data != NULL )
6237 return( 0 );
6238
Hanno Becker01315ea2018-08-21 17:22:17 +01006239 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006240 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006241 hs->buffering.total_bytes_buffered ) )
6242 {
6243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006244 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006245 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006246 return( 0 );
6247 }
6248
Hanno Becker5f066e72018-08-16 14:56:31 +01006249 /* Buffer record */
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6251 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006253
6254 /* ssl_parse_record_header() only considers records
6255 * of the next epoch as candidates for buffering. */
6256 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006257 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006258
6259 hs->buffering.future_record.data =
6260 mbedtls_calloc( 1, hs->buffering.future_record.len );
6261 if( hs->buffering.future_record.data == NULL )
6262 {
6263 /* If we run out of RAM trying to buffer a
6264 * record from the next epoch, just ignore. */
6265 return( 0 );
6266 }
6267
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006268 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006269
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006270 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006271 return( 0 );
6272}
6273
6274#endif /* MBEDTLS_SSL_PROTO_DTLS */
6275
Hanno Beckere74d5562018-08-15 14:26:08 +01006276static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006277{
6278 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006279 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006280
Hanno Becker5f066e72018-08-16 14:56:31 +01006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282 /* We might have buffered a future record; if so,
6283 * and if the epoch matches now, load it.
6284 * On success, this call will set ssl->in_left to
6285 * the length of the buffered record, so that
6286 * the calls to ssl_fetch_input() below will
6287 * essentially be no-ops. */
6288 ret = ssl_load_buffered_record( ssl );
6289 if( ret != 0 )
6290 return( ret );
6291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006292
Hanno Becker8b09b732019-05-08 12:03:28 +01006293 /* Ensure that we have enough space available for the default form
6294 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6295 * with no space for CIDs counted in). */
6296 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6297 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006300 return( ret );
6301 }
6302
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006303 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6304 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006307 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006308 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006309 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6310 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006311 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006312 if( ret != 0 )
6313 return( ret );
6314
6315 /* Fall through to handling of unexpected records */
6316 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6317 }
6318
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006319 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6320 {
Hanno Becker87b56262019-07-10 14:37:41 +01006321#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006322 /* Reset in pointers to default state for TLS/DTLS records,
6323 * assuming no CID and no offset between record content and
6324 * record plaintext. */
6325 ssl_update_in_pointers( ssl );
6326
Hanno Becker69412452019-07-12 08:33:49 +01006327 /* Setup internal message pointers from record structure. */
6328 ssl->in_msgtype = rec.type;
6329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6330 ssl->in_len = ssl->in_cid + rec.cid_len;
6331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006332 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006333 ssl->in_msglen = rec.data_len;
6334
Hanno Becker87b56262019-07-10 14:37:41 +01006335 ret = ssl_check_client_reconnect( ssl );
6336 if( ret != 0 )
6337 return( ret );
6338#endif
6339
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006340 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006341 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006342
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6344 "(header)" ) );
6345 }
6346 else
6347 {
6348 /* Skip invalid record and the rest of the datagram */
6349 ssl->next_record_offset = 0;
6350 ssl->in_left = 0;
6351
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6353 "(header)" ) );
6354 }
6355
6356 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006357 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006358 }
Hanno Becker87b56262019-07-10 14:37:41 +01006359 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006360#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006361 {
6362 return( ret );
6363 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006364 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006367 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006368 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006369 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006370 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006371 if( ssl->next_record_offset < ssl->in_left )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6374 }
6375 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006376 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006378#if defined(MBEDTLS_SSL_PROTO_TLS)
6379 {
Hanno Beckere0452772019-07-10 17:12:07 +01006380 /*
6381 * Fetch record contents from underlying transport.
6382 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006383 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006384 if( ret != 0 )
6385 {
6386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6387 return( ret );
6388 }
6389
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006390 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006391 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006392#endif /* MBEDTLS_SSL_PROTO_TLS */
6393
6394 /*
6395 * Decrypt record contents.
6396 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006397
Hanno Beckera89610a2019-07-11 13:07:45 +01006398 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006401 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006402 {
6403 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006404 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006405 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006406 /* Except when waiting for Finished as a bad mac here
6407 * probably means something went wrong in the handshake
6408 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6409 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6410 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6411 {
6412#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6413 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6414 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006415 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006416 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6417 }
6418#endif
6419 return( ret );
6420 }
6421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006423 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6424 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006428 }
6429#endif
6430
Hanno Becker4a810fb2017-05-24 16:27:30 +01006431 /* As above, invalid records cause
6432 * dismissal of the whole datagram. */
6433
6434 ssl->next_record_offset = 0;
6435 ssl->in_left = 0;
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006438 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006439 }
6440
6441 return( ret );
6442 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006443 MBEDTLS_SSL_TRANSPORT_ELSE
6444#endif /* MBEDTLS_SSL_PROTO_DTLS */
6445#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006446 {
6447 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6449 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006450 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006451 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006453 }
6454#endif
6455 return( ret );
6456 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006457#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006458 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006459
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006460
6461 /* Reset in pointers to default state for TLS/DTLS records,
6462 * assuming no CID and no offset between record content and
6463 * record plaintext. */
6464 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006465#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6466 ssl->in_len = ssl->in_cid + rec.cid_len;
6467#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006468 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006469
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006470 /* The record content type may change during decryption,
6471 * so re-read it. */
6472 ssl->in_msgtype = rec.type;
6473 /* Also update the input buffer, because unfortunately
6474 * the server-side ssl_parse_client_hello() reparses the
6475 * record header when receiving a ClientHello initiating
6476 * a renegotiation. */
6477 ssl->in_hdr[0] = rec.type;
6478 ssl->in_msg = rec.buf + rec.data_offset;
6479 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006480 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006481
Simon Butcher99000142016-10-13 17:21:01 +01006482 return( 0 );
6483}
6484
6485int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6486{
6487 int ret;
6488
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006489 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006490 * Handle particular types of records
6491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 {
Simon Butcher99000142016-10-13 17:21:01 +01006494 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6495 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006496 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006497 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 }
6499
Hanno Beckere678eaa2018-08-21 14:57:46 +01006500 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006501 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006502 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006503 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6505 ssl->in_msglen ) );
6506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006507 }
6508
Hanno Beckere678eaa2018-08-21 14:57:46 +01006509 if( ssl->in_msg[0] != 1 )
6510 {
6511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6512 ssl->in_msg[0] ) );
6513 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6514 }
6515
6516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006517 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006518 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6519 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6520 {
6521 if( ssl->handshake == NULL )
6522 {
6523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6524 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6525 }
6526
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6528 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6529 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006530#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006531 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006534 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006535 if( ssl->in_msglen != 2 )
6536 {
6537 /* Note: Standard allows for more than one 2 byte alert
6538 to be packed in a single message, but Mbed TLS doesn't
6539 currently support this. */
6540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6541 ssl->in_msglen ) );
6542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6543 }
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 ssl->in_msg[0], ssl->in_msg[1] ) );
6547
6548 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006549 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006554 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006556 }
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6559 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6562 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006563 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006564
6565#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6566 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6567 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6568 {
Hanno Becker90333da2017-10-10 11:27:13 +01006569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006570 /* Will be handled when trying to parse ServerHello */
6571 return( 0 );
6572 }
6573#endif
6574
6575#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006576 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006577 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6578 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006579 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6580 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6581 {
6582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6583 /* Will be handled in mbedtls_ssl_parse_certificate() */
6584 return( 0 );
6585 }
6586#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6587
6588 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006589 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 }
6591
Hanno Beckerc76c6192017-06-06 10:03:17 +01006592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006593 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006594 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006595 /* Drop unexpected ApplicationData records,
6596 * except at the beginning of renegotiations */
6597 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6598 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6599#if defined(MBEDTLS_SSL_RENEGOTIATION)
6600 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6601 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006602#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006603 )
6604 {
6605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6606 return( MBEDTLS_ERR_SSL_NON_FATAL );
6607 }
6608
6609 if( ssl->handshake != NULL &&
6610 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6611 {
6612 ssl_handshake_wrapup_free_hs_transform( ssl );
6613 }
6614 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006615#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006616
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 return( 0 );
6618}
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006621{
6622 int ret;
6623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6625 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6626 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006627 {
6628 return( ret );
6629 }
6630
6631 return( 0 );
6632}
6633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006635 unsigned char level,
6636 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006637{
6638 int ret;
6639
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006640 if( ssl == NULL || ssl->conf == NULL )
6641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006647 ssl->out_msglen = 2;
6648 ssl->out_msg[0] = level;
6649 ssl->out_msg[1] = message;
6650
Hanno Becker67bc7c32018-08-06 11:33:50 +01006651 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006654 return( ret );
6655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006657
6658 return( 0 );
6659}
6660
Hanno Becker17572472019-02-08 07:19:04 +00006661#if defined(MBEDTLS_X509_CRT_PARSE_C)
6662static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6663{
6664#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6665 if( session->peer_cert != NULL )
6666 {
6667 mbedtls_x509_crt_free( session->peer_cert );
6668 mbedtls_free( session->peer_cert );
6669 session->peer_cert = NULL;
6670 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006671#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006672 if( session->peer_cert_digest != NULL )
6673 {
6674 /* Zeroization is not necessary. */
6675 mbedtls_free( session->peer_cert_digest );
6676 session->peer_cert_digest = NULL;
6677 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6678 session->peer_cert_digest_len = 0;
6679 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006680#else
6681 ((void) session);
6682#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006683}
6684#endif /* MBEDTLS_X509_CRT_PARSE_C */
6685
Paul Bakker5121ce52009-01-03 21:22:43 +00006686/*
6687 * Handshake functions
6688 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006689#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006690/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692{
Hanno Beckerdf645962019-06-26 13:02:22 +01006693 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6694 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006697
Hanno Becker5097cba2019-02-05 13:36:46 +00006698 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006701 ssl->state++;
6702 return( 0 );
6703 }
6704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6706 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006707}
6708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006710{
Hanno Beckerdf645962019-06-26 13:02:22 +01006711 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6712 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006715
Hanno Becker5097cba2019-02-05 13:36:46 +00006716 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006719 ssl->state++;
6720 return( 0 );
6721 }
6722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006725}
Gilles Peskinef9828522017-05-03 12:28:43 +02006726
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006727#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006728/* Some certificate support -> implement write and parse */
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006733 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006735 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6736 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006739
Hanno Becker5097cba2019-02-05 13:36:46 +00006740 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006743 ssl->state++;
6744 return( 0 );
6745 }
6746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006748 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6749 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 {
6751 if( ssl->client_auth == 0 )
6752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 ssl->state++;
6755 return( 0 );
6756 }
6757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 /*
6760 * If using SSLv3 and got no cert, send an Alert message
6761 * (otherwise an empty Certificate message will be sent).
6762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006764 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 {
6766 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6768 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6769 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006772 goto write_msg;
6773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#endif /* MBEDTLS_SSL_CLI_C */
6777#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006778 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6783 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 }
6785 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006786#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
6790 /*
6791 * 0 . 0 handshake type
6792 * 1 . 3 handshake length
6793 * 4 . 6 length of all certs
6794 * 7 . 9 length of cert. 1
6795 * 10 . n-1 peer certificate
6796 * n . n+2 length of cert. 2
6797 * n+3 . ... upper level cert, etc.
6798 */
6799 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
Paul Bakker29087132010-03-21 21:03:34 +00006802 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 {
6804 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006805 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006808 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810 }
6811
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006812 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
6814 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6815 i += n; crt = crt->next;
6816 }
6817
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006818 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
6820 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6822 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006824#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006825write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006826#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
6828 ssl->state++;
6829
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006830 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 return( ret );
6834 }
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Paul Bakkered27a042013-04-18 22:46:23 +02006838 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839}
6840
Hanno Becker285ff0c2019-01-31 07:44:03 +00006841#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006842
6843#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006844static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6845 unsigned char *crt_buf,
6846 size_t crt_buf_len )
6847{
6848 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6849
6850 if( peer_crt == NULL )
6851 return( -1 );
6852
6853 if( peer_crt->raw.len != crt_buf_len )
6854 return( -1 );
6855
Hanno Becker68b856d2019-02-08 14:00:04 +00006856 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006857}
Hanno Becker5882dd02019-06-06 16:25:57 +01006858#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006859static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6860 unsigned char *crt_buf,
6861 size_t crt_buf_len )
6862{
6863 int ret;
6864 unsigned char const * const peer_cert_digest =
6865 ssl->session->peer_cert_digest;
6866 mbedtls_md_type_t const peer_cert_digest_type =
6867 ssl->session->peer_cert_digest_type;
6868 mbedtls_md_info_t const * const digest_info =
6869 mbedtls_md_info_from_type( peer_cert_digest_type );
6870 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6871 size_t digest_len;
6872
6873 if( peer_cert_digest == NULL || digest_info == NULL )
6874 return( -1 );
6875
6876 digest_len = mbedtls_md_get_size( digest_info );
6877 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6878 return( -1 );
6879
6880 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6881 if( ret != 0 )
6882 return( -1 );
6883
6884 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6885}
Hanno Becker5882dd02019-06-06 16:25:57 +01006886#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006887#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006888
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006889/*
6890 * Once the certificate message is read, parse it into a cert chain and
6891 * perform basic checks, but leave actual verification to the caller
6892 */
Hanno Becker35e41772019-02-05 15:37:23 +00006893static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6894 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006895{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006896 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006897#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6898 int crt_cnt=0;
6899#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006900 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006901 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006906 mbedtls_ssl_pend_fatal_alert( ssl,
6907 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 }
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6912 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006915 mbedtls_ssl_pend_fatal_alert( ssl,
6916 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 }
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006921
Paul Bakker5121ce52009-01-03 21:22:43 +00006922 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006924 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006925 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006926
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006927 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006931 mbedtls_ssl_pend_fatal_alert( ssl,
6932 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 }
6935
Hanno Becker33c3dc82019-01-30 14:46:46 +00006936 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6937 i += 3;
6938
Hanno Becker33c3dc82019-01-30 14:46:46 +00006939 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 while( i < ssl->in_hslen )
6941 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006942 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006943 if ( i + 3 > ssl->in_hslen ) {
6944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006945 mbedtls_ssl_pend_fatal_alert( ssl,
6946 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006947 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6948 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006949 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6950 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 if( ssl->in_msg[i] != 0 )
6952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006954 mbedtls_ssl_pend_fatal_alert( ssl,
6955 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006957 }
6958
Hanno Becker33c3dc82019-01-30 14:46:46 +00006959 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006960 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 i += 3;
6962
6963 if( n < 128 || i + n > ssl->in_hslen )
6964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006966 mbedtls_ssl_pend_fatal_alert( ssl,
6967 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 }
6970
Hanno Becker33c3dc82019-01-30 14:46:46 +00006971 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006972#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6973 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006974 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6975 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006976 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006977 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006978 /* During client-side renegotiation, check that the server's
6979 * end-CRTs hasn't changed compared to the initial handshake,
6980 * mitigating the triple handshake attack. On success, reuse
6981 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6983 if( ssl_check_peer_crt_unchanged( ssl,
6984 &ssl->in_msg[i],
6985 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006986 {
Hanno Becker35e41772019-02-05 15:37:23 +00006987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006988 mbedtls_ssl_pend_fatal_alert( ssl,
6989 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006990 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006991 }
Hanno Becker35e41772019-02-05 15:37:23 +00006992
6993 /* Now we can safely free the original chain. */
6994 ssl_clear_peer_cert( ssl->session );
6995 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006996#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6997
Hanno Becker33c3dc82019-01-30 14:46:46 +00006998 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006999#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007000 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007001#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007002 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007003 * it in-place from the input buffer instead of making a copy. */
7004 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7005#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007006 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007008 case 0: /*ok*/
7009 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7010 /* Ignore certificate with an unknown algorithm: maybe a
7011 prior certificate was already trusted. */
7012 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007013
Hanno Becker33c3dc82019-01-30 14:46:46 +00007014 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7015 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7016 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007017
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7019 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7020 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007021
Hanno Becker33c3dc82019-01-30 14:46:46 +00007022 default:
7023 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7024 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007025 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007026 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7027 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007028 }
7029
7030 i += n;
7031 }
7032
Hanno Becker35e41772019-02-05 15:37:23 +00007033 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007034 return( 0 );
7035}
7036
Hanno Beckerb8a08572019-02-05 12:49:06 +00007037#if defined(MBEDTLS_SSL_SRV_C)
7038static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7039{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007040 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007041 return( -1 );
7042
7043#if defined(MBEDTLS_SSL_PROTO_SSL3)
7044 /*
7045 * Check if the client sent an empty certificate
7046 */
Hanno Becker2881d802019-05-22 14:44:53 +01007047 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007048 {
7049 if( ssl->in_msglen == 2 &&
7050 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7051 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7052 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7053 {
7054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7055 return( 0 );
7056 }
7057
7058 return( -1 );
7059 }
7060#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7061
7062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7064 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7065 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7066 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7067 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7068 {
7069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7070 return( 0 );
7071 }
7072
Hanno Beckerb8a08572019-02-05 12:49:06 +00007073#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7074 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007075
7076 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007077}
7078#endif /* MBEDTLS_SSL_SRV_C */
7079
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007080/* Check if a certificate message is expected.
7081 * Return either
7082 * - SSL_CERTIFICATE_EXPECTED, or
7083 * - SSL_CERTIFICATE_SKIP
7084 * indicating whether a Certificate message is expected or not.
7085 */
7086#define SSL_CERTIFICATE_EXPECTED 0
7087#define SSL_CERTIFICATE_SKIP 1
7088static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7089 int authmode )
7090{
Hanno Becker473f98f2019-06-26 10:27:32 +01007091 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007092 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007093
7094 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7095 return( SSL_CERTIFICATE_SKIP );
7096
7097#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007098 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007099 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007100 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7101 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7102 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007103 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007104 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007105
7106 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7107 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007108 ssl->session_negotiate->verify_result =
7109 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7110 return( SSL_CERTIFICATE_SKIP );
7111 }
7112 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007113#else
7114 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007115#endif /* MBEDTLS_SSL_SRV_C */
7116
7117 return( SSL_CERTIFICATE_EXPECTED );
7118}
7119
Hanno Becker3cf50612019-02-05 14:36:34 +00007120static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7121 int authmode,
7122 mbedtls_x509_crt *chain,
7123 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007124{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007125 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007127 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007128 mbedtls_x509_crt *ca_chain;
7129 mbedtls_x509_crl *ca_crl;
7130
7131 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7132 return( 0 );
7133
7134#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7135 if( ssl->handshake->sni_ca_chain != NULL )
7136 {
7137 ca_chain = ssl->handshake->sni_ca_chain;
7138 ca_crl = ssl->handshake->sni_ca_crl;
7139 }
7140 else
7141#endif
7142 {
7143 ca_chain = ssl->conf->ca_chain;
7144 ca_crl = ssl->conf->ca_crl;
7145 }
7146
7147 /*
7148 * Main check: verify certificate
7149 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007150 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007151 chain,
7152 ca_chain, ca_crl,
7153 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007154#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007155 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007156#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007157 &ssl->session_negotiate->verify_result,
7158 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7159
Hanno Becker8c13ee62019-02-26 16:48:17 +00007160 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007161 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007162 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007163 }
7164
7165#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007166 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7168#endif
7169
7170 /*
7171 * Secondary checks: always done, but change 'ret' only if it was 0
7172 */
7173
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007174#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007175 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007176 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007177#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007178 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007179#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007180 mbedtls_pk_context *pk;
7181 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7182 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007183 {
7184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007185 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007186 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007187
7188 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007189 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007190 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007192 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007193
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007194 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007195#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007196
7197 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007198 {
7199 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7200
7201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007202 if( verify_ret == 0 )
7203 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007204 }
7205 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007206#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007207
7208 if( mbedtls_ssl_check_cert_usage( chain,
7209 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007210 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7211 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007212 &ssl->session_negotiate->verify_result ) != 0 )
7213 {
7214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007215 if( verify_ret == 0 )
7216 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007217 }
7218
7219 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7220 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7221 * with details encoded in the verification flags. All other kinds
7222 * of error codes, including those from the user provided f_vrfy
7223 * functions, are treated as fatal and lead to a failure of
7224 * ssl_parse_certificate even if verification was optional. */
7225 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007226 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7227 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007228 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007229 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007230 }
7231
7232 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7233 {
7234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007235 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007236 }
7237
Hanno Becker8c13ee62019-02-26 16:48:17 +00007238 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007239 {
7240 uint8_t alert;
7241
7242 /* The certificate may have been rejected for several reasons.
7243 Pick one and send the corresponding alert. Which alert to send
7244 may be a subject of debate in some cases. */
7245 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7246 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7248 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7250 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7251 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7252 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7254 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7256 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7260 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7262 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7264 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7265 else
7266 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007267 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007268 }
7269
7270#if defined(MBEDTLS_DEBUG_C)
7271 if( ssl->session_negotiate->verify_result != 0 )
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7274 ssl->session_negotiate->verify_result ) );
7275 }
7276 else
7277 {
7278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7279 }
7280#endif /* MBEDTLS_DEBUG_C */
7281
Hanno Becker8c13ee62019-02-26 16:48:17 +00007282 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007283}
7284
Hanno Becker34106f62019-02-08 14:59:05 +00007285#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007286
7287#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007288static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7289 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007290{
7291 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007292 /* Remember digest of the peer's end-CRT. */
7293 ssl->session_negotiate->peer_cert_digest =
7294 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7295 if( ssl->session_negotiate->peer_cert_digest == NULL )
7296 {
7297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7298 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007299 mbedtls_ssl_pend_fatal_alert( ssl,
7300 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007301
7302 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7303 }
7304
7305 ret = mbedtls_md( mbedtls_md_info_from_type(
7306 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7307 start, len,
7308 ssl->session_negotiate->peer_cert_digest );
7309
7310 ssl->session_negotiate->peer_cert_digest_type =
7311 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7312 ssl->session_negotiate->peer_cert_digest_len =
7313 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7314
7315 return( ret );
7316}
Hanno Becker5882dd02019-06-06 16:25:57 +01007317#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007318
7319static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7320 unsigned char *start, size_t len )
7321{
7322 unsigned char *end = start + len;
7323 int ret;
7324
7325 /* Make a copy of the peer's raw public key. */
7326 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7327 ret = mbedtls_pk_parse_subpubkey( &start, end,
7328 &ssl->handshake->peer_pubkey );
7329 if( ret != 0 )
7330 {
7331 /* We should have parsed the public key before. */
7332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7333 }
7334
7335 return( 0 );
7336}
7337#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7338
Hanno Becker3cf50612019-02-05 14:36:34 +00007339int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7340{
7341 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007342 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007343#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7344 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7345 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007346 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007347#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007348 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007349#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007350 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007351 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007352
7353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7354
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007355 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7356 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007357 {
7358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007359 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007360 }
7361
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007362#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7363 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007364 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007365 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007366 chain = ssl->handshake->ecrs_peer_cert;
7367 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007368 goto crt_verify;
7369 }
7370#endif
7371
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007372 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373 {
7374 /* mbedtls_ssl_read_record may have sent an alert already. We
7375 let it decide whether to alert. */
7376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007377 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378 }
7379
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007380#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007381 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7382 {
7383 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007384
Hanno Beckerb8a08572019-02-05 12:49:06 +00007385 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007386 ret = 0;
7387 else
7388 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007389
Hanno Becker613d4902019-02-05 13:11:17 +00007390 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007391 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007392#endif /* MBEDTLS_SSL_SRV_C */
7393
Hanno Becker35e41772019-02-05 15:37:23 +00007394 /* Clear existing peer CRT structure in case we tried to
7395 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007396 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007397
7398 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7399 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007400 {
7401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7402 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007403 mbedtls_ssl_pend_fatal_alert( ssl,
7404 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007405
Hanno Beckere4aeb762019-02-05 17:19:52 +00007406 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7407 goto exit;
7408 }
7409 mbedtls_x509_crt_init( chain );
7410
7411 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007412 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007413 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007414
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007415#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7416 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007417 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007418
7419crt_verify:
7420 if( ssl->handshake->ecrs_enabled)
7421 rs_ctx = &ssl->handshake->ecrs_ctx;
7422#endif
7423
Hanno Becker3cf50612019-02-05 14:36:34 +00007424 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007425 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007426 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007427 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007428
Hanno Becker3008d282019-02-05 17:02:28 +00007429#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007430 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007431 size_t pk_len;
7432 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Hanno Becker34106f62019-02-08 14:59:05 +00007434 /* We parse the CRT chain without copying, so
7435 * these pointers point into the input buffer,
7436 * and are hence still valid after freeing the
7437 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007438
Hanno Becker5882dd02019-06-06 16:25:57 +01007439#if defined(MBEDTLS_SSL_RENEGOTIATION)
7440 unsigned char *crt_start;
7441 size_t crt_len;
7442
Hanno Becker34106f62019-02-08 14:59:05 +00007443 crt_start = chain->raw.p;
7444 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007445#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007446
Hanno Becker8c13ee62019-02-26 16:48:17 +00007447 pk_start = chain->cache->pk_raw.p;
7448 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007449
7450 /* Free the CRT structures before computing
7451 * digest and copying the peer's public key. */
7452 mbedtls_x509_crt_free( chain );
7453 mbedtls_free( chain );
7454 chain = NULL;
7455
Hanno Becker5882dd02019-06-06 16:25:57 +01007456#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007457 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007459 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007460#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007461
Hanno Becker34106f62019-02-08 14:59:05 +00007462 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007464 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007465 }
Hanno Becker34106f62019-02-08 14:59:05 +00007466#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7467 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007468 ssl->session_negotiate->peer_cert = chain;
7469 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007470#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
Hanno Becker613d4902019-02-05 13:11:17 +00007474exit:
7475
Hanno Beckere4aeb762019-02-05 17:19:52 +00007476 if( ret == 0 )
7477 ssl->state++;
7478
7479#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7480 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7481 {
7482 ssl->handshake->ecrs_peer_cert = chain;
7483 chain = NULL;
7484 }
7485#endif
7486
7487 if( chain != NULL )
7488 {
7489 mbedtls_x509_crt_free( chain );
7490 mbedtls_free( chain );
7491 }
7492
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 return( ret );
7494}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007495#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007498{
7499 int ret;
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 ssl->out_msglen = 1;
7505 ssl->out_msg[0] = 1;
7506
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 ssl->state++;
7508
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007509 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007512 return( ret );
7513 }
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516
7517 return( 0 );
7518}
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007521{
7522 int ret;
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Hanno Becker327c93b2018-08-15 13:56:18 +01007526 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 return( ret );
7530 }
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007535 mbedtls_ssl_pend_fatal_alert( ssl,
7536 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 }
7539
Hanno Beckere678eaa2018-08-21 14:57:46 +01007540 /* CCS records are only accepted if they have length 1 and content '1',
7541 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007543 /*
7544 * Switch to our negotiated transform and session parameters for inbound
7545 * data.
7546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007548 ssl->transform_in = ssl->transform_negotiate;
7549 ssl->session_in = ssl->session_negotiate;
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007552 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007555 ssl_dtls_replay_reset( ssl );
7556#endif
7557
7558 /* Increment epoch */
7559 if( ++ssl->in_epoch == 0 )
7560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007562 /* This is highly unlikely to happen for legitimate reasons, so
7563 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007565 }
7566 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007567 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007569#if defined(MBEDTLS_SSL_PROTO_TLS)
7570 {
7571 memset( ssl->in_ctr, 0, 8 );
7572 }
7573#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574
Hanno Beckerf5970a02019-05-08 09:38:41 +01007575 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7578 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007583 mbedtls_ssl_pend_fatal_alert( ssl,
7584 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007586 }
7587 }
7588#endif
7589
Paul Bakker5121ce52009-01-03 21:22:43 +00007590 ssl->state++;
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
7594 return( 0 );
7595}
7596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7600 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007601 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007602 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7605#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007606 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007609 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007612}
7613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007617
7618 /*
7619 * Free our handshake params
7620 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007621 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007623 ssl->handshake = NULL;
7624
7625 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007627 */
7628 if( ssl->transform )
7629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_transform_free( ssl->transform );
7631 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007632 }
7633 ssl->transform = ssl->transform_negotiate;
7634 ssl->transform_negotiate = NULL;
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637}
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_RENEGOTIATION)
7644 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647 ssl->renego_records_seen = 0;
7648 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007649#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007650
7651 /*
7652 * Free the previous session and switch in the current one
7653 */
Paul Bakker0a597072012-09-25 21:55:46 +00007654 if( ssl->session )
7655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007657 /* RFC 7366 3.1: keep the EtM state */
7658 ssl->session_negotiate->encrypt_then_mac =
7659 ssl->session->encrypt_then_mac;
7660#endif
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_ssl_session_free( ssl->session );
7663 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007664 }
7665 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007666 ssl->session_negotiate = NULL;
7667
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007668#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007669 /*
7670 * Add cache entry
7671 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007672 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007673 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007674 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007675 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007676 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007678 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007679#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007682 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007683 ssl->handshake->flight != NULL )
7684 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007685 /* Cancel handshake timer */
7686 ssl_set_timer( ssl, 0 );
7687
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007688 /* Keep last flight around in case we need to resend it:
7689 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007691 }
7692 else
7693#endif
7694 ssl_handshake_wrapup_free_hs_transform( ssl );
7695
Paul Bakker48916f92012-09-16 19:57:18 +00007696 ssl->state++;
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007699}
7700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007703 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007707 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007708
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007709 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7710 mbedtls_ssl_suite_get_mac(
7711 mbedtls_ssl_ciphersuite_from_id(
7712 mbedtls_ssl_session_get_ciphersuite(
7713 ssl->session_negotiate ) ) ),
7714 ssl, ssl->out_msg + 4,
7715 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007716
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007717 /*
7718 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7719 * may define some other value. Currently (early 2016), no defined
7720 * ciphersuite does this (and this is unlikely to change as activity has
7721 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7722 */
Hanno Becker2881d802019-05-22 14:44:53 +01007723 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007726 ssl->verify_data_len = hash_len;
7727 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007728#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007729
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7732 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007733
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007734#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 /*
7736 * In case of session resuming, invert the client and server
7737 * ChangeCipherSpec messages order.
7738 */
Paul Bakker0a597072012-09-25 21:55:46 +00007739 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007742 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7743 MBEDTLS_SSL_IS_CLIENT )
7744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007746 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007749 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7750 MBEDTLS_SSL_IS_SERVER )
7751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007753 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 }
7756 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007757#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 ssl->state++;
7759
Paul Bakker48916f92012-09-16 19:57:18 +00007760 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007761 * Switch to our negotiated transform and session parameters for outbound
7762 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007768 {
7769 unsigned char i;
7770
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007771 /* Remember current epoch settings for resending */
7772 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007773 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007774
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007775 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007776 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007777
7778 /* Increment epoch */
7779 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007780 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007781 break;
7782
7783 /* The loop goes to its end iff the counter is wrapping */
7784 if( i == 0 )
7785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7787 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007788 }
7789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007790 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007792#if defined(MBEDTLS_SSL_PROTO_TLS)
7793 {
7794 memset( ssl->cur_out_ctr, 0, 8 );
7795 }
7796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007797
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007798 ssl->transform_out = ssl->transform_negotiate;
7799 ssl->session_out = ssl->session_negotiate;
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7802 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007808 }
7809 }
7810#endif
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007813 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007815#endif
7816
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007817 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007818 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 return( ret );
7821 }
7822
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007824 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007825 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7826 {
7827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7828 return( ret );
7829 }
7830#endif
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833
7834 return( 0 );
7835}
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007838#define SSL_MAX_HASH_LEN 36
7839#else
7840#define SSL_MAX_HASH_LEN 12
7841#endif
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844{
Paul Bakker23986e52011-04-24 08:57:21 +00007845 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007846 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007847 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007851 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7852 mbedtls_ssl_suite_get_mac(
7853 mbedtls_ssl_ciphersuite_from_id(
7854 mbedtls_ssl_session_get_ciphersuite(
7855 ssl->session_negotiate ) ) ),
7856 ssl, buf,
7857 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007858
Hanno Becker327c93b2018-08-15 13:56:18 +01007859 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 return( ret );
7863 }
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007868 mbedtls_ssl_pend_fatal_alert( ssl,
7869 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007871 }
7872
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007873 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007875 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007876 hash_len = 36;
7877 else
7878#endif
7879 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7882 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007885 mbedtls_ssl_pend_fatal_alert( ssl,
7886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 }
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007891 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007894 mbedtls_ssl_pend_fatal_alert( ssl,
7895 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007897 }
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007900 ssl->verify_data_len = hash_len;
7901 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007902#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007903
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007904#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007905 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007908 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007912 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007915 }
7916 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007917#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 ssl->state++;
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007923#endif
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007926
7927 return( 0 );
7928}
7929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7935 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7936 mbedtls_md5_init( &handshake->fin_md5 );
7937 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007938 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7939 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7942#if defined(MBEDTLS_SHA256_C)
7943 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007944 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SHA512_C)
7947 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007948 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951
Hanno Becker7e5437a2017-04-28 17:15:26 +01007952#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7953 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7954 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7955#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_DHM_C)
7958 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_ECDH_C)
7961 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007963#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007964 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007965#if defined(MBEDTLS_SSL_CLI_C)
7966 handshake->ecjpake_cache = NULL;
7967 handshake->ecjpake_cache_len = 0;
7968#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007969#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007970
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007971#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007972 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007973#endif
7974
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007975#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7976 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7977#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007978
7979#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7980 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7981 mbedtls_pk_init( &handshake->peer_pubkey );
7982#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007983}
7984
Hanno Becker611a83b2018-01-03 14:27:32 +00007985void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7990 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007991
Hanno Becker92231322018-01-03 15:32:51 +00007992#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_md_init( &transform->md_ctx_enc );
7994 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007995#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996}
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008001}
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008004{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008006 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008010 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008011 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012
8013 /*
8014 * Either the pointers are now NULL or cleared properly and can be freed.
8015 * Now allocate missing structures.
8016 */
8017 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008018 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008019 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008020 }
Paul Bakker48916f92012-09-16 19:57:18 +00008021
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008023 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008024 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008025 }
Paul Bakker48916f92012-09-16 19:57:18 +00008026
Paul Bakker82788fb2014-10-20 13:59:19 +02008027 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008028 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008029 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008030 }
Paul Bakker48916f92012-09-16 19:57:18 +00008031
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008032 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008033 if( ssl->handshake == NULL ||
8034 ssl->transform_negotiate == NULL ||
8035 ssl->session_negotiate == NULL )
8036 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 mbedtls_free( ssl->handshake );
8040 mbedtls_free( ssl->transform_negotiate );
8041 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008042
8043 ssl->handshake = NULL;
8044 ssl->transform_negotiate = NULL;
8045 ssl->session_negotiate = NULL;
8046
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008048 }
8049
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008050 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008052 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008053 ssl_handshake_params_init( ssl->handshake );
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008056 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008057 {
8058 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008059
Hanno Becker2d9623f2019-06-13 12:07:05 +01008060 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008061 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8062 else
8063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8064 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008065#endif
8066
Paul Bakker48916f92012-09-16 19:57:18 +00008067 return( 0 );
8068}
8069
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008070#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008071/* Dummy cookie callbacks for defaults */
8072static int ssl_cookie_write_dummy( void *ctx,
8073 unsigned char **p, unsigned char *end,
8074 const unsigned char *cli_id, size_t cli_id_len )
8075{
8076 ((void) ctx);
8077 ((void) p);
8078 ((void) end);
8079 ((void) cli_id);
8080 ((void) cli_id_len);
8081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008083}
8084
8085static int ssl_cookie_check_dummy( void *ctx,
8086 const unsigned char *cookie, size_t cookie_len,
8087 const unsigned char *cli_id, size_t cli_id_len )
8088{
8089 ((void) ctx);
8090 ((void) cookie);
8091 ((void) cookie_len);
8092 ((void) cli_id);
8093 ((void) cli_id_len);
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008096}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008097#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008098
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008099/* Once ssl->out_hdr as the address of the beginning of the
8100 * next outgoing record is set, deduce the other pointers.
8101 *
8102 * Note: For TLS, we save the implicit record sequence number
8103 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8104 * and the caller has to make sure there's space for this.
8105 */
8106
8107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8108 mbedtls_ssl_transform *transform )
8109{
8110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008111 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008112 {
8113 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008115 ssl->out_cid = ssl->out_ctr + 8;
8116 ssl->out_len = ssl->out_cid;
8117 if( transform != NULL )
8118 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008119#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008120 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008121#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008122 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008123 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008124 MBEDTLS_SSL_TRANSPORT_ELSE
8125#endif /* MBEDTLS_SSL_PROTO_DTLS */
8126#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008127 {
8128 ssl->out_ctr = ssl->out_hdr - 8;
8129 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008130#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008131 ssl->out_cid = ssl->out_len;
8132#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133 ssl->out_iv = ssl->out_hdr + 5;
8134 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008135#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008136
8137 /* Adjust out_msg to make space for explicit IV, if used. */
8138 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008139 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008140 {
8141 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8142 }
8143 else
8144 ssl->out_msg = ssl->out_iv;
8145}
8146
8147/* Once ssl->in_hdr as the address of the beginning of the
8148 * next incoming record is set, deduce the other pointers.
8149 *
8150 * Note: For TLS, we save the implicit record sequence number
8151 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8152 * and the caller has to make sure there's space for this.
8153 */
8154
Hanno Beckerf5970a02019-05-08 09:38:41 +01008155static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008156{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008157 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008158 * of unprotected TLS/DTLS records, with ssl->in_msg
8159 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008160 *
8161 * When decrypting a protected record, ssl->in_msg
8162 * will be shifted to point to the beginning of the
8163 * record plaintext.
8164 */
8165
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008167 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 {
Hanno Becker70e79282019-05-03 14:34:53 +01008169 /* This sets the header pointers to match records
8170 * without CID. When we receive a record containing
8171 * a CID, the fields are shifted accordingly in
8172 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008173 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008175 ssl->in_cid = ssl->in_ctr + 8;
8176 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008177#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008178 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008179#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008180 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008181 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008182 MBEDTLS_SSL_TRANSPORT_ELSE
8183#endif /* MBEDTLS_SSL_PROTO_DTLS */
8184#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008185 {
8186 ssl->in_ctr = ssl->in_hdr - 8;
8187 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008188#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008189 ssl->in_cid = ssl->in_len;
8190#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008191 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008192 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008193#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008194}
8195
Paul Bakker5121ce52009-01-03 21:22:43 +00008196/*
8197 * Initialize an SSL context
8198 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008199void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8200{
8201 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8202}
8203
8204/*
8205 * Setup an SSL context
8206 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008207
8208static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8209{
8210 /* Set the incoming and outgoing record pointers. */
8211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008213 {
8214 ssl->out_hdr = ssl->out_buf;
8215 ssl->in_hdr = ssl->in_buf;
8216 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008217 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008218#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008220 {
8221 ssl->out_hdr = ssl->out_buf + 8;
8222 ssl->in_hdr = ssl->in_buf + 8;
8223 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008224#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008225
8226 /* Derive other internal pointers. */
8227 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008228 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008229}
8230
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008231int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008232 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233{
Paul Bakker48916f92012-09-16 19:57:18 +00008234 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008235
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008236 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008237
Hanno Beckeref982d52019-07-23 15:56:18 +01008238#if defined(MBEDTLS_USE_TINYCRYPT)
8239 uECC_set_rng( &uecc_rng_wrapper );
8240#endif
8241
Paul Bakker62f2dee2012-09-28 07:31:51 +00008242 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008243 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008244 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008245
8246 /* Set to NULL in case of an error condition */
8247 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008248
Angus Grattond8213d02016-05-25 20:56:48 +10008249 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8250 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008251 {
Angus Grattond8213d02016-05-25 20:56:48 +10008252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008253 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008254 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008255 }
8256
8257 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8258 if( ssl->out_buf == NULL )
8259 {
8260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008261 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008262 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008263 }
8264
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008265 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008266
Paul Bakker48916f92012-09-16 19:57:18 +00008267 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008268 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008269
Hanno Beckerc8f52992019-07-25 11:15:08 +01008270 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008271
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008273
8274error:
8275 mbedtls_free( ssl->in_buf );
8276 mbedtls_free( ssl->out_buf );
8277
8278 ssl->conf = NULL;
8279
8280 ssl->in_buf = NULL;
8281 ssl->out_buf = NULL;
8282
8283 ssl->in_hdr = NULL;
8284 ssl->in_ctr = NULL;
8285 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008286 ssl->in_msg = NULL;
8287
8288 ssl->out_hdr = NULL;
8289 ssl->out_ctr = NULL;
8290 ssl->out_len = NULL;
8291 ssl->out_iv = NULL;
8292 ssl->out_msg = NULL;
8293
k-stachowiak9f7798e2018-07-31 16:52:32 +02008294 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008295}
8296
8297/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298 * Reset an initialized and used SSL context for re-use while retaining
8299 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008300 *
8301 * If partial is non-zero, keep data in the input buffer and client ID.
8302 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008303 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008304static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008305{
Paul Bakker48916f92012-09-16 19:57:18 +00008306 int ret;
8307
Hanno Becker7e772132018-08-10 12:38:21 +01008308#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8309 !defined(MBEDTLS_SSL_SRV_C)
8310 ((void) partial);
8311#endif
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008314
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008315 /* Cancel any possibly running timer */
8316 ssl_set_timer( ssl, 0 );
8317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_RENEGOTIATION)
8319 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008320 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008321
8322 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8324 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008327
Paul Bakker7eb013f2011-10-06 12:37:39 +00008328 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008329 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008330
8331 ssl->in_msgtype = 0;
8332 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008334 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008335 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008338 ssl_dtls_replay_reset( ssl );
8339#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008340
8341 ssl->in_hslen = 0;
8342 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008343
8344 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008345
8346 ssl->out_msgtype = 0;
8347 ssl->out_msglen = 0;
8348 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8350 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008351 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008352#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008353
Hanno Becker19859472018-08-06 09:40:20 +01008354 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8355
Paul Bakker48916f92012-09-16 19:57:18 +00008356 ssl->transform_in = NULL;
8357 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008358
Hanno Becker78640902018-08-13 16:35:15 +01008359 ssl->session_in = NULL;
8360 ssl->session_out = NULL;
8361
Angus Grattond8213d02016-05-25 20:56:48 +10008362 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008363
8364#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008365 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008366#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8367 {
8368 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008369 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008370 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8373 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8376 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8379 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008381 }
8382#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008383
Paul Bakker48916f92012-09-16 19:57:18 +00008384 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 mbedtls_ssl_transform_free( ssl->transform );
8387 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008388 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008389 }
Paul Bakker48916f92012-09-16 19:57:18 +00008390
Paul Bakkerc0463502013-02-14 11:19:38 +01008391 if( ssl->session )
8392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 mbedtls_ssl_session_free( ssl->session );
8394 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008395 ssl->session = NULL;
8396 }
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008399 ssl->alpn_chosen = NULL;
8400#endif
8401
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008402#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008403#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008404 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008405#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008406 {
8407 mbedtls_free( ssl->cli_id );
8408 ssl->cli_id = NULL;
8409 ssl->cli_id_len = 0;
8410 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008411#endif
8412
Paul Bakker48916f92012-09-16 19:57:18 +00008413 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8414 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008415
8416 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008417}
8418
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008419/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008420 * Reset an initialized and used SSL context for re-use while retaining
8421 * all application-set variables, function pointers and data.
8422 */
8423int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8424{
8425 return( ssl_session_reset_int( ssl, 0 ) );
8426}
8427
8428/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008429 * SSL set accessors
8430 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008431#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008435}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008436#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008437
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008438void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008441}
8442
Hanno Becker7f376f42019-06-12 16:20:48 +01008443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8444 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008446{
Hanno Becker7f376f42019-06-12 16:20:48 +01008447 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008448}
Hanno Becker7f376f42019-06-12 16:20:48 +01008449#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008450
Hanno Beckerde671542019-06-12 16:30:46 +01008451#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8452 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8453void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8454 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008455{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008456 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008457}
Hanno Beckerde671542019-06-12 16:30:46 +01008458#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008461
Hanno Becker1841b0a2018-08-24 11:13:57 +01008462void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8463 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008464{
8465 ssl->disable_datagram_packing = !allow_packing;
8466}
8467
Hanno Becker1f835fa2019-06-13 10:14:59 +01008468#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8469 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008470void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8471 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008473 conf->hs_timeout_min = min;
8474 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008475}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008476#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8477 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8478void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8479 uint32_t min, uint32_t max )
8480{
8481 ((void) conf);
8482 ((void) min);
8483 ((void) max);
8484}
8485#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8486 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8487
8488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008489
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008490void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008491{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008492#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8493 conf->authmode = authmode;
8494#else
8495 ((void) conf);
8496 ((void) authmode);
8497#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008498}
8499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008501void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008502 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008503 void *p_vrfy )
8504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008505 conf->f_vrfy = f_vrfy;
8506 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008509
Hanno Beckerece325c2019-06-13 15:39:27 +01008510#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008511void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008512 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008513 void *p_rng )
8514{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008515 conf->f_rng = f_rng;
8516 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
Hanno Beckerece325c2019-06-13 15:39:27 +01008518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008519
Hanno Becker14a4a442019-07-02 17:00:34 +01008520#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008522 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008523 void *p_dbg )
8524{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008525 conf->f_dbg = f_dbg;
8526 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008527}
Hanno Becker14a4a442019-07-02 17:00:34 +01008528#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008529
Hanno Beckera58a8962019-06-13 16:11:15 +01008530#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8531 !defined(MBEDTLS_SSL_CONF_SEND) && \
8532 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008534 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008535 mbedtls_ssl_send_t *f_send,
8536 mbedtls_ssl_recv_t *f_recv,
8537 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008538{
Hanno Beckera58a8962019-06-13 16:11:15 +01008539 ssl->p_bio = p_bio;
8540 ssl->f_send = f_send;
8541 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008542 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008543}
Hanno Beckera58a8962019-06-13 16:11:15 +01008544#else
8545void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8546 void *p_bio )
8547{
8548 ssl->p_bio = p_bio;
8549}
8550#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008551
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008552#if defined(MBEDTLS_SSL_PROTO_DTLS)
8553void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8554{
8555 ssl->mtu = mtu;
8556}
8557#endif
8558
Hanno Becker1f835fa2019-06-13 10:14:59 +01008559#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008560void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008561{
8562 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008563}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008564#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008565
Hanno Becker0ae6b242019-06-13 16:45:36 +01008566#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8567 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008568void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8569 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008570 mbedtls_ssl_set_timer_t *f_set_timer,
8571 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008572{
8573 ssl->p_timer = p_timer;
8574 ssl->f_set_timer = f_set_timer;
8575 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008576 /* Make sure we start with no timer running */
8577 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008578}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008579#else
8580void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8581 void *p_timer )
8582{
8583 ssl->p_timer = p_timer;
8584 /* Make sure we start with no timer running */
8585 ssl_set_timer( ssl, 0 );
8586}
8587#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008588
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008589#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008591 void *p_cache,
8592 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8593 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008594{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008595 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008597 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008598}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008599#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008600
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008601#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008603{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008604 int ret;
8605
8606 if( ssl == NULL ||
8607 session == NULL ||
8608 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008609 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008612 }
8613
Hanno Becker58fccf22019-02-06 14:30:46 +00008614 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8615 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008616 return( ret );
8617
Paul Bakker0a597072012-09-25 21:55:46 +00008618 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008619
8620 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008621}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008622#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008623
Hanno Becker73f4cb12019-06-27 13:51:07 +01008624#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008625void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8629 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8630 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8631 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008632}
8633
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008634void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008635 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008636 int major, int minor )
8637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008639 return;
8640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008642 return;
8643
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008644 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008645}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008646#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008649void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008650 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008651{
8652 conf->cert_profile = profile;
8653}
8654
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008655/* Append a new keycert entry to a (possibly empty) list */
8656static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8657 mbedtls_x509_crt *cert,
8658 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008659{
niisato8ee24222018-06-25 19:05:48 +09008660 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008661
niisato8ee24222018-06-25 19:05:48 +09008662 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8663 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008664 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008665
niisato8ee24222018-06-25 19:05:48 +09008666 new_cert->cert = cert;
8667 new_cert->key = key;
8668 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008669
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008670 /* Update head is the list was null, else add to the end */
8671 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008672 {
niisato8ee24222018-06-25 19:05:48 +09008673 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008674 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008675 else
8676 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008677 mbedtls_ssl_key_cert *cur = *head;
8678 while( cur->next != NULL )
8679 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008680 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008681 }
8682
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008683 return( 0 );
8684}
8685
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008686int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008687 mbedtls_x509_crt *own_cert,
8688 mbedtls_pk_context *pk_key )
8689{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008690 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691}
8692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008693void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008694 mbedtls_x509_crt *ca_chain,
8695 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008696{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008697 conf->ca_chain = ca_chain;
8698 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008701
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008702#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8703int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8704 mbedtls_x509_crt *own_cert,
8705 mbedtls_pk_context *pk_key )
8706{
8707 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8708 own_cert, pk_key ) );
8709}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008710
8711void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8712 mbedtls_x509_crt *ca_chain,
8713 mbedtls_x509_crl *ca_crl )
8714{
8715 ssl->handshake->sni_ca_chain = ca_chain;
8716 ssl->handshake->sni_ca_crl = ca_crl;
8717}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008718
8719void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8720 int authmode )
8721{
8722 ssl->handshake->sni_authmode = authmode;
8723}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008724#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8725
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008726#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008727/*
8728 * Set EC J-PAKE password for current handshake
8729 */
8730int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8731 const unsigned char *pw,
8732 size_t pw_len )
8733{
8734 mbedtls_ecjpake_role role;
8735
Janos Follath8eb64132016-06-03 15:40:57 +01008736 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8738
Hanno Becker2d9623f2019-06-13 12:07:05 +01008739 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008740 role = MBEDTLS_ECJPAKE_SERVER;
8741 else
8742 role = MBEDTLS_ECJPAKE_CLIENT;
8743
8744 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8745 role,
8746 MBEDTLS_MD_SHA256,
8747 MBEDTLS_ECP_DP_SECP256R1,
8748 pw, pw_len ) );
8749}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008750#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008753int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008754 const unsigned char *psk, size_t psk_len,
8755 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008756{
Paul Bakker6db455e2013-09-18 17:29:31 +02008757 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008762
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008763 /* Identity len will be encoded on two bytes */
8764 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008765 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008766 {
8767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8768 }
8769
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008770 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008771 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008772 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008773
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008774 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008775 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008776 conf->psk_len = 0;
8777 }
8778 if( conf->psk_identity != NULL )
8779 {
8780 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008781 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008782 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008783 }
8784
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008785 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8786 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008787 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008788 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008789 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008790 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008791 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008793 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008794
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008795 conf->psk_len = psk_len;
8796 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008797
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008798 memcpy( conf->psk, psk, conf->psk_len );
8799 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008800
8801 return( 0 );
8802}
8803
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008804int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8805 const unsigned char *psk, size_t psk_len )
8806{
8807 if( psk == NULL || ssl->handshake == NULL )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809
8810 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8812
8813 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008814 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008815 mbedtls_platform_zeroize( ssl->handshake->psk,
8816 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008817 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008818 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008819 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008820
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008821 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008823
8824 ssl->handshake->psk_len = psk_len;
8825 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8826
8827 return( 0 );
8828}
8829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008830void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008832 size_t),
8833 void *p_psk )
8834{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008835 conf->f_psk = f_psk;
8836 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008839
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008840#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008841
8842#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844{
8845 int ret;
8846
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008847 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8848 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8849 {
8850 mbedtls_mpi_free( &conf->dhm_P );
8851 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
8855 return( 0 );
8856}
Hanno Becker470a8c42017-10-04 15:28:46 +01008857#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Hanno Beckera90658f2017-10-04 15:29:08 +01008859int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8860 const unsigned char *dhm_P, size_t P_len,
8861 const unsigned char *dhm_G, size_t G_len )
8862{
8863 int ret;
8864
8865 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8866 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8867 {
8868 mbedtls_mpi_free( &conf->dhm_P );
8869 mbedtls_mpi_free( &conf->dhm_G );
8870 return( ret );
8871 }
8872
8873 return( 0 );
8874}
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008876int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008877{
8878 int ret;
8879
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8881 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8882 {
8883 mbedtls_mpi_free( &conf->dhm_P );
8884 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008885 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008887
8888 return( 0 );
8889}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008891
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8893/*
8894 * Set the minimum length for Diffie-Hellman parameters
8895 */
8896void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8897 unsigned int bitlen )
8898{
8899 conf->dhm_min_bitlen = bitlen;
8900}
8901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8902
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008903#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008904/*
8905 * Set allowed/preferred hashes for handshake signatures
8906 */
8907void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8908 const int *hashes )
8909{
Hanno Becker56595f42019-06-19 16:31:38 +01008910#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008911 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008912#else
8913 ((void) conf);
8914 ((void) hashes);
8915#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008916}
Hanno Becker947194e2017-04-07 13:25:49 +01008917#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008918
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008919#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008920#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008921/*
8922 * Set the allowed elliptic curves
8923 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008924void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008925 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008926{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008927 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008928}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008929#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008930#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008931
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008932#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008934{
Hanno Becker947194e2017-04-07 13:25:49 +01008935 /* Initialize to suppress unnecessary compiler warning */
8936 size_t hostname_len = 0;
8937
8938 /* Check if new hostname is valid before
8939 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008940 if( hostname != NULL )
8941 {
8942 hostname_len = strlen( hostname );
8943
8944 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8946 }
8947
8948 /* Now it's clear that we will overwrite the old hostname,
8949 * so we can free it safely */
8950
8951 if( ssl->hostname != NULL )
8952 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008953 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008954 mbedtls_free( ssl->hostname );
8955 }
8956
8957 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008958
Paul Bakker5121ce52009-01-03 21:22:43 +00008959 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008960 {
8961 ssl->hostname = NULL;
8962 }
8963 else
8964 {
8965 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008966 if( ssl->hostname == NULL )
8967 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008968
Hanno Becker947194e2017-04-07 13:25:49 +01008969 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008970
Hanno Becker947194e2017-04-07 13:25:49 +01008971 ssl->hostname[hostname_len] = '\0';
8972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008973
8974 return( 0 );
8975}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008976#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008977
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008978#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008979void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008981 const unsigned char *, size_t),
8982 void *p_sni )
8983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 conf->f_sni = f_sni;
8985 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008990int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008991{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008992 size_t cur_len, tot_len;
8993 const char **p;
8994
8995 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008996 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8997 * MUST NOT be truncated."
8998 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008999 */
9000 tot_len = 0;
9001 for( p = protos; *p != NULL; p++ )
9002 {
9003 cur_len = strlen( *p );
9004 tot_len += cur_len;
9005
9006 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009008 }
9009
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009010 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009011
9012 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013}
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009016{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009017 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009020
Hanno Becker33b9b252019-07-05 11:23:25 +01009021#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9022 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9023void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9024 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009025{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009026 conf->max_major_ver = major;
9027 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009028}
Hanno Becker33b9b252019-07-05 11:23:25 +01009029#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9030 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009031
Hanno Becker33b9b252019-07-05 11:23:25 +01009032#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9033 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9034void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9035 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009037 conf->min_major_ver = major;
9038 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009039}
Hanno Becker33b9b252019-07-05 11:23:25 +01009040#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9041 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009044void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009045{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009046 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009047}
9048#endif
9049
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009050#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009051void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9052 char cert_req_ca_list )
9053{
9054 conf->cert_req_ca_list = cert_req_ca_list;
9055}
9056#endif
9057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009059void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009060{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009061 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009062}
9063#endif
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009066#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009067void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009069 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009070}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009071#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9072#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009073void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009074 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009075{
9076 conf->enforce_extended_master_secret = ems_enf;
9077}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009078#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009079#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009080
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009082void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009083{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009084 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009085}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009086#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009089int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009092 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009095 }
9096
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009097 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098
9099 return( 0 );
9100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009104void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009106 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009111void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009112{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009113 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009114}
9115#endif
9116
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009117#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009120 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009121}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009122#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009128}
9129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009132 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009133}
9134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009136 const unsigned char period[8] )
9137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#if defined(MBEDTLS_SSL_CLI_C)
9144void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009145{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009146 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009147}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009148#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009149
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009150#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009151void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9152 mbedtls_ssl_ticket_write_t *f_ticket_write,
9153 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9154 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009156 conf->f_ticket_write = f_ticket_write;
9157 conf->f_ticket_parse = f_ticket_parse;
9158 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009159}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009162
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009163#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9164void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9165 mbedtls_ssl_export_keys_t *f_export_keys,
9166 void *p_export_keys )
9167{
9168 conf->f_export_keys = f_export_keys;
9169 conf->p_export_keys = p_export_keys;
9170}
9171#endif
9172
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009173#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009174void mbedtls_ssl_conf_async_private_cb(
9175 mbedtls_ssl_config *conf,
9176 mbedtls_ssl_async_sign_t *f_async_sign,
9177 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9178 mbedtls_ssl_async_resume_t *f_async_resume,
9179 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009180 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009181{
9182 conf->f_async_sign_start = f_async_sign;
9183 conf->f_async_decrypt_start = f_async_decrypt;
9184 conf->f_async_resume = f_async_resume;
9185 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009186 conf->p_async_config_data = async_config_data;
9187}
9188
Gilles Peskine8f97af72018-04-26 11:46:10 +02009189void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9190{
9191 return( conf->p_async_config_data );
9192}
9193
Gilles Peskine1febfef2018-04-30 11:54:39 +02009194void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009195{
9196 if( ssl->handshake == NULL )
9197 return( NULL );
9198 else
9199 return( ssl->handshake->user_async_ctx );
9200}
9201
Gilles Peskine1febfef2018-04-30 11:54:39 +02009202void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009203 void *ctx )
9204{
9205 if( ssl->handshake != NULL )
9206 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009207}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009208#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009209
Paul Bakker5121ce52009-01-03 21:22:43 +00009210/*
9211 * SSL get accessors
9212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009214{
9215 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9216}
9217
Hanno Becker8b170a02017-10-10 11:51:19 +01009218int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9219{
9220 /*
9221 * Case A: We're currently holding back
9222 * a message for further processing.
9223 */
9224
9225 if( ssl->keep_current_message == 1 )
9226 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009228 return( 1 );
9229 }
9230
9231 /*
9232 * Case B: Further records are pending in the current datagram.
9233 */
9234
9235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009236 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 ssl->in_left > ssl->next_record_offset )
9238 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009240 return( 1 );
9241 }
9242#endif /* MBEDTLS_SSL_PROTO_DTLS */
9243
9244 /*
9245 * Case C: A handshake message is being processed.
9246 */
9247
Hanno Becker8b170a02017-10-10 11:51:19 +01009248 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9249 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009251 return( 1 );
9252 }
9253
9254 /*
9255 * Case D: An application data message is being processed
9256 */
9257 if( ssl->in_offt != NULL )
9258 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 return( 1 );
9261 }
9262
9263 /*
9264 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009265 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009266 * we implement support for multiple alerts in single records.
9267 */
9268
9269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9270 return( 0 );
9271}
9272
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009273uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009274{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009275 if( ssl->session != NULL )
9276 return( ssl->session->verify_result );
9277
9278 if( ssl->session_negotiate != NULL )
9279 return( ssl->session_negotiate->verify_result );
9280
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009281 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009282}
9283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009285{
Hanno Beckere02758c2019-06-26 15:31:31 +01009286 int suite;
9287
Paul Bakker926c8e42013-03-06 10:23:34 +01009288 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009289 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009290
Hanno Beckere02758c2019-06-26 15:31:31 +01009291 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9292 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009293}
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009298 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009299 {
Hanno Becker2881d802019-05-22 14:44:53 +01009300 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009303 return( "DTLSv1.0" );
9304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009306 return( "DTLSv1.2" );
9307
9308 default:
9309 return( "unknown (DTLS)" );
9310 }
9311 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009312 MBEDTLS_SSL_TRANSPORT_ELSE
9313#endif /* MBEDTLS_SSL_PROTO_DTLS */
9314#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315 {
Hanno Becker2881d802019-05-22 14:44:53 +01009316 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009317 {
9318 case MBEDTLS_SSL_MINOR_VERSION_0:
9319 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009321 case MBEDTLS_SSL_MINOR_VERSION_1:
9322 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009324 case MBEDTLS_SSL_MINOR_VERSION_2:
9325 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009327 case MBEDTLS_SSL_MINOR_VERSION_3:
9328 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009329
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009330 default:
9331 return( "unknown" );
9332 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009333 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009334#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009338{
Hanno Becker3136ede2018-08-17 15:28:19 +01009339 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009341
Hanno Becker43395762019-05-03 14:46:38 +01009342 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9343
Hanno Becker78640902018-08-13 16:35:15 +01009344 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009345 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347#if defined(MBEDTLS_ZLIB_SUPPORT)
9348 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009350#endif
9351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009353 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009354#if defined(MBEDTLS_GCM_C) || \
9355 defined(MBEDTLS_CCM_C) || \
9356 defined(MBEDTLS_CHACHAPOLY_C)
9357#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009359#endif
9360#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009362#endif
9363#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009364 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009365#endif
9366 transform_expansion =
9367 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368 break;
9369
Hanno Beckera9d5c452019-07-25 16:47:12 +01009370#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9371 MBEDTLS_CHACHAPOLY_C */
9372
9373#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9374 case MBEDTLS_MODE_STREAM:
9375 transform_expansion = transform->maclen;
9376 break;
9377#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9378
9379#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009381 {
9382 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009383
9384 block_size = mbedtls_cipher_get_block_size(
9385 &transform->cipher_ctx_enc );
9386
Hanno Becker3136ede2018-08-17 15:28:19 +01009387 /* Expansion due to the addition of the MAC. */
9388 transform_expansion += transform->maclen;
9389
9390 /* Expansion due to the addition of CBC padding;
9391 * Theoretically up to 256 bytes, but we never use
9392 * more than the block size of the underlying cipher. */
9393 transform_expansion += block_size;
9394
9395 /* For TLS 1.1 or higher, an explicit IV is added
9396 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009397#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009398 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009399 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009400#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009401
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009402 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009403 }
9404#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009405
9406 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009409 }
9410
Hanno Beckera5a2b082019-05-15 14:03:01 +01009411#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009412 if( transform->out_cid_len != 0 )
9413 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009415
Hanno Becker43395762019-05-03 14:46:38 +01009416 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009417}
9418
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009419#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9420size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9421{
9422 size_t max_len;
9423
9424 /*
9425 * Assume mfl_code is correct since it was checked when set
9426 */
Angus Grattond8213d02016-05-25 20:56:48 +10009427 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009428
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009429 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009430 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009431 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009432 {
Angus Grattond8213d02016-05-25 20:56:48 +10009433 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009434 }
9435
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009436 /* During a handshake, use the value being negotiated */
9437 if( ssl->session_negotiate != NULL &&
9438 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9439 {
9440 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9441 }
9442
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009443 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009444}
9445#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9446
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009447#if defined(MBEDTLS_SSL_PROTO_DTLS)
9448static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9449{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009450 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009451 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009452 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9453 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009454 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009455
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009456 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9457 return( ssl->mtu );
9458
9459 if( ssl->mtu == 0 )
9460 return( ssl->handshake->mtu );
9461
9462 return( ssl->mtu < ssl->handshake->mtu ?
9463 ssl->mtu : ssl->handshake->mtu );
9464}
9465#endif /* MBEDTLS_SSL_PROTO_DTLS */
9466
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9468{
9469 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9470
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009471#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9472 !defined(MBEDTLS_SSL_PROTO_DTLS)
9473 (void) ssl;
9474#endif
9475
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009476#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9477 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9478
9479 if( max_len > mfl )
9480 max_len = mfl;
9481#endif
9482
9483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009484 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009485 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009486 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009487 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9488 const size_t overhead = (size_t) ret;
9489
9490 if( ret < 0 )
9491 return( ret );
9492
9493 if( mtu <= overhead )
9494 {
9495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9496 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9497 }
9498
9499 if( max_len > mtu - overhead )
9500 max_len = mtu - overhead;
9501 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009503
Hanno Becker0defedb2018-08-10 12:35:02 +01009504#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9505 !defined(MBEDTLS_SSL_PROTO_DTLS)
9506 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009507#endif
9508
9509 return( (int) max_len );
9510}
9511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_X509_CRT_PARSE_C)
9513const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009514{
9515 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009516 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009517
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009518#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009519 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009520#else
9521 return( NULL );
9522#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009527int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9528 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009529{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009530 if( ssl == NULL ||
9531 dst == NULL ||
9532 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009533 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536 }
9537
Hanno Becker58fccf22019-02-06 14:30:46 +00009538 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009541
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009542const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9543{
9544 if( ssl == NULL )
9545 return( NULL );
9546
9547 return( ssl->session );
9548}
9549
Paul Bakker5121ce52009-01-03 21:22:43 +00009550/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009551 * Define ticket header determining Mbed TLS version
9552 * and structure of the ticket.
9553 */
9554
Hanno Becker41527622019-05-16 12:50:45 +01009555/*
Hanno Becker26829e92019-05-28 14:30:45 +01009556 * Define bitflag determining compile-time settings influencing
9557 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009558 */
9559
Hanno Becker26829e92019-05-28 14:30:45 +01009560#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009561#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009562#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009563#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009564#endif /* MBEDTLS_HAVE_TIME */
9565
9566#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009567#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009568#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009569#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009570#endif /* MBEDTLS_X509_CRT_PARSE_C */
9571
9572#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009574#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009575#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009576#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9577
9578#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009580#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009581#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009582#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9583
9584#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009585#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009586#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009587#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009588#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9589
9590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009591#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009592#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009593#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009594#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9595
Hanno Becker41527622019-05-16 12:50:45 +01009596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9597#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9598#else
9599#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9600#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9601
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009602#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9603#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9604#else
9605#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9606#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9607
Hanno Becker88440552019-07-03 14:16:13 +01009608#if defined(MBEDTLS_ZLIB_SUPPORT)
9609#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9610#else
9611#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9613
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009614#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9615#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9616#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9617#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9618#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9619#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9620#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009621#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009622#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009623
Hanno Becker26829e92019-05-28 14:30:45 +01009624#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009625 ( (uint16_t) ( \
9626 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9627 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9628 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9629 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9630 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9631 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009632 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009633 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009634 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009635
Hanno Becker557fe9f2019-05-16 12:41:07 +01009636static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009637 MBEDTLS_VERSION_MAJOR,
9638 MBEDTLS_VERSION_MINOR,
9639 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009640 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9641 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009642};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009643
9644/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009645 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009646 * (in the presentation language of TLS, RFC 8446 section 3)
9647 *
Hanno Becker26829e92019-05-28 14:30:45 +01009648 * opaque mbedtls_version[3]; // major, minor, patch
9649 * opaque session_format[2]; // version-specific 16-bit field determining
9650 * // the format of the remaining
9651 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009652 *
9653 * Note: When updating the format, remember to keep
9654 * these version+format bytes.
9655 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009656 * // In this version, `session_format` determines
9657 * // the setting of those compile-time
9658 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009659 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009660 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009661 * uint8 ciphersuite[2]; // defined by the standard
9662 * uint8 compression; // 0 or 1
9663 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009664 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009665 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009666 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009667 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9668 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9669 * case disabled: uint8_t peer_cert_digest_type;
9670 * opaque peer_cert_digest<0..2^8-1>;
9671 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009672 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009673 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009674 * uint8 mfl_code; // up to 255 according to standard
9675 * uint8 trunc_hmac; // 0 or 1
9676 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009677 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009678 * The order is the same as in the definition of the structure, except
9679 * verify_result is put before peer_cert so that all mandatory fields come
9680 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009681 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009682static int ssl_session_save( const mbedtls_ssl_session *session,
9683 unsigned char omit_header,
9684 unsigned char *buf,
9685 size_t buf_len,
9686 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009687{
9688 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009689 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009690#if defined(MBEDTLS_HAVE_TIME)
9691 uint64_t start;
9692#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009693#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009694#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009695 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009696#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009697#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009698
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009699 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009700 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009701 /*
9702 * Add version identifier
9703 */
9704
9705 used += sizeof( ssl_serialized_session_header );
9706
9707 if( used <= buf_len )
9708 {
9709 memcpy( p, ssl_serialized_session_header,
9710 sizeof( ssl_serialized_session_header ) );
9711 p += sizeof( ssl_serialized_session_header );
9712 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009713 }
9714
9715 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009716 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009717 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009718#if defined(MBEDTLS_HAVE_TIME)
9719 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009720
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009721 if( used <= buf_len )
9722 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009723 start = (uint64_t) session->start;
9724
9725 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9727 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9728 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9729 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9730 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9731 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9732 *p++ = (unsigned char)( ( start ) & 0xFF );
9733 }
9734#endif /* MBEDTLS_HAVE_TIME */
9735
9736 /*
9737 * Basic mandatory fields
9738 */
Hanno Becker88440552019-07-03 14:16:13 +01009739 {
9740 size_t const ciphersuite_len = 2;
9741#if defined(MBEDTLS_ZLIB_SUPPORT)
9742 size_t const compression_len = 1;
9743#else
9744 size_t const compression_len = 0;
9745#endif
9746 size_t const id_len_len = 1;
9747 size_t const id_len = 32;
9748 size_t const master_len = 48;
9749 size_t const verif_result_len = 4;
9750
9751 size_t const basic_len =
9752 ciphersuite_len +
9753 compression_len +
9754 id_len_len +
9755 id_len +
9756 master_len +
9757 verif_result_len;
9758
9759 used += basic_len;
9760 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009761
9762 if( used <= buf_len )
9763 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009764 const int ciphersuite =
9765 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009766 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009767
Hanno Becker88440552019-07-03 14:16:13 +01009768#if defined(MBEDTLS_ZLIB_SUPPORT)
9769 *p++ = (unsigned char)(
9770 mbedtls_ssl_session_get_compression( session ) );
9771#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009772
9773 *p++ = (unsigned char)( session->id_len & 0xFF );
9774 memcpy( p, session->id, 32 );
9775 p += 32;
9776
9777 memcpy( p, session->master, 48 );
9778 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009779 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009780 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009781
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009782 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009783 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009784 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009785#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009786#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009787 if( session->peer_cert == NULL )
9788 cert_len = 0;
9789 else
9790 cert_len = session->peer_cert->raw.len;
9791
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009792 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009793
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009794 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009795 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009796 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009797
9798 if( session->peer_cert != NULL )
9799 {
9800 memcpy( p, session->peer_cert->raw.p, cert_len );
9801 p += cert_len;
9802 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009803 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009804
Hanno Becker5882dd02019-06-06 16:25:57 +01009805#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009806 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009807 if( session->peer_cert_digest != NULL )
9808 {
9809 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9810 if( used <= buf_len )
9811 {
9812 *p++ = (unsigned char) session->peer_cert_digest_type;
9813 *p++ = (unsigned char) session->peer_cert_digest_len;
9814 memcpy( p, session->peer_cert_digest,
9815 session->peer_cert_digest_len );
9816 p += session->peer_cert_digest_len;
9817 }
9818 }
9819 else
9820 {
9821 used += 2;
9822 if( used <= buf_len )
9823 {
9824 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9825 *p++ = 0;
9826 }
9827 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009828#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009829#endif /* MBEDTLS_X509_CRT_PARSE_C */
9830
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009831 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009832 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009833 */
9834#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009835 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009836
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009837 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009838 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009839 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009840
9841 if( session->ticket != NULL )
9842 {
9843 memcpy( p, session->ticket, session->ticket_len );
9844 p += session->ticket_len;
9845 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009846
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009847 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009848 }
9849#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9850
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009851 /*
9852 * Misc extension-related info
9853 */
9854#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9855 used += 1;
9856
9857 if( used <= buf_len )
9858 *p++ = session->mfl_code;
9859#endif
9860
9861#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9862 used += 1;
9863
9864 if( used <= buf_len )
9865 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9866#endif
9867
9868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9869 used += 1;
9870
9871 if( used <= buf_len )
9872 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9873#endif
9874
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009875 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009876 *olen = used;
9877
9878 if( used > buf_len )
9879 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009880
9881 return( 0 );
9882}
9883
9884/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009885 * Public wrapper for ssl_session_save()
9886 */
9887int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9888 unsigned char *buf,
9889 size_t buf_len,
9890 size_t *olen )
9891{
9892 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9893}
9894
9895/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009896 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009897 *
9898 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009899 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009900 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009901static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009902 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009903 const unsigned char *buf,
9904 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009905{
9906 const unsigned char *p = buf;
9907 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009908 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009909#if defined(MBEDTLS_HAVE_TIME)
9910 uint64_t start;
9911#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009912#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009913#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009914 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009915#endif
9916#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009917
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009918 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009919 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009920 /*
9921 * Check version identifier
9922 */
9923
9924 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9926
9927 if( memcmp( p, ssl_serialized_session_header,
9928 sizeof( ssl_serialized_session_header ) ) != 0 )
9929 {
9930 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9931 }
9932 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009933 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009934
9935 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009936 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009937 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009938#if defined(MBEDTLS_HAVE_TIME)
9939 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009940 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9941
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009942 start = ( (uint64_t) p[0] << 56 ) |
9943 ( (uint64_t) p[1] << 48 ) |
9944 ( (uint64_t) p[2] << 40 ) |
9945 ( (uint64_t) p[3] << 32 ) |
9946 ( (uint64_t) p[4] << 24 ) |
9947 ( (uint64_t) p[5] << 16 ) |
9948 ( (uint64_t) p[6] << 8 ) |
9949 ( (uint64_t) p[7] );
9950 p += 8;
9951
9952 session->start = (time_t) start;
9953#endif /* MBEDTLS_HAVE_TIME */
9954
9955 /*
9956 * Basic mandatory fields
9957 */
Hanno Becker88440552019-07-03 14:16:13 +01009958 {
9959 size_t const ciphersuite_len = 2;
9960#if defined(MBEDTLS_ZLIB_SUPPORT)
9961 size_t const compression_len = 1;
9962#else
9963 size_t const compression_len = 0;
9964#endif
9965 size_t const id_len_len = 1;
9966 size_t const id_len = 32;
9967 size_t const master_len = 48;
9968 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009969
Hanno Becker88440552019-07-03 14:16:13 +01009970 size_t const basic_len =
9971 ciphersuite_len +
9972 compression_len +
9973 id_len_len +
9974 id_len +
9975 master_len +
9976 verif_result_len;
9977
9978 if( basic_len > (size_t)( end - p ) )
9979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9980 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009981
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009982 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009983 p += 2;
9984
Hanno Becker73f4cb12019-06-27 13:51:07 +01009985#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009986 session->ciphersuite = ciphersuite;
9987#else
9988 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009989 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009990 {
9991 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9992 }
9993#endif
9994
Hanno Becker88440552019-07-03 14:16:13 +01009995#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009996 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +01009997#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009998
9999 session->id_len = *p++;
10000 memcpy( session->id, p, 32 );
10001 p += 32;
10002
10003 memcpy( session->master, p, 48 );
10004 p += 48;
10005
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010006 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010007 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010008
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010009 /* Immediately clear invalid pointer values that have been read, in case
10010 * we exit early before we replaced them with valid ones. */
10011#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010012#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010013 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010014#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010015 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010016#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010017#endif
10018#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10019 session->ticket = NULL;
10020#endif
10021
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010022 /*
10023 * Peer certificate
10024 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010025#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010026#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010027 if( 3 > (size_t)( end - p ) )
10028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10029
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010030 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10031
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010032 p += 3;
10033
10034 if( cert_len == 0 )
10035 {
10036 session->peer_cert = NULL;
10037 }
10038 else
10039 {
10040 int ret;
10041
10042 if( cert_len > (size_t)( end - p ) )
10043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10044
10045 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10046
10047 if( session->peer_cert == NULL )
10048 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10049
10050 mbedtls_x509_crt_init( session->peer_cert );
10051
10052 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10053 p, cert_len ) ) != 0 )
10054 {
10055 mbedtls_x509_crt_free( session->peer_cert );
10056 mbedtls_free( session->peer_cert );
10057 session->peer_cert = NULL;
10058 return( ret );
10059 }
10060
10061 p += cert_len;
10062 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010063#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010064 /* Deserialize CRT digest from the end of the ticket. */
10065 if( 2 > (size_t)( end - p ) )
10066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10067
10068 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10069 session->peer_cert_digest_len = (size_t) *p++;
10070
10071 if( session->peer_cert_digest_len != 0 )
10072 {
Hanno Becker2326d202019-06-06 14:54:55 +010010073 const mbedtls_md_info_t *md_info =
10074 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10075 if( md_info == NULL )
10076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10077 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10079
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010080 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10082
10083 session->peer_cert_digest =
10084 mbedtls_calloc( 1, session->peer_cert_digest_len );
10085 if( session->peer_cert_digest == NULL )
10086 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10087
10088 memcpy( session->peer_cert_digest, p,
10089 session->peer_cert_digest_len );
10090 p += session->peer_cert_digest_len;
10091 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010092#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010093#endif /* MBEDTLS_X509_CRT_PARSE_C */
10094
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010095 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010096 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010097 */
10098#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10099 if( 3 > (size_t)( end - p ) )
10100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10101
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010102 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010103 p += 3;
10104
10105 if( session->ticket_len != 0 )
10106 {
10107 if( session->ticket_len > (size_t)( end - p ) )
10108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10109
10110 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10111 if( session->ticket == NULL )
10112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10113
10114 memcpy( session->ticket, p, session->ticket_len );
10115 p += session->ticket_len;
10116 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010117
10118 if( 4 > (size_t)( end - p ) )
10119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10120
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010121 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010122 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010123#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10124
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010125 /*
10126 * Misc extension-related info
10127 */
10128#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10129 if( 1 > (size_t)( end - p ) )
10130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10131
10132 session->mfl_code = *p++;
10133#endif
10134
10135#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10136 if( 1 > (size_t)( end - p ) )
10137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10138
10139 session->trunc_hmac = *p++;
10140#endif
10141
10142#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10143 if( 1 > (size_t)( end - p ) )
10144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10145
10146 session->encrypt_then_mac = *p++;
10147#endif
10148
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010149 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010150 if( p != end )
10151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10152
10153 return( 0 );
10154}
10155
10156/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010157 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010158 */
10159int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10160 const unsigned char *buf,
10161 size_t len )
10162{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010163 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010164
10165 if( ret != 0 )
10166 mbedtls_ssl_session_free( session );
10167
10168 return( ret );
10169}
10170
10171/*
Paul Bakker1961b702013-01-25 14:49:24 +010010172 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010177
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010178 if( ssl == NULL || ssl->conf == NULL )
10179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010182 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010186 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010188#endif
10189
Hanno Beckerb82350b2019-07-26 07:24:05 +010010190 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010191 return( ret );
10192}
10193
10194/*
10195 * Perform the SSL handshake
10196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010198{
10199 int ret = 0;
10200
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010201 if( ssl == NULL || ssl->conf == NULL )
10202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010209
10210 if( ret != 0 )
10211 break;
10212 }
10213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010215
10216 return( ret );
10217}
10218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#if defined(MBEDTLS_SSL_RENEGOTIATION)
10220#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010221/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010222 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010225{
10226 int ret;
10227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010229
10230 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10232 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010233
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010234 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010235 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010237 return( ret );
10238 }
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010241
10242 return( 0 );
10243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010245
10246/*
10247 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 * - any side: calling mbedtls_ssl_renegotiate(),
10249 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10250 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010251 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010252 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010256{
10257 int ret;
10258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010260
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010261 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10262 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010263
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010264 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10265 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010267 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010269 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010270 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10271 MBEDTLS_SSL_IS_SERVER )
10272 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010273 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010274 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010275 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010276 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010277 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010278 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010279 }
10280#endif
10281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10283 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010288 return( ret );
10289 }
10290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010292
10293 return( 0 );
10294}
10295
10296/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010297 * Renegotiate current connection on client,
10298 * or request renegotiation on server
10299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010303
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010304 if( ssl == NULL || ssl->conf == NULL )
10305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010307#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010308 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010309 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010315
10316 /* Did we already try/start sending HelloRequest? */
10317 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010319
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010320 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010325 /*
10326 * On client, either start the renegotiation process or,
10327 * if already in progress, continue the handshake
10328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010333
10334 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010337 return( ret );
10338 }
10339 }
10340 else
10341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010345 return( ret );
10346 }
10347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010349
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010350 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010351}
10352
10353/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010354 * Check record counters and renegotiate if they're above the limit.
10355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010357{
Andres AG2196c7f2016-12-15 17:01:16 +000010358 size_t ep_len = ssl_ep_len( ssl );
10359 int in_ctr_cmp;
10360 int out_ctr_cmp;
10361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10363 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010364 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010365 {
10366 return( 0 );
10367 }
10368
Andres AG2196c7f2016-12-15 17:01:16 +000010369 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10370 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010371 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010372 ssl->conf->renego_period + ep_len, 8 - ep_len );
10373
10374 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010375 {
10376 return( 0 );
10377 }
10378
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010380 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010381}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010383
10384/*
10385 * Receive application data decrypted from the SSL layer
10386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010388{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010389 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010390 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010391
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010392 if( ssl == NULL || ssl->conf == NULL )
10393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010398 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010401 return( ret );
10402
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010403 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010405 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010406 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010407 return( ret );
10408 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010409 }
10410#endif
10411
Hanno Becker4a810fb2017-05-24 16:27:30 +010010412 /*
10413 * Check if renegotiation is necessary and/or handshake is
10414 * in process. If yes, perform/continue, and fall through
10415 * if an unexpected packet is received while the client
10416 * is waiting for the ServerHello.
10417 *
10418 * (There is no equivalent to the last condition on
10419 * the server-side as it is not treated as within
10420 * a handshake while waiting for the ClientHello
10421 * after a renegotiation request.)
10422 */
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010425 ret = ssl_check_ctr_renegotiate( ssl );
10426 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10427 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010430 return( ret );
10431 }
10432#endif
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010437 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10438 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010441 return( ret );
10442 }
10443 }
10444
Hanno Beckere41158b2017-10-23 13:30:32 +010010445 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010446 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010447 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010448 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010449 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10450 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010451 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010452 ssl_set_timer( ssl,
10453 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010454 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010455
Hanno Becker327c93b2018-08-15 13:56:18 +010010456 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010457 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010458 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10459 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010460
Hanno Becker4a810fb2017-05-24 16:27:30 +010010461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10462 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010463 }
10464
10465 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010467 {
10468 /*
10469 * OpenSSL sends empty messages to randomize the IV
10470 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010471 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010474 return( 0 );
10475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010477 return( ret );
10478 }
10479 }
10480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010484
Hanno Becker4a810fb2017-05-24 16:27:30 +010010485 /*
10486 * - For client-side, expect SERVER_HELLO_REQUEST.
10487 * - For server-side, expect CLIENT_HELLO.
10488 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10489 */
10490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010492 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10493 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010495 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010498
10499 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010501 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010502 {
10503 continue;
10504 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010505 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010506#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010507#if defined(MBEDTLS_SSL_PROTO_TLS)
10508 {
10509 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10510 }
10511#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010512 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010513#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010514
Hanno Becker4a810fb2017-05-24 16:27:30 +010010515#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010516 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10517 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010521
10522 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010524 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010525 {
10526 continue;
10527 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010528 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010529#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010530#if defined(MBEDTLS_SSL_PROTO_TLS)
10531 {
10532 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10533 }
10534#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010535 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010536#endif /* MBEDTLS_SSL_SRV_C */
10537
Hanno Becker21df7f92017-10-17 11:03:26 +010010538#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010539 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010540 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10541 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010542 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010543 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10544 {
10545 /*
10546 * Accept renegotiation request
10547 */
Paul Bakker48916f92012-09-16 19:57:18 +000010548
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010549 /* DTLS clients need to know renego is server-initiated */
10550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010551 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010552 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10553 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010554 {
10555 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10556 }
10557#endif
10558 ret = ssl_start_renegotiation( ssl );
10559 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10560 ret != 0 )
10561 {
10562 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10563 return( ret );
10564 }
10565 }
10566 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010567#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010568 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010569 /*
10570 * Refuse renegotiation
10571 */
10572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010576 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010577 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010578 /* SSLv3 does not have a "no_renegotiation" warning, so
10579 we send a fatal alert and abort the connection. */
10580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10581 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10582 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010583 }
10584 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010585#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10586#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10587 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010588 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010589 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010590 ret = mbedtls_ssl_send_alert_message( ssl,
10591 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10592 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10593 if( ret != 0 )
10594 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010595 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010596 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10598 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010602 }
Paul Bakker48916f92012-09-16 19:57:18 +000010603 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010604
Hanno Becker90333da2017-10-10 11:27:13 +010010605 /* At this point, we don't know whether the renegotiation has been
10606 * completed or not. The cases to consider are the following:
10607 * 1) The renegotiation is complete. In this case, no new record
10608 * has been read yet.
10609 * 2) The renegotiation is incomplete because the client received
10610 * an application data record while awaiting the ServerHello.
10611 * 3) The renegotiation is incomplete because the client received
10612 * a non-handshake, non-application data message while awaiting
10613 * the ServerHello.
10614 * In each of these case, looping will be the proper action:
10615 * - For 1), the next iteration will read a new record and check
10616 * if it's application data.
10617 * - For 2), the loop condition isn't satisfied as application data
10618 * is present, hence continue is the same as break
10619 * - For 3), the loop condition is satisfied and read_record
10620 * will re-deliver the message that was held back by the client
10621 * when expecting the ServerHello.
10622 */
10623 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010624 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010625#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010627 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010628 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010629 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010630 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010633 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010635 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010636 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10641 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010644 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010645 }
10646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10650 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010651 }
10652
10653 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010654
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010655 /* We're going to return something now, cancel timer,
10656 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010658 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010659
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010661 /* If we requested renego but received AppData, resend HelloRequest.
10662 * Do it now, after setting in_offt, to avoid taking this branch
10663 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010665 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10666 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010668 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010669 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010672 return( ret );
10673 }
10674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010676#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010677 }
10678
10679 n = ( len < ssl->in_msglen )
10680 ? len : ssl->in_msglen;
10681
10682 memcpy( buf, ssl->in_offt, n );
10683 ssl->in_msglen -= n;
10684
10685 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010686 {
10687 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010688 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010689 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010690 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010691 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010692 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010693 /* more data available */
10694 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010695 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010698
Paul Bakker23986e52011-04-24 08:57:21 +000010699 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010700}
10701
10702/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010703 * Send application data to be encrypted by the SSL layer, taking care of max
10704 * fragment length and buffer size.
10705 *
10706 * According to RFC 5246 Section 6.2.1:
10707 *
10708 * Zero-length fragments of Application data MAY be sent as they are
10709 * potentially useful as a traffic analysis countermeasure.
10710 *
10711 * Therefore, it is possible that the input message length is 0 and the
10712 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010713 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010714static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010715 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010716{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010717 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10718 const size_t max_len = (size_t) ret;
10719
10720 if( ret < 0 )
10721 {
10722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10723 return( ret );
10724 }
10725
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010726 if( len > max_len )
10727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010732 "maximum fragment length: %d > %d",
10733 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010735 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010736 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010737#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010738#if defined(MBEDTLS_SSL_PROTO_TLS)
10739 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010740 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010741 }
10742#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010743 }
Paul Bakker887bd502011-06-08 13:10:54 +000010744
Paul Bakker5121ce52009-01-03 21:22:43 +000010745 if( ssl->out_left != 0 )
10746 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010747 /*
10748 * The user has previously tried to send the data and
10749 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10750 * written. In this case, we expect the high-level write function
10751 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010756 return( ret );
10757 }
10758 }
Paul Bakker887bd502011-06-08 13:10:54 +000010759 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010760 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010761 /*
10762 * The user is trying to send a message the first time, so we need to
10763 * copy the data into the internal buffers and setup the data structure
10764 * to keep track of partial writes
10765 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010766 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010768 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010769
Hanno Becker67bc7c32018-08-06 11:33:50 +010010770 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010773 return( ret );
10774 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010775 }
10776
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010777 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010778}
10779
10780/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010781 * Write application data, doing 1/n-1 splitting if necessary.
10782 *
10783 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010784 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010785 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010788static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010789 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010790{
10791 int ret;
10792
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010793 if( ssl->conf->cbc_record_splitting ==
10794 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010795 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010796 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10798 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010799 {
10800 return( ssl_write_real( ssl, buf, len ) );
10801 }
10802
10803 if( ssl->split_done == 0 )
10804 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010805 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010806 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010807 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010808 }
10809
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010810 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10811 return( ret );
10812 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010813
10814 return( ret + 1 );
10815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010817
10818/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010819 * Write application data (public-facing wrapper)
10820 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010821int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010822{
10823 int ret;
10824
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010826
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010827 if( ssl == NULL || ssl->conf == NULL )
10828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10829
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010830#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010831 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10832 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010834 return( ret );
10835 }
10836#endif
10837
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010838 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010839 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010840 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010841 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010843 return( ret );
10844 }
10845 }
10846
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010847#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010848 ret = ssl_write_split( ssl, buf, len );
10849#else
10850 ret = ssl_write_real( ssl, buf, len );
10851#endif
10852
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010854
10855 return( ret );
10856}
10857
10858/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010859 * Notify the peer that the connection is being closed
10860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010862{
10863 int ret;
10864
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010865 if( ssl == NULL || ssl->conf == NULL )
10866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010869
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010870 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10876 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10877 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010880 return( ret );
10881 }
10882 }
10883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010885
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010886 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010887}
10888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010889void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010890{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010891 if( transform == NULL )
10892 return;
10893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010895 deflateEnd( &transform->ctx_deflate );
10896 inflateEnd( &transform->ctx_inflate );
10897#endif
10898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10900 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010901
Hanno Becker92231322018-01-03 15:32:51 +000010902#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903 mbedtls_md_free( &transform->md_ctx_enc );
10904 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010905#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010906
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010907 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010908}
10909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910#if defined(MBEDTLS_X509_CRT_PARSE_C)
10911static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010914
10915 while( cur != NULL )
10916 {
10917 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010919 cur = next;
10920 }
10921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010923
Hanno Becker0271f962018-08-16 13:23:47 +010010924#if defined(MBEDTLS_SSL_PROTO_DTLS)
10925
10926static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10927{
10928 unsigned offset;
10929 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10930
10931 if( hs == NULL )
10932 return;
10933
Hanno Becker283f5ef2018-08-24 09:34:47 +010010934 ssl_free_buffered_record( ssl );
10935
Hanno Becker0271f962018-08-16 13:23:47 +010010936 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010937 ssl_buffering_free_slot( ssl, offset );
10938}
10939
10940static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10941 uint8_t slot )
10942{
10943 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10944 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010945
10946 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10947 return;
10948
Hanno Beckere605b192018-08-21 15:59:07 +010010949 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010950 {
Hanno Beckere605b192018-08-21 15:59:07 +010010951 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010952 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010953 mbedtls_free( hs_buf->data );
10954 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010955 }
10956}
10957
10958#endif /* MBEDTLS_SSL_PROTO_DTLS */
10959
Gilles Peskine9b562d52018-04-25 20:32:43 +020010960void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010961{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010962 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10963
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010964 if( handshake == NULL )
10965 return;
10966
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010967#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10968 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10969 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010970 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010971 handshake->async_in_progress = 0;
10972 }
10973#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10974
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010975#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10976 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10977 mbedtls_md5_free( &handshake->fin_md5 );
10978 mbedtls_sha1_free( &handshake->fin_sha1 );
10979#endif
10980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10981#if defined(MBEDTLS_SHA256_C)
10982 mbedtls_sha256_free( &handshake->fin_sha256 );
10983#endif
10984#if defined(MBEDTLS_SHA512_C)
10985 mbedtls_sha512_free( &handshake->fin_sha512 );
10986#endif
10987#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989#if defined(MBEDTLS_DHM_C)
10990 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010991#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992#if defined(MBEDTLS_ECDH_C)
10993 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010994#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010995#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010996 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010997#if defined(MBEDTLS_SSL_CLI_C)
10998 mbedtls_free( handshake->ecjpake_cache );
10999 handshake->ecjpake_cache = NULL;
11000 handshake->ecjpake_cache_len = 0;
11001#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011002#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011003
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011004#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11005 if( handshake->psk != NULL )
11006 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011007 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011008 mbedtls_free( handshake->psk );
11009 }
11010#endif
11011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11013 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011014 /*
11015 * Free only the linked list wrapper, not the keys themselves
11016 * since the belong to the SNI callback
11017 */
11018 if( handshake->sni_key_cert != NULL )
11019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011021
11022 while( cur != NULL )
11023 {
11024 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011026 cur = next;
11027 }
11028 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011030
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011031#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011032 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011033 if( handshake->ecrs_peer_cert != NULL )
11034 {
11035 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11036 mbedtls_free( handshake->ecrs_peer_cert );
11037 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011038#endif
11039
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011040#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11041 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11042 mbedtls_pk_free( &handshake->peer_pubkey );
11043#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045#if defined(MBEDTLS_SSL_PROTO_DTLS)
11046 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011047 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011048 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011049#endif
11050
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011051 mbedtls_platform_zeroize( handshake,
11052 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011053}
11054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011056{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011057 if( session == NULL )
11058 return;
11059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011061 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011062#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011063
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011064#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011066#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011067
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011068 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011069}
11070
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011071#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011072
11073#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11074#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11075#else
11076#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11077#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11078
11079#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11080#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11081#else
11082#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11083#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11084
11085#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11086#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11087#else
11088#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11089#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11090
11091#if defined(MBEDTLS_SSL_ALPN)
11092#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11093#else
11094#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11095#endif /* MBEDTLS_SSL_ALPN */
11096
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11098#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11099#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11100#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11101
11102#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11103 ( (uint32_t) ( \
11104 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11105 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11106 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11107 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11108 0u ) )
11109
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011110static unsigned char ssl_serialized_context_header[] = {
11111 MBEDTLS_VERSION_MAJOR,
11112 MBEDTLS_VERSION_MINOR,
11113 MBEDTLS_VERSION_PATCH,
11114 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11115 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011116 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11117 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11118 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011119};
11120
Paul Bakker5121ce52009-01-03 21:22:43 +000011121/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011122 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011123 *
11124 * The format of the serialized data is:
11125 * (in the presentation language of TLS, RFC 8446 section 3)
11126 *
11127 * // header
11128 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011129 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011130 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011131 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011132 * Note: When updating the format, remember to keep these
11133 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011134 *
11135 * // session sub-structure
11136 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11137 * // transform sub-structure
11138 * uint8 random[64]; // ServerHello.random+ClientHello.random
11139 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11140 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11141 * // fields from ssl_context
11142 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11143 * uint64 in_window_top; // DTLS: last validated record seq_num
11144 * uint64 in_window; // DTLS: bitmask for replay protection
11145 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11146 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11147 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11148 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11149 *
11150 * Note that many fields of the ssl_context or sub-structures are not
11151 * serialized, as they fall in one of the following categories:
11152 *
11153 * 1. forced value (eg in_left must be 0)
11154 * 2. pointer to dynamically-allocated memory (eg session, transform)
11155 * 3. value can be re-derived from other data (eg session keys from MS)
11156 * 4. value was temporary (eg content of input buffer)
11157 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011158 */
11159int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11160 unsigned char *buf,
11161 size_t buf_len,
11162 size_t *olen )
11163{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011164 unsigned char *p = buf;
11165 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011166 size_t session_len;
11167 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011168
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011169 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011170 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11171 * this function's documentation.
11172 *
11173 * These are due to assumptions/limitations in the implementation. Some of
11174 * them are likely to stay (no handshake in progress) some might go away
11175 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011176 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011177 /* The initial handshake must be over */
11178 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011180 if( ssl->handshake != NULL )
11181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11182 /* Double-check that sub-structures are indeed ready */
11183 if( ssl->transform == NULL || ssl->session == NULL )
11184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11185 /* There must be no pending incoming or outgoing data */
11186 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11188 if( ssl->out_left != 0 )
11189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11190 /* Protocol must be DLTS, not TLS */
11191 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11193 /* Version must be 1.2 */
11194 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11196 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11198 /* We must be using an AEAD ciphersuite */
11199 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11201 /* Renegotiation must not be enabled */
11202 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011204
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011205 /*
11206 * Version and format identifier
11207 */
11208 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011209
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011210 if( used <= buf_len )
11211 {
11212 memcpy( p, ssl_serialized_context_header,
11213 sizeof( ssl_serialized_context_header ) );
11214 p += sizeof( ssl_serialized_context_header );
11215 }
11216
11217 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011218 * Session (length + data)
11219 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011220 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011221 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11222 return( ret );
11223
11224 used += 4 + session_len;
11225 if( used <= buf_len )
11226 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011227 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011228
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011229 ret = ssl_session_save( ssl->session, 1,
11230 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011231 if( ret != 0 )
11232 return( ret );
11233
11234 p += session_len;
11235 }
11236
11237 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011238 * Transform
11239 */
11240 used += sizeof( ssl->transform->randbytes );
11241 if( used <= buf_len )
11242 {
11243 memcpy( p, ssl->transform->randbytes,
11244 sizeof( ssl->transform->randbytes ) );
11245 p += sizeof( ssl->transform->randbytes );
11246 }
11247
11248#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11249 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11250 if( used <= buf_len )
11251 {
11252 *p++ = ssl->transform->in_cid_len;
11253 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11254 p += ssl->transform->in_cid_len;
11255
11256 *p++ = ssl->transform->out_cid_len;
11257 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11258 p += ssl->transform->out_cid_len;
11259 }
11260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11261
11262 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011263 * Saved fields from top-level ssl_context structure
11264 */
11265#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11266 used += 4;
11267 if( used <= buf_len )
11268 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011269 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011270 }
11271#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11272
11273#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11274 used += 16;
11275 if( used <= buf_len )
11276 {
11277 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11278 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11279 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11280 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11281 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11282 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11283 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11284 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11285
11286 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11287 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11288 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11289 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11290 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11291 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11292 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11293 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11294 }
11295#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11296
11297#if defined(MBEDTLS_SSL_PROTO_DTLS)
11298 used += 1;
11299 if( used <= buf_len )
11300 {
11301 *p++ = ssl->disable_datagram_packing;
11302 }
11303#endif /* MBEDTLS_SSL_PROTO_DTLS */
11304
11305 used += 8;
11306 if( used <= buf_len )
11307 {
11308 memcpy( p, ssl->cur_out_ctr, 8 );
11309 p += 8;
11310 }
11311
11312#if defined(MBEDTLS_SSL_PROTO_DTLS)
11313 used += 2;
11314 if( used <= buf_len )
11315 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011316 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011317 }
11318#endif /* MBEDTLS_SSL_PROTO_DTLS */
11319
11320#if defined(MBEDTLS_SSL_ALPN)
11321 {
11322 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011323 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011324 : 0;
11325
11326 used += 1 + alpn_len;
11327 if( used <= buf_len )
11328 {
11329 *p++ = alpn_len;
11330
11331 if( ssl->alpn_chosen != NULL )
11332 {
11333 memcpy( p, ssl->alpn_chosen, alpn_len );
11334 p += alpn_len;
11335 }
11336 }
11337 }
11338#endif /* MBEDTLS_SSL_ALPN */
11339
11340 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011341 * Done
11342 */
11343 *olen = used;
11344
11345 if( used > buf_len )
11346 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011347
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011348 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11349
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011350 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011351}
11352
11353/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011354 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011355 *
11356 * This internal version is wrapped by a public function that cleans up in
11357 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011358 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011359static int ssl_context_load( mbedtls_ssl_context *ssl,
11360 const unsigned char *buf,
11361 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011362{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011363 const unsigned char *p = buf;
11364 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011365 size_t session_len;
11366 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011367
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011368 /*
11369 * The context should have been freshly setup or reset.
11370 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011371 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011372 * renegotiating, or if the user mistakenly loaded a session first.)
11373 */
11374 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11375 ssl->session != NULL )
11376 {
11377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11378 }
11379
11380 /*
11381 * We can't check that the config matches the initial one, but we can at
11382 * least check it matches the requirements for serializing.
11383 */
11384 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011385 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11386 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11387 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11388 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11389 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11390 MBEDTLS_SSL_MINOR_VERSION_3 ||
11391 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11392 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011393 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011394 {
11395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11396 }
11397
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011398 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11399
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011400 /*
11401 * Check version identifier
11402 */
11403 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11405
11406 if( memcmp( p, ssl_serialized_context_header,
11407 sizeof( ssl_serialized_context_header ) ) != 0 )
11408 {
11409 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11410 }
11411 p += sizeof( ssl_serialized_context_header );
11412
11413 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011414 * Session
11415 */
11416 if( (size_t)( end - p ) < 4 )
11417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11418
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011419 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011420 p += 4;
11421
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011422 /* This has been allocated by ssl_handshake_init(), called by
11423 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11424 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011425 ssl->session_in = ssl->session;
11426 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011427 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011428
11429 if( (size_t)( end - p ) < session_len )
11430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11431
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011432 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011433 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011434 {
11435 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011436 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011437 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011438
11439 p += session_len;
11440
11441 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011442 * Transform
11443 */
11444
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011445 /* This has been allocated by ssl_handshake_init(), called by
11446 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11447 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011448 ssl->transform_in = ssl->transform;
11449 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011450 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011451
11452 /* Read random bytes and populate structure */
11453 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11454 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11455
11456 ret = ssl_populate_transform( ssl->transform,
11457 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11458 ssl->session->master,
11459#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11461 ssl->session->encrypt_then_mac,
11462#endif
11463#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11464 ssl->session->trunc_hmac,
11465#endif
11466#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11467#if defined(MBEDTLS_ZLIB_SUPPORT)
11468 ssl->session->compression,
11469#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011470 p, /* currently pointing to randbytes */
11471 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11472 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11473 ssl );
11474 if( ret != 0 )
11475 return( ret );
11476
11477 p += sizeof( ssl->transform->randbytes );
11478
11479#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11480 /* Read connection IDs and store them */
11481 if( (size_t)( end - p ) < 1 )
11482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11483
11484 ssl->transform->in_cid_len = *p++;
11485
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011486 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11488
11489 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11490 p += ssl->transform->in_cid_len;
11491
11492 ssl->transform->out_cid_len = *p++;
11493
11494 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11496
11497 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11498 p += ssl->transform->out_cid_len;
11499#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11500
11501 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011502 * Saved fields from top-level ssl_context structure
11503 */
11504#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11505 if( (size_t)( end - p ) < 4 )
11506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11507
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011508 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011509 p += 4;
11510#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11511
11512#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11513 if( (size_t)( end - p ) < 16 )
11514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11515
11516 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11517 ( (uint64_t) p[1] << 48 ) |
11518 ( (uint64_t) p[2] << 40 ) |
11519 ( (uint64_t) p[3] << 32 ) |
11520 ( (uint64_t) p[4] << 24 ) |
11521 ( (uint64_t) p[5] << 16 ) |
11522 ( (uint64_t) p[6] << 8 ) |
11523 ( (uint64_t) p[7] );
11524 p += 8;
11525
11526 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11527 ( (uint64_t) p[1] << 48 ) |
11528 ( (uint64_t) p[2] << 40 ) |
11529 ( (uint64_t) p[3] << 32 ) |
11530 ( (uint64_t) p[4] << 24 ) |
11531 ( (uint64_t) p[5] << 16 ) |
11532 ( (uint64_t) p[6] << 8 ) |
11533 ( (uint64_t) p[7] );
11534 p += 8;
11535#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11536
11537#if defined(MBEDTLS_SSL_PROTO_DTLS)
11538 if( (size_t)( end - p ) < 1 )
11539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11540
11541 ssl->disable_datagram_packing = *p++;
11542#endif /* MBEDTLS_SSL_PROTO_DTLS */
11543
11544 if( (size_t)( end - p ) < 8 )
11545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11546
11547 memcpy( ssl->cur_out_ctr, p, 8 );
11548 p += 8;
11549
11550#if defined(MBEDTLS_SSL_PROTO_DTLS)
11551 if( (size_t)( end - p ) < 2 )
11552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011553 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011554 p += 2;
11555#endif /* MBEDTLS_SSL_PROTO_DTLS */
11556
11557#if defined(MBEDTLS_SSL_ALPN)
11558 {
11559 uint8_t alpn_len;
11560 const char **cur;
11561
11562 if( (size_t)( end - p ) < 1 )
11563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11564
11565 alpn_len = *p++;
11566
11567 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11568 {
11569 /* alpn_chosen should point to an item in the configured list */
11570 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11571 {
11572 if( strlen( *cur ) == alpn_len &&
11573 memcmp( p, cur, alpn_len ) == 0 )
11574 {
11575 ssl->alpn_chosen = *cur;
11576 break;
11577 }
11578 }
11579 }
11580
11581 /* can only happen on conf mismatch */
11582 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11584
11585 p += alpn_len;
11586 }
11587#endif /* MBEDTLS_SSL_ALPN */
11588
11589 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011590 * Forced fields from top-level ssl_context structure
11591 *
11592 * Most of them already set to the correct value by mbedtls_ssl_init() and
11593 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11594 */
11595 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11596
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011597#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011598 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011599#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11600#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011601 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011602#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011603
Hanno Becker83985822019-08-30 10:42:49 +010011604 /* Adjust pointers for header fields of outgoing records to
11605 * the given transform, accounting for explicit IV and CID. */
11606 ssl_update_out_pointers( ssl, ssl->transform );
11607
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011608#if defined(MBEDTLS_SSL_PROTO_DTLS)
11609 ssl->in_epoch = 1;
11610#endif
11611
11612 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11613 * which we don't want - otherwise we'd end up freeing the wrong transform
11614 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11615 if( ssl->handshake != NULL )
11616 {
11617 mbedtls_ssl_handshake_free( ssl );
11618 mbedtls_free( ssl->handshake );
11619 ssl->handshake = NULL;
11620 }
11621
11622 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011623 * Done - should have consumed entire buffer
11624 */
11625 if( p != end )
11626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011627
11628 return( 0 );
11629}
11630
11631/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011632 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011633 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011634int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011635 const unsigned char *buf,
11636 size_t len )
11637{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011638 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011639
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011640 if( ret != 0 )
11641 mbedtls_ssl_free( context );
11642
11643 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011644}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011645#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011646
11647/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011648 * Free an SSL context
11649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011650void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011651{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011652 if( ssl == NULL )
11653 return;
11654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011656
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011657 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011658 {
Angus Grattond8213d02016-05-25 20:56:48 +100011659 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011660 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011661 }
11662
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011663 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011664 {
Angus Grattond8213d02016-05-25 20:56:48 +100011665 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011666 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011667 }
11668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011669#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011670 if( ssl->compress_buf != NULL )
11671 {
Angus Grattond8213d02016-05-25 20:56:48 +100011672 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011673 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011674 }
11675#endif
11676
Paul Bakker48916f92012-09-16 19:57:18 +000011677 if( ssl->transform )
11678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011679 mbedtls_ssl_transform_free( ssl->transform );
11680 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011681 }
11682
11683 if( ssl->handshake )
11684 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011685 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011686 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11687 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011689 mbedtls_free( ssl->handshake );
11690 mbedtls_free( ssl->transform_negotiate );
11691 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011692 }
11693
Paul Bakkerc0463502013-02-14 11:19:38 +010011694 if( ssl->session )
11695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011696 mbedtls_ssl_session_free( ssl->session );
11697 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011698 }
11699
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011700#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011701 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011702 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011703 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011704 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011705 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011706#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011708#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11709 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11712 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011713 }
11714#endif
11715
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011716#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011718#endif
11719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011721
Paul Bakker86f04f42013-02-14 11:20:09 +010011722 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011723 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011724}
11725
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011726/*
11727 * Initialze mbedtls_ssl_config
11728 */
11729void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11730{
11731 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011732
11733#if !defined(MBEDTLS_SSL_PROTO_TLS)
11734 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11735#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011736}
11737
Simon Butcherc97b6972015-12-27 23:48:17 +000011738#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011739#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011740static int ssl_preset_default_hashes[] = {
11741#if defined(MBEDTLS_SHA512_C)
11742 MBEDTLS_MD_SHA512,
11743 MBEDTLS_MD_SHA384,
11744#endif
11745#if defined(MBEDTLS_SHA256_C)
11746 MBEDTLS_MD_SHA256,
11747 MBEDTLS_MD_SHA224,
11748#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011749#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011750 MBEDTLS_MD_SHA1,
11751#endif
11752 MBEDTLS_MD_NONE
11753};
Simon Butcherc97b6972015-12-27 23:48:17 +000011754#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011755#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011756
Hanno Becker73f4cb12019-06-27 13:51:07 +010011757#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011758static int ssl_preset_suiteb_ciphersuites[] = {
11759 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11760 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11761 0
11762};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011763#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011764
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011765#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011766#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011767static int ssl_preset_suiteb_hashes[] = {
11768 MBEDTLS_MD_SHA256,
11769 MBEDTLS_MD_SHA384,
11770 MBEDTLS_MD_NONE
11771};
11772#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011773#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011774
Hanno Beckerc1096e72019-06-19 12:30:41 +010011775#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011776static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011777#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011778 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011779#endif
11780#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011781 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011782#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011783 MBEDTLS_ECP_DP_NONE
11784};
11785#endif
11786
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011787/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011788 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011789 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011790int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011791 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011792{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011793#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011794 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011795#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011796
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011797 /* Use the functions here so that they are covered in tests,
11798 * but otherwise access member directly for efficiency */
11799 mbedtls_ssl_conf_endpoint( conf, endpoint );
11800 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011801
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011802 /*
11803 * Things that are common to all presets
11804 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011805#if defined(MBEDTLS_SSL_CLI_C)
11806 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11807 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011808#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011809 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011810#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011811#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11812 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11813#endif
11814 }
11815#endif
11816
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011817#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011818 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011819#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011820
11821#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11822 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11823#endif
11824
11825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011826#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011827 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011828#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11829#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011830 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011831 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011832#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011833#endif
11834
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011835#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11836 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11837#endif
11838
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011839#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011840 conf->f_cookie_write = ssl_cookie_write_dummy;
11841 conf->f_cookie_check = ssl_cookie_check_dummy;
11842#endif
11843
Hanno Becker7f376f42019-06-12 16:20:48 +010011844#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11845 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011846 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11847#endif
11848
Janos Follath088ce432017-04-10 12:42:31 +010011849#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011850#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011851 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011852#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11853#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011854
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011856#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011857 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011858#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11859#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011860 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011861#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11862#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011863
11864#if defined(MBEDTLS_SSL_RENEGOTIATION)
11865 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011866 memset( conf->renego_period, 0x00, 2 );
11867 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011868#endif
11869
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11871 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11872 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011873 const unsigned char dhm_p[] =
11874 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11875 const unsigned char dhm_g[] =
11876 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11877
Hanno Beckera90658f2017-10-04 15:29:08 +010011878 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11879 dhm_p, sizeof( dhm_p ),
11880 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011881 {
11882 return( ret );
11883 }
11884 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011885#endif
11886
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011887 /*
11888 * Preset-specific defaults
11889 */
11890 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011891 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011892 /*
11893 * NSA Suite B
11894 */
11895 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011896#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011897 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011898#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11899#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011900 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011901#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11902#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011903 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011904#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11905#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011906 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011907#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011908
Hanno Becker73f4cb12019-06-27 13:51:07 +010011909#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011910 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11911 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11912 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11913 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11914 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011915#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011916
11917#if defined(MBEDTLS_X509_CRT_PARSE_C)
11918 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011919#endif
11920
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011921#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011922#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011923 conf->sig_hashes = ssl_preset_suiteb_hashes;
11924#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011925#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011926
11927#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011928#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011929 conf->curve_list = ssl_preset_suiteb_curves;
11930#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011931#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011932 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011933
11934 /*
11935 * Default
11936 */
11937 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011938#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011939 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11940 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11941 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11942 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011943#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11944#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011945 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11946 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11947 MBEDTLS_SSL_MIN_MINOR_VERSION :
11948 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011950 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011951 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11952#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011953#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11954#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11955 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11956#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11957#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11958 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11959#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011960
Hanno Becker73f4cb12019-06-27 13:51:07 +010011961#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011962 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11963 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11964 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11966 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011967#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011968
11969#if defined(MBEDTLS_X509_CRT_PARSE_C)
11970 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11971#endif
11972
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011973#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011974#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011975 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011976#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011977#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011978
11979#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011980#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011981 conf->curve_list = mbedtls_ecp_grp_id_list();
11982#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011983#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011984
11985#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11986 conf->dhm_min_bitlen = 1024;
11987#endif
11988 }
11989
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011990 return( 0 );
11991}
11992
11993/*
11994 * Free mbedtls_ssl_config
11995 */
11996void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11997{
11998#if defined(MBEDTLS_DHM_C)
11999 mbedtls_mpi_free( &conf->dhm_P );
12000 mbedtls_mpi_free( &conf->dhm_G );
12001#endif
12002
12003#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12004 if( conf->psk != NULL )
12005 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012006 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012007 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012008 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012009 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012010 }
12011
12012 if( conf->psk_identity != NULL )
12013 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012014 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012015 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012016 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012017 conf->psk_identity_len = 0;
12018 }
12019#endif
12020
12021#if defined(MBEDTLS_X509_CRT_PARSE_C)
12022 ssl_key_cert_free( conf->key_cert );
12023#endif
12024
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012025 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012026}
12027
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012028#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012029 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12030 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012031/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012032 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012034unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012036#if defined(MBEDTLS_RSA_C)
12037 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12038 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012039#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012040#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012041 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12042 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012044 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012045}
12046
Hanno Becker7e5437a2017-04-28 17:15:26 +010012047unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12048{
12049 switch( type ) {
12050 case MBEDTLS_PK_RSA:
12051 return( MBEDTLS_SSL_SIG_RSA );
12052 case MBEDTLS_PK_ECDSA:
12053 case MBEDTLS_PK_ECKEY:
12054 return( MBEDTLS_SSL_SIG_ECDSA );
12055 default:
12056 return( MBEDTLS_SSL_SIG_ANON );
12057 }
12058}
12059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012060mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012061{
12062 switch( sig )
12063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012064#if defined(MBEDTLS_RSA_C)
12065 case MBEDTLS_SSL_SIG_RSA:
12066 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012067#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012068#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012069 case MBEDTLS_SSL_SIG_ECDSA:
12070 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012071#endif
12072 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012073 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012074 }
12075}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012076#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012077
Hanno Becker7e5437a2017-04-28 17:15:26 +010012078#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12079 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12080
12081/* Find an entry in a signature-hash set matching a given hash algorithm. */
12082mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12083 mbedtls_pk_type_t sig_alg )
12084{
12085 switch( sig_alg )
12086 {
12087 case MBEDTLS_PK_RSA:
12088 return( set->rsa );
12089 case MBEDTLS_PK_ECDSA:
12090 return( set->ecdsa );
12091 default:
12092 return( MBEDTLS_MD_NONE );
12093 }
12094}
12095
12096/* Add a signature-hash-pair to a signature-hash set */
12097void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12098 mbedtls_pk_type_t sig_alg,
12099 mbedtls_md_type_t md_alg )
12100{
12101 switch( sig_alg )
12102 {
12103 case MBEDTLS_PK_RSA:
12104 if( set->rsa == MBEDTLS_MD_NONE )
12105 set->rsa = md_alg;
12106 break;
12107
12108 case MBEDTLS_PK_ECDSA:
12109 if( set->ecdsa == MBEDTLS_MD_NONE )
12110 set->ecdsa = md_alg;
12111 break;
12112
12113 default:
12114 break;
12115 }
12116}
12117
12118/* Allow exactly one hash algorithm for each signature. */
12119void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12120 mbedtls_md_type_t md_alg )
12121{
12122 set->rsa = md_alg;
12123 set->ecdsa = md_alg;
12124}
12125
12126#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12127 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12128
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012129/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012130 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012132mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012133{
12134 switch( hash )
12135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012136#if defined(MBEDTLS_MD5_C)
12137 case MBEDTLS_SSL_HASH_MD5:
12138 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012140#if defined(MBEDTLS_SHA1_C)
12141 case MBEDTLS_SSL_HASH_SHA1:
12142 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012144#if defined(MBEDTLS_SHA256_C)
12145 case MBEDTLS_SSL_HASH_SHA224:
12146 return( MBEDTLS_MD_SHA224 );
12147 case MBEDTLS_SSL_HASH_SHA256:
12148 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012150#if defined(MBEDTLS_SHA512_C)
12151 case MBEDTLS_SSL_HASH_SHA384:
12152 return( MBEDTLS_MD_SHA384 );
12153 case MBEDTLS_SSL_HASH_SHA512:
12154 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012155#endif
12156 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012158 }
12159}
12160
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012161/*
12162 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12163 */
12164unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12165{
12166 switch( md )
12167 {
12168#if defined(MBEDTLS_MD5_C)
12169 case MBEDTLS_MD_MD5:
12170 return( MBEDTLS_SSL_HASH_MD5 );
12171#endif
12172#if defined(MBEDTLS_SHA1_C)
12173 case MBEDTLS_MD_SHA1:
12174 return( MBEDTLS_SSL_HASH_SHA1 );
12175#endif
12176#if defined(MBEDTLS_SHA256_C)
12177 case MBEDTLS_MD_SHA224:
12178 return( MBEDTLS_SSL_HASH_SHA224 );
12179 case MBEDTLS_MD_SHA256:
12180 return( MBEDTLS_SSL_HASH_SHA256 );
12181#endif
12182#if defined(MBEDTLS_SHA512_C)
12183 case MBEDTLS_MD_SHA384:
12184 return( MBEDTLS_SSL_HASH_SHA384 );
12185 case MBEDTLS_MD_SHA512:
12186 return( MBEDTLS_SSL_HASH_SHA512 );
12187#endif
12188 default:
12189 return( MBEDTLS_SSL_HASH_NONE );
12190 }
12191}
12192
Hanno Beckeree902df2019-08-23 13:47:47 +010012193#if defined(MBEDTLS_USE_TINYCRYPT)
12194/*
12195 * Check if a curve proposed by the peer is in our list.
12196 * Return 0 if we're willing to use it, -1 otherwise.
12197 */
12198int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12199 mbedtls_uecc_group_id grp_id )
12200{
12201 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12202 if( own_ec_id == grp_id )
12203 return( 0 );
12204 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12205
12206 return( -1 );
12207}
12208#endif /* MBEDTLS_USE_TINYCRYPT */
12209
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012210#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012211/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012212 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012213 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012214 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012215int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12216 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012217{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012218 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12219 if( own_ec_id == grp_id )
12220 return( 0 );
12221 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012222
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012223 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012224}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012225#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012226
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012227#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012228/*
12229 * Check if a hash proposed by the peer is in our list.
12230 * Return 0 if we're willing to use it, -1 otherwise.
12231 */
12232int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12233 mbedtls_md_type_t md )
12234{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012235 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12236 if( md_alg == md )
12237 return( 0 );
12238 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012239
12240 return( -1 );
12241}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012242#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012244#if defined(MBEDTLS_X509_CRT_PARSE_C)
12245int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012246 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012247 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012248 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012249{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012250 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012251#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012252 int usage = 0;
12253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012254#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012255 const char *ext_oid;
12256 size_t ext_len;
12257#endif
12258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012259#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12260 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012261 ((void) cert);
12262 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012263 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012264#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012266#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12267 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012268 {
12269 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012270 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012272 case MBEDTLS_KEY_EXCHANGE_RSA:
12273 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012274 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012275 break;
12276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012277 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12278 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12279 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12280 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012281 break;
12282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012283 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12284 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012285 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012286 break;
12287
12288 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012289 case MBEDTLS_KEY_EXCHANGE_NONE:
12290 case MBEDTLS_KEY_EXCHANGE_PSK:
12291 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12292 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012293 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012294 usage = 0;
12295 }
12296 }
12297 else
12298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012299 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12300 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012301 }
12302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012303 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012304 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012305 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012306 ret = -1;
12307 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012308#else
12309 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012310#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012312#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12313 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012315 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12316 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012317 }
12318 else
12319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012320 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12321 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012322 }
12323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012324 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012325 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012326 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012327 ret = -1;
12328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012329#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012330
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012331 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012333#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012334
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012335#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12336 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12337int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12338 unsigned char *output,
12339 unsigned char *data, size_t data_len )
12340{
12341 int ret = 0;
12342 mbedtls_md5_context mbedtls_md5;
12343 mbedtls_sha1_context mbedtls_sha1;
12344
12345 mbedtls_md5_init( &mbedtls_md5 );
12346 mbedtls_sha1_init( &mbedtls_sha1 );
12347
12348 /*
12349 * digitally-signed struct {
12350 * opaque md5_hash[16];
12351 * opaque sha_hash[20];
12352 * };
12353 *
12354 * md5_hash
12355 * MD5(ClientHello.random + ServerHello.random
12356 * + ServerParams);
12357 * sha_hash
12358 * SHA(ClientHello.random + ServerHello.random
12359 * + ServerParams);
12360 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012361 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012362 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012364 goto exit;
12365 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012366 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012367 ssl->handshake->randbytes, 64 ) ) != 0 )
12368 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012370 goto exit;
12371 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012372 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012373 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012375 goto exit;
12376 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012377 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012378 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012380 goto exit;
12381 }
12382
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012383 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012384 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012386 goto exit;
12387 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012388 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012389 ssl->handshake->randbytes, 64 ) ) != 0 )
12390 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392 goto exit;
12393 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012394 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012395 data_len ) ) != 0 )
12396 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012398 goto exit;
12399 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012400 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012401 output + 16 ) ) != 0 )
12402 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012404 goto exit;
12405 }
12406
12407exit:
12408 mbedtls_md5_free( &mbedtls_md5 );
12409 mbedtls_sha1_free( &mbedtls_sha1 );
12410
12411 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012412 mbedtls_ssl_pend_fatal_alert( ssl,
12413 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012414
12415 return( ret );
12416
12417}
12418#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12419 MBEDTLS_SSL_PROTO_TLS1_1 */
12420
12421#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12422 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12423int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012424 unsigned char *hash, size_t *hashlen,
12425 unsigned char *data, size_t data_len,
12426 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012427{
12428 int ret = 0;
12429 mbedtls_md_context_t ctx;
12430 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012431 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012432
12433 mbedtls_md_init( &ctx );
12434
12435 /*
12436 * digitally-signed struct {
12437 * opaque client_random[32];
12438 * opaque server_random[32];
12439 * ServerDHParams params;
12440 * };
12441 */
12442 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12443 {
12444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12445 goto exit;
12446 }
12447 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12448 {
12449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12450 goto exit;
12451 }
12452 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12453 {
12454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12455 goto exit;
12456 }
12457 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12458 {
12459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12460 goto exit;
12461 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012462 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012463 {
12464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12465 goto exit;
12466 }
12467
12468exit:
12469 mbedtls_md_free( &ctx );
12470
12471 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012472 mbedtls_ssl_pend_fatal_alert( ssl,
12473 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012474
12475 return( ret );
12476}
12477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12478 MBEDTLS_SSL_PROTO_TLS1_2 */
12479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012480#endif /* MBEDTLS_SSL_TLS_C */